Submission #446900

#TimeUsernameProblemLanguageResultExecution timeMemory
446900JerryLiu06Regions (IOI09_regions)C++17
100 / 100
4107 ms30272 KiB
#include <bits/stdc++.h>
 
using namespace std;
 
using ll = long long;
using ld = long double;
using db = double;
using str = string;
 
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;
 
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;
 
#define mp make_pair
#define f first
#define s second
 
#define sz(x) (int)(x).size()
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert 
#define ft front()
#define bk back()
#define pb push_back
#define pf push_front
 
#define lb lower_bound
#define ub upper_bound
 
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define R0F(i, a) ROF(i, 0, a)
#define EACH(a, x) for (auto& a : x)
 
ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); }
ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); }
 
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
 
template<class T> void remDup(vector<T>& v) { sor(v); v.erase(unique(all(v)), v.end()); }
 
const int MOD = 1e9 + 7;
const int MXN = 2e5 + 10;
const int MXR = 25e3 + 10;
const ll INF = 1e18;

const int BLOCK = 450; int N, R, Q; int reg[MXN]; vi adj[MXN];

int timer = 1; int in[MXN], out[MXN]; vi S[MXR]; // Sorted list of {in, out}

vi allLarge; int ind[MXR]; int par[BLOCK][MXR], child[BLOCK][MXR];

void DFS(int X) {
    in[X] = timer++; S[reg[X]].pb(X);

    EACH(Y, adj[X]) DFS(Y); out[X] = timer - 1;
}
bool isAnc(int A, int B) { return in[A] <= in[B] && out[B] <= out[A]; }

// Get answer for (A, B), sz[A] > BLOCK, sz[B] <= BLOCK ~ O(N)

void genPar(int X, int large, int cnt) {
    par[ind[large]][reg[X]] += cnt; int nCnt = cnt + (reg[X] == large);

    EACH(Y, adj[X]) genPar(Y, large, nCnt);
}

// Get answer for (A, B), sz[A] <= BLOCK, sz[B] > BLOCK ~ O(N)

int genChild(int X, int large) {
    int sub = (reg[X] == large); EACH(Y, adj[X]) sub += genChild(Y, large);

    child[ind[large]][reg[X]] += sub; return sub;
}

// Get answer for (A, B), sz[A], sz[B] <= BLOCK ~ O(BLOCK)

int getAns(int A, int B) {
    int L = 0, R = 0; int ans = 0; stack<int> anc;

    while (L < sz(S[A]) || R < sz(S[B])) {
        if (L < sz(S[A]) && (R == sz(S[B]) || in[S[A][L]] < in[S[B][R]])) {
            while (!anc.empty() && !isAnc(anc.top(), S[A][L])) anc.pop(); anc.push(S[A][L]); L++;
        }
        else { while (!anc.empty() && !isAnc(anc.top(), S[B][R])) anc.pop(); ans += sz(anc); R++; }
    }
    return ans;
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);

    cin >> N >> R >> Q >> reg[1]; FOR(i, 2, N + 1) { int A; cin >> A >> reg[i]; adj[A].pb(i); }

    DFS(1); int cnt = 0; FOR(i, 1, R + 1) if (sz(S[i]) > BLOCK) allLarge.pb(i), ind[i] = cnt++; 

    EACH(i, allLarge) { genPar(1, i, 0); genChild(1, i); }

    F0R(i, Q) {
        int A, B; cin >> A >> B;

        if (sz(S[A]) <= BLOCK && sz(S[B]) <= BLOCK) { cout << getAns(A, B) << endl; continue; }

        if (sz(S[A]) > BLOCK) cout << par[ind[A]][B] << endl; else cout << child[ind[B]][A] << endl;
    }
}

Compilation message (stderr)

regions.cpp: In function 'void DFS(int)':
regions.cpp:45:20: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   45 | #define EACH(a, x) for (auto& a : x)
      |                    ^~~
regions.cpp:69:5: note: in expansion of macro 'EACH'
   69 |     EACH(Y, adj[X]) DFS(Y); out[X] = timer - 1;
      |     ^~~~
regions.cpp:69:29: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   69 |     EACH(Y, adj[X]) DFS(Y); out[X] = timer - 1;
      |                             ^~~
regions.cpp: In function 'int getAns(int, int)':
regions.cpp:96:13: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
   96 |             while (!anc.empty() && !isAnc(anc.top(), S[A][L])) anc.pop(); anc.push(S[A][L]); L++;
      |             ^~~~~
regions.cpp:96:75: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
   96 |             while (!anc.empty() && !isAnc(anc.top(), S[A][L])) anc.pop(); anc.push(S[A][L]); L++;
      |                                                                           ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...