#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 200001;
// #define LOCAL
#ifdef LOCAL
#else
#include "rainbow.h"
#endif
template<int SZ> struct Seg {
vi v[SZ];
vpi tmp;
void INS(int x, int y) {
tmp.pb({x,y});
}
void ins(int x, int y) { for (;x<SZ;x+=(x&-x)) v[x].pb(y); }
int query(int x, int y) {
int ret = 0;
for (;x;x-=(x&-x)) ret += ub(all(v[x]),y)-v[x].begin();
return ret;
}
void build() {
sort(all(tmp)); tmp.erase(unique(all(tmp)),tmp.end());
for (auto a: tmp) ins(a.f,a.s);
FOR(i,1,SZ) {
sort(all(v[i]));
}
}
int query(int x0, int x1, int y0, int y1) {
return query(x1,y1)-query(x0-1,y1)-query(x1,y0-1)+query(x0-1,y0-1);
}
};
Seg<MX> seg[4];
pi rr, cc;
void ins(pi x) {
seg[0].INS(x.f,x.s);
F0R(i,2) if (x.f > i) {
seg[1].INS(x.f-i,x.s);
F0R(j,2) if (x.s > j) seg[3].INS(x.f-i,x.s-j);
}
F0R(j,2) if (x.s > j) seg[2].INS(x.f,x.s-j);
}
void init(int R, int C, int sr, int sc, int M, char *S) {
set<pi> tmp[4];
pi cur = {sr,sc}; ins(cur);
rr = {cur.f,cur.f}, cc = {cur.s,cur.s};
F0R(i,M) {
switch(S[i]) {
case 'N':
cur.f --;
break;
case 'W':
cur.s --;
break;
case 'E':
cur.s ++;
break;
case 'S':
cur.f ++;
break;
}
rr.f = min(rr.f,cur.f);
rr.s = max(rr.s,cur.f);
cc.f = min(cc.f,cur.s);
cc.s = max(cc.s,cur.s);
ins(cur);
}
F0R(i,4) seg[i].build();
}
int query(int x0, int x1, int y0, int y1) {
// cout << x0 << " " << x1 << " " << y0 << " " << y1 << " " << seg[0].query(x0,x1,y0,y1) << "\n";
return 1-seg[0].query(x0,x1,y0,y1)-seg[3].query(x0,x1-1,y0,y1-1) // square, shaded
+seg[1].query(x0,x1-1,y0,y1)+seg[2].query(x0,x1,y0,y1-1); // vert, hori
}
int colour(int ar, int ac, int br, int bc) {
if (ar < rr.f && rr.s < br && ac < cc.f && cc.s < bc)
return query(rr.f-1,rr.s+1,cc.f-1,cc.s+1)+1;
return query(ar,br,ac,bc);
}
#ifdef LOCAL
#include <stdio.h>
static int R, C, M, Q;
static int sr, sc;
static char S[100000 + 5];
int main() {
scanf("%d %d %d %d", &R, &C, &M, &Q);
scanf("%d %d", &sr, &sc);
if (M > 0) {
scanf(" %s ", S);
}
init(R, C, sr, sc, M, S);
int query;
for (query = 0; query < Q; query++) {
int ar, ac, br, bc;
scanf("%d %d %d %d", &ar, &ac, &br, &bc);
printf("%d\n", colour(ar, ac, br, bc));
}
return 0;
}
#endif
/* Look for:
* the exact constraints (multiple sets are too slow for n=10^6 :( )
* special cases (n=1?)
* overflow (ll vs int?)
* array bounds
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
19192 KB |
Output is correct |
2 |
Correct |
28 ms |
19744 KB |
Output is correct |
3 |
Correct |
25 ms |
19744 KB |
Output is correct |
4 |
Correct |
24 ms |
19744 KB |
Output is correct |
5 |
Correct |
37 ms |
20108 KB |
Output is correct |
6 |
Correct |
25 ms |
20108 KB |
Output is correct |
7 |
Correct |
26 ms |
20108 KB |
Output is correct |
8 |
Correct |
25 ms |
20108 KB |
Output is correct |
9 |
Correct |
26 ms |
20108 KB |
Output is correct |
10 |
Correct |
24 ms |
20108 KB |
Output is correct |
11 |
Correct |
27 ms |
20108 KB |
Output is correct |
12 |
Correct |
33 ms |
20108 KB |
Output is correct |
13 |
Correct |
36 ms |
20376 KB |
Output is correct |
14 |
Correct |
45 ms |
20824 KB |
Output is correct |
15 |
Correct |
26 ms |
20824 KB |
Output is correct |
16 |
Correct |
25 ms |
20824 KB |
Output is correct |
17 |
Correct |
24 ms |
20824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
20824 KB |
Output is correct |
2 |
Correct |
24 ms |
20824 KB |
Output is correct |
3 |
Correct |
622 ms |
49096 KB |
Output is correct |
4 |
Correct |
1028 ms |
71796 KB |
Output is correct |
5 |
Correct |
939 ms |
71796 KB |
Output is correct |
6 |
Correct |
856 ms |
71796 KB |
Output is correct |
7 |
Correct |
752 ms |
71796 KB |
Output is correct |
8 |
Correct |
137 ms |
71796 KB |
Output is correct |
9 |
Correct |
728 ms |
71800 KB |
Output is correct |
10 |
Correct |
827 ms |
71800 KB |
Output is correct |
11 |
Correct |
774 ms |
71800 KB |
Output is correct |
12 |
Correct |
993 ms |
71800 KB |
Output is correct |
13 |
Correct |
768 ms |
71980 KB |
Output is correct |
14 |
Correct |
754 ms |
71980 KB |
Output is correct |
15 |
Correct |
687 ms |
71980 KB |
Output is correct |
16 |
Correct |
672 ms |
71980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
20824 KB |
Output is correct |
2 |
Correct |
472 ms |
71980 KB |
Output is correct |
3 |
Correct |
254 ms |
71980 KB |
Output is correct |
4 |
Correct |
270 ms |
71980 KB |
Output is correct |
5 |
Correct |
263 ms |
71980 KB |
Output is correct |
6 |
Correct |
170 ms |
71980 KB |
Output is correct |
7 |
Correct |
220 ms |
71980 KB |
Output is correct |
8 |
Correct |
539 ms |
71980 KB |
Output is correct |
9 |
Correct |
522 ms |
71980 KB |
Output is correct |
10 |
Correct |
136 ms |
71980 KB |
Output is correct |
11 |
Correct |
274 ms |
71980 KB |
Output is correct |
12 |
Correct |
487 ms |
71980 KB |
Output is correct |
13 |
Correct |
245 ms |
71980 KB |
Output is correct |
14 |
Correct |
255 ms |
71980 KB |
Output is correct |
15 |
Correct |
245 ms |
71980 KB |
Output is correct |
16 |
Correct |
197 ms |
71980 KB |
Output is correct |
17 |
Correct |
240 ms |
71980 KB |
Output is correct |
18 |
Correct |
500 ms |
71980 KB |
Output is correct |
19 |
Correct |
362 ms |
71980 KB |
Output is correct |
20 |
Correct |
403 ms |
71980 KB |
Output is correct |
21 |
Correct |
555 ms |
71980 KB |
Output is correct |
22 |
Correct |
480 ms |
71980 KB |
Output is correct |
23 |
Correct |
128 ms |
71980 KB |
Output is correct |
24 |
Correct |
249 ms |
71980 KB |
Output is correct |
25 |
Correct |
468 ms |
71980 KB |
Output is correct |
26 |
Correct |
221 ms |
71980 KB |
Output is correct |
27 |
Correct |
232 ms |
71980 KB |
Output is correct |
28 |
Correct |
259 ms |
71980 KB |
Output is correct |
29 |
Correct |
164 ms |
71980 KB |
Output is correct |
30 |
Correct |
265 ms |
71980 KB |
Output is correct |
31 |
Correct |
606 ms |
71980 KB |
Output is correct |
32 |
Correct |
370 ms |
71980 KB |
Output is correct |
33 |
Correct |
407 ms |
71980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
19192 KB |
Output is correct |
2 |
Correct |
28 ms |
19744 KB |
Output is correct |
3 |
Correct |
25 ms |
19744 KB |
Output is correct |
4 |
Correct |
24 ms |
19744 KB |
Output is correct |
5 |
Correct |
37 ms |
20108 KB |
Output is correct |
6 |
Correct |
25 ms |
20108 KB |
Output is correct |
7 |
Correct |
26 ms |
20108 KB |
Output is correct |
8 |
Correct |
25 ms |
20108 KB |
Output is correct |
9 |
Correct |
26 ms |
20108 KB |
Output is correct |
10 |
Correct |
24 ms |
20108 KB |
Output is correct |
11 |
Correct |
27 ms |
20108 KB |
Output is correct |
12 |
Correct |
33 ms |
20108 KB |
Output is correct |
13 |
Correct |
36 ms |
20376 KB |
Output is correct |
14 |
Correct |
45 ms |
20824 KB |
Output is correct |
15 |
Correct |
26 ms |
20824 KB |
Output is correct |
16 |
Correct |
25 ms |
20824 KB |
Output is correct |
17 |
Correct |
24 ms |
20824 KB |
Output is correct |
18 |
Correct |
1422 ms |
71980 KB |
Output is correct |
19 |
Correct |
360 ms |
71980 KB |
Output is correct |
20 |
Correct |
282 ms |
71980 KB |
Output is correct |
21 |
Correct |
321 ms |
71980 KB |
Output is correct |
22 |
Correct |
431 ms |
71980 KB |
Output is correct |
23 |
Correct |
365 ms |
71980 KB |
Output is correct |
24 |
Correct |
347 ms |
71980 KB |
Output is correct |
25 |
Correct |
403 ms |
71980 KB |
Output is correct |
26 |
Correct |
401 ms |
71980 KB |
Output is correct |
27 |
Correct |
719 ms |
73164 KB |
Output is correct |
28 |
Correct |
587 ms |
73164 KB |
Output is correct |
29 |
Correct |
578 ms |
76144 KB |
Output is correct |
30 |
Correct |
1219 ms |
105492 KB |
Output is correct |
31 |
Correct |
25 ms |
105492 KB |
Output is correct |
32 |
Correct |
1022 ms |
105492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
19192 KB |
Output is correct |
2 |
Correct |
28 ms |
19744 KB |
Output is correct |
3 |
Correct |
25 ms |
19744 KB |
Output is correct |
4 |
Correct |
24 ms |
19744 KB |
Output is correct |
5 |
Correct |
37 ms |
20108 KB |
Output is correct |
6 |
Correct |
25 ms |
20108 KB |
Output is correct |
7 |
Correct |
26 ms |
20108 KB |
Output is correct |
8 |
Correct |
25 ms |
20108 KB |
Output is correct |
9 |
Correct |
26 ms |
20108 KB |
Output is correct |
10 |
Correct |
24 ms |
20108 KB |
Output is correct |
11 |
Correct |
27 ms |
20108 KB |
Output is correct |
12 |
Correct |
33 ms |
20108 KB |
Output is correct |
13 |
Correct |
36 ms |
20376 KB |
Output is correct |
14 |
Correct |
45 ms |
20824 KB |
Output is correct |
15 |
Correct |
26 ms |
20824 KB |
Output is correct |
16 |
Correct |
25 ms |
20824 KB |
Output is correct |
17 |
Correct |
24 ms |
20824 KB |
Output is correct |
18 |
Correct |
1422 ms |
71980 KB |
Output is correct |
19 |
Correct |
360 ms |
71980 KB |
Output is correct |
20 |
Correct |
282 ms |
71980 KB |
Output is correct |
21 |
Correct |
321 ms |
71980 KB |
Output is correct |
22 |
Correct |
431 ms |
71980 KB |
Output is correct |
23 |
Correct |
365 ms |
71980 KB |
Output is correct |
24 |
Correct |
347 ms |
71980 KB |
Output is correct |
25 |
Correct |
403 ms |
71980 KB |
Output is correct |
26 |
Correct |
401 ms |
71980 KB |
Output is correct |
27 |
Correct |
719 ms |
73164 KB |
Output is correct |
28 |
Correct |
587 ms |
73164 KB |
Output is correct |
29 |
Correct |
578 ms |
76144 KB |
Output is correct |
30 |
Correct |
1219 ms |
105492 KB |
Output is correct |
31 |
Correct |
25 ms |
105492 KB |
Output is correct |
32 |
Correct |
1022 ms |
105492 KB |
Output is correct |
33 |
Correct |
472 ms |
71980 KB |
Output is correct |
34 |
Correct |
254 ms |
71980 KB |
Output is correct |
35 |
Correct |
270 ms |
71980 KB |
Output is correct |
36 |
Correct |
263 ms |
71980 KB |
Output is correct |
37 |
Correct |
170 ms |
71980 KB |
Output is correct |
38 |
Correct |
220 ms |
71980 KB |
Output is correct |
39 |
Correct |
539 ms |
71980 KB |
Output is correct |
40 |
Correct |
522 ms |
71980 KB |
Output is correct |
41 |
Correct |
136 ms |
71980 KB |
Output is correct |
42 |
Correct |
274 ms |
71980 KB |
Output is correct |
43 |
Correct |
487 ms |
71980 KB |
Output is correct |
44 |
Correct |
245 ms |
71980 KB |
Output is correct |
45 |
Correct |
255 ms |
71980 KB |
Output is correct |
46 |
Correct |
245 ms |
71980 KB |
Output is correct |
47 |
Correct |
197 ms |
71980 KB |
Output is correct |
48 |
Correct |
240 ms |
71980 KB |
Output is correct |
49 |
Correct |
500 ms |
71980 KB |
Output is correct |
50 |
Correct |
362 ms |
71980 KB |
Output is correct |
51 |
Correct |
403 ms |
71980 KB |
Output is correct |
52 |
Correct |
555 ms |
71980 KB |
Output is correct |
53 |
Correct |
480 ms |
71980 KB |
Output is correct |
54 |
Correct |
128 ms |
71980 KB |
Output is correct |
55 |
Correct |
249 ms |
71980 KB |
Output is correct |
56 |
Correct |
468 ms |
71980 KB |
Output is correct |
57 |
Correct |
221 ms |
71980 KB |
Output is correct |
58 |
Correct |
232 ms |
71980 KB |
Output is correct |
59 |
Correct |
259 ms |
71980 KB |
Output is correct |
60 |
Correct |
164 ms |
71980 KB |
Output is correct |
61 |
Correct |
265 ms |
71980 KB |
Output is correct |
62 |
Correct |
606 ms |
71980 KB |
Output is correct |
63 |
Correct |
370 ms |
71980 KB |
Output is correct |
64 |
Correct |
407 ms |
71980 KB |
Output is correct |
65 |
Correct |
971 ms |
110168 KB |
Output is correct |
66 |
Correct |
1124 ms |
110168 KB |
Output is correct |
67 |
Correct |
532 ms |
110168 KB |
Output is correct |
68 |
Correct |
750 ms |
110168 KB |
Output is correct |
69 |
Correct |
947 ms |
110168 KB |
Output is correct |
70 |
Correct |
952 ms |
117156 KB |
Output is correct |
71 |
Correct |
805 ms |
119504 KB |
Output is correct |
72 |
Correct |
726 ms |
119504 KB |
Output is correct |
73 |
Correct |
432 ms |
119504 KB |
Output is correct |
74 |
Correct |
541 ms |
119504 KB |
Output is correct |
75 |
Correct |
805 ms |
140040 KB |
Output is correct |
76 |
Correct |
875 ms |
140040 KB |
Output is correct |
77 |
Correct |
773 ms |
140040 KB |
Output is correct |
78 |
Correct |
622 ms |
49096 KB |
Output is correct |
79 |
Correct |
1028 ms |
71796 KB |
Output is correct |
80 |
Correct |
939 ms |
71796 KB |
Output is correct |
81 |
Correct |
856 ms |
71796 KB |
Output is correct |
82 |
Correct |
752 ms |
71796 KB |
Output is correct |
83 |
Correct |
137 ms |
71796 KB |
Output is correct |
84 |
Correct |
728 ms |
71800 KB |
Output is correct |
85 |
Correct |
827 ms |
71800 KB |
Output is correct |
86 |
Correct |
774 ms |
71800 KB |
Output is correct |
87 |
Correct |
993 ms |
71800 KB |
Output is correct |
88 |
Correct |
768 ms |
71980 KB |
Output is correct |
89 |
Correct |
754 ms |
71980 KB |
Output is correct |
90 |
Correct |
687 ms |
71980 KB |
Output is correct |
91 |
Correct |
672 ms |
71980 KB |
Output is correct |