Submission #646750

# Submission time Handle Problem Language Result Execution time Memory
646750 2022-09-30T14:40:42 Z dooompy Inside information (BOI21_servers) C++17
100 / 100
831 ms 62440 KB
#include "bits/stdc++.h"

using namespace std;

void abc() {cout << endl;}
template <typename T, typename ...U> void abc(T a, U ...b) {
    cout << a << ' ', abc(b...);
}
template <typename T> void printv(T l, T r) {
    while (l != r) cout << *l << " \n"[++l == r];
}
template <typename A, typename B> istream& operator >> (istream& o, pair<A, B> &a) {
    return o >> a.first >> a.second;
}
template <typename A, typename B> ostream& operator << (ostream& o, pair<A, B> a) {
    return o << '(' << a.first << ", " << a.second << ')';
}
template <typename T> ostream& operator << (ostream& o, vector<T> a) {
    bool is = false;
    for (T i : a) {o << (is ? ' ' : '{'), is = true, o << i;}
    return o << '}';
}

#ifdef local
#define test(args...) abc("[" + string(#args) + "]", args)
#else
#define test(args...) void(0)
#endif

using ll = long long;

struct edge {
    int to, cost;

    bool operator <(edge o) const {
        return cost > o.cost;
    }
};

const int MAXN = 3e5;

int tree[4 * MAXN];

void update(int x, int l, int r, int p, int v) {
    if (l == r) {
        tree[x] += v;
        return;
    }

    int mid = (l + r) / 2;
    if (p <= mid) update(2 * x, l, mid, p, v);
    else update(2 * x + 1, mid + 1, r, p, v);

    tree[x] = tree[2 * x] + tree[2 * x + 1];
}

int query(int x, int l, int r, int ql, int qr) {
    if (ql <= l && r <= qr) return tree[x];
    if (l > qr || ql > r) return 0;

    int mid = (l + r) / 2;

    return query(2 * x, l, mid, ql, qr) + query(2 * x + 1, mid + 1, r, ql, qr);
}

set<edge> adj[200005];
set<pair<int, int>> query_q[200005];
vector<int> query_c[200005];
int ans[300005];
int sz[200005];

void dfs_centroid(int node, int par = -1) {
    sz[node] = 1;

    for (auto nxt : adj[node]) {
        if (nxt.to == par) continue;
        dfs_centroid(nxt.to, node);
        sz[node] += sz[nxt.to];
    }

    return;
}

int find_centroid(int node, int total, int par = -1) {
    for (auto nxt : adj[node]) {
        if (nxt.to == par) continue;
        if (sz[nxt.to] > total / 2) return find_centroid(nxt.to, total, node);
    }

    return node;
}

vector<int> children;
map<int, int> L;

void dfs_one(int node, int par, int cost) {
    auto itr = query_q[node].begin();

    while (itr != query_q[node].end()) {
        auto m_itr = L.find(itr->first);

        if (m_itr == L.end() || m_itr->second > itr->second) {
            itr++; continue;
        }

        ans[itr->second] = -1;
        itr = query_q[node].erase(itr);
    }

    children.push_back(node);

    for (auto nxt : adj[node]) {
        if (nxt.to == par) continue;
        if (nxt.cost < cost) dfs_one(nxt.to, node, nxt.cost);
    }
}

vector<pair<int, int>> rev;

void dfs_two(int node, int par, int cost) {
    L[node] = cost;
    update(1, 1, MAXN, cost, 1);
    rev.push_back({cost, -1});

    for (auto nxt : adj[node]) {
        if (nxt.to == par) continue;
        if (nxt.cost > cost) dfs_two(nxt.to, node, nxt.cost);
    }
}

void solve(int node) {
    L.clear();

    for (auto nxt : adj[node]) {
        children.clear();
        L[node] = nxt.cost;
        dfs_one(nxt.to, node, nxt.cost);

        for (auto child : children) {
            for (auto q : query_c[child]) {
                if (q > nxt.cost) {
                    ans[q] += query(1, 1, MAXN, 1, q) + 1;
                }
            }
        }

        dfs_two(nxt.to, node, nxt.cost);
    }

    auto itr = query_q[node].begin();

    while (itr != query_q[node].end()) {
        auto m_itr = L.find(itr->first);

        if (m_itr == L.end() || m_itr->second > itr->second) {
            itr++; continue;
        }

        ans[itr->second] = -1;
        itr = query_q[node].erase(itr);
    }

    for (auto q : query_c[node]) {
            ans[q] += query(1, 1, MAXN, 1, q) + 1;
    }

    while (!rev.empty()) {
        update(1, 1, MAXN, rev.back().first, rev.back().second);
        rev.pop_back();
    }
}

