CF368 E - Garlands

时间:2022-06-20 11:37:38

主席树 其实暴力二维树状还更快

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 2005; int N,M,K,Q;
struct Node{
int x,y,w;
Node(int a=0, int b=0, int c=0):x(a),y(b),w(c){}
bool operator < (Node T) {
if(x != T.x) return x < T.x;
else return y < T.y;
}
};
struct Ask{
int x1,y1,x2,y2; int ty;
}Do[1000005];
ll ans[1000005]; int vis[MAXN];
vector<Node> gar[MAXN];
vector<int> has[MAXN]; int root[MAXN];
struct Pode{
int ls,rs; ll sum;
}tree[MAXN*30];
int tot;
int Update(int pos,int num,int l,int r,int pre){
int rt = ++tot;
tree[rt] = tree[pre];
tree[rt].sum += num;
if(l == r) return rt;
int m = (l+r)>>1;
if(pos <= m) tree[rt].ls = Update(pos,num,l,m,tree[pre].ls);
else tree[rt].rs = Update(pos,num,m+1,r,tree[pre].rs);
return rt;
}
ll Query(int L,int R,int l,int r,int rt) {
if(!rt) return 0;
if(L <= l && r <= R) return tree[rt].sum;
int m = (l+r) >>1;
ll ans = 0;
if(L <= m) ans += Query(L,R,l,m,tree[rt].ls);
if(R > m) ans += Query(L,R,m+1,r,tree[rt].rs);
return ans;
} int main(){
while(~scanf("%d %d %d",&N,&M,&K)) {
memset(ans,0,sizeof(ans));
for(int i = 1; i <= K; ++i) gar[i].clear(), has[i].clear(); for(int i = 1; i <= K; ++i) {
vis[i] = 1;
int a; scanf("%d",&a);
for(int j = 0; j < a; ++j) {
int b,c,d; scanf("%d %d %d",&b,&c,&d);
gar[i].push_back(Node(b,c,d));
}
sort(gar[i].begin(), gar[i].end());
} scanf("%d",&Q);
for(int i = 1; i <= Q; ++i) {
char a[10]; scanf("%s",a);
if(a[0] == 'S') {
int b; scanf("%d",&b); Do[i].ty = 2;
vis[b] ^= 1;
}else {
scanf("%d %d %d %d",&Do[i].x1,&Do[i].y1,&Do[i].x2,&Do[i].y2); Do[i].ty = 1;
for(int j = 1; j <= K; ++j) {
if(vis[j]) {
has[j].push_back(i);
}
}
}
} tree[0].ls = tree[0].rs = tree[0].sum = 0;
for(int i = 1; i <= K; ++i) {
tot = 0; root[0] = 0;
for(int j = 0; j < (int)gar[i].size(); ++j) {
root[j+1] = Update(gar[i][j].y, gar[i][j].w, 1,M,root[j]);
}
for(int j = 0; j < (int)has[i].size(); ++j) {
int tt = has[i][j];
int x1 = Do[tt].x1; int y1 = Do[tt].y1; int x2 = Do[tt].x2; int y2 = Do[tt].y2; Node t1 = Node(x2,M+1);
int pos = lower_bound(gar[i].begin(), gar[i].end(), t1)-gar[i].begin();
if(pos) ans[tt] += Query(y1,y2,1,M,root[pos]); Node t2 = Node(x1,0);
int _pos = lower_bound(gar[i].begin(), gar[i].end(), t2)-gar[i].begin();
if(_pos) ans[tt] -= Query(y1,y2,1,M,root[_pos]);
}
}
for(int i = 1; i <= Q; ++i) {
if(Do[i].ty == 1) printf("%lld\n",ans[i]);
}
}
return 0;
}