Submission #490587

#TimeUsernameProblemLanguageResultExecution timeMemory
490587CyberSleeperRegions (IOI09_regions)C++14
100 / 100
3917 ms50720 KiB
#include <bits/stdc++.h>
#define fastio      ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define debug(x)    cout << "Line " << __LINE__ << ", " << #x << " is " << x << endl
#define fi          first
#define se          second
#define mp          make_pair
#define pb          push_back
#define ll          long long
#define ld          long double
#define pii         pair<int, int>
#define pll         pair<ll, ll>
#define ar3         array<int, 3>
#define nl          endl
#define tb          '\t'
#define sp          ' '
using namespace std;

const int MX=200005, MOD=1e9+7, BLOCK=500, INF=1e9+7, INFF=1e18+7;
const ld ERR=1e-7, pi=3.14159265358979323846;

int N, R, Q, op[MX], ed[MX], reg[MX];
vector<int> adj[MX], euler;
vector<pii> rakyat[25005];

void DFS(int p, int x){
    op[x]=euler.size();
    euler.pb(reg[x]);
    for(auto i:adj[x]){
        DFS(x, i);
    }
    ed[x]=euler.size()-1;
}

int main(){
    //fastio;
    cin >> N >> R >> Q >> reg[1];
    for(int i=2, P; i<=N; i++){
        cin >> P >> reg[i];
        adj[P].pb(i);
    }
    DFS(0, 1);
    for(int i=1; i<=N; i++){
        int tmp=reg[i];
        rakyat[tmp].pb({op[i], ed[i]});
    }
    for(int i=0; i<=R; i++)
        sort(rakyat[i].begin(), rakyat[i].end());
    map<pii, int> prec;
    for(int i=1; i<=R; i++){
        if(rakyat[i].size()>BLOCK){
            int cnt=0, idx=0;
            vector<int> sweep(N+1, 0);
            for(auto j:rakyat[i]){
                sweep[j.fi]++;
                sweep[j.se+1]--;
            }
            for(int j=0; j<N; j++){
                cnt+=sweep[j];
                prec[{i, euler[j]}]+=cnt;
            }
        }
    }
    for(int u, v; Q--;){
        cin >> u >> v;
        if(rakyat[u].size()>BLOCK)
            cout << prec[{u, v}] << nl;
        else{
            int ans=0;
            for(auto i:rakyat[u]){
                int add=upper_bound(rakyat[v].begin(), rakyat[v].end(), mp(i.se, INF))-rakyat[v].begin();
                int dic=lower_bound(rakyat[v].begin(), rakyat[v].end(), mp(i.fi, -INF))-rakyat[v].begin();
                ans+=add-dic;
            }
            cout << ans << nl;
        }
    }
}

Compilation message (stderr)

regions.cpp:18:64: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
   18 | const int MX=200005, MOD=1e9+7, BLOCK=500, INF=1e9+7, INFF=1e18+7;
      |                                                            ~~~~^~
regions.cpp: In function 'int main()':
regions.cpp:51:24: warning: unused variable 'idx' [-Wunused-variable]
   51 |             int cnt=0, idx=0;
      |                        ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...