A Simple Tree Problem

Time Limit: 3000ms
Memory Limit: 65536KB

This problem will be judged on ZJU. Original ID: 3686
64-bit integer IO format: %lld      Java class name: Main

Type:

None

 

None

Graph Theory

2-SAT

Articulation/Bridge/Biconnected Component

Cycles/Topological Sorting/Strongly Connected Component

Shortest Path

Bellman Ford

Dijkstra/Floyd Warshall

Euler Trail/Circuit

Heavy-Light Decomposition

Minimum Spanning Tree

Stable Marriage Problem

Trees

Directed Minimum Spanning Tree

Flow/Matching

Graph Matching

Bipartite Matching

Hopcroft–Karp Bipartite Matching

Weighted Bipartite Matching/Hungarian Algorithm

Flow

Max Flow/Min Cut

Min Cost Max Flow

DFS-like

Backtracking with Pruning/Branch and Bound

Basic Recursion

IDA* Search

Parsing/Grammar

Breadth First Search/Depth First Search

Advanced Search Techniques

Binary Search/Bisection

Ternary Search

Geometry

Basic Geometry

Computational Geometry

Convex Hull

Pick's Theorem

Game Theory

Green Hackenbush/Colon Principle/Fusion Principle

Nim

Sprague-Grundy Number

Matrix

Gaussian Elimination

Matrix Exponentiation

Data Structures

Basic Data Structures

Binary Indexed Tree

Binary Search Tree

Hashing

Orthogonal Range Search

Range Minimum Query/Lowest Common Ancestor

Segment Tree/Interval Tree

Trie Tree

Sorting

Disjoint Set

String

Aho Corasick

Knuth-Morris-Pratt

Suffix Array/Suffix Tree

Math

Basic Math

Big Integer Arithmetic

Number Theory

Chinese Remainder Theorem

Extended Euclid

Inclusion/Exclusion

Modular Arithmetic

Combinatorics

Group Theory/Burnside's lemma

Counting

Probability/Expected Value

Others

Tricky

Hardest

Unusual

Brute Force

Implementation

Constructive Algorithms

Two Pointer

Bitmask

Beginner

Discrete Logarithm/Shank's Baby-step Giant-step Algorithm

Greedy

Divide and Conquer

Dynamic Programming

Tag it!

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

题目大意:有棵根节点为1的树,共有n个节点。有m次询问。第二行为从2--->n各个节点对应的父亲节点编号。下面的m行是询问,o  ai表示将节点为ai的子树所有节点的值进行异或即0变1,1变0。q ai表示询问目前该子树的节点的和值为多少。

解题思路:其实这个题目重点在如何将多子树转化成线段树进行操作。我们如果重新将树编号,那么可以让每个节点对应一段区间。从根节点1开始深搜,编号为1,每当搜到一个节点,就让编号的值加1,让这个编号等于该节点的区间左端点,等把该节点的所有子节点访问完后,将这时的编号赋值给该节点的区间右端点。这时这个区间内的所有节点都是该节点的子节点。  后边就是区间更新的问题了。

#include<bits/stdc++.h>
using namespace std;
#define mid (L+R)/2
#define lson rt*2,L,mid
#define rson rt*2+1,mid+1,R
const int maxn=1e5+50;
vector<int>G[maxn];
int Lt[maxn],Rt[maxn];
int sumv[maxn*4],lazy[maxn*4];
int n,cn;
void dfs(int u){
Lt[u]=++cn;
int v;
for(int i=0;i<G[u].size();i++){
v=G[u][i];
dfs(v);
}
Rt[u]=cn;
}
void build(int rt,int L,int R){
sumv[rt]=lazy[rt]=0;
if(L==R)
return ;
build(lson);
build(rson);
}
void PushDown(int rt,int L,int R){
if(lazy[rt]){
lazy[rt*2]^=1;
lazy[rt*2+1]^=1;
sumv[rt*2]=(mid-L+1)-sumv[rt*2];
sumv[rt*2+1]=(R-mid)-sumv[rt*2+1];
lazy[rt]=0;
}
}
void PushUp(int rt){
sumv[rt]=sumv[rt*2]+sumv[rt*2+1];
}
void update(int rt,int L,int R,int l_ran,int r_ran){
if(l_ran<=L&&R<=r_ran){
lazy[rt]^=1;
sumv[rt]=R-L+1-sumv[rt];
return ;
}
PushDown(rt,L,R);
if(l_ran<=mid){
update(lson,l_ran,r_ran);
}
if(r_ran>mid){
update(rson,l_ran,r_ran);
}
PushUp(rt);
}
int query(int rt,int L,int R,int l_ran,int r_ran){
if(l_ran<=L&&R<=r_ran){
return sumv[rt];
}
int ret=0;
PushDown(rt,L,R); //lazy下放
if(l_ran<=mid){
ret+=query(lson,l_ran,r_ran);
}
if(r_ran>mid){
ret+=query(rson,l_ran,r_ran);
}
return ret;
}
int main(){
char s[4];
int m,subt,a,res;
while(scanf("%d%d",&n,&m)!=EOF){
build(1,1,n);
for(int i=0;i<=n;i++)
G[i].clear();
for(int i=2;i<=n;i++){
scanf("%d",&a);
G[a].push_back(i);
}
cn=0;
dfs(1);
for(int i=0;i<m;i++){
scanf("%s%d",s,&subt);
if(s[0]=='o'){
update(1,1,n,Lt[subt],Rt[subt]);
}else{
res=query(1,1,n,Lt[subt],Rt[subt]);
printf("%d\n",res);
}
}printf("\n"); }
return 0;
} /*
6 5
1 2 1 4 4
o 4
q 4
q 5
q 6
q 1
*/

  

