이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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) {
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]) == 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";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |