답안 #675922

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
675922 2022-12-28T11:20:20 Z stanislavpolyn Inside information (BOI21_servers) C++17
100 / 100
1291 ms 70832 KB
#include <bits/stdc++.h>

#define fr(i, a, b) for (int i = (a); i <= (b); i++)
#define rf(i, a, b) for (int i = (a); i >= (b); i--)
#define fe(x, y) for (auto& x : y)

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define mt make_tuple

#define pw(x) (1LL << (x))
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()

using namespace std;

mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define fbo find_by_order
#define ook order_of_key

template <typename T>
bool umn(T& a, T b) {
    return a > b ? a = b, 1 : 0;
}
template <typename T>
bool umx(T& a, T b) {
    return a < b ? a = b, 1 : 0;
}

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <typename T>
using ve = vector<T>;

const int N = 3e5 + 5;

#define dec abcd

int n, k;
ve<pii> g[N];
ve<int> dp[N];
ve<array<int, 3>> Q;

int timer;
int tin[N], tout[N];
int up[N][20];
int par[N], val[N];

bool inc[N][20];
bool dec[N][20];
int dep[N];

void dfs(int v = 1, int p = 0) {
    up[v][0] = p;
    fr (i, 1, 19) up[v][i] = up[up[v][i - 1]][i - 1];
    tin[v] = timer++;

    fe (to, g[v]) {
        if (to.fi == p) continue;
        par[to.fi] = v;
        val[to.fi] = to.se;
        dep[to.fi] = dep[v] + 1;
        dfs(to.fi, v);
    }

    tout[v] = timer++;
}

bool isUpper(int a, int b) {
    return tin[a] <= tin[b] && tout[a] >= tin[b];
}

int getLCA(int a, int b) {
    if (isUpper(a, b)) return a;
    if (isUpper(b, a)) return b;
    rf (i, 19, 0) {
        if (up[a][i] && !isUpper(up[a][i], b)) {
            a = up[a][i];
        }
    }
    return up[a][0];
}

int getDist(int a, int b) {
    return dep[a] + dep[b] - 2 * dep[getLCA(a, b)];
}

int goUp(int v, int d) {
    rf (i, 19, 0) {
        if (d - pw(i) >= 0) {
            v = up[v][i];
            d -= pw(i);
        }
    }
    assert(v);
    return v;
}

bool checkInc(int v, int d) {
    if (d == 0) return 1;
    int p = __lg(d);
    return inc[v][p] && inc[goUp(v, d - pw(p))][p];
}


bool checkDec(int v, int d) {
    if (d == 0) return 1;
    int p = __lg(d);
    return dec[v][p] && dec[goUp(v, d - pw(p))][p];
}

int getLastEdge(int a, int b) {
    assert(a != b);

    if (isUpper(a, b)) {
        return val[b];
    }
    if (isUpper(b, a)) {
        int v = goUp(a, dep[a] - dep[b] - 1);
        return val[v];
    }
    return val[b];
}

int checkPath(int a, int b) {
    if (a == b) return 1;

    if (isUpper(a, b)) {
        return checkDec(b, dep[b] - dep[a]);
    }
    if (isUpper(b, a)) {
        return checkInc(a, dep[a] - dep[b]);
    }
    int c = getLCA(a, b);

    int e1 = val[goUp(a, dep[a] - dep[c] - 1)];
    int e2 = val[goUp(b, dep[b] - dep[c] - 1)];
    if (e1 > e2) return 0;

    return checkInc(a, dep[a] - dep[c]) && checkDec(b, dep[b] - dep[c]);
}

ve<int> G[N];
int P[N];
int sz[N];
int cmpSz;
bool block[N];

void calcSz(int v, int p) {
    sz[v] = 1;
    fe (to, g[v]) {
        if (to.fi == p || block[to.fi]) continue;
        calcSz(to.fi, v);
        sz[v] += sz[to.fi];
    }
}

int findCentroid(int v, int p) {
    fe (to, g[v]) {
        if (to.fi == p || block[to.fi]) continue;
        if (sz[to.fi] > cmpSz / 2) {
            return findCentroid(to.fi, v);
        }
    }
    return v;
}

int go(int v = 1) {
    calcSz(v, 0);
    cmpSz = sz[v];
    int c = findCentroid(v, 0);
    block[c] = 1;
    fe (to, g[c]) {
        if (!block[to.fi]) {
            P[go(to.fi)] = c;
        }
    }
    return c;
}

int TIN[N], TOUT[N];

