This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |