Submission #1039603

# Submission time Handle Problem Language Result Execution time Memory
1039603 2024-07-31T05:20:21 Z Plurm Inside information (BOI21_servers) C++11
2.5 / 100
3500 ms 56252 KB
#include <bits/stdc++.h>
using namespace std;

vector<int> g[500005];
vector<int> rg[500005];
int cnt[500005];
int primpar[500005];
int par[500005][20];
int st[500005];
int ed[500005];
int dep[500005];
int t;
void dfs(int u, int p = 0){
    par[u][0] = p;
    dep[u] = dep[p] + 1;
    st[u] = ++t;
    for(int i = 1; i < 20; i++) par[u][i] = par[par[u][i-1]][i-1];
    for(int v : rg[u]){
        dfs(v, u);
    }
    ed[u] = t;
}
int FT[500005];
void add(int idx, int amt){
    while(idx < 500005){
        FT[idx] += amt;
        idx += idx & -idx;
    }
}
int sum(int idx){
    int ret = 0;
    while(idx > 0){
        ret += FT[idx];
        idx -= idx & -idx;
    }
    return ret;
}
int lca(int u, int v){
    if(dep[u] > dep[v]) swap(u, v);
    for(int i = 19; i >= 0; i--){
        if(dep[u] <= dep[par[v][i]]) v = par[v][i];
    }
    if(u == v) return u;
    for(int i = 19; i >= 0; i--){
        if(par[u][i] != par[v][i]){
            u = par[u][i];
            v = par[v][i];
        }
    }
    return par[u][0];
}
bool search(int a, int d){
    return lca(a, primpar[d]) == primpar[d];
}
int count(int d){
    return sum(ed[primpar[d]]) - sum(st[primpar[d]]-1);
}
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++;
                if(ids[a] != a){
                    g[last].push_back(ids[a]);
                    rg[ids[a]].push_back(last);
                }else{
                    primpar[a] = last;
                }
                if(ids[b] != b){
                    g[last].push_back(ids[b]);
                    rg[ids[b]].push_back(last);
                }else{
                    primpar[b] = 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(n+1);
    for(int i = 1; i <= n; i++){
        ids[i] = i;
        cnt[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]);
                cnt[ids[a]]--;
                if(a != ids[a])
                    add(st[ids[a]], -1);
                cnt[ids[b]]--;
                if(b != ids[b])
                    add(st[ids[b]], -1);
                last++;
                ids[a] = ids[b] = last;
                cnt[last] += 2;
                add(st[last], 2);
                break;
            case 1:
                a = get<1>(cmds[i]);
                d = get<2>(cmds[i]);
                if(search(ids[a], d)) cout << "yes" << endl;
                else cout << "no" << endl;
                break;
            case 2:
                d = get<1>(cmds[i]);
                cout << count(d) << endl;
                break;
        }
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Execution timed out 3553 ms 26320 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3553 ms 26320 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 152 ms 26824 KB Output is correct
2 Correct 236 ms 56252 KB Output is correct
3 Correct 301 ms 56156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 152 ms 26824 KB Output is correct
2 Correct 236 ms 56252 KB Output is correct
3 Correct 301 ms 56156 KB Output is correct
4 Incorrect 149 ms 26816 KB Extra information in the output file
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3544 ms 26308 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3544 ms 26308 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3563 ms 26312 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3563 ms 26312 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3540 ms 26308 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3540 ms 26308 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3564 ms 26320 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 3564 ms 26320 KB Time limit exceeded
2 Halted 0 ms 0 KB -