답안 #736432

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
736432 2023-05-05T15:59:22 Z He_Huanglu Inside information (BOI21_servers) C++17
100 / 100
624 ms 35844 KB
#include <bits/stdc++.h>
#define ii pair<int, int>
using namespace std;

const int N = 12e4 + 5, S = 17;
int n, nq, f[N], anc[N], sz[N], ans[2 * N], dis[N], par[N][S], tang[N], giam[N], bit[2 * N];
bool block[N], inc[N], red[N];
vector <ii> ke[N], cur;
vector <int> ques[N];
struct sieungu {
    int fi, se, th;
}; vector <sieungu> dull;

void dfs(int u, int pre, int p) {
    for(auto [v, w] : ke[u]) if(!block[v] && v != pre) {
        f[v] = w;
        if(!p) {
            anc[v] = v;
            inc[v] = red[v] = 1;
            cur.push_back({v, w});
        }
        else {
            inc[v] = (inc[u] && f[v] > f[u]);
            red[v] = (red[u] && f[v] < f[u]);
            anc[v] = anc[u];
        }
        if(red[v]) {
            for(int i : ques[v]) if(f[anc[v]] < i) ans[i]++;
        }
        dfs(v, u, anc[v]);
    }
}

void upd(int u, int pre, int val) {
    if(inc[u]) {
        int x = f[u];
        while (x < n + nq) {
            bit[x] += val;
            x += (x & -x);
        }
    }
    for(auto [v, w] : ke[u]) if(!block[v] && v != pre) upd(v, u, val);
}
int get(int x) {
    int ret = 0;
    while (x) {
        ret += bit[x];
        x -= (x & -x);
    }
    return ret;
}

void scan(int u, int pre) {
    if(red[u]) {
        for(auto i : ques[u]) if(f[anc[u]] < i) ans[i] += get(i) - get(f[anc[u]]);
    }
    for(auto [v, w] : ke[u]) if(v != pre && !block[v]) scan(v, u);
}

void solve(int u) {
    f[u] = 0, inc[u] = 1, red[u] = 1, anc[u] = 0;
    cur.clear();
    dfs(u, 0, 0);
    sort(cur.begin(), cur.end(), [] (ii x, ii y) {
        return x.second > y.second;
    });
    for(auto [v, w] : cur) {
        scan(v, u);
        upd(v, 0, 1);
    }
    for(int i : ques[u]) ans[i] += get(i);
    for(auto [v, w] : cur) upd(v, 0, -1);
}

int cnt(int u, int pre) {
    sz[u] = 1;
    for(auto [v, w] : ke[u]) if(v != pre && !block[v]) sz[u] += cnt(v, u);
    return sz[u];
}

int centroid(int u, int pre, int cnt) {
    for(auto [v, w] : ke[u]) if(!block[v] && v != pre && sz[v] > cnt / 2) return centroid(v, u, cnt);
    return u;
}

void build(int u) {
    int cen = centroid(u, 0, cnt(u, 0));
    block[cen] = 1;
    solve(cen);
    for(auto [v, w] : ke[cen]) if(!block[v]) build(v);
}

void dfss(int u, int pre) {
    for(auto [v, w] : ke[u]) if(v != pre) {
        dis[v] = dis[u] + 1;
        f[v] = w;
        if(!pre) tang[v] = giam[v] = 1;
        else {
            tang[v] = (f[v] > f[u] ? tang[u] + 1 : 1);
            giam[v] = (f[v] < f[u] ? giam[u] + 1 : 1);
        }
        par[v][0] = u;
        for(int i = 1; i < S; i++) par[v][i] = par[par[v][i - 1]][i - 1];
        dfss(v, u);
    }
}

int lca(int u, int v) {
    if(dis[u] < dis[v]) swap(u, v);
    int delta = dis[u] - dis[v];
    for(int i = S - 1; i >= 0; i--) if((delta >> i) & 1) u = par[u][i];
    if(u == v) return u;
    for(int i = S - 1; i >= 0; i--) if(par[u][i] != par[v][i])
        u = par[u][i], v = par[v][i];
    return par[v][0];
}

int pre_lca(int u, int o) {
    int delta = dis[u] - dis[o] - 1;
    for(int i = S - 1; i >= 0; i--) if((delta >> i) & 1) u = par[u][i];
    return u;
}

