xtu数据结构 I. A Simple Tree Problem
I. A Simple Tree Problem
64-bit integer IO format: %lld Java class name: Main
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 2
1 1
o 2
q 1
Sample Output
1
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#define LL long long
#define INF 0x3f3f3f
using namespace std;
const int maxn = ;
int n,m,id;
vector<int>g[maxn];
int len[maxn<<],cnt[maxn<<],mark[maxn<<];
struct node{
int lt,rt;
}tree[maxn<<];
void dfs(int u){
tree[u].lt = ++id;
for(int i = ; i < g[u].size(); i++){
dfs(g[u][i]);
}
tree[u].rt = id;
}
void build(int lt,int rt,int v){
cnt[v] = mark[v] = ;
len[v] = rt-lt+;
if(lt == rt) return;
int mid = (lt+rt)>>;
build(lt,mid,v<<);
build(mid+,rt,v<<|);
}
void push_down(int v){
if(mark[v]){
cnt[v<<] = len[v<<]-cnt[v<<];
mark[v<<] ^= ;
cnt[v<<|] = len[v<<|]-cnt[v<<|];
mark[v<<|] ^= ;
mark[v] = ;
}
}
void update(int x,int y,int lt,int rt,int v){
if(lt >= x && rt <= y){
cnt[v] = len[v] - cnt[v];
if(lt == rt) return;
mark[v] ^= ;
return;
}
push_down(v);
int mid = (lt+rt)>>;
if(x <= mid) update(x,y,lt,mid,v<<);
if(y > mid) update(x,y,mid+,rt,v<<|);
cnt[v] = cnt[v<<]+cnt[v<<|];
}
int query(int x,int y,int lt,int rt,int v){
int ans = ;
if(x <= lt && rt <= y){
return cnt[v];
}
push_down(v);
int mid = (lt+rt)>>;
if(x <= mid) ans += query(x,y,lt,mid,v<<);
if(y > mid) ans += query(x,y,mid+,rt,v<<|);
return ans;
}
int main(){
int i,temp;
char s;
while(~scanf("%d%d",&n,&m)){
for(i = ; i <= n; i++)
g[i].clear();
for(i = ; i <= n; i++){
scanf("%d",&temp);
g[temp].push_back(i);
}
id = ;
dfs();
build(,n,);
while(m--){
cin>>s>>temp;
if(s == 'o'){
update(tree[temp].lt,tree[temp].rt,,n,);
}else cout<<query(tree[temp].lt,tree[temp].rt,,n,)<<endl;
}
cout<<endl;
}
return ;
}
xtu数据结构 I. A Simple Tree Problem的更多相关文章
- ZOJ 3686 A Simple Tree Problem
A Simple Tree Problem Time Limit: 3 Seconds Memory Limit: 65536 KB Given a rooted tree, each no ...
- BNU 28887——A Simple Tree Problem——————【将多子树转化成线段树+区间更新】
A Simple Tree Problem Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on ZJU. O ...
- 2014 Super Training #9 F A Simple Tree Problem --DFS+线段树
原题: ZOJ 3686 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 这题本来是一个比较水的线段树,结果一个ma ...
- ZOJ 3686 A Simple Tree Problem(线段树)
Description Given a rooted tree, each node has a boolean (0 or 1) labeled on it. Initially, all the ...
- ZOJ-3686 A Simple Tree Problem 线段树
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 题意:给定一颗有根树,每个节点有0和1两种值.有两种操作: ...
- zoj 3686 A Simple Tree Problem (线段树)
Solution: 根据树的遍历道的时间给树的节点编号,记录下进入节点和退出节点的时间.这个时间区间覆盖了这个节点的所有子树,可以当做连续的区间利用线段树进行操作. /* 线段树 */ #pragma ...
- BZOJ 3489: A simple rmq problem
3489: A simple rmq problem Time Limit: 40 Sec Memory Limit: 600 MBSubmit: 1594 Solved: 520[Submit] ...
- hdu4976 A simple greedy problem. (贪心+DP)
http://acm.hdu.edu.cn/showproblem.php?pid=4976 2014 Multi-University Training Contest 10 1006 A simp ...
- hdu 1757 A Simple Math Problem (乘法矩阵)
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
随机推荐
- IBatis.net特性展示代码
最近公司计划设计新业务平台架构.数据访问层框架要使用ibatis.net.头让我做些例子给其他同事演示下 ibatis的基本特性.然后评估下看是否使用.本来以后上官方下载NPetshop演示下就行了那 ...
- ios,弹窗遮罩滚动穿透解决方案
- 【Android】SlidingMenu属性详解
SlidingMenu 常用属性介绍: menu.setMode(SlidingMenu.LEFT);//设置左滑菜单menu.setTouchModeAbove(SlidingMenu.TOUCHM ...
- 解决spring http输入流和输出流只能读取一次
1.需求:给某些请求接口记录日志,记录请求的数据和响应的数据和请求所花费的时间.这里采用非侵入式编程,也业务代码进行解耦.按照spring AOP 的编程思想. 2.编程设计:在spring 拦截器中 ...
- vijos 1164 曹冲养猪
描述 自从曹冲搞定了大象以后,曹操就开始捉摸让儿子干些事业,于是派他到中原养猪场养猪,可是曹冲满不高兴,于是在工作中马马虎虎,有一次曹操想知道母猪的数量,于是曹冲想狠狠耍曹操一把.举个例子,假如有16 ...
- 使用JavaScript ES6的新特性计算Fibonacci(非波拉契数列)
程序员面试系列 Java面试系列-webapp文件夹和WebContent文件夹的区别? 程序员面试系列:Spring MVC能响应HTTP请求的原因? Java程序员面试系列-什么是Java Mar ...
- codeforces Gym 100338F Spam Filter 垃圾邮件过滤器(模拟,实现)
阅读题, 概要:给出垃圾邮件和非垃圾邮件的集合,然后按照题目给出的贝叶斯公式计算概率一封邮件是垃圾邮件的概率. 逐个单词判断,将公式化简一下就是在垃圾邮件中出现的次数和在总次数的比值,大于二分之一就算 ...
- tomcat BIO 、NIO 、AIO
11.11活动当天,服务器负载过大,导致部分页面出现了不可访问的状态.那后来主管就要求调优了,下面是tomcat bio.nio.apr模式以及后来自己测试的一些性能结果. 原理方面的资料都是从网上找 ...
- Sass基本数据类型和各类型的原生方法
数据类型: 数字:1,2,3,11,10px (可以带单位) 字符串:"asd",'asd',asd (有引号和无引号都是字符串类型) 如 $name : zhang san ; ...
- CSS在线压缩
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...