void centroid(int node) {
    dfs_centroid(node);

    int cent = find_centroid(node, sz[node]);

    solve(cent);

    // clean up

    vector<edge> del(adj[cent].begin(), adj[cent].end());

    for (auto e : del) {
        adj[cent].erase(e);
        adj[e.to].erase(adj[e.to].find({cent, e.cost}));
        centroid(e.to);
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
//    freopen("", "r", stdin);
//    freopen("", "w", stdout);
    int n, k; cin >> n >> k;

    fill(ans, ans+300005, -3);

    for (int i = 1; i <= n + k -1; i++) {
        char type; cin >> type;
        if (type == 'S') {
            int a, b; cin >> a >> b;
            adj[a].insert({b, i});
            adj[b].insert({a, i});
        } else if (type == 'Q') {
            int a, b; cin >> a >> b;
            // if b can reach a
            if (a == b) ans[i] = -1;
            else {
                query_q[b].insert({a, i});
                ans[i] = -2;
            }
        } else {
            // type c
            int d; cin >> d;
            query_c[d].push_back(i);
            ans[i] = 0;
        }
    }

    centroid(1);

    for (int i = 1; i <= n + k - 1; i++) {
        if (ans[i] == -3) continue;
        if (ans[i] >= 0) {
            cout << ans[i];
        } else {
            cout << (ans[i] == -1 ? "yes" : "no");
        }

        cout << "\n";
    }
}
# Verdict Execution time Memory Grader output
1 Correct 68 ms 31576 KB Output is correct
2 Correct 100 ms 33040 KB Output is correct
3 Correct 91 ms 34336 KB Output is correct
4 Correct 102 ms 34544 KB Output is correct
5 Correct 82 ms 33164 KB Output is correct
6 Correct 96 ms 33484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 31576 KB Output is correct
2 Correct 100 ms 33040 KB Output is correct
3 Correct 91 ms 34336 KB Output is correct
4 Correct 102 ms 34544 KB Output is correct
5 Correct 82 ms 33164 KB Output is correct
6 Correct 96 ms 33484 KB Output is correct
7 Correct 63 ms 32160 KB Output is correct
8 Correct 86 ms 31988 KB Output is correct
9 Correct 83 ms 31608 KB Output is correct
10 Correct 85 ms 31796 KB Output is correct
11 Correct 91 ms 30412 KB Output is correct
12 Correct 83 ms 30688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 31564 KB Output is correct
2 Correct 334 ms 51188 KB Output is correct
3 Correct 362 ms 53972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 31564 KB Output is correct
2 Correct 334 ms 51188 KB Output is correct
3 Correct 362 ms 53972 KB Output is correct
4 Correct 53 ms 32188 KB Output is correct
5 Correct 325 ms 53100 KB Output is correct
6 Correct 227 ms 49624 KB Output is correct
7 Correct 221 ms 49896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 31600 KB Output is correct
2 Correct 393 ms 53692 KB Output is correct
3 Correct 385 ms 57032 KB Output is correct
4 Correct 527 ms 62408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 31600 KB Output is correct
2 Correct 393 ms 53692 KB Output is correct
3 Correct 385 ms 57032 KB Output is correct
4 Correct 527 ms 62408 KB Output is correct
5 Correct 55 ms 32100 KB Output is correct
6 Correct 396 ms 56068 KB Output is correct
7 Correct 595 ms 59900 KB Output is correct
8 Correct 425 ms 54820 KB Output is correct
9 Correct 409 ms 54856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 31524 KB Output is correct
2 Correct 524 ms 48068 KB Output is correct
3 Correct 347 ms 47556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 31524 KB Output is correct
2 Correct 524 ms 48068 KB Output is correct
3 Correct 347 ms 47556 KB Output is correct
4 Correct 59 ms 32224 KB Output is correct
5 Correct 557 ms 49924 KB Output is correct
6 Correct 365 ms 46716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 31564 KB Output is correct
2 Correct 374 ms 53692 KB Output is correct
3 Correct 404 ms 57068 KB Output is correct
4 Correct 519 ms 62372 KB Output is correct
5 Correct 57 ms 32420 KB Output is correct
6 Correct 486 ms 51332 KB Output is correct
7 Correct 359 ms 47704 KB Output is correct
8 Correct 332 ms 47752 KB Output is correct
9 Correct 370 ms 48216 KB Output is correct
10 Correct 571 ms 51656 KB Output is correct
11 Correct 624 ms 51304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 31564 KB Output is correct
2 Correct 374 ms 53692 KB Output is correct
3 Correct 404 ms 57068 KB Output is correct
4 Correct 519 ms 62372 KB Output is correct
5 Correct 57 ms 32420 KB Output is correct
6 Correct 486 ms 51332 KB Output is correct
7 Correct 359 ms 47704 KB Output is correct
8 Correct 332 ms 47752 KB Output is correct
9 Correct 370 ms 48216 KB Output is correct
10 Correct 571 ms 51656 KB Output is correct
11 Correct 624 ms 51304 KB Output is correct
12 Correct 53 ms 32096 KB Output is correct
13 Correct 410 ms 56108 KB Output is correct
14 Correct 577 ms 60064 KB Output is correct
15 Correct 408 ms 54932 KB Output is correct
16 Correct 434 ms 54840 KB Output is correct
17 Correct 56 ms 32188 KB Output is correct
18 Correct 595 ms 49848 KB Output is correct
19 Correct 353 ms 46716 KB Output is correct
20 Correct 373 ms 46052 KB Output is correct
21 Correct 366 ms 46976 KB Output is correct
22 Correct 660 ms 49120 KB Output is correct
23 Correct 831 ms 51612 KB Output is correct
24 Correct 786 ms 52856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 31564 KB Output is correct
2 Correct 92 ms 33052 KB Output is correct
3 Correct 95 ms 34344 KB Output is correct
4 Correct 81 ms 34576 KB Output is correct
5 Correct 73 ms 33172 KB Output is correct
6 Correct 85 ms 33512 KB Output is correct
7 Correct 59 ms 32520 KB Output is correct
8 Correct 333 ms 54016 KB Output is correct
9 Correct 363 ms 53976 KB Output is correct
10 Correct 55 ms 32468 KB Output is correct
11 Correct 448 ms 56956 KB Output is correct
12 Correct 386 ms 57080 KB Output is correct
13 Correct 549 ms 62440 KB Output is correct
14 Correct 57 ms 32452 KB Output is correct
15 Correct 500 ms 51308 KB Output is correct
16 Correct 357 ms 47560 KB Output is correct
17 Correct 361 ms 47708 KB Output is correct
18 Correct 359 ms 48148 KB Output is correct
19 Correct 605 ms 51556 KB Output is correct
20 Correct 614 ms 51284 KB Output is correct
21 Correct 419 ms 54456 KB Output is correct
22 Correct 458 ms 52640 KB Output is correct
23 Correct 439 ms 49308 KB Output is correct
24 Correct 429 ms 49288 KB Output is correct
25 Correct 520 ms 54768 KB Output is correct
26 Correct 330 ms 47656 KB Output is correct
27 Correct 344 ms 47220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 31564 KB Output is correct
2 Correct 92 ms 33052 KB Output is correct
3 Correct 95 ms 34344 KB Output is correct
4 Correct 81 ms 34576 KB Output is correct
5 Correct 73 ms 33172 KB Output is correct
6 Correct 85 ms 33512 KB Output is correct
7 Correct 59 ms 32520 KB Output is correct
8 Correct 333 ms 54016 KB Output is correct
9 Correct 363 ms 53976 KB Output is correct
10 Correct 55 ms 32468 KB Output is correct
11 Correct 448 ms 56956 KB Output is correct
12 Correct 386 ms 57080 KB Output is correct
13 Correct 549 ms 62440 KB Output is correct
14 Correct 57 ms 32452 KB Output is correct
15 Correct 500 ms 51308 KB Output is correct
16 Correct 357 ms 47560 KB Output is correct
17 Correct 361 ms 47708 KB Output is correct
18 Correct 359 ms 48148 KB Output is correct
19 Correct 605 ms 51556 KB Output is correct
20 Correct 614 ms 51284 KB Output is correct
21 Correct 419 ms 54456 KB Output is correct
22 Correct 458 ms 52640 KB Output is correct
23 Correct 439 ms 49308 KB Output is correct
24 Correct 429 ms 49288 KB Output is correct
25 Correct 520 ms 54768 KB Output is correct
26 Correct 330 ms 47656 KB Output is correct
27 Correct 344 ms 47220 KB Output is correct
28 Correct 60 ms 32164 KB Output is correct
29 Correct 80 ms 31720 KB Output is correct
30 Correct 78 ms 31636 KB Output is correct
31 Correct 92 ms 31940 KB Output is correct
32 Correct 81 ms 30480 KB Output is correct
33 Correct 96 ms 30772 KB Output is correct
34 Correct 52 ms 32180 KB Output is correct
35 Correct 342 ms 53252 KB Output is correct
36 Correct 224 ms 49668 KB Output is correct
37 Correct 234 ms 49960 KB Output is correct
38 Correct 63 ms 32156 KB Output is correct
39 Correct 409 ms 56184 KB Output is correct
40 Correct 577 ms 59968 KB Output is correct
41 Correct 405 ms 54732 KB Output is correct
42 Correct 409 ms 54852 KB Output is correct
43 Correct 57 ms 32112 KB Output is correct
44 Correct 586 ms 49836 KB Output is correct
45 Correct 375 ms 46620 KB Output is correct
46 Correct 377 ms 46088 KB Output is correct
47 Correct 374 ms 47048 KB Output is correct
48 Correct 598 ms 48988 KB Output is correct
49 Correct 822 ms 51680 KB Output is correct
50 Correct 753 ms 52876 KB Output is correct
51 Correct 325 ms 50748 KB Output is correct
52 Correct 238 ms 50692 KB Output is correct
53 Correct 233 ms 50404 KB Output is correct
54 Correct 245 ms 50724 KB Output is correct
55 Correct 277 ms 47772 KB Output is correct
56 Correct 427 ms 46484 KB Output is correct
57 Correct 376 ms 51944 KB Output is correct
58 Correct 474 ms 46288 KB Output is correct
59 Correct 350 ms 47120 KB Output is correct