void dfs2(int v) {
    TIN[v] = timer++;
    fe (to, G[v]) {
        dfs2(to);
    }
    TOUT[v] = timer++;
}

bool inside(int v, int c) {
    return TIN[c] <= TIN[v] && TIN[v] <= TOUT[c];
}

ve<array<int, 3>> e;
int from[N];

ve<int> T[N];
int tot[N];

void upd(int pos, int x, ve<int>& t) {
    while (pos < sz(t)) {
        t[pos] += x;
        pos = pos | (pos + 1);
    }
}

int get(int pos, ve<int>& t) {
    int ans = 0;
    while (pos >= 0) {
        ans += t[pos];
        pos = (pos & (pos + 1)) - 1;
    }
    return ans;
}

void addEdge(int v) {
    int c = v;
    while (1) {
        if (inside(par[v], c)) {
            int a = v;
            int b = par[v];
            if (getDist(a, c) > getDist(b, c)) swap(a, b);

            if (checkPath(c, b)) {
                int e = getLastEdge(b, c);

                int p = lower_bound(all(g[c]), mp(-1, e), [](pii i, pii j) {
                    return i.se < j.se;
                }) - g[c].begin();
                assert(g[c][p].se == e);
                upd(p, 1, T[c]);
                tot[c]++;
                // dp[c][p]++;
            }
        }
        if (!P[c]) break;
        c = P[c];
    }
}

int getAns(int v, int t) {
    int c = v;
    int ans = 0;
    while (1) {
        if (checkPath(v, c)) {
            int e = (v == c ? -1 : getLastEdge(v, c));
            if (e <= t) {
                ans++;

                int p = upper_bound(all(g[c]), mp(-1, e), [](pii i, pii j) {
                    return i.se < j.se;
                }) - g[c].begin();

                ans += tot[c] - get(p - 1, T[c]);
                // rf (i, sz(g[c]) - 1, 0) {
                //     if (g[c][i].se > e) {
                //         ans += dp[c][i];
                //     } else {
                //         break;
                //     }
                // }
            }
        }

        if (!P[c]) break;
        c = P[c];
    }
    return ans;
}

int main() {
#ifndef LOCAL
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
#else
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
#endif

    cin >> n >> k;

    fr (i, 1, n + k - 1) {
        char c;
        cin >> c;
        if (c == 'S') {
            int a, b;
            cin >> a >> b;

            g[a].pb({b, i});
            g[b].pb({a, i});
            e.pb({i, a, b});
            // cout << a << " " << b << " " << i << "\n";
        }
        if (c == 'Q') {
            int a, d;
            cin >> a >> d;
            Q.pb({i, a, d});
        }
        if (c == 'C') {
            int x;
            cin >> x;
            Q.pb({i, x, -1});
        }
    }

    fr (i, 1, n) {
        // dp[i].resize(sz(g[i]));
        T[i].resize(sz(g[i]));
        sort(all(g[i]), [](pii a, pii b) {
            return a.se < b.se;
        });
    }

    dfs();

    fr (i, 1, n) {
        if (par[i]) {
            dec[i][0] = 1;
            inc[i][0] = 1;
        }
    }

    fr (p, 1, 19) {
        fr (i, 1, n) {
            if (up[i][p] && inc[i][p - 1] && inc[up[i][p - 1]][p - 1]) {
                if (val[goUp(i, pw(p - 1) - 1)] < val[up[i][p - 1]]) {
                    inc[i][p] = 1;
                }
            }

            if (up[i][p] && dec[i][p - 1] && dec[up[i][p - 1]][p - 1]) {
                if (val[goUp(i, pw(p - 1) - 1)] > val[up[i][p - 1]]) {
                    dec[i][p] = 1;
                }
            }
        }
    }

    int root = go();

    fr (i, 1, n) {
        if (P[i]) {
            G[P[i]].pb(i);
        }
    }
    timer = 0;
    dfs2(root);

    int ptr = 0;

    fe (cur, Q) {
        while (ptr < sz(e) && e[ptr][0] <= cur[0]) {
            if (isUpper(e[ptr][1], e[ptr][2])) swap(e[ptr][1], e[ptr][2]);
            addEdge(e[ptr][1]);
            ptr++;
        }

        if (cur[2] != -1) {
            if (cur[2] == cur[1]) {
                cout << "yes\n";
                continue;
            }

            if (checkPath(cur[2], cur[1]) && getLastEdge(cur[2], cur[1]) <= cur[0]) {
                cout << "yes\n";
            } else {
                cout << "no\n";
            }
        } else {
            cout << getAns(cur[1], cur[0]) << "\n";
        }
    }

    return 0;
}

