Gym - 100851A Adjustment Office(O求行列和)

Posted yzm10

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Gym - 100851A Adjustment Office(O求行列和)相关的知识,希望对你有一定的参考价值。

Adjustment Office

Gym - 100851A

 

2       3       4

3       4       5

4       5       6

 

n<=10^6,q<=10^5

每个坐标点的值为x+y。

q次查询,每次求某行或某列的和,然后清空。

 

in                   out

3 7                 12
R 2                 10
C 3                 0
R 2                 5
R 1                 5
C 2                 4
C 1                 0
R 3

 

#include <bits/stdc++.h>
#define MAX 2000005
typedef long long ll;
using namespace std;

int bh[MAX],bl[MAX];
ll pre[MAX];
ll h[MAX],l[MAX];
char s[5];
ll vh=0,vl=0,vhc=0,vlc=0;

void init(){
    pre[1]=1;
    for(int i=2;i<=2000000;i++){
        pre[i]=pre[i-1]+i;
    }
}
int main(void)
{
    freopen("adjustment.in","r",stdin);
    freopen("adjustment.out","w",stdout);
    init();
    int n,q;
    ll x;
    scanf("%d%d",&n,&q);
    for(int i=1;i<=n;i++){
        h[i]=pre[i+n]-pre[i];
        l[i]=pre[i+n]-pre[i];
    }
    while(q--){
        scanf(" %s %I64d",s,&x);
        if(s[0]==R){
            if(bh[x]==1){
                printf("0
");
                continue;
            }
            bh[x]=1;
            vh+=x;
            vhc++;
            ll ans=h[x];
            ans-=vlc*x+vl;
            printf("%I64d
",ans);
        }
        else{
            if(bl[x]==1){
                printf("0
");
                continue;
            }
            bl[x]=1;
            vl+=x;
            vlc++;
            ll ans=l[x];
            ans-=vhc*x+vh;
            printf("%I64d
",ans);
        }
    }
    return 0;
}

 

以上是关于Gym - 100851A Adjustment Office(O求行列和)的主要内容,如果未能解决你的问题,请参考以下文章

Gym 100851A Adjustment Office (思维)

模拟NEERC15 A Adjustment Office (2015-2016 ACM-ICPC)(Codeforces GYM 100851)

Minimum Adjustment Cost

Minimum Adjustment Cost

Minimum Adjustment Cost

lintcode-medium-Minimum Adjustment Cost