#include <bits/stdc++.h>
using namespace std;
#define M ((L+R)/2)
const int MAXN = 200000;
const int SIZE = (6e5+9)*19+1;
struct pres{
set<int> s[MAXN+5];
void add(int x,int y){
s[x].insert(y);
}
int root[MAXN+5];
void bld(){
root[1] = build();
for(int i = 1;i<=MAXN;i++){
root[i+1] = root[i];
for(auto j:s[i]){
int vl = query(j,j,root[i+1]);vl++;
root[i+1] = update(j,root[i+1],vl);
}
}
}
int qry(int l,int r,int L,int R){
if(L>R)return 0;
return query(L,R,root[r+1])-(query(L,R,root[l]));
}
int l[SIZE], r[SIZE], st[SIZE], NODES = 0;
int n;
int newleaf(int value) {
int p = ++NODES;
l[p] = r[p] = 0; // null
st[p] = value;
return p;
}
int newparent(int lef, int rig) {
int p = ++NODES;
l[p] = lef;
r[p] = rig;
st[p] = st[lef] + st[rig]; // immediately update value from children
return p;
}
int update(int i,int p,int x, int L=1, int R=MAXN) {
if (L == R)return newleaf(x);
if (i <= M)return newparent(update(i,l[p],x, L, M), r[p]);
else return newparent(l[p], update(i,r[p],x, M + 1, R));
}
int build(int L=1, int R=MAXN) {
if (L == R) return newleaf(0); // construct as leaf
else return newparent(build(L, M),build(M+1,R)); // construct as parent
}
int query(int lq,int rq,int p,int L = 1,int R = MAXN){
if(L>=lq&&R<=rq)return st[p];
if(L>rq||R<lq)return 0;
return query(lq,rq,l[p],L,M)+query(lq,rq,r[p],M+1,R);
}
}ver,edgehorz,edgevert,rivers;
void add_river(int x,int y){
rivers.add(x,y);
ver.add(x,y);
ver.add(x+1,y);
ver.add(x,y+1);
ver.add(x+1,y+1);
edgehorz.add(x,y);
edgehorz.add(x+1,y);
edgevert.add(x,y);
edgevert.add(x,y+1);
}
int mxr ,mnr,mxc,mnc;
void init(int R, int C, int sr, int sc, int MM, char *S) {
add_river(sr,sc);
mxr = sr,mnr = sr,mxc = sc,mnc = sc;
for(int i = 0;i<MM;i++){
if(S[i]=='N')sr--;
if(S[i]=='S')sr++;
if(S[i]=='W')sc--;
if(S[i]=='E')sc++;
//cout<<sr<<" "<<sc<<endl;
add_river(sr,sc);
mxr = max(mxr,sr);
mnr = min(mnr,sr);
mxc = max(mxc,sc);
mnc = min(mnc,sc);
}
rivers.bld();ver.bld();
edgehorz.bld();edgevert.bld();
}
int colour(int ar, int ac, int br, int bc) {
int E = edgehorz.qry(ar + 1, br, ac, bc) + edgevert.qry(ar, br, ac + 1, bc);
int V = ver.qry(ar + 1, br, ac + 1, bc);
int R = rivers.qry(ar, br, ac, bc);
//cout<<R<<endl;
int C = (ar >= mnr || br <= mxr || ac >= mnc || bc <= mxc ? 1 : 2);
return E - V + C - R;
}/*
int main(){
init(6, 4,
3, 3, 9,
"NWESSWEWS");
cout<<colour(1,2,5,3);
}*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
60116 KB |
Output is correct |
2 |
Correct |
33 ms |
61188 KB |
Output is correct |
3 |
Correct |
32 ms |
60164 KB |
Output is correct |
4 |
Correct |
32 ms |
60248 KB |
Output is correct |
5 |
Correct |
34 ms |
61384 KB |
Output is correct |
6 |
Correct |
31 ms |
59812 KB |
Output is correct |
7 |
Correct |
30 ms |
59904 KB |
Output is correct |
8 |
Correct |
29 ms |
59860 KB |
Output is correct |
9 |
Correct |
29 ms |
59796 KB |
Output is correct |
10 |
Correct |
30 ms |
59892 KB |
Output is correct |
11 |
Correct |
32 ms |
60312 KB |
Output is correct |
12 |
Correct |
36 ms |
60856 KB |
Output is correct |
13 |
Correct |
35 ms |
61940 KB |
Output is correct |
14 |
Correct |
35 ms |
62420 KB |
Output is correct |
15 |
Correct |
30 ms |
59860 KB |
Output is correct |
16 |
Correct |
31 ms |
59860 KB |
Output is correct |
17 |
Correct |
30 ms |
59780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
59860 KB |
Output is correct |
2 |
Correct |
30 ms |
59780 KB |
Output is correct |
3 |
Correct |
757 ms |
155996 KB |
Output is correct |
4 |
Correct |
1008 ms |
220376 KB |
Output is correct |
5 |
Correct |
990 ms |
219084 KB |
Output is correct |
6 |
Correct |
880 ms |
182776 KB |
Output is correct |
7 |
Correct |
849 ms |
180448 KB |
Output is correct |
8 |
Correct |
415 ms |
60664 KB |
Output is correct |
9 |
Correct |
1008 ms |
220364 KB |
Output is correct |
10 |
Correct |
1014 ms |
219192 KB |
Output is correct |
11 |
Correct |
890 ms |
182600 KB |
Output is correct |
12 |
Correct |
388 ms |
209080 KB |
Output is correct |
13 |
Correct |
411 ms |
220324 KB |
Output is correct |
14 |
Correct |
420 ms |
219156 KB |
Output is correct |
15 |
Correct |
381 ms |
182724 KB |
Output is correct |
16 |
Correct |
860 ms |
173440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
59860 KB |
Output is correct |
2 |
Correct |
372 ms |
219720 KB |
Output is correct |
3 |
Correct |
176 ms |
217292 KB |
Output is correct |
4 |
Correct |
221 ms |
219724 KB |
Output is correct |
5 |
Correct |
183 ms |
178544 KB |
Output is correct |
6 |
Correct |
95 ms |
89764 KB |
Output is correct |
7 |
Correct |
159 ms |
119556 KB |
Output is correct |
8 |
Correct |
323 ms |
202708 KB |
Output is correct |
9 |
Correct |
302 ms |
186484 KB |
Output is correct |
10 |
Correct |
107 ms |
91436 KB |
Output is correct |
11 |
Correct |
183 ms |
138160 KB |
Output is correct |
12 |
Correct |
356 ms |
219812 KB |
Output is correct |
13 |
Correct |
179 ms |
217304 KB |
Output is correct |
14 |
Correct |
225 ms |
219732 KB |
Output is correct |
15 |
Correct |
183 ms |
178532 KB |
Output is correct |
16 |
Correct |
87 ms |
83736 KB |
Output is correct |
17 |
Correct |
145 ms |
119576 KB |
Output is correct |
18 |
Correct |
295 ms |
219900 KB |
Output is correct |
19 |
Correct |
267 ms |
220832 KB |
Output is correct |
20 |
Correct |
307 ms |
220772 KB |
Output is correct |
21 |
Correct |
308 ms |
202712 KB |
Output is correct |
22 |
Correct |
307 ms |
186484 KB |
Output is correct |
23 |
Correct |
111 ms |
91592 KB |
Output is correct |
24 |
Correct |
199 ms |
138232 KB |
Output is correct |
25 |
Correct |
352 ms |
219736 KB |
Output is correct |
26 |
Correct |
182 ms |
217248 KB |
Output is correct |
27 |
Correct |
218 ms |
219804 KB |
Output is correct |
28 |
Correct |
183 ms |
178512 KB |
Output is correct |
29 |
Correct |
87 ms |
83820 KB |
Output is correct |
30 |
Correct |
152 ms |
119772 KB |
Output is correct |
31 |
Correct |
281 ms |
219836 KB |
Output is correct |
32 |
Correct |
264 ms |
220880 KB |
Output is correct |
33 |
Correct |
266 ms |
220792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
60116 KB |
Output is correct |
2 |
Correct |
33 ms |
61188 KB |
Output is correct |
3 |
Correct |
32 ms |
60164 KB |
Output is correct |
4 |
Correct |
32 ms |
60248 KB |
Output is correct |
5 |
Correct |
34 ms |
61384 KB |
Output is correct |
6 |
Correct |
31 ms |
59812 KB |
Output is correct |
7 |
Correct |
30 ms |
59904 KB |
Output is correct |
8 |
Correct |
29 ms |
59860 KB |
Output is correct |
9 |
Correct |
29 ms |
59796 KB |
Output is correct |
10 |
Correct |
30 ms |
59892 KB |
Output is correct |
11 |
Correct |
32 ms |
60312 KB |
Output is correct |
12 |
Correct |
36 ms |
60856 KB |
Output is correct |
13 |
Correct |
35 ms |
61940 KB |
Output is correct |
14 |
Correct |
35 ms |
62420 KB |
Output is correct |
15 |
Correct |
30 ms |
59860 KB |
Output is correct |
16 |
Correct |
31 ms |
59860 KB |
Output is correct |
17 |
Correct |
30 ms |
59780 KB |
Output is correct |
18 |
Correct |
694 ms |
141308 KB |
Output is correct |
19 |
Correct |
222 ms |
64140 KB |
Output is correct |
20 |
Correct |
168 ms |
61164 KB |
Output is correct |
21 |
Correct |
194 ms |
61900 KB |
Output is correct |
22 |
Correct |
189 ms |
62568 KB |
Output is correct |
23 |
Correct |
216 ms |
64012 KB |
Output is correct |
24 |
Correct |
175 ms |
61768 KB |
Output is correct |
25 |
Correct |
182 ms |
62340 KB |
Output is correct |
26 |
Correct |
193 ms |
62820 KB |
Output is correct |
27 |
Correct |
400 ms |
125880 KB |
Output is correct |
28 |
Correct |
293 ms |
91952 KB |
Output is correct |
29 |
Correct |
400 ms |
120116 KB |
Output is correct |
30 |
Correct |
597 ms |
220492 KB |
Output is correct |
31 |
Correct |
33 ms |
59908 KB |
Output is correct |
32 |
Correct |
554 ms |
126592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
60116 KB |
Output is correct |
2 |
Correct |
33 ms |
61188 KB |
Output is correct |
3 |
Correct |
32 ms |
60164 KB |
Output is correct |
4 |
Correct |
32 ms |
60248 KB |
Output is correct |
5 |
Correct |
34 ms |
61384 KB |
Output is correct |
6 |
Correct |
31 ms |
59812 KB |
Output is correct |
7 |
Correct |
30 ms |
59904 KB |
Output is correct |
8 |
Correct |
29 ms |
59860 KB |
Output is correct |
9 |
Correct |
29 ms |
59796 KB |
Output is correct |
10 |
Correct |
30 ms |
59892 KB |
Output is correct |
11 |
Correct |
32 ms |
60312 KB |
Output is correct |
12 |
Correct |
36 ms |
60856 KB |
Output is correct |
13 |
Correct |
35 ms |
61940 KB |
Output is correct |
14 |
Correct |
35 ms |
62420 KB |
Output is correct |
15 |
Correct |
30 ms |
59860 KB |
Output is correct |
16 |
Correct |
31 ms |
59860 KB |
Output is correct |
17 |
Correct |
30 ms |
59780 KB |
Output is correct |
18 |
Correct |
694 ms |
141308 KB |
Output is correct |
19 |
Correct |
222 ms |
64140 KB |
Output is correct |
20 |
Correct |
168 ms |
61164 KB |
Output is correct |
21 |
Correct |
194 ms |
61900 KB |
Output is correct |
22 |
Correct |
189 ms |
62568 KB |
Output is correct |
23 |
Correct |
216 ms |
64012 KB |
Output is correct |
24 |
Correct |
175 ms |
61768 KB |
Output is correct |
25 |
Correct |
182 ms |
62340 KB |
Output is correct |
26 |
Correct |
193 ms |
62820 KB |
Output is correct |
27 |
Correct |
400 ms |
125880 KB |
Output is correct |
28 |
Correct |
293 ms |
91952 KB |
Output is correct |
29 |
Correct |
400 ms |
120116 KB |
Output is correct |
30 |
Correct |
597 ms |
220492 KB |
Output is correct |
31 |
Correct |
33 ms |
59908 KB |
Output is correct |
32 |
Correct |
554 ms |
126592 KB |
Output is correct |
33 |
Correct |
372 ms |
219720 KB |
Output is correct |
34 |
Correct |
176 ms |
217292 KB |
Output is correct |
35 |
Correct |
221 ms |
219724 KB |
Output is correct |
36 |
Correct |
183 ms |
178544 KB |
Output is correct |
37 |
Correct |
95 ms |
89764 KB |
Output is correct |
38 |
Correct |
159 ms |
119556 KB |
Output is correct |
39 |
Correct |
323 ms |
202708 KB |
Output is correct |
40 |
Correct |
302 ms |
186484 KB |
Output is correct |
41 |
Correct |
107 ms |
91436 KB |
Output is correct |
42 |
Correct |
183 ms |
138160 KB |
Output is correct |
43 |
Correct |
356 ms |
219812 KB |
Output is correct |
44 |
Correct |
179 ms |
217304 KB |
Output is correct |
45 |
Correct |
225 ms |
219732 KB |
Output is correct |
46 |
Correct |
183 ms |
178532 KB |
Output is correct |
47 |
Correct |
87 ms |
83736 KB |
Output is correct |
48 |
Correct |
145 ms |
119576 KB |
Output is correct |
49 |
Correct |
295 ms |
219900 KB |
Output is correct |
50 |
Correct |
267 ms |
220832 KB |
Output is correct |
51 |
Correct |
307 ms |
220772 KB |
Output is correct |
52 |
Correct |
308 ms |
202712 KB |
Output is correct |
53 |
Correct |
307 ms |
186484 KB |
Output is correct |
54 |
Correct |
111 ms |
91592 KB |
Output is correct |
55 |
Correct |
199 ms |
138232 KB |
Output is correct |
56 |
Correct |
352 ms |
219736 KB |
Output is correct |
57 |
Correct |
182 ms |
217248 KB |
Output is correct |
58 |
Correct |
218 ms |
219804 KB |
Output is correct |
59 |
Correct |
183 ms |
178512 KB |
Output is correct |
60 |
Correct |
87 ms |
83820 KB |
Output is correct |
61 |
Correct |
152 ms |
119772 KB |
Output is correct |
62 |
Correct |
281 ms |
219836 KB |
Output is correct |
63 |
Correct |
264 ms |
220880 KB |
Output is correct |
64 |
Correct |
266 ms |
220792 KB |
Output is correct |
65 |
Correct |
757 ms |
155996 KB |
Output is correct |
66 |
Correct |
1008 ms |
220376 KB |
Output is correct |
67 |
Correct |
990 ms |
219084 KB |
Output is correct |
68 |
Correct |
880 ms |
182776 KB |
Output is correct |
69 |
Correct |
849 ms |
180448 KB |
Output is correct |
70 |
Correct |
415 ms |
60664 KB |
Output is correct |
71 |
Correct |
1008 ms |
220364 KB |
Output is correct |
72 |
Correct |
1014 ms |
219192 KB |
Output is correct |
73 |
Correct |
890 ms |
182600 KB |
Output is correct |
74 |
Correct |
388 ms |
209080 KB |
Output is correct |
75 |
Correct |
411 ms |
220324 KB |
Output is correct |
76 |
Correct |
420 ms |
219156 KB |
Output is correct |
77 |
Correct |
381 ms |
182724 KB |
Output is correct |
78 |
Correct |
860 ms |
173440 KB |
Output is correct |
79 |
Correct |
1095 ms |
203208 KB |
Output is correct |
80 |
Correct |
1070 ms |
187288 KB |
Output is correct |
81 |
Correct |
384 ms |
92216 KB |
Output is correct |
82 |
Correct |
525 ms |
138976 KB |
Output is correct |
83 |
Correct |
953 ms |
220448 KB |
Output is correct |
84 |
Correct |
707 ms |
218008 KB |
Output is correct |
85 |
Correct |
701 ms |
220492 KB |
Output is correct |
86 |
Correct |
693 ms |
179252 KB |
Output is correct |
87 |
Correct |
497 ms |
84428 KB |
Output is correct |
88 |
Correct |
580 ms |
120412 KB |
Output is correct |
89 |
Correct |
725 ms |
220552 KB |
Output is correct |
90 |
Correct |
921 ms |
221472 KB |
Output is correct |
91 |
Correct |
750 ms |
221464 KB |
Output is correct |