/*
4 4
S 1 2
S 1 3
S 3 4
Q 2 1
Q 2 2
Q 2 3
Q 2 4
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 30400 KB Output is correct
2 Correct 60 ms 31284 KB Output is correct
3 Correct 54 ms 31348 KB Output is correct
4 Correct 74 ms 31368 KB Output is correct
5 Correct 42 ms 31508 KB Output is correct
6 Correct 53 ms 31308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 30400 KB Output is correct
2 Correct 60 ms 31284 KB Output is correct
3 Correct 54 ms 31348 KB Output is correct
4 Correct 74 ms 31368 KB Output is correct
5 Correct 42 ms 31508 KB Output is correct
6 Correct 53 ms 31308 KB Output is correct
7 Correct 47 ms 30452 KB Output is correct
8 Correct 75 ms 31236 KB Output is correct
9 Correct 63 ms 31500 KB Output is correct
10 Correct 99 ms 31388 KB Output is correct
11 Correct 57 ms 31436 KB Output is correct
12 Correct 54 ms 31500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 49 ms 30528 KB Output is correct
2 Correct 188 ms 60176 KB Output is correct
3 Correct 183 ms 60072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 49 ms 30528 KB Output is correct
2 Correct 188 ms 60176 KB Output is correct
3 Correct 183 ms 60072 KB Output is correct
4 Correct 45 ms 30524 KB Output is correct
5 Correct 191 ms 60168 KB Output is correct
6 Correct 149 ms 60484 KB Output is correct
7 Correct 151 ms 62232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 30468 KB Output is correct
2 Correct 333 ms 67784 KB Output is correct
3 Correct 333 ms 67844 KB Output is correct
4 Correct 351 ms 67788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 30468 KB Output is correct
2 Correct 333 ms 67784 KB Output is correct
3 Correct 333 ms 67844 KB Output is correct
4 Correct 351 ms 67788 KB Output is correct
5 Correct 34 ms 30484 KB Output is correct
6 Correct 373 ms 67728 KB Output is correct
7 Correct 484 ms 67832 KB Output is correct
8 Correct 392 ms 67564 KB Output is correct
9 Correct 382 ms 67560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 30476 KB Output is correct
2 Correct 268 ms 60748 KB Output is correct
3 Correct 321 ms 60868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 30476 KB Output is correct
2 Correct 268 ms 60748 KB Output is correct
3 Correct 321 ms 60868 KB Output is correct
4 Correct 44 ms 30452 KB Output is correct
5 Correct 356 ms 60796 KB Output is correct
6 Correct 351 ms 60672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 30484 KB Output is correct
2 Correct 323 ms 67836 KB Output is correct
3 Correct 350 ms 67832 KB Output is correct
4 Correct 327 ms 67712 KB Output is correct
5 Correct 42 ms 30468 KB Output is correct
6 Correct 270 ms 60828 KB Output is correct
7 Correct 321 ms 60912 KB Output is correct
8 Correct 520 ms 60940 KB Output is correct
9 Correct 499 ms 60844 KB Output is correct
10 Correct 768 ms 63748 KB Output is correct
11 Correct 839 ms 63472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 30484 KB Output is correct
2 Correct 323 ms 67836 KB Output is correct
3 Correct 350 ms 67832 KB Output is correct
4 Correct 327 ms 67712 KB Output is correct
5 Correct 42 ms 30468 KB Output is correct
6 Correct 270 ms 60828 KB Output is correct
7 Correct 321 ms 60912 KB Output is correct
8 Correct 520 ms 60940 KB Output is correct
9 Correct 499 ms 60844 KB Output is correct
10 Correct 768 ms 63748 KB Output is correct
11 Correct 839 ms 63472 KB Output is correct
12 Correct 33 ms 30456 KB Output is correct
13 Correct 353 ms 67736 KB Output is correct
14 Correct 482 ms 67776 KB Output is correct
15 Correct 385 ms 67632 KB Output is correct
16 Correct 383 ms 67740 KB Output is correct
17 Correct 45 ms 30428 KB Output is correct
18 Correct 343 ms 60932 KB Output is correct
19 Correct 357 ms 60792 KB Output is correct
20 Correct 588 ms 60680 KB Output is correct
21 Correct 590 ms 60892 KB Output is correct
22 Correct 1291 ms 62996 KB Output is correct
23 Correct 1176 ms 64152 KB Output is correct
24 Correct 894 ms 64128 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 30400 KB Output is correct
2 Correct 59 ms 31396 KB Output is correct
3 Correct 60 ms 31416 KB Output is correct
4 Correct 65 ms 31440 KB Output is correct
5 Correct 41 ms 31608 KB Output is correct
6 Correct 53 ms 31336 KB Output is correct
7 Correct 44 ms 30524 KB Output is correct
8 Correct 199 ms 60140 KB Output is correct
9 Correct 192 ms 60120 KB Output is correct
10 Correct 33 ms 30408 KB Output is correct
11 Correct 317 ms 67860 KB Output is correct
12 Correct 320 ms 67776 KB Output is correct
13 Correct 335 ms 67784 KB Output is correct
14 Correct 45 ms 30536 KB Output is correct
15 Correct 266 ms 60768 KB Output is correct
16 Correct 312 ms 60932 KB Output is correct
17 Correct 513 ms 60936 KB Output is correct
18 Correct 499 ms 60896 KB Output is correct
19 Correct 773 ms 63752 KB Output is correct
20 Correct 833 ms 63228 KB Output is correct
21 Correct 203 ms 59868 KB Output is correct
22 Correct 202 ms 59980 KB Output is correct
23 Correct 313 ms 60340 KB Output is correct
24 Correct 328 ms 60364 KB Output is correct
25 Correct 412 ms 64136 KB Output is correct
26 Correct 353 ms 60640 KB Output is correct
27 Correct 321 ms 60812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 30400 KB Output is correct
2 Correct 59 ms 31396 KB Output is correct
3 Correct 60 ms 31416 KB Output is correct
4 Correct 65 ms 31440 KB Output is correct
5 Correct 41 ms 31608 KB Output is correct
6 Correct 53 ms 31336 KB Output is correct
7 Correct 44 ms 30524 KB Output is correct
8 Correct 199 ms 60140 KB Output is correct
9 Correct 192 ms 60120 KB Output is correct
10 Correct 33 ms 30408 KB Output is correct
11 Correct 317 ms 67860 KB Output is correct
12 Correct 320 ms 67776 KB Output is correct
13 Correct 335 ms 67784 KB Output is correct
14 Correct 45 ms 30536 KB Output is correct
15 Correct 266 ms 60768 KB Output is correct
16 Correct 312 ms 60932 KB Output is correct
17 Correct 513 ms 60936 KB Output is correct
18 Correct 499 ms 60896 KB Output is correct
19 Correct 773 ms 63752 KB Output is correct
20 Correct 833 ms 63228 KB Output is correct
21 Correct 203 ms 59868 KB Output is correct
22 Correct 202 ms 59980 KB Output is correct
23 Correct 313 ms 60340 KB Output is correct
24 Correct 328 ms 60364 KB Output is correct
25 Correct 412 ms 64136 KB Output is correct
26 Correct 353 ms 60640 KB Output is correct
27 Correct 321 ms 60812 KB Output is correct
28 Correct 45 ms 30400 KB Output is correct
29 Correct 76 ms 31380 KB Output is correct
30 Correct 62 ms 31452 KB Output is correct
31 Correct 99 ms 31316 KB Output is correct
32 Correct 56 ms 31468 KB Output is correct
33 Correct 52 ms 31360 KB Output is correct
34 Correct 47 ms 30500 KB Output is correct
35 Correct 189 ms 60136 KB Output is correct
36 Correct 147 ms 60408 KB Output is correct
37 Correct 154 ms 62160 KB Output is correct
38 Correct 35 ms 31260 KB Output is correct
39 Correct 363 ms 70628 KB Output is correct
40 Correct 478 ms 70832 KB Output is correct
41 Correct 434 ms 70372 KB Output is correct
42 Correct 390 ms 70192 KB Output is correct
43 Correct 46 ms 31280 KB Output is correct
44 Correct 351 ms 63772 KB Output is correct
45 Correct 360 ms 63744 KB Output is correct
46 Correct 601 ms 63684 KB Output is correct
47 Correct 627 ms 63852 KB Output is correct
48 Correct 1260 ms 65964 KB Output is correct
49 Correct 1168 ms 67436 KB Output is correct
50 Correct 911 ms 67328 KB Output is correct
51 Correct 194 ms 63096 KB Output is correct
52 Correct 164 ms 63040 KB Output is correct
53 Correct 158 ms 62648 KB Output is correct
54 Correct 154 ms 63272 KB Output is correct
55 Correct 168 ms 62916 KB Output is correct
56 Correct 315 ms 63340 KB Output is correct
57 Correct 384 ms 66748 KB Output is correct
58 Correct 526 ms 63364 KB Output is correct
59 Correct 381 ms 63964 KB Output is correct