题目链接: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 线段树的更多相关文章

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

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

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

  4. bzoj 3489 A simple rmq problem - 线段树

    Description 因为是OJ上的题,就简单点好了.给出一个长度为n的序列,给出M个询问:在[l,r]之间找到一个在这个区间里只出现过一次的数,并且要求找的这个数尽可能大.如果找不到这样的数,则直 ...

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

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

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

  7. 【BZOJ4999】This Problem Is Too Simple!(线段树)

    [BZOJ4999]This Problem Is Too Simple!(线段树) 题面 BZOJ 题解 对于每个值,维护一棵线段树就好啦 动态开点,否则空间开不下 剩下的就是很简单的问题啦 当然了 ...

  8. xtu数据结构 I. A Simple Tree Problem

    I. A Simple Tree Problem Time Limit: 3000ms Memory Limit: 65536KB 64-bit integer IO format: %lld     ...

  9. hdu 5274 Dylans loves tree(LCA + 线段树)

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

随机推荐

  1. ASP.NET MVC3 实例(六) 增加、修改和删除操作(二)

    http://www.jquery001.com/asp.net-mvc3-instance-add-update-delete2.html 上篇我们在 ASP.NET MVC3 中实现了添加操作,由 ...

  2. Linux Kernel CMPXCHG函数分析

    原文地址:http://blog.csdn.net/penngrove/article/details/44175387 最近看到Linux Kernel cmpxchg的代码,对实现很不理解.上网查 ...

  3. HibernateTemplate 查询

    Spring中常用的hql查询方法getHibernateTemplate()上     一.find(String queryString);   示例:this.getHibernateTempl ...

  4. 简单了解JAVA8的新特性

    JAVA8新特性会颠覆整个JAVA程序员的编程习惯 甚至如果您坚守JAVA7之前的编程习惯,今后你看比较年轻的程序员写的JAVA代码都会无法理解 所以为了保证不脱钩,我觉得有必要学习JAVA8的新特性 ...

  5. sql server 2008r2 清除数据库日志

    USE [master] GO ALTER DATABASE data SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE data  SET REC ...

  6. [转]JAVA中Action层, Service层 ,modle层 和 Dao层的功能区分

    首先这是现在最基本的分层方式,结合了SSH架构.modle层就是对应的数据库表的实体类.Dao层是使用了Hibernate连接数据库.操作数据库(增删改查).Service层:引用对应的Dao数据库操 ...

  7. 单点登录系统构建之一——基础知识(Kerberous/SAML)

    http://web.mit.edu/kerberos/ Kerberos Kerberous是一个网络身份验证协议,它被设计为客户端/服务器提供基于密钥的强加密机制.该协议最初由MIT实现并被广泛商 ...

  8. laravel/laravel和laravel/framework有何区别?

    在安装laravel的时候,我们一般是download github上的laravel/laravel,随后执行composer install,在这个过程中,你会发现composer其中的一项工作是 ...

  9. UINavigationController的使用

    1.UINavigationController使用流程 UINavigationController为导航控制器,在iOS里经常用到. 我们看看它的如何使用: 下面的图显示了导航控制器的流程.最左侧 ...

  10. The Guide To Understanding mysqlreport

    The Guide To Understanding mysqlreport This guide to understanding mysqlreport explains everything t ...