main () {
    cin.tie(0)->sync_with_stdio(0);
    if(fopen("task.inp", "r")) {
        freopen("task.inp", "r", stdin);
        freopen("wa.out", "w", stdout);
    }
    cin >> n >> nq; nq += n - 1;
    for(int i = 1; i <= nq; i++) {
        char type; cin >> type;
        if(type == 'S') {
            int a, b; cin >> a >> b;
            ke[a].push_back({b, i});
            ke[b].push_back({a, i});
            ans[i] = -1;
        }
        if(type == 'Q') {
            int a, d; cin >> a >> d;
            dull.push_back({a, d, i});
            ans[i] = -2;
        }
        if(type == 'C') {
            int d; cin >> d;
            ques[d].push_back(i);
        }
    }
    dfss(1, 0);
    for(auto [a, d, i] : dull) {
        int o = lca(a, d), x = pre_lca(d, o);
        int s1 = dis[d] - dis[o], s2 = dis[a] - dis[o];
        if((s1 == giam[d] - giam[o] || giam[d] == s1) && (s2 == tang[a] - tang[o] || tang[a] == s2)) {
            if((f[pre_lca(d, o)] < f[pre_lca(a, o)] && f[a] < i) || (a == o && f[x] < i) || (d == o && f[a] < i))
                ans[i] = -3;
        }
    }
    build(1);
    for(int i = 1; i <= nq; i++) {
        if(ans[i] >= 0) cout << ans[i] + 1 << "\n";
        else if(ans[i] == -3) cout << "yes\n";
        else if(ans[i] == -2) cout << "no\n";
    }
}

Compilation message

