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 ...
随机推荐
- Linux屏幕录制gif的工具及教程
准备 要两个工具配合使用.它们可以用命令行安装,也可以用软件管理器安装. 1,byzanz 安装后有两个命令 byzanz-record 录制,既能录制 Gif 动画,又可录制 Ogv 视 ...
- SQL[连载3]sql的一些高级用法
SQL[连载3]sql的一些高级用法 SQL 高级教程 SQL SELECT TOP SQL SELECT TOP 子句 SELECT TOP 子句用于规定要返回的记录的数目. SELECT TOP ...
- HDFS的java操作方式
1.RPC 1.1 RPC (remote procedure call)远程过程调用. 远程过程指的是不是同一个进程. 1.2 RPC至少有两个过程.调用方(client),被调用方(server) ...
- Android开源库--Asynchronous Http Client异步http客户端
如果说我比别人看得更远些,那是因为我站在了巨人的肩上. github地址:https://github.com/loopj/android-async-http Api文档地址:http://loop ...
- WebForm页面运行机制
阅读目录 开始 WebForm前台与后台的关系及运行原理 前台页面 <% @ Page Language="C#" AutoEventWireup="true&qu ...
- Asp.Net验证码2
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System. ...
- 菜鸟学习笔记4——jquery事件
方法 描述 bind() 向匹配元素附加一个或更多事件处理器 blur() 触发.或将函数绑定到指定元素的 blur 事件 change() 触发.或将函数绑定到指定元素的 change 事件 cli ...
- RAC 环境下参数文件(spfile)管理
RAC环境下,初始化参数文件与但实例下参数文件有些异同,主要表现在初始化参数可以为多个实例公用,也可以单独设置各个实例的初始化参数.对于那些非共用的初始化参数则必须要单独设置,而共用的则可以单独设置, ...
- Senparc.Weixin.MP SDK 微信公众平台开发教程(二):成为开发者
Senparc.Weixin.MP SDK 微信公众平台开发教程(二):成为开发者 这一篇主要讲作为一名使用公众平台接口的开发者,你需要知道的一些东西.其中也涉及到一些微信官方的规定或比较掩蔽的注意点 ...
- C++编程常见错误
1.成员变量要记得在构造函数中初始化 2.还是初始化!初始化!初始化!