답안 #1113970

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1113970 2024-11-18T01:10:26 Z spycoderyt 동기화 (JOI13_synchronization) C++17
0 / 100
1731 ms 38464 KB
#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int N = 3e5+5;
int st[N],in[N],out[N],p[33][N],fen[N],dp[2][N],child[N],dep[N];
vector<int> A[N];
int cnt = 1;
int n,m,q,a,b,x;
vector<int> v;
void add(int pos,int val) {
    for(int i = pos;i<N;i+=i&-i) fen[i]+=val;
}
int qry(int pos) {
    if(!pos)return 0;
    int res = 0;
    for(int i = pos;i>0;i-=i&-i) res += fen[i];
    return res;
}
void dfs(int u,int par = -1) {
    in[u]=cnt++;
    v.push_back(u);
    for(auto nxt : A[u])if(nxt!=par) {
        dep[nxt] = dep[u]+1;
        dfs(nxt,u);
        p[0][nxt] = u;
    }
    out[u]=cnt;
}
void dolca() {
    for(int i = 1;i<32;i++) {
        for(int j = 1;j<=n;j++) p[i][j] = p[i-1][p[i-1][j]];
    }
}
int fp(int x) {
    for(int i = 31;i>=0;i--) {
        int cur = p[i][x];
        if(qry(in[x]) - qry(in[cur]-1) == 0) x = cur;
    }
    return x;
}
int main() {
    v.push_back(0);
    cin>>n>>m>>q;
    vector<pair<int,int> > tmp;
    for(int i = 0;i<n-1;i++) {
        cin>>a>>b;
        A[a].push_back(b);A[b].push_back(a);
        tmp.push_back({a,b});
    }
    dfs(1);
    p[0][1] = 1;
    dolca();
    for(int i = 0;i<n-1;i++) {
        if(dep[tmp[i].f] < dep[tmp[i].s]) swap(tmp[i].f,tmp[i].s);
        child[i+1] = tmp[i].f;
    }
    dp[0][1]=1;
    for(int i = 2;i<=n;i++) add(in[i],1),add(out[i]+1,-1),dp[0][i]=1;
    // for(int i = 1;i<=n;i++)cerr<<qry(i)-qry(i-1)<< " ";
    // cerr<<"\n";
    for(int i = 0;i<m;i++) {
        cin>>x;
        x = child[x];
        st[x] = 1-st[x];
        if(st[x]) {
            //turn on
            int anc = fp(p[0][x]);
            cerr << x << " " << anc << "\n";
            add(in[x],-1),add(out[x]+1,1);
            dp[0][anc] += dp[0][x] - dp[1][x];
        } else {
            // // turn off
            int anc = fp(p[0][x]);
            cerr << x << " " << anc << "\n";
            add(in[x],1),add(out[x]+1,-1);
            dp[0][x] = dp[1][x] = dp[0][anc];
        }
    }
    for(int i = 0;i<q;i++) {
        cin>>x;
        if(st[x]) {
            st[x] = 1-st[x];
            int anc = fp(p[0][x]);
            add(in[x],1),add(out[x]+1,-1);
            dp[0][x] = dp[1][x] = dp[0][anc];
        }
        cout << dp[0][x] << "\n";
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7760 KB Output is correct
2 Correct 8 ms 7760 KB Output is correct
3 Incorrect 6 ms 7760 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1272 ms 33312 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 7 ms 7760 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1731 ms 38464 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7760 KB Output is correct
2 Incorrect 5 ms 7584 KB Output isn't correct
3 Halted 0 ms 0 KB -