I. A Simple Tree Problem

Time Limit: 3000ms
Memory Limit: 65536KB

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
 解题:利用dfs记录时间戳,也就是记录区间长度,恰好的包含关系,为建立线段树带来很大的方便,不需要建立N棵线段树,但以某一点为根节点的字孩子区间一定是父节点的区间的子区间。这样好的性质,恰好可以映射到线段树上。。。。。
 
 #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的更多相关文章

  1. ZOJ 3686 A Simple Tree Problem

    A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each no ...

  2. BNU 28887——A Simple Tree Problem——————【将多子树转化成线段树+区间更新】

    A Simple Tree Problem Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on ZJU. O ...

  3. 2014 Super Training #9 F A Simple Tree Problem --DFS+线段树

    原题: ZOJ 3686 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 这题本来是一个比较水的线段树,结果一个ma ...

  4. 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 ...

  5. ZOJ-3686 A Simple Tree Problem 线段树

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 题意:给定一颗有根树,每个节点有0和1两种值.有两种操作: ...

  6. zoj 3686 A Simple Tree Problem (线段树)

    Solution: 根据树的遍历道的时间给树的节点编号,记录下进入节点和退出节点的时间.这个时间区间覆盖了这个节点的所有子树,可以当做连续的区间利用线段树进行操作. /* 线段树 */ #pragma ...

  7. BZOJ 3489: A simple rmq problem

    3489: A simple rmq problem Time Limit: 40 Sec  Memory Limit: 600 MBSubmit: 1594  Solved: 520[Submit] ...

  8. hdu4976 A simple greedy problem. (贪心+DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=4976 2014 Multi-University Training Contest 10 1006 A simp ...

  9. hdu 1757 A Simple Math Problem (乘法矩阵)

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

随机推荐

  1. 【C#】.net 导出Excel功能

    将DataSet对象导出成Excel文档 一.不带格式控制 void btnExport_Click(object sender, EventArgs e) { IList<string> ...

  2. spring cloud各种超时时间设置

    如果是zuul(网关)的超时时间需要设置zuul.hystrix.ribbon等三部分: #zuul超时设置#默认1000zuul.host.socket-timeout-millis=2000#默认 ...

  3. OAuth2.0基本原理及应用

    OAuth2.0基本原理及应用 一.OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛应用,目前的版本是2.0版. 在详细讲解OAuth 2.0之前,需要了解几个专 ...

  4. Linux中grep、sed、awk使用介绍

    linux文件操作命令介绍1)grepgrep 用于在文件中查找符合条件的记录grep 参数 过滤条件 文件过滤的条件中可使用正则表达式-c 显示符合的行数-i 忽略大小写-n 显示符合要求的记录,包 ...

  5. Clown without borders 2017/1/9

    原文 Taking laughter to those who need it most "When will you all return again?"the Croatian ...

  6. 【2016新年版】年度精品 XP,32/64位Win7,32/64位Win8,32/64位Win10系统

    本系统是10月5日最新完整版本的Windows10 安装版镜像,win10正式版,更新了重要补丁,提升应用加载速度,微软和百度今天宣布达成合作,百度成为win10 Edge浏览器中国默认主页和搜索引擎 ...

  7. codevs 1008 选数

    时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description 已知 n 个整数 x1,x2,…,xn,以及一个整数 k(k<n).从 n ...

  8. 快速生成导入亿级测试数据到sqlserver

    如果采用insert into 循环一条一条插入速度比较慢 可以先将数据插入临时表,然后在临时表数据量到达批量插入的行数时执行例如:目标表 (col1,col2,col3) --根据目标表结构复制一个 ...

  9. 限制UITextField输入长度

    如果要限制UITextField输入长度最长不超过kMaxLength,那么需要实现做以下操作: 1.实现UITextFieldDelegate协议: 2.实现textField:shouldChan ...

  10. SQLITE-更新查询

    SQLite -更新查询 SQLite UPDATE查询用于修改现有表中的记录.您可以使用WHERE子句与更新查询更新选中的行,否则会被更新的所有行. 语法: UPDATE查询的WHERE子句的基本语 ...