Submission #446892

# Submission time Handle Problem Language Result Execution time Memory
446892 2021-07-23T17:45:23 Z JerryLiu06 Regions (IOI09_regions) C++17
35 / 100
3786 ms 80144 KB
#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 = 320; 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[A][ind[B]] << endl;
    }
}

Compilation message

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 time Memory Grader output
1 Correct 3 ms 5576 KB Output is correct
2 Correct 4 ms 5576 KB Output is correct
3 Correct 5 ms 5576 KB Output is correct
4 Correct 9 ms 5576 KB Output is correct
5 Correct 17 ms 5576 KB Output is correct
6 Correct 33 ms 5576 KB Output is correct
7 Correct 41 ms 5576 KB Output is correct
8 Correct 43 ms 5704 KB Output is correct
9 Correct 63 ms 6088 KB Output is correct
10 Correct 113 ms 6088 KB Output is correct
11 Correct 131 ms 6344 KB Output is correct
12 Correct 183 ms 6728 KB Output is correct
13 Correct 218 ms 6472 KB Output is correct
14 Correct 234 ms 6984 KB Output is correct
15 Correct 223 ms 9416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1192 ms 10336 KB Output isn't correct
2 Incorrect 1479 ms 9120 KB Output isn't correct
3 Incorrect 2234 ms 12320 KB Output isn't correct
4 Correct 341 ms 6984 KB Output is correct
5 Correct 369 ms 8648 KB Output is correct
6 Runtime error 240 ms 24216 KB Execution killed with signal 11
7 Runtime error 236 ms 26048 KB Execution killed with signal 11
8 Runtime error 396 ms 47656 KB Execution killed with signal 11
9 Correct 2419 ms 13804 KB Output is correct
10 Runtime error 768 ms 80144 KB Execution killed with signal 11
11 Correct 3786 ms 13376 KB Output is correct
12 Runtime error 177 ms 31012 KB Execution killed with signal 11
13 Runtime error 139 ms 31924 KB Execution killed with signal 11
14 Runtime error 765 ms 37820 KB Execution killed with signal 11
15 Runtime error 119 ms 41592 KB Execution killed with signal 11
16 Runtime error 128 ms 56480 KB Execution killed with signal 11
17 Runtime error 292 ms 60232 KB Execution killed with signal 11