博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ZOJ 3686 A Simple Tree Problem
阅读量:5164 次
发布时间:2019-06-13

本文共 2582 字,大约阅读时间需要 8 分钟。

 

A Simple Tree Problem

Time Limit: 3 Seconds      
Memory Limit: 65536 KB

Given a rooted tree, each node has a boolean (0 or 1) labeled on it. Initially, all the labels are 0.

We define this kind of operation: given a subtree, negate all its labels.

And we want to query the numbers of 1's of a subtree.

Input

Multiple test cases.

First line, two integer N and M, denoting the numbers of nodes and numbers of operations and queries.(1<=N<=100000, 1<=M<=10000)

Then a line with N-1 integers, denoting the parent of node 2..N. Root is node 1.

Then M lines, each line are in the format "o node" or "q node", denoting we want to operate or query on the subtree with root of a certain node.

Output

For each query, output an integer in a line.

Output a blank line after each test case.

Sample Input

3 21 1o 2q 1

Sample Output

1

Author: CUI, Tianyi

Contest: ZOJ Monthly, March 2013

 

1 #include 
2 #include
3 #include
4 #include
5 6 #define lson l,m,rt<<1 7 #define rson m+1,r,rt<<1|1 8 9 using namespace std; 10 11 vector
g[200010]; 12 int n,m,id; 13 14 const int maxn=200010; 15 16 struct Interval 17 { 18 int from,to; 19 }I[200010]; 20 21 void dfs(int node) 22 { 23 I[node].from=id; 24 id++; 25 int t=g[node].size(); 26 for(int i=0;i
>1; 60 push_down(rt); 61 build(lson); build(rson); 62 push_up(rt); 63 } 64 65 void update(int L,int R,int l,int r,int rt) 66 { 67 if(L<=l&&r<=R) 68 { 69 xxo[rt]^=1; 70 swap(m0[rt],m1[rt]); 71 return ; 72 } 73 int m=(l+r)>>1; 74 push_down(rt); 75 if(L<=m) update(L,R,lson); 76 if(R>m) update(L,R,rson); 77 push_up(rt); 78 } 79 80 int query(int L,int R,int l,int r,int rt) 81 { 82 if(L<=l&&r<=R) 83 { 84 push_down(rt); 85 return m1[rt]; 86 } 87 int m=(l+r)>>1,ret=0; 88 push_down(rt); 89 if(L<=m) ret+=query(L,R,lson); 90 if(R>m) ret+=query(L,R,rson); 91 push_up(rt); 92 return ret; 93 } 94 95 int main() 96 { 97 while(scanf("%d%d",&n,&m)!=EOF) 98 { 99 for(int i=0;i<=n+10;i++) g[i].clear();100 memset(m0,0,sizeof(m0));101 memset(m1,0,sizeof(m1));102 memset(xxo,0,sizeof(xxo));103 for(int i=2;i<=n;i++)104 {105 int a;106 scanf("%d",&a);107 g[a].push_back(i);108 }109 id=1;110 dfs(1);111 build(1,n,1);112 while(m--)113 {114 char cmd[5]; int a;115 scanf("%s%d",cmd,&a);116 if(cmd[0]=='o') update(I[a].from,I[a].to,1,n,1);117 else if(cmd[0]=='q') printf("%d\n",query(I[a].from,I[a].to,1,n,1));118 }119 putchar(10);120 }121 return 0;122 }

 

转载于:https://www.cnblogs.com/CKboss/p/3419993.html

你可能感兴趣的文章
AQS(AbstractQueuedSynchronizer)
查看>>
java例程练习(多线程[join()方法])
查看>>
Divide and conquer:Median(POJ 3579)
查看>>
springMVC4 注解配置实例
查看>>
单片机编程
查看>>
LeetCode-327 Count of Range Sum
查看>>
根据文件夹地址获取txt文件并获取txt内容索引
查看>>
js控制只能输入数字
查看>>
Filter in Servlet
查看>>
HDU4662(SummerTrainingDay03-B)
查看>>
JavaScript基础——定义变量
查看>>
MySql避免重复插入记录
查看>>
Linux--SquashFS
查看>>
Application Pool Identities
查看>>
Nginx服务编译安装、日志功能、状态模块及访问认证模式实操
查看>>
2017-3-24 开通博客园
查看>>
【MySQL性能优化】MySQL常见SQL错误用法
查看>>
3.6 字符串
查看>>
Vue2全家桶之一:vue-cli(vue脚手架)超详细教程
查看>>
nginx负载均衡 ->Tomcat8集群 -> sentinel集群 -> redis3主从
查看>>