servers.cpp:124:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  124 | main () {
      | ^~~~
servers.cpp: In function 'int main()':
servers.cpp:127:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  127 |         freopen("task.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
servers.cpp:128:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  128 |         freopen("wa.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 9572 KB Output is correct
2 Correct 49 ms 10576 KB Output is correct
3 Correct 43 ms 10748 KB Output is correct
4 Correct 58 ms 10784 KB Output is correct
5 Correct 44 ms 10528 KB Output is correct
6 Correct 38 ms 10436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 9572 KB Output is correct
2 Correct 49 ms 10576 KB Output is correct
3 Correct 43 ms 10748 KB Output is correct
4 Correct 58 ms 10784 KB Output is correct
5 Correct 44 ms 10528 KB Output is correct
6 Correct 38 ms 10436 KB Output is correct
7 Correct 32 ms 9532 KB Output is correct
8 Correct 46 ms 10136 KB Output is correct
9 Correct 39 ms 10032 KB Output is correct
10 Correct 48 ms 10116 KB Output is correct
11 Correct 44 ms 9880 KB Output is correct
12 Correct 39 ms 9864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 9660 KB Output is correct
2 Correct 162 ms 30236 KB Output is correct
3 Correct 153 ms 30232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 9660 KB Output is correct
2 Correct 162 ms 30236 KB Output is correct
3 Correct 153 ms 30232 KB Output is correct
4 Correct 36 ms 9556 KB Output is correct
5 Correct 191 ms 30048 KB Output is correct
6 Correct 137 ms 27524 KB Output is correct
7 Correct 114 ms 27728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 9576 KB Output is correct
2 Correct 406 ms 35396 KB Output is correct
3 Correct 388 ms 35328 KB Output is correct
4 Correct 252 ms 35268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 9576 KB Output is correct
2 Correct 406 ms 35396 KB Output is correct
3 Correct 388 ms 35328 KB Output is correct
4 Correct 252 ms 35268 KB Output is correct
5 Correct 30 ms 9520 KB Output is correct
6 Correct 496 ms 35704 KB Output is correct
7 Correct 303 ms 35836 KB Output is correct
8 Correct 417 ms 35436 KB Output is correct
9 Correct 494 ms 35472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 9660 KB Output is correct
2 Correct 237 ms 28472 KB Output is correct
3 Correct 278 ms 29212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 9660 KB Output is correct
2 Correct 237 ms 28472 KB Output is correct
3 Correct 278 ms 29212 KB Output is correct
4 Correct 31 ms 9540 KB Output is correct
5 Correct 259 ms 28936 KB Output is correct
6 Correct 300 ms 29128 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 9532 KB Output is correct
2 Correct 387 ms 35364 KB Output is correct
3 Correct 464 ms 35348 KB Output is correct
4 Correct 243 ms 35268 KB Output is correct
5 Correct 32 ms 9536 KB Output is correct
6 Correct 224 ms 28632 KB Output is correct
7 Correct 297 ms 29268 KB Output is correct
8 Correct 353 ms 29180 KB Output is correct
9 Correct 324 ms 29640 KB Output is correct
10 Correct 411 ms 33544 KB Output is correct
11 Correct 376 ms 33172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 9532 KB Output is correct
2 Correct 387 ms 35364 KB Output is correct
3 Correct 464 ms 35348 KB Output is correct
4 Correct 243 ms 35268 KB Output is correct
5 Correct 32 ms 9536 KB Output is correct
6 Correct 224 ms 28632 KB Output is correct
7 Correct 297 ms 29268 KB Output is correct
8 Correct 353 ms 29180 KB Output is correct
9 Correct 324 ms 29640 KB Output is correct
10 Correct 411 ms 33544 KB Output is correct
11 Correct 376 ms 33172 KB Output is correct
12 Correct 32 ms 9476 KB Output is correct
13 Correct 432 ms 35640 KB Output is correct
14 Correct 272 ms 35816 KB Output is correct
15 Correct 475 ms 35468 KB Output is correct
16 Correct 465 ms 35584 KB Output is correct
17 Correct 31 ms 9468 KB Output is correct
18 Correct 274 ms 28996 KB Output is correct
19 Correct 293 ms 29256 KB Output is correct
20 Correct 358 ms 29420 KB Output is correct
21 Correct 434 ms 29912 KB Output is correct
22 Correct 405 ms 32716 KB Output is correct
23 Correct 587 ms 32380 KB Output is correct
24 Correct 510 ms 31952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 9560 KB Output is correct
2 Correct 42 ms 10576 KB Output is correct
3 Correct 44 ms 10752 KB Output is correct
4 Correct 51 ms 10764 KB Output is correct
5 Correct 47 ms 10540 KB Output is correct
6 Correct 41 ms 10436 KB Output is correct
7 Correct 41 ms 9548 KB Output is correct
8 Correct 143 ms 30208 KB Output is correct
9 Correct 145 ms 30236 KB Output is correct
10 Correct 30 ms 9552 KB Output is correct
11 Correct 401 ms 35324 KB Output is correct
12 Correct 370 ms 35372 KB Output is correct
13 Correct 253 ms 35256 KB Output is correct
14 Correct 30 ms 9552 KB Output is correct
15 Correct 216 ms 28456 KB Output is correct
16 Correct 278 ms 29212 KB Output is correct
17 Correct 369 ms 29176 KB Output is correct
18 Correct 336 ms 29620 KB Output is correct
19 Correct 422 ms 33528 KB Output is correct
20 Correct 475 ms 33188 KB Output is correct
21 Correct 167 ms 29856 KB Output is correct
22 Correct 183 ms 29828 KB Output is correct
23 Correct 231 ms 29116 KB Output is correct
24 Correct 211 ms 29300 KB Output is correct
25 Correct 425 ms 34988 KB Output is correct
26 Correct 338 ms 28656 KB Output is correct
27 Correct 327 ms 28776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 9560 KB Output is correct
2 Correct 42 ms 10576 KB Output is correct
3 Correct 44 ms 10752 KB Output is correct
4 Correct 51 ms 10764 KB Output is correct
5 Correct 47 ms 10540 KB Output is correct
6 Correct 41 ms 10436 KB Output is correct
7 Correct 41 ms 9548 KB Output is correct
8 Correct 143 ms 30208 KB Output is correct
9 Correct 145 ms 30236 KB Output is correct
10 Correct 30 ms 9552 KB Output is correct
11 Correct 401 ms 35324 KB Output is correct
12 Correct 370 ms 35372 KB Output is correct
13 Correct 253 ms 35256 KB Output is correct
14 Correct 30 ms 9552 KB Output is correct
15 Correct 216 ms 28456 KB Output is correct
16 Correct 278 ms 29212 KB Output is correct
17 Correct 369 ms 29176 KB Output is correct
18 Correct 336 ms 29620 KB Output is correct
19 Correct 422 ms 33528 KB Output is correct
20 Correct 475 ms 33188 KB Output is correct
21 Correct 167 ms 29856 KB Output is correct
22 Correct 183 ms 29828 KB Output is correct
23 Correct 231 ms 29116 KB Output is correct
24 Correct 211 ms 29300 KB Output is correct
25 Correct 425 ms 34988 KB Output is correct
26 Correct 338 ms 28656 KB Output is correct
27 Correct 327 ms 28776 KB Output is correct
28 Correct 35 ms 9456 KB Output is correct
29 Correct 57 ms 10116 KB Output is correct
30 Correct 52 ms 10120 KB Output is correct
31 Correct 48 ms 10208 KB Output is correct
32 Correct 44 ms 9880 KB Output is correct
33 Correct 46 ms 9796 KB Output is correct
34 Correct 33 ms 9540 KB Output is correct
35 Correct 133 ms 30072 KB Output is correct
36 Correct 108 ms 27512 KB Output is correct
37 Correct 116 ms 27704 KB Output is correct
38 Correct 31 ms 9468 KB Output is correct
39 Correct 387 ms 35756 KB Output is correct
40 Correct 282 ms 35844 KB Output is correct
41 Correct 495 ms 35536 KB Output is correct
42 Correct 432 ms 35472 KB Output is correct
43 Correct 30 ms 9540 KB Output is correct
44 Correct 252 ms 28900 KB Output is correct
45 Correct 339 ms 29128 KB Output is correct
46 Correct 358 ms 29444 KB Output is correct
47 Correct 359 ms 30020 KB Output is correct
48 Correct 457 ms 32756 KB Output is correct
49 Correct 624 ms 32340 KB Output is correct
50 Correct 516 ms 31948 KB Output is correct
51 Correct 143 ms 28336 KB Output is correct
52 Correct 115 ms 28496 KB Output is correct
53 Correct 115 ms 28172 KB Output is correct
54 Correct 125 ms 28796 KB Output is correct
55 Correct 123 ms 28004 KB Output is correct
56 Correct 209 ms 28440 KB Output is correct
57 Correct 433 ms 33140 KB Output is correct
58 Correct 525 ms 28848 KB Output is correct
59 Correct 324 ms 28852 KB Output is correct