原题: ZOJ 3686 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686

这题本来是一个比较水的线段树,结果一个mark坑了我好几个小时。。哎。太弱。

先DFS这棵树,树形结构转换为线性结构,每个节点有一个第一次遍历的时间和最后一次遍历的时间,之间的时间戳都为子树的时间戳,用线段树更新这段区间即可实现更新子树的效果,用到懒操作节省时间。

坑我的地方: update时,不能写成:tree[rt].mark = 1, 而要写成 tree[rt].mark ^= 1; 因为如果持续update偶数次相当于什么都没做,但是每次update确实是更新了的啊,每次pushdown都会将tree[rt].mark变为0的啊,也就是说tree[rt].mark是不能保持的,所以我搞不懂的是为什么要异或1,而不能直接令等于1,有待向大神请教,如果有看到并知道的仁兄可以指教一下我。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
#define N 100007 struct node
{
int l,r;
}p[N]; struct Tree
{
int sum,mark;
}tree[*N];
int Time;
vector<int> G[N]; void pushup(int rt)
{
tree[rt].sum = tree[*rt].sum + tree[*rt+].sum;
} void build(int l,int r,int rt)
{
tree[rt].sum = tree[rt].mark = ;
if(l == r)
return;
int mid = (l+r)/;
build(l,mid,*rt);
build(mid+,r,*rt+);
pushup(rt);
} void pushdown(int l,int r,int rt)
{
if(!tree[rt].mark)
return;
int mid = (l+r)/;
tree[*rt].mark ^= ; //not "tree[2*rt].mark = tree[rt].mark"
tree[*rt+].mark ^= ;
tree[*rt].sum = (mid-l+)-tree[*rt].sum;
tree[*rt+].sum = (r-mid)-tree[*rt+].sum;
tree[rt].mark = ;
} void update(int l,int r,int aa,int bb,int rt)
{
if(aa<=l && bb>=r)
{
tree[rt].mark ^= ; //not "tree[rt].mark = 1", 因为偶数次操作相互抵消
tree[rt].sum = r-l+-tree[rt].sum;
return;
}
pushdown(l,r,rt);
int mid = (l+r)/;
if(aa <= mid)
update(l,mid,aa,bb,*rt);
if(bb > mid)
update(mid+,r,aa,bb,*rt+);
pushup(rt);
} void dfs(int u)
{
p[u].l = Time++;
for(int i=;i<G[u].size();i++)
dfs(G[u][i]);
p[u].r = Time++;
} int query(int l,int r,int aa,int bb,int rt)
{
if(aa>r || bb<l)
return ;
if(aa<=l && bb>=r)
return tree[rt].sum;
pushdown(l,r,rt);
int mid = (l+r)/;
return query(l,mid,aa,bb,*rt)+query(mid+,r,aa,bb,*rt+);
} int main()
{
int n,m,i,j,x;
char ss[];
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=;i<=n;i++)
G[i].clear();
for(i=;i<=n;i++)
{
scanf("%d",&x);
G[x].push_back(i);
}
Time = ;
dfs();
build(,*n,);
while(m--)
{
scanf("%s%d",ss,&x);
if(ss[] == 'o')
update(,*n,p[x].l,p[x].r,);
else
printf("%d\n",query(,*n,p[x].l,p[x].r,)/);
}
puts("");
}
return ;
}

2014 Super Training #9 F A Simple Tree Problem --DFS+线段树的更多相关文章

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

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

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

  3. 2014 Super Training #7 F Power of Fibonacci --数学+逆元+快速幂

    原题:ZOJ 3774  http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3774 --------------------- ...

  4. 2014 Super Training #6 F Search in the Wiki --集合取交+暴力

    原题: ZOJ 3674 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3674 题意不难理解,很容易想到用暴力,但是无从下 ...

  5. 2014 Super Training #2 F The Bridges of Kolsberg --DP

    原题:UVA 1172  http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  6. 2014 Super Training #1 F Passage 概率DP

    原题: HDU 3366   http://acm.hdu.edu.cn/showproblem.php?pid=3366 本来用贪心去做,怎么都WA,后来看网上原来是一个DP题. 首先按P/Q来做排 ...

  7. 2014 Super Training #10 C Shadow --SPFA/随便搞/DFS

    原题: FZU 2169 http://acm.fzu.edu.cn/problem.php?pid=2169 这题貌似有两种解法,DFS和SPFA,但是DFS怎么都RE,SPFA也要用邻接表表示边, ...

  8. ZOJ 3686 A Simple Tree Problem

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

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

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

随机推荐

  1. SQL Server 全局变量

    SQL Server中所有全局变量都使用两个@符号作为前缀 --1.@@error 最后一个T-SQL错误的错误号(目的是或得违反约束的错误号) insert into Subject values( ...

  2. [ASP.NET MVC] 使用Bootsnipp样式

    [ASP.NET MVC] 使用Bootsnipp样式 前言 在「[ASP.NET MVC] 使用Bootstrap套件」这篇文章中,介绍了如何在Web项目里使用Bootstrap套件,让用户界面更加 ...

  3. seajs学习笔记

    seajs配置 seajs.config({ //别名配置 alias:{ 'es5-safe':'gallery/es5-safe/0.9.3/es5-safe', 'jquery':'jquery ...

  4. Hexo建博小结

    本来只写在自己的github pages中的,想一想万一有人看呢,虽然同类的文章有不少了,但有些新坑他们没填啊,姑且放出来啦... 拥有自己的博客是一个很酷的事情,但自己建站总是太麻烦了,步骤繁多,管 ...

  5. ASP.NET MVC 微信公共平台开发之验证消息的真实性

    ASP.NET MVC 微信公共平台开发 验证消息的真实性 在MVC Controller所在项目中添加过滤器,在过滤器中重写 public override void OnActionExecuti ...

  6. SAP_Web_Service开发配置

    第一章    SAP创建WS 1.1       概要技术说明 1.2       创建RFC函数 1.3       创建WS 1.4       外部系统访问配置 第二章    SAP调用WS 2 ...

  7. 记录一个调了半天的问题:java.lang.SecurityException: Permission denied (missing INTERNET permission?)

    Move the <uses-permission> elements outside of <application>. They need to be immediate ...

  8. 解析plist文件(字典里包着数组,数组中又包含字典)

    #import "RootTableViewController.h" #import "City.h" @interface RootTableViewCon ...

  9. Web应用程序系统的多用户权限控制设计及实现-首页模块【5】

    首页模块就是展示不同权限的最终结果了,在阅读这章之前若有些不明白,可看看后续的单独的权限模块,用户模块,目录模块后从整体上再看首页模块. 阅读该模块需要一定或者是比较熟练的js知识,EasyUI Ta ...

  10. 2、IOS开发--iPad之仿制QQ空间 (初始化HomeViewController子控件视图)

    1.先初始化侧边的duck,效果图: 实现步骤: 2.然后初始化BottomMenu,效果: 步骤: 其实到这里,会出现一个小bug,那就是: 子控件的位置移高了,主要原因是: 逻辑分析图: 问题解决 ...