ZOJ-3686 A Simple Tree Problem 线段树
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686
题意:给定一颗有根树,每个节点有0和1两种值。有两种操作:o a操作,把以a为根节点的子树的权值全部取反;q a操作,求以a为根节点的子树权值为1的节点个数。
先求出树的先序遍历结果,并且记录每颗子树的节点个数,然后就可以用线段树维护了。。
//STATUS:C++_AC_240MS_6524KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef long long LL;
typedef unsigned long long ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=1e+,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End int first[N],next[N*],e[N*],ra[N],id[N],sum[N],one[N<<],rev[N<<];
int n,m,mt,cnt,ans; void adde(int a,int b)
{
e[mt]=b;
next[mt]=first[a],first[a]=mt++;
e[mt]=a;
next[mt]=first[b],first[b]=mt++;
} int dfs(int u,int fa)
{
ra[cnt++]=u;
int i,j;
sum[u]=;
for(i=first[u];i!=-;i=next[i]){
if(e[i]==fa)continue;
sum[u]+=dfs(e[i],u);
}
return sum[u];
} void pushdown(int rt,int llen,int rlen)
{
if(rev[rt]){
rev[rt]=;
one[rt<<]=llen-one[rt<<];
one[rt<<|]=rlen-one[rt<<|];
rev[rt<<]^=,rev[rt<<|]^=;
}
} void update(int l,int r,int rt,int L,int R)
{
if(L<=l && r<=R){
rev[rt]^=;
one[rt]=r-l+-one[rt];
return ;
}
int mid=(l+r)>>;
pushdown(rt,mid-l+,r-mid);
if(L<=mid)update(lson,L,R);
if(R>mid)update(rson,L,R);
one[rt]=one[rt<<]+one[rt<<|];
} void query(int l,int r,int rt,int L,int R)
{
if(L<=l && r<=R){
ans+=one[rt];
return ;
}
int mid=(l+r)>>;
pushdown(rt,mid-l+,r-mid);
if(L<=mid)query(lson,L,R);
if(R>mid)query(rson,L,R);
one[rt]=one[rt<<]+one[rt<<|];
} int main()
{
// freopen("in.txt","r",stdin);
int i,j,t;
char op[];
while(~scanf("%d%d",&n,&m))
{
mem(first,-);mt=;
for(i=;i<=n;i++){
scanf("%d",&t);
adde(t,i);
}
cnt=;
dfs(,-);
for(i=;i<=n;i++)id[ra[i]]=i; mem(one,);mem(rev,);
while(m--){
scanf("%s%d",op,&t);
if(op[]=='o'){
update(,n,,id[t],id[t]+sum[t]-);
}
else {
ans=;
query(,n,,id[t],id[t]+sum[t]-);
printf("%d\n",ans);
}
}
putchar('\n');
}
return ;
}
ZOJ-3686 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 ...
- 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 (线段树)
Solution: 根据树的遍历道的时间给树的节点编号,记录下进入节点和退出节点的时间.这个时间区间覆盖了这个节点的所有子树,可以当做连续的区间利用线段树进行操作. /* 线段树 */ #pragma ...
- bzoj 3489 A simple rmq problem - 线段树
Description 因为是OJ上的题,就简单点好了.给出一个长度为n的序列,给出M个询问:在[l,r]之间找到一个在这个区间里只出现过一次的数,并且要求找的这个数尽可能大.如果找不到这样的数,则直 ...
- hdu 4973 A simple simulation problem. (线段树)
题目链接 题意: 给定n长的序列 m个操作 序列默认为 1, 2, 3···n 操作1:D [l,r] 把[l,r]区间增长 :( 1,2,3,4 进行 D [1,3]变成 1,1,2,2,3,3,4 ...
- BNU 28887——A Simple Tree Problem——————【将多子树转化成线段树+区间更新】
A Simple Tree Problem Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on ZJU. O ...
- 【BZOJ4999】This Problem Is Too Simple!(线段树)
[BZOJ4999]This Problem Is Too Simple!(线段树) 题面 BZOJ 题解 对于每个值,维护一棵线段树就好啦 动态开点,否则空间开不下 剩下的就是很简单的问题啦 当然了 ...
- xtu数据结构 I. A Simple Tree Problem
I. A Simple Tree Problem Time Limit: 3000ms Memory Limit: 65536KB 64-bit integer IO format: %lld ...
- hdu 5274 Dylans loves tree(LCA + 线段树)
Dylans loves tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
随机推荐
- poj-2376 Cleaning Shifts (排序+贪心)
http://poj.org/problem?id=2376 john有n头牛做打扫工作,他想在t时间内每个时间都至少有一头牛在做打扫工作,第一头牛在1,最后一头牛在t时间,每一头牛工作都有一个开始时 ...
- 总结Selenium自动化测试方法(一)自动化测试基础
总结Selenium自动化测试方法 一.自动化测试基础 1.什么样的项目适合自动化测试 ①任务测试明确,不会频繁变动 ②每日构建后的测试验证 ③比较频繁的回归测试 ④软件系统界面稳定.变动少 ⑤需要在 ...
- 《OD学storm》20160827
http://www.cnblogs.com/lujinhong2/p/4686512.html http://blog.csdn.net/paul_wei2008/article/details/2 ...
- maven命令大全
1. mvn help:describe 你是否因为记不清某个插件有哪些goal而痛苦过,你是否因为想不起某个goal有哪些参数而苦恼,那就试试这个命令吧,它会告诉你一切的. 参数: 1. -Dplu ...
- Codeforces Beta Round #97 (Div. 1)
B 判矩阵的时候 出了点错 根据点积判垂直 叉积判平行 面积不能为0 #include <iostream> #include<cstdio> #include<cstr ...
- 网络攻击之二:XSS(之一是SQL注入,前面有文章)
学习了 http://www.oschina.net/question/565065_57506 (这里做了转载 http://blog.csdn.net/stilling2006/article/d ...
- android之Itent.ACTION_PICK Intent.ACTION_GET_CONTENT妙用
你是不是很多时候,想从弹出的电话本姓名列表中中查找到某个人,然后再获取该人的详细信息呢? 你是不是想选择从弹出的列表中选择一张图片,然后将其进行进一步的操作呢? 如果,你想,那你是不是很像知道,我们应 ...
- git和其他版本控制系统的区别
所有除了Git以外的版本控制系统都使用增量存储方式来保存不同版本,而Git则在每一个commit时,保存一个整个文件的content copy,除非那个文件没有做过改动.Git和其他版本系统的主要区别 ...
- MVC简捷调用EasyUI的datagrid
一直想在项目中使用EasyUi的datagrid,但种种原因,没有实现. 这两天在开发一个项目中,愿望终于得以实现. 先看效果: 实现步骤是这样的: 1,在页面中画dataGrid,具体代码如下: & ...
- UVa 11134 (区间上的贪心) Fabled Rooks
这道题真是WA得我心力交瘁,好讨厌的感觉啊! 简直木有写题解的心情了 题意: n×n的棋盘里,放置n个车,使得任意两车不同行且不同列,且第i个车必须放在给定的第i个矩形范围内.输出一种方案,即每个车的 ...