Adjustment Office
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; }