Submission #1040605

# Submission time Handle Problem Language Result Execution time Memory
1040605 2024-08-01T07:45:07 Z Plurm Inside information (BOI21_servers) C++11
65 / 100
3500 ms 230592 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;

vector<int> g[500005];
vector<int> rg[500005];
int st[500005];
int dep[500005];
int ddep[500005];
int t;
int ett[1000005];
int rev[1000005];
void dfs(int u, int p = 0, int d = 0){
    t++;
    ddep[u] = d;
    ett[t] = dep[u] = dep[p]+1;
    st[u] = t;
    rev[t] = u;
    for(int v : g[u]){
        if(v == p) continue;
        dfs(v, u, d+1);
        t++;
        ett[t] = dep[u];
        rev[t] = u;
    }
    for(int v : rg[u]){
        if(v == p) continue;
        dfs(v, u, d-1);
        t++;
        ett[t] = dep[u];
        rev[t] = u;
    }
}
pair<int, int> sparse[21][1048576];
void build(){
    for(int i = 0; i < 1000005; i++) sparse[0][i] = {ett[i], rev[i]};
    for(int i = 1; i < 21; i++){
        for(int j = 0; j + (1 << i) < 1048576; j++){
            sparse[i][j] = min(sparse[i-1][j], sparse[i-1][j+(1<<(i-1))]);
        }
    }
}
inline pair<int,int> rmq(const int& l, const int& r){
    int d = r-l+1;
    int lg = 31 - __builtin_clz(d);
    return min(sparse[lg][l], sparse[lg][r-(1<<lg)+1]);
}
inline int lca(const int& u, const int& v){
    if(st[u] > st[v]) return rmq(st[v], st[u]).second;
    else return rmq(st[u], st[v]).second;
}
inline bool search(const int& a, const int& d){
    int l = lca(a, d);
    int dist = dep[a] + dep[d] - 2*dep[l];
    int ddist = ddep[d] - ddep[a];
    return dist == ddist;
}
vector<tuple<int,int,int>> buffer;
const int THRESH = 1600;
int qs[500005];
int evp[500005];
int update(int u, int p = 0, int d = 0){
    for(int v : rg[u]){
        if(v == p) continue;
        d += update(v, u, 0);
    }
    d += evp[u];
    qs[u] += d;
    for(int v : g[u]){
        if(v == p) continue;
        update(v, u, d);
    }
    return d;
}
int count(int d){
    if(buffer.size() >= THRESH){
        for(auto p : buffer){
            evp[get<0>(p)]--;
            evp[get<1>(p)]--;
            evp[get<2>(p)] += 2;
        }
        update(1);
        for(auto p : buffer){
            evp[get<0>(p)]++;
            evp[get<1>(p)]++;
            evp[get<2>(p)] -= 2;
        }
        buffer.clear();
    }
    int ret = qs[d];
    for(auto p : buffer){
        if(search(get<0>(p), d) || search(get<1>(p), d)){
            ret++;
        }else if(search(get<2>(p), d)){
            ret += 2;
        }
    }
    return ret;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, k;
    cin >> n >> k;
    k += n-1;
    vector<tuple<int,int,int>> cmds;
    vector<int> ids(n+1);
    for(int i = 1; i <= n; i++){
        ids[i] = i;
    }
    int last = n;
    for(int i = 0; i < k; i++){
        string cmd;
        cin >> cmd;
        int a, b, d;
        switch(cmd[0]){
            case 'S':
                cin >> a >> b;
                last++;
                g[last].push_back(ids[a]);
                rg[ids[a]].push_back(last);
                g[last].push_back(ids[b]);
                rg[ids[b]].push_back(last);
                ids[a] = ids[b] = last;
                cmds.push_back({0, a, b});
                break;
            case 'Q':
                cin >> a >> d;
                cmds.push_back({1, a, d});
                break;
            case 'C':
                cin >> d;
                cmds.push_back({2, d, 0});
                break;
        }
    }
    dfs(1);
    build();
    for(int i = 1; i <= n; i++){
        ids[i] = i;
        qs[i]++;
    }
    last = n;
    for(int i = 0; i < k; i++){
        int a, b, d;
        switch(get<0>(cmds[i])){
            case 0:
                a = get<1>(cmds[i]);
                b = get<2>(cmds[i]);
                last++;
                buffer.push_back({ids[a], ids[b], last});
                ids[a] = ids[b] = last;
                break;
            case 1:
                a = get<1>(cmds[i]);
                d = get<2>(cmds[i]);
                if(search(ids[a], d)) cout << "yes\n";
                else cout << "no\n";
                break;
            case 2:
                d = get<1>(cmds[i]);
                cout << count(d) << "\n";
                break;
        }
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 52 ms 197064 KB Output is correct
2 Correct 58 ms 197596 KB Output is correct
3 Correct 57 ms 197816 KB Output is correct
4 Correct 60 ms 197592 KB Output is correct
5 Correct 65 ms 197852 KB Output is correct
6 Correct 63 ms 198004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 197064 KB Output is correct
2 Correct 58 ms 197596 KB Output is correct
3 Correct 57 ms 197816 KB Output is correct
4 Correct 60 ms 197592 KB Output is correct
5 Correct 65 ms 197852 KB Output is correct
6 Correct 63 ms 198004 KB Output is correct
7 Correct 55 ms 197064 KB Output is correct
8 Correct 1006 ms 197500 KB Output is correct
9 Correct 776 ms 197696 KB Output is correct
10 Correct 966 ms 197524 KB Output is correct
11 Correct 837 ms 197892 KB Output is correct
12 Correct 398 ms 198144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 197068 KB Output is correct
2 Correct 111 ms 230144 KB Output is correct
3 Correct 116 ms 230144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 197068 KB Output is correct
2 Correct 111 ms 230144 KB Output is correct
3 Correct 116 ms 230144 KB Output is correct
4 Correct 51 ms 197096 KB Output is correct
5 Correct 604 ms 227492 KB Output is correct
6 Correct 1044 ms 227816 KB Output is correct
7 Correct 1276 ms 227584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 197064 KB Output is correct
2 Correct 140 ms 229116 KB Output is correct
3 Correct 143 ms 229116 KB Output is correct
4 Correct 104 ms 230272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 197064 KB Output is correct
2 Correct 140 ms 229116 KB Output is correct
3 Correct 143 ms 229116 KB Output is correct
4 Correct 104 ms 230272 KB Output is correct
5 Correct 57 ms 197076 KB Output is correct
6 Correct 2688 ms 226236 KB Output is correct
7 Correct 774 ms 230592 KB Output is correct
8 Execution timed out 3552 ms 228708 KB Time limit exceeded
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 53 ms 197060 KB Output is correct
2 Correct 102 ms 218624 KB Output is correct
3 Correct 134 ms 218028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 197060 KB Output is correct
2 Correct 102 ms 218624 KB Output is correct
3 Correct 134 ms 218028 KB Output is correct
4 Correct 55 ms 197064 KB Output is correct
5 Correct 963 ms 215956 KB Output is correct
6 Correct 2064 ms 216756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 197072 KB Output is correct
2 Correct 138 ms 229124 KB Output is correct
3 Correct 138 ms 229072 KB Output is correct
4 Correct 102 ms 230320 KB Output is correct
5 Correct 50 ms 197092 KB Output is correct
6 Correct 114 ms 218556 KB Output is correct
7 Correct 168 ms 217980 KB Output is correct
8 Correct 169 ms 218372 KB Output is correct
9 Correct 178 ms 218368 KB Output is correct
10 Correct 125 ms 223232 KB Output is correct
11 Correct 125 ms 222460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 197072 KB Output is correct
2 Correct 138 ms 229124 KB Output is correct
3 Correct 138 ms 229072 KB Output is correct
4 Correct 102 ms 230320 KB Output is correct
5 Correct 50 ms 197092 KB Output is correct
6 Correct 114 ms 218556 KB Output is correct
7 Correct 168 ms 217980 KB Output is correct
8 Correct 169 ms 218372 KB Output is correct
9 Correct 178 ms 218368 KB Output is correct
10 Correct 125 ms 223232 KB Output is correct
11 Correct 125 ms 222460 KB Output is correct
12 Correct 63 ms 197080 KB Output is correct
13 Correct 3388 ms 226268 KB Output is correct
14 Correct 849 ms 227436 KB Output is correct
15 Execution timed out 3521 ms 226052 KB Time limit exceeded
16 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 53 ms 196960 KB Output is correct
2 Correct 59 ms 197576 KB Output is correct
3 Correct 62 ms 197788 KB Output is correct
4 Correct 57 ms 197596 KB Output is correct
5 Correct 59 ms 197892 KB Output is correct
6 Correct 61 ms 197892 KB Output is correct
7 Correct 51 ms 197072 KB Output is correct
8 Correct 121 ms 230144 KB Output is correct
9 Correct 150 ms 230148 KB Output is correct
10 Correct 51 ms 197196 KB Output is correct
11 Correct 148 ms 229028 KB Output is correct
12 Correct 145 ms 228924 KB Output is correct
13 Correct 105 ms 230268 KB Output is correct
14 Correct 51 ms 197064 KB Output is correct
15 Correct 108 ms 218552 KB Output is correct
16 Correct 163 ms 218104 KB Output is correct
17 Correct 157 ms 218276 KB Output is correct
18 Correct 160 ms 218348 KB Output is correct
19 Correct 135 ms 223188 KB Output is correct
20 Correct 128 ms 222464 KB Output is correct
21 Correct 135 ms 222748 KB Output is correct
22 Correct 136 ms 223064 KB Output is correct
23 Correct 146 ms 219504 KB Output is correct
24 Correct 146 ms 219648 KB Output is correct
25 Correct 142 ms 225016 KB Output is correct
26 Correct 126 ms 218788 KB Output is correct
27 Correct 139 ms 218880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 196960 KB Output is correct
2 Correct 59 ms 197576 KB Output is correct
3 Correct 62 ms 197788 KB Output is correct
4 Correct 57 ms 197596 KB Output is correct
5 Correct 59 ms 197892 KB Output is correct
6 Correct 61 ms 197892 KB Output is correct
7 Correct 51 ms 197072 KB Output is correct
8 Correct 121 ms 230144 KB Output is correct
9 Correct 150 ms 230148 KB Output is correct
10 Correct 51 ms 197196 KB Output is correct
11 Correct 148 ms 229028 KB Output is correct
12 Correct 145 ms 228924 KB Output is correct
13 Correct 105 ms 230268 KB Output is correct
14 Correct 51 ms 197064 KB Output is correct
15 Correct 108 ms 218552 KB Output is correct
16 Correct 163 ms 218104 KB Output is correct
17 Correct 157 ms 218276 KB Output is correct
18 Correct 160 ms 218348 KB Output is correct
19 Correct 135 ms 223188 KB Output is correct
20 Correct 128 ms 222464 KB Output is correct
21 Correct 135 ms 222748 KB Output is correct
22 Correct 136 ms 223064 KB Output is correct
23 Correct 146 ms 219504 KB Output is correct
24 Correct 146 ms 219648 KB Output is correct
25 Correct 142 ms 225016 KB Output is correct
26 Correct 126 ms 218788 KB Output is correct
27 Correct 139 ms 218880 KB Output is correct
28 Correct 58 ms 197120 KB Output is correct
29 Correct 1097 ms 197332 KB Output is correct
30 Correct 848 ms 197668 KB Output is correct
31 Correct 996 ms 197444 KB Output is correct
32 Correct 848 ms 197888 KB Output is correct
33 Correct 394 ms 197992 KB Output is correct
34 Correct 51 ms 197068 KB Output is correct
35 Correct 629 ms 227448 KB Output is correct
36 Correct 1049 ms 227840 KB Output is correct
37 Correct 1263 ms 227944 KB Output is correct
38 Correct 56 ms 197064 KB Output is correct
39 Correct 2679 ms 226304 KB Output is correct
40 Correct 786 ms 227584 KB Output is correct
41 Execution timed out 3535 ms 226048 KB Time limit exceeded
42 Halted 0 ms 0 KB -