BNU 28887——A Simple Tree Problem——————【将多子树转化成线段树+区间更新】的更多相关文章

  1. POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)

    POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...

  2. poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 75541   ...

  3. POJ 3468:A Simple Problem with Integers(线段树区间更新模板)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 141093 ...

  4. poj3468 A Simple Problem with Integers(线段树区间更新)

    https://vjudge.net/problem/POJ-3468 线段树区间更新(lazy数组)模板题 #include<iostream> #include<cstdio&g ...

  5. POJ 3468A Simple Problem with Integers(线段树区间更新)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 112228 ...

  6. POJ-3468-A Simple Problem with Integers(线段树 区间更新 区间和)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 139191 ...

  7. POJ 3468 A Simple Problem with Integers(线段树区间更新区间查询)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 92632   ...

  8. [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  9. 暑期训练狂刷系列——poj 3468 A Simple Problem with Integers (线段树+区间更新)

    题目连接: http://poj.org/problem?id=3468 题目大意: 给出n个数,有两种操作: 1:"C a b c",[a,b]中的每一个数都加上c. 2:&qu ...

随机推荐

  1. Android Studio无法找到tool.jar解决方法!

    今天安装并配置了JDK,可以在DOS窗口中使用“java -version”命令查看JAVA版本信息了,随后安装Android Studio,但是等Android Studio安装完毕,启动时候发现, ...

  2. 题解 P1434 【滑雪】

    题目链接 此题运用功能强大的 ~~暴力搜索~~ 记忆化搜索才是重点!!! 然而,这是一道经典的DP问题 如果我们用$dis[i][j]$来表示坐标为$(i,j)$时的高度 $cnt[i][j]$ 是我 ...

  3. POJ-1321-棋盘问题(深搜)

    棋盘问题 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 65409   Accepted: 31227 Descriptio ...

  4. C#中实现https的双向认证

    1.  把浏览器中的证书导出为cer文件. 2.   代码如下: using System; using System.Net; using System.IO; using System.Secur ...

  5. 洛谷 P2234 [HNOI2002]营业额统计

    题目描述 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额.分析营业情况是 ...

  6. JDBC记录

    13:55 2018/7/22 用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问 ---------常用API--------- |- Driver接口: 表示java驱动程序接 ...

  7. C++_IO与文件1-输入与输出概述

    为了方便起步先从istream类对象cin和ostream类对象cout开始,了解输入和输出的基本方法: 同时使用ifstream和ofstream对象进行文件的输入和输出: 然后详细学习cin和co ...

  8. C++_函数4-函数重载与函数模板

    函数重载 多个函数,函数名相同,但是调用的时候,参数不同. 函数多态是C++在C语言的基础上新增的功能.多态指的是有多种形式,因此函数多态允许函数可以有多种形式.术语“函数重载”指的是可以有多个同名的 ...

  9. abp + angular 项目 图标字体注意事项

    用的字体建议下载到本地,否则部署环境没有网络的话,则图片字体会不正常显示.

  10. 石头剪刀布(2019Wannafly winter camp day3 i) 带权并查集+按秩合并 好题

    题目传送门 思路: 按照题意描述,所有y挑战x的关系最后会形成一棵树的结构,n个人的总方案数是 3n 种,假设一个人被挑战(主场作战)a次,挑战别人(客场)b次,那么这个人存活到最后的方案数就是3n* ...