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>
#ifndef SKY
#include "rainbow.h"
#endif // SKY
using namespace std;
#define N 200010
#define ll long long
#define fs first
#define sc second
#define ii pair<ll,int>
#define pb push_back
int r,c,m;
struct BIT
{
    unordered_map<int,int>bit[N];
    map<int,int>mp[N];
    void update(int x,int y,int val)
    {
        x=r-x+2;
        y=c-y+2;
        for(int u=x;u>0;u-=u&(-u))
            for(int v=y;v>0;v-=v&(-v))
                bit[u][v]+=val;
    }
    int get(int x,int y)
    {
        x=r-x+2;
        y=c-y+2;
        int res=0;
        for(int u=x;u<=r+1;u+=u&(-u))
            for(int v=y;v<=c+1;v+=v&(-v))
                if(bit[u].find(v)!=bit[u].end())
                    res+=bit[u][v];
        return res;
    }
    void init(int u,int v)
    {
        if(++mp[u][v]==1)
            update(u,v,1);
    }
    int solve(int ar, int ac, int br, int bc)
    {
        if(ar>br||ac>bc)
            return 0;
        return get(br,bc)+get(ar-1,ac-1)-get(ar-1,bc)-get(br,ac-1);
    }
}row,col,p,cell;
void build(int u,int v)
{
    p.init(u,v);
    p.init(u+1,v);
    p.init(u,v+1);
    p.init(u+1,v+1);
    row.init(u,v);
    row.init(u+1,v);
    col.init(u,v);
    col.init(u,v+1);
    cell.init(u,v);
}
void init(int RRR, int CCC, int sr, int sc, int MMM, char *S)
{
    r=RRR;
    c=CCC;
    m=MMM;
    build(sr,sc);
    for(int i=1;i<=m;i++)
    {
        char ch=*S;
        if(i!=m)
            S++;
        if(ch=='N')
            sr--;
        if(ch=='S')
            sr++;
        if(ch=='E')
            sc++;
        if(ch=='W')
            sc--;
        build(sr,sc);
    }
}
int colour(int ar, int ac, int br, int bc)
{
    if(cell.solve(ar,ac,br,bc)==0)
        return 1;
    int res=0;
    res+=row.solve(ar+1,ac,br,bc)+col.solve(ar,ac+1,br,bc);
   // cout<<res<<endl;
    res-=p.solve(ar+1,ac+1,br,bc);
 //   cout<<p.solve(ar+1,ac+1,br,bc)<<endl;
    res-=cell.solve(ar,ac,br,bc);
    if(cell.solve(ar,ac,ar,bc)>0||cell.solve(br,ac,br,bc)>0||cell.solve(ar,ac,br,ac)>0||cell.solve(ar,bc,br,bc)>0)
        return res+1;
    return res+2;
}
#ifdef SKY
int main()
{
    freopen("A.inp","r",stdin);
    freopen("A.out","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    int r,c,sr,sc,m;
    string S;
    cin>>r>>c>>sr>>sc>>m>>S;
    init(r,c,sr,sc,m,&S[0]);
    int q;
    cin>>q;
    while(q--)
    {
        int ar,ac,br,bc;
        cin>>ar>>ac>>br>>bc;
        cout<<colour(ar,ac,br,bc)<<endl;
    }
    return 0;
}
#endif
| # | 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... |