p4390 [boi2007]mokia 摩基亚(cdq分治)


一样是cdq的板子

照着园丁的烦恼就好了

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128

#include <cstring>
#include <algorithm>
using namespace std;
int w,cntq,cnta,nothing,type,qid,aid;
namespace BIT{
int bit[2000100];
int (int x){
return x&(-x);
}
void add(int pos,int x){
while(pos<=w){
bit[pos]+=x;
pos+=lowbit(pos);
}
}
int query(int pos){
int ans=0;
while(pos){
ans+=bit[pos];
pos-=lowbit(pos);
}
return ans;
}
void clear(int pos){
while(pos<=w){
if(bit[pos])
bit[pos]=0;
else
break;
pos+=lowbit(pos);
}
}
};

int ans[10100];
struct Query{
int type,val,posx,posy,IorD,aid;
bool operator < (const Query &b) const{
return posx==b.posx?type<b.type:posx<b.posx;
}
}query[200100];
Query tmp[200100];
void cdq(int L,int R){

if(R<=L+1)
return;
int mid=(L+R)>>1;
cdq(L,mid);
cdq(mid,R);
int l=L,r=mid,tot=0;
while(l<mid&&r<R){
if(query[l]<query[r]){
if(query[l].type==1)
BIT::add(query[l].posy,query[l].val);
tmp[++tot]=query[l++];
}
else{
if(query[r].type==2)
ans[query[r].aid]+=query[r].IorD*BIT::query(query[r].posy);
tmp[++tot]=query[r++];
}
}
while(l<mid)
tmp[++tot]=query[l++];
while(r<R){
if(query[r].type==2)
ans[query[r].aid]+=query[r].IorD*BIT::query(query[r].posy);
tmp[++tot]=query[r++];
}
for(int i=1;i<=tot;i++){
BIT::clear(tmp[i].posy);
query[L+i-1]=tmp[i];
}
}
int main(){
scanf("%d %d",&nothing,&w);
scanf("%d",&type);
w++;
while(type!=3){
if(type==1){
int x,y,a;
scanf("%d %d %d",&x,&y,&a);
// x+=2;y+=2;
x++,y++;

query[++qid].type=1;
query[qid].posy=y;
query[qid].posx=x;
query[qid].val=a;
}
else{
int x1,y1,x2,y2;
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
// x1+=2,x2+=2,y1+=2,y2+=2;
x1++,x2++,y1++,y2++;

query[++qid].type=2;
query[qid].aid=++aid;
query[qid].IorD=1;
query[qid].posx=x2;
query[qid].posy=y2;

query[++qid].type=2;
query[qid].aid=aid;
query[qid].IorD=-1;
query[qid].posx=x1-1;
query[qid].posy=y2;

query[++qid].type=2;
query[qid].aid=aid;
query[qid].IorD=-1;
query[qid].posx=x2;
query[qid].posy=y1-1;

query[++qid].type=2;
query[qid].aid=aid;
query[qid].IorD=1;
query[qid].posx=x1-1;
query[qid].posy=y1-1;
}
scanf("%d",&type);
}
cdq(0,qid+1);
for(int i=1;i<=aid;i++)
printf("%dn",ans[i]);
return 0;
}