//by szh
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define debug(x) cerr<<#x<<"="<<x<<endl
#define pq priority_queue
#define inf 0x3f
#define rep(i,a,b) for (int i=a;i<(b);i++)
#define MP make_pair
#define SZ(x) (int(x.size()))
#define ll long long
#define mod 1000000007
#define ALL(x) x.begin(),x.end()
void inc(int &a,int b) {a=(a+b)%mod;}
void dec(int &a,int b) {a=(a-b+mod)%mod;}
int lowbit(int x) {return x&(-x);}
ll p0w(ll base,ll p) {ll ret=1;while(p>0){if (p%2ll==1ll) ret=ret*base%mod;base=base*base%mod;p/=2ll;}return ret;}
#include "rainbow.h"
const int maxn = 1e6+10;
int r,c;
vector <pii> river;
struct psegtree{
int rt[maxn<<4],tree[maxn<<4],ls[maxn<<4],rs[maxn<<4];
vector <pii> pos;
int tot;
int build(int cl,int cr) {
int cur = tot++;
if (cl<cr) {
int mid=cl+cr>>1;
ls[cur] = build(cl,mid);
rs[cur] = build(mid+1,cr);
}
tree[cur] = 0;
return cur;
}
int update(int k,int l,int r,int root) {
assert(root!=-1);
int cur = tot++;
tree[cur] = tree[root] + 1;
if (l==r) return cur;
int mid=l+r>>1;
if (k<=mid) ls[cur] = update(k,l,mid,ls[root]), rs[cur] = rs[root];
else rs[cur] = update(k,mid+1,r,rs[root]), ls[cur] = ls[root];
return cur;
}
void init() {
sort(ALL(pos));
pos.erase(unique(ALL(pos)),pos.end());
tot = 0;
rt[0] = build(0,c);
rep(i,0,SZ(pos)) {
rt[i+1] = update(pos[i].se,0,c,rt[i]);
assert(tree[rt[i+1]]==i+1);
}
}
int query(int node,int cl,int cr,int l,int r) {
assert(node!=-1);
if (node==-1) return 0;
if (l<=cl and cr<=r) return tree[node];
int mid = cl+cr>>1;
int ret = 0;
if (l<=mid) ret += query(ls[node],cl,mid,l,r);
if (r>mid) ret += query(rs[node],mid+1,cr,l,r);
return ret;
}
int query(int ar,int ac,int br,int bc) {
int ed = upper_bound(ALL(pos),MP(br,mod))-pos.begin();
int bg = lower_bound(ALL(pos),MP(ar,-1))-pos.begin();
// debug(SZ(pos));
// debug(ed),debug(bg);
// debug(query(rt[ed],0,c,ac,bc));
return query(rt[ed],0,c,ac,bc) - query(rt[bg],0,c,ac,bc);
}
}node,horizontal,vertical,grid;
int minr = mod,maxr=-1,minc = mod,maxc = -1;
void init(int R, int C, int sr, int sc, int M, char *S) {
r = R, c = C;
river.pb({sr,sc});
minr = min(minr,sr),maxr = max(maxr,sr);
minc = min(minc,sc),maxc = max(maxc,sc);
rep(i,0,M) {
if (S[i]=='S') sr++;
else if (S[i]=='E') sc++;
else if (S[i]=='W') sc--;
else sr--;
river.pb({sr,sc});
minr = min(minr,sr),maxr = max(maxr,sr);
minc = min(minc,sc),maxc = max(maxc,sc);
}
node.rt[0] = 0;
horizontal.rt[0] = 0;
vertical.rt[0] = 0;
grid.rt[0] = 0;
rep(i,0,SZ(river)) {
node.pos.pb(river[i]);
horizontal.pos.pb({river[i].fi,river[i].se-1});
horizontal.pos.pb(river[i]);
vertical.pos.pb({river[i].fi-1,river[i].se});
vertical.pos.pb(river[i]);
grid.pos.pb({river[i].fi-1,river[i].se});
grid.pos.pb({river[i].fi,river[i].se-1});
grid.pos.pb({river[i].fi-1,river[i].se-1});
grid.pos.pb({river[i].fi,river[i].se});
}
node.init();
horizontal.init();
vertical.init();
grid.init();
}
int colour(int ar, int ac, int br, int bc) {
ll V = 1ll*(br-ar+1)*(bc-ac+1) - node.query(ar,ac,br,bc);
ll E = 1ll*(br-ar+1)*(bc-ac) - horizontal.query(ar,ac,br,bc-1)
+ 1ll*(br-ar)*(bc-ac+1) - vertical.query(ar,ac,br-1,bc);
ll F = 1ll*(br-ar)*(bc-ac) - grid.query(ar,ac,br-1,bc-1);
// debug(V),debug(E),debug(F);
if (ar<minr and ac<minc and br>maxr and bc>maxc) F++;
return V-E+F;
}
Compilation message
rainbow.cpp: In member function 'int psegtree::build(int, int)':
rainbow.cpp:39:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
39 | int mid=cl+cr>>1;
| ~~^~~
rainbow.cpp: In member function 'int psegtree::update(int, int, int, int)':
rainbow.cpp:52:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
52 | int mid=l+r>>1;
| ~^~
rainbow.cpp: In member function 'int psegtree::query(int, int, int, int, int)':
rainbow.cpp:71:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
71 | int mid = cl+cr>>1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
4 ms |
1016 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
5 ms |
1364 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
3 ms |
724 KB |
Output is correct |
12 |
Correct |
4 ms |
984 KB |
Output is correct |
13 |
Correct |
4 ms |
1236 KB |
Output is correct |
14 |
Correct |
6 ms |
1748 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
832 ms |
107996 KB |
Output is correct |
4 |
Correct |
960 ms |
166792 KB |
Output is correct |
5 |
Correct |
1096 ms |
165748 KB |
Output is correct |
6 |
Correct |
884 ms |
135092 KB |
Output is correct |
7 |
Correct |
956 ms |
133368 KB |
Output is correct |
8 |
Correct |
587 ms |
32800 KB |
Output is correct |
9 |
Correct |
953 ms |
166728 KB |
Output is correct |
10 |
Correct |
952 ms |
165680 KB |
Output is correct |
11 |
Correct |
916 ms |
135244 KB |
Output is correct |
12 |
Correct |
418 ms |
156720 KB |
Output is correct |
13 |
Correct |
434 ms |
166696 KB |
Output is correct |
14 |
Correct |
490 ms |
165628 KB |
Output is correct |
15 |
Correct |
439 ms |
135476 KB |
Output is correct |
16 |
Correct |
909 ms |
125188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
164 ms |
163380 KB |
Output is correct |
3 |
Correct |
137 ms |
160868 KB |
Output is correct |
4 |
Correct |
143 ms |
163428 KB |
Output is correct |
5 |
Correct |
119 ms |
127632 KB |
Output is correct |
6 |
Correct |
104 ms |
54496 KB |
Output is correct |
7 |
Correct |
138 ms |
79424 KB |
Output is correct |
8 |
Correct |
163 ms |
119380 KB |
Output is correct |
9 |
Correct |
161 ms |
104020 KB |
Output is correct |
10 |
Correct |
54 ms |
22260 KB |
Output is correct |
11 |
Correct |
129 ms |
69360 KB |
Output is correct |
12 |
Correct |
187 ms |
163376 KB |
Output is correct |
13 |
Correct |
136 ms |
160952 KB |
Output is correct |
14 |
Correct |
159 ms |
163444 KB |
Output is correct |
15 |
Correct |
132 ms |
127668 KB |
Output is correct |
16 |
Correct |
105 ms |
49436 KB |
Output is correct |
17 |
Correct |
131 ms |
79444 KB |
Output is correct |
18 |
Correct |
180 ms |
163380 KB |
Output is correct |
19 |
Correct |
150 ms |
164572 KB |
Output is correct |
20 |
Correct |
148 ms |
164396 KB |
Output is correct |
21 |
Correct |
154 ms |
119372 KB |
Output is correct |
22 |
Correct |
156 ms |
104008 KB |
Output is correct |
23 |
Correct |
59 ms |
22268 KB |
Output is correct |
24 |
Correct |
132 ms |
69300 KB |
Output is correct |
25 |
Correct |
173 ms |
163352 KB |
Output is correct |
26 |
Correct |
134 ms |
160956 KB |
Output is correct |
27 |
Correct |
141 ms |
163396 KB |
Output is correct |
28 |
Correct |
126 ms |
127764 KB |
Output is correct |
29 |
Correct |
103 ms |
49460 KB |
Output is correct |
30 |
Correct |
131 ms |
79444 KB |
Output is correct |
31 |
Correct |
173 ms |
163432 KB |
Output is correct |
32 |
Correct |
155 ms |
164540 KB |
Output is correct |
33 |
Correct |
166 ms |
164428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
4 ms |
1016 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
5 ms |
1364 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
3 ms |
724 KB |
Output is correct |
12 |
Correct |
4 ms |
984 KB |
Output is correct |
13 |
Correct |
4 ms |
1236 KB |
Output is correct |
14 |
Correct |
6 ms |
1748 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
781 ms |
54344 KB |
Output is correct |
19 |
Correct |
229 ms |
6200 KB |
Output is correct |
20 |
Correct |
189 ms |
4064 KB |
Output is correct |
21 |
Correct |
211 ms |
4500 KB |
Output is correct |
22 |
Correct |
215 ms |
4924 KB |
Output is correct |
23 |
Correct |
252 ms |
6092 KB |
Output is correct |
24 |
Correct |
180 ms |
4680 KB |
Output is correct |
25 |
Correct |
193 ms |
5036 KB |
Output is correct |
26 |
Correct |
209 ms |
5364 KB |
Output is correct |
27 |
Correct |
439 ms |
46656 KB |
Output is correct |
28 |
Correct |
302 ms |
29700 KB |
Output is correct |
29 |
Correct |
372 ms |
43748 KB |
Output is correct |
30 |
Correct |
554 ms |
93948 KB |
Output is correct |
31 |
Correct |
3 ms |
468 KB |
Output is correct |
32 |
Correct |
727 ms |
46940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
4 ms |
1016 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
5 ms |
1364 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
3 ms |
724 KB |
Output is correct |
12 |
Correct |
4 ms |
984 KB |
Output is correct |
13 |
Correct |
4 ms |
1236 KB |
Output is correct |
14 |
Correct |
6 ms |
1748 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
781 ms |
54344 KB |
Output is correct |
19 |
Correct |
229 ms |
6200 KB |
Output is correct |
20 |
Correct |
189 ms |
4064 KB |
Output is correct |
21 |
Correct |
211 ms |
4500 KB |
Output is correct |
22 |
Correct |
215 ms |
4924 KB |
Output is correct |
23 |
Correct |
252 ms |
6092 KB |
Output is correct |
24 |
Correct |
180 ms |
4680 KB |
Output is correct |
25 |
Correct |
193 ms |
5036 KB |
Output is correct |
26 |
Correct |
209 ms |
5364 KB |
Output is correct |
27 |
Correct |
439 ms |
46656 KB |
Output is correct |
28 |
Correct |
302 ms |
29700 KB |
Output is correct |
29 |
Correct |
372 ms |
43748 KB |
Output is correct |
30 |
Correct |
554 ms |
93948 KB |
Output is correct |
31 |
Correct |
3 ms |
468 KB |
Output is correct |
32 |
Correct |
727 ms |
46940 KB |
Output is correct |
33 |
Correct |
164 ms |
163380 KB |
Output is correct |
34 |
Correct |
137 ms |
160868 KB |
Output is correct |
35 |
Correct |
143 ms |
163428 KB |
Output is correct |
36 |
Correct |
119 ms |
127632 KB |
Output is correct |
37 |
Correct |
104 ms |
54496 KB |
Output is correct |
38 |
Correct |
138 ms |
79424 KB |
Output is correct |
39 |
Correct |
163 ms |
119380 KB |
Output is correct |
40 |
Correct |
161 ms |
104020 KB |
Output is correct |
41 |
Correct |
54 ms |
22260 KB |
Output is correct |
42 |
Correct |
129 ms |
69360 KB |
Output is correct |
43 |
Correct |
187 ms |
163376 KB |
Output is correct |
44 |
Correct |
136 ms |
160952 KB |
Output is correct |
45 |
Correct |
159 ms |
163444 KB |
Output is correct |
46 |
Correct |
132 ms |
127668 KB |
Output is correct |
47 |
Correct |
105 ms |
49436 KB |
Output is correct |
48 |
Correct |
131 ms |
79444 KB |
Output is correct |
49 |
Correct |
180 ms |
163380 KB |
Output is correct |
50 |
Correct |
150 ms |
164572 KB |
Output is correct |
51 |
Correct |
148 ms |
164396 KB |
Output is correct |
52 |
Correct |
154 ms |
119372 KB |
Output is correct |
53 |
Correct |
156 ms |
104008 KB |
Output is correct |
54 |
Correct |
59 ms |
22268 KB |
Output is correct |
55 |
Correct |
132 ms |
69300 KB |
Output is correct |
56 |
Correct |
173 ms |
163352 KB |
Output is correct |
57 |
Correct |
134 ms |
160956 KB |
Output is correct |
58 |
Correct |
141 ms |
163396 KB |
Output is correct |
59 |
Correct |
126 ms |
127764 KB |
Output is correct |
60 |
Correct |
103 ms |
49460 KB |
Output is correct |
61 |
Correct |
131 ms |
79444 KB |
Output is correct |
62 |
Correct |
173 ms |
163432 KB |
Output is correct |
63 |
Correct |
155 ms |
164540 KB |
Output is correct |
64 |
Correct |
166 ms |
164428 KB |
Output is correct |
65 |
Correct |
832 ms |
107996 KB |
Output is correct |
66 |
Correct |
960 ms |
166792 KB |
Output is correct |
67 |
Correct |
1096 ms |
165748 KB |
Output is correct |
68 |
Correct |
884 ms |
135092 KB |
Output is correct |
69 |
Correct |
956 ms |
133368 KB |
Output is correct |
70 |
Correct |
587 ms |
32800 KB |
Output is correct |
71 |
Correct |
953 ms |
166728 KB |
Output is correct |
72 |
Correct |
952 ms |
165680 KB |
Output is correct |
73 |
Correct |
916 ms |
135244 KB |
Output is correct |
74 |
Correct |
418 ms |
156720 KB |
Output is correct |
75 |
Correct |
434 ms |
166696 KB |
Output is correct |
76 |
Correct |
490 ms |
165628 KB |
Output is correct |
77 |
Correct |
439 ms |
135476 KB |
Output is correct |
78 |
Correct |
909 ms |
125188 KB |
Output is correct |
79 |
Correct |
1114 ms |
122780 KB |
Output is correct |
80 |
Correct |
1080 ms |
107412 KB |
Output is correct |
81 |
Correct |
437 ms |
25816 KB |
Output is correct |
82 |
Correct |
603 ms |
72712 KB |
Output is correct |
83 |
Correct |
774 ms |
166948 KB |
Output is correct |
84 |
Correct |
875 ms |
164556 KB |
Output is correct |
85 |
Correct |
769 ms |
167156 KB |
Output is correct |
86 |
Correct |
757 ms |
131172 KB |
Output is correct |
87 |
Correct |
594 ms |
52864 KB |
Output is correct |
88 |
Correct |
683 ms |
83120 KB |
Output is correct |
89 |
Correct |
702 ms |
166924 KB |
Output is correct |
90 |
Correct |
949 ms |
168040 KB |
Output is correct |
91 |
Correct |
744 ms |
167828 KB |
Output is correct |