10933 | 2024-04-20 15:56:34 | k_balint | Gyors utak | cpp17 | Accepted 100/100 | 207ms | 41476 KiB |
#include <bits/stdc++.h>
using namespace std;
const int c=2e5+5;
int tree[4*c][2], lazy[4*c];
int n,q,tt;
vector<int> adj[c];
int tin[c],tout[c],arr[c], mp[c];
void dfs(int v, int p){
arr[v]^=arr[p];
tin[v]=tout[v]=++tt;
mp[tin[v]]=v;
for(int x:adj[v]){
if(x != p){
dfs(x,v);
tout[v]=tout[x];
}
}
}
void build(int v, int l, int r){
if(l==r){
int x=mp[l];
tree[v][arr[x]]=1;
return;
}
int mid=l+r>>1;
build(2*v,l,mid);
build(2*v+1,mid+1,r);
tree[v][0]=tree[2*v][0]+tree[2*v+1][0];
tree[v][1]=tree[2*v][1]+tree[2*v+1][1];
}
void push(int v, int l, int r){
if(lazy[v]==0) return;
swap(tree[v][0],tree[v][1]);
if(l!=r){
lazy[2*v]^=1;
lazy[2*v+1]^=1;
}
lazy[v]=0;
}
void update(int v, int l, int r, int x, int y){
push(v,l,r);
if(l>r || l>y || x>r || x>y) return;
if(x<=l && r<=y){
swap(tree[v][0], tree[v][1]);
if(l!=r){
lazy[2*v]^=1;
lazy[2*v+1]^=1;
}
return;
}
int mid=l+r>>1;
update(2*v,l,mid,x,y);
update(2*v+1,mid+1,r,x,y);
tree[v][0]=tree[2*v][0]+tree[2*v+1][0];
tree[v][1]=tree[2*v][1]+tree[2*v+1][1];
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
tt=0;
cin>>n>>q;
for(int i=2;i<=n;i++){
int a; cin>>a;
adj[a].push_back(i);
adj[i].push_back(a);
}
for(int i=2;i<=n;i++) cin>>arr[i];
dfs(1,0);
build(1,1,n);
cout << 1ll*tree[1][0]*(tree[1][0]-1)/2 + 1ll*tree[1][1]*(tree[1][1]-1)/2 << ' ';
while(q--){
int x; cin>>x;
++x;
update(1,1,n,tin[x],tout[x]);
cout << 1ll*tree[1][0]*(tree[1][0]-1)/2 + 1ll*tree[1][1]*(tree[1][1]-1)/2 << ' ';
}
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 6ms | 11344 KiB | ||||
2 | Accepted | 8ms | 12460 KiB | ||||
subtask2 | 10/10 | ||||||
3 | Accepted | 6ms | 12148 KiB | ||||
4 | Accepted | 6ms | 11896 KiB | ||||
5 | Accepted | 6ms | 12108 KiB | ||||
6 | Accepted | 6ms | 12116 KiB | ||||
7 | Accepted | 6ms | 12300 KiB | ||||
8 | Accepted | 6ms | 12256 KiB | ||||
subtask3 | 11/11 | ||||||
9 | Accepted | 6ms | 12148 KiB | ||||
10 | Accepted | 6ms | 11896 KiB | ||||
11 | Accepted | 6ms | 12108 KiB | ||||
12 | Accepted | 6ms | 12116 KiB | ||||
13 | Accepted | 6ms | 12300 KiB | ||||
14 | Accepted | 6ms | 12256 KiB | ||||
15 | Accepted | 8ms | 12992 KiB | ||||
16 | Accepted | 8ms | 12992 KiB | ||||
17 | Accepted | 8ms | 13048 KiB | ||||
18 | Accepted | 8ms | 13056 KiB | ||||
19 | Accepted | 8ms | 12992 KiB | ||||
20 | Accepted | 8ms | 13256 KiB | ||||
21 | Accepted | 9ms | 13236 KiB | ||||
22 | Accepted | 9ms | 13464 KiB | ||||
23 | Accepted | 9ms | 13544 KiB | ||||
24 | Accepted | 9ms | 13464 KiB | ||||
subtask4 | 21/21 | ||||||
25 | Accepted | 187ms | 41060 KiB | ||||
26 | Accepted | 187ms | 40940 KiB | ||||
27 | Accepted | 189ms | 41028 KiB | ||||
28 | Accepted | 193ms | 41228 KiB | ||||
29 | Accepted | 187ms | 41336 KiB | ||||
30 | Accepted | 187ms | 41476 KiB | ||||
subtask5 | 24/24 | ||||||
31 | Accepted | 7ms | 13388 KiB | ||||
32 | Accepted | 14ms | 15352 KiB | ||||
33 | Accepted | 108ms | 29108 KiB | ||||
34 | Accepted | 180ms | 29200 KiB | ||||
subtask6 | 34/34 | ||||||
35 | Accepted | 6ms | 12148 KiB | ||||
36 | Accepted | 6ms | 11896 KiB | ||||
37 | Accepted | 6ms | 12108 KiB | ||||
38 | Accepted | 6ms | 12116 KiB | ||||
39 | Accepted | 6ms | 12300 KiB | ||||
40 | Accepted | 6ms | 12256 KiB | ||||
41 | Accepted | 8ms | 12992 KiB | ||||
42 | Accepted | 8ms | 12992 KiB | ||||
43 | Accepted | 8ms | 13048 KiB | ||||
44 | Accepted | 8ms | 13056 KiB | ||||
45 | Accepted | 8ms | 12992 KiB | ||||
46 | Accepted | 8ms | 13256 KiB | ||||
47 | Accepted | 9ms | 13236 KiB | ||||
48 | Accepted | 9ms | 13464 KiB | ||||
49 | Accepted | 9ms | 13544 KiB | ||||
50 | Accepted | 9ms | 13464 KiB | ||||
51 | Accepted | 187ms | 41060 KiB | ||||
52 | Accepted | 187ms | 40940 KiB | ||||
53 | Accepted | 189ms | 41028 KiB | ||||
54 | Accepted | 193ms | 41228 KiB | ||||
55 | Accepted | 187ms | 41336 KiB | ||||
56 | Accepted | 187ms | 41476 KiB | ||||
57 | Accepted | 7ms | 13388 KiB | ||||
58 | Accepted | 14ms | 15352 KiB | ||||
59 | Accepted | 108ms | 29108 KiB | ||||
60 | Accepted | 180ms | 29200 KiB | ||||
61 | Accepted | 184ms | 29432 KiB | ||||
62 | Accepted | 180ms | 30244 KiB | ||||
63 | Accepted | 186ms | 30032 KiB | ||||
64 | Accepted | 181ms | 30084 KiB | ||||
65 | Accepted | 192ms | 29496 KiB | ||||
66 | Accepted | 188ms | 29512 KiB | ||||
67 | Accepted | 194ms | 29540 KiB | ||||
68 | Accepted | 202ms | 29564 KiB | ||||
69 | Accepted | 165ms | 29272 KiB | ||||
70 | Accepted | 197ms | 29640 KiB | ||||
71 | Accepted | 157ms | 29064 KiB | ||||
72 | Accepted | 200ms | 30108 KiB | ||||
73 | Accepted | 207ms | 30624 KiB | ||||
74 | Accepted | 127ms | 30628 KiB | ||||
75 | Accepted | 192ms | 29484 KiB | ||||
76 | Accepted | 193ms | 29492 KiB | ||||
77 | Accepted | 178ms | 30516 KiB |