Danil and a Part-time Job codeforces dfs序,线段树
来源:互联网 发布:廖雪峰java 编辑:程序博客网 时间:2024/05/17 06:13
Danil decided to earn some money, so he had found a part-time job. The interview have went well, so now he is a light switcher.
Danil works in a rooted tree (undirected connected acyclic graph) with n vertices, vertex 1 is the root of the tree. There is a room in each vertex, light can be switched on or off in each room. Danil's duties include switching light in all rooms of the subtree of the vertex. It means that if light is switched on in some room of the subtree, he should switch it off. Otherwise, he should switch it on.
Unfortunately (or fortunately), Danil is very lazy. He knows that his boss is not going to personally check the work. Instead, he will send Danil tasks usingWorkforces personal messages.
There are two types of tasks:
- pow v describes a task to switch lights in the subtree of vertexv.
- get v describes a task to count the number of rooms in the subtree ofv, in which the light is turned on. Danil should send the answer to his boss usingWorkforces messages.
A subtree of vertex v is a set of vertices for which the shortest path from them to the root passes throughv. In particular, the vertexv is in the subtree of v.
Danil is not going to perform his duties. He asks you to write a program, which answers the boss instead of him.
The first line contains a single integer n (1 ≤ n ≤ 200 000) — the number of vertices in the tree.
The second line contains n - 1 space-separated integersp2, p3, ..., pn (1 ≤ pi < i), wherepi is the ancestor of vertexi.
The third line contains n space-separated integerst1, t2, ..., tn (0 ≤ ti ≤ 1), whereti is1, if the light is turned on in vertexi and 0 otherwise.
The fourth line contains a single integer q (1 ≤ q ≤ 200 000) — the number of tasks.
The next q lines are get v or pow v (1 ≤ v ≤ n) — the tasks described above.
For each task get v print the number of rooms in the subtree ofv, in which the light is turned on.
41 1 11 0 0 19get 1get 2get 3get 4pow 1get 1get 2get 3get 4
20012110
#include<bits/stdc++.h>using namespace std;const int M=2e5+10;int tree[4*M],lazy[4*M],root=1,pre[4*M],cnt,tot,in[4*M],out[4*M],idx[4*M],node[4*M];struct Edge{ int u,v,next;} edge[2*M];void pushdown(int now,int l,int r){ int mid=(l+r)/2; int lch=now*2,rch=now*2+1; lazy[lch]+=lazy[now]; lazy[rch]+=lazy[now]; if(lazy[now]%2) { lazy[now]=0; tree[lch]=(mid-l+1)-tree[lch]; tree[rch]=(r-mid)-tree[rch]; }}void update(int now,int l,int r,int al,int ar){ if(al>r||ar<l) return; if(l>=al&&r<=ar) { lazy[now]++; tree[now]=(r-l+1)-tree[now]; return ; } pushdown(now,l,r); int mid=(l+r)/2; update(now*2,l,mid,al,ar); update(now*2+1,mid+1,r,al,ar); tree[now]=tree[now*2]+tree[now*2+1];}int query(int now,int l,int r,int al,int ar){ if(al>r||ar<l) return 0; if(l>=al&&r<=ar) { return tree[now]; } pushdown(now,l,r); int mid=(l+r)/2; return query(now*2,l,mid,al,ar)+query(now*2+1,mid+1,r,al,ar);}void creat(int now,int l,int r,int x[]){ lazy[now]=0; if(l==r) { tree[now]=node[x[l]]; return ; } int mid=(l+r)/2; creat(now*2,l,mid,x); creat(now*2+1,mid+1,r,x); tree[now]=tree[now*2]+tree[now*2+1];}void add(int u,int v){ edge[cnt].u=u; edge[cnt].v=v; edge[cnt].next=pre[u]; pre[u]=cnt++;}void dfs(int now,int fa){ in[now]=++tot; idx[tot]=now; for(int i=pre[now]; i!=-1; i=edge[i].next) { int v=edge[i].v; if(v!=fa) dfs(v,now); } out[now]=tot;}int main(){ int n; while(scanf("%d",&n)!=EOF) { cnt=0; tot=0; memset(pre,-1,sizeof(pre)); for(int i=2; i<=n; i++) { int tep; scanf("%d",&tep); add(tep,i); add(i,tep); } for(int i=1; i<=n; i++) scanf("%d",&node[i]); dfs(1,1); creat(1,1,n,idx); int q; scanf("%d",&q); while(q--) { char op[10]; int nod; scanf("%s%d",op,&nod); if(op[0]=='g') { int l=in[nod]; int r=out[nod]; int ans=query(1,1,n,l,r); printf("%d\n",ans); } else { int l=in[nod]; int r=out[nod]; update(1,1,n,l,r); } } }}
- Codeforces 877E Danil and a Part-time Job【Dfs序+线段树】
- Codeforces 877 E Danil and a Part-time Job(线段树+dfs序)
- Danil and a Part-time Job codeforces dfs序,线段树
- Codeforces Round #442 (Div. 2) E. Danil and a Part-time Job【线段树+dfs序】
- [codeforces] 877E. Danil and a Part-time Job(DFS序+线段树)
- CodeForces 877E Danil and a Part-time Job (dfs序+线段树)
- Codeforces-877E:Danil and a Part-time Job(DFS序列+线段树)
- codeforces 877E. Danil and a Part-time Job (DFS序列+线段树)
- Codeforces Round #442 (Div. 2) Danil and a Part-time Job dfs序+线段树区间反转
- Codeforces Round #442 (Div. 2)-E-Danil and a Part-time Job(DFS序+线段树区间更新)
- Codeforces Round #442 (Div. 2) 877 E. Danil and a Part-time Job DFS序 线段树
- Codeforces Round #442 (Div. 2) E. Danil and a Part-time Job DFS序+树链剖分+线段树区间^
- Codeforces Round #442 (Div. 2) E. Danil and a Part-time Job (dfs序+线段树)
- codeforces 877 problem E Danil and a Part-time Job 【dfs序 + 线段树区间异或修改】
- Codeforces Round #442E-dfs序&线段树的区间更新区间查询-Danil and a Part-time Job
- CF#877 E. Danil and a Part-time Job(DFS序+线段树)
- [CodeForces877 E. Danil and a Part-time Job]dfs序+线段树
- Codeforces Round #442 (Div. 2) E. Danil and a Part-time Job (dfs序树型转线性 线段树区间修改区间查询)
- Docker实战:使用Docker安装部署Gitlab
- 电阻桥(惠斯通电桥)的分析方法及用途
- 程序在内存中的分布
- 学习笔记TF053:循环神经网络,TensorFlow Model Zoo,强化学习,深度森林,深度学习艺术
- 计算机视觉
- Danil and a Part-time Job codeforces dfs序,线段树
- 进程、信号
- 最常见的英文写作问题
- 小甲鱼学python之序列
- P1755 斐波那契的拆分
- id ma s2h
- 使用SwitchyOmega设置Chrome代理
- 百度阿波罗计划是什么?
- 51单片机做的74HC595驱动控制LED