对线段树的区间更新有了初步的了解。。。

A Simple Tree Problem


Time Limit: 3 Seconds      Memory Limit: 65536 KB

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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <map>
#include <queue>
#include <sstream>
#include <iostream>
using namespace std;
#define INF 0x3fffffff
#define N 100100 int n,m;
struct node
{
int to,next;
}edge[*N]; struct node1
{
int b,d,mid;
int flag,sum;
}tnode[*N]; int cnt,pre[N];
int savex[N],savey[N];
int id; // 线段树的区间更新初步了解! void add_edge(int u,int v)
{
edge[cnt].to=v;
edge[cnt].next=pre[u];
pre[u]=cnt++;
} void dfs(int s,int path)
{
int tmp;
++id;
tmp=id;
for(int p=pre[s];p!=-;p=edge[p].next)
{
int v=edge[p].to;
if(v!=path)
{
dfs(v,s);
}
}
savex[s]=tmp;
savey[s]=id;
} void built(int b,int d,int s)
{
tnode[s].b=b;
tnode[s].d=d;
tnode[s].mid=(b+d)/;
tnode[s].sum=;
tnode[s].flag=;
if(b==d) return ;
built(b,tnode[s].mid,*s);
built(tnode[s].mid+,d,*s+);
} void insert(int b,int d,int s)
{
if(tnode[s].b==b&&tnode[s].d==d)
{
tnode[s].flag^=;
tnode[s].sum=(d-b+) - tnode[s].sum;
return ;
}
if(tnode[s].flag!=)
{
tnode[s].flag=;
tnode[*s].flag^=;
tnode[*s].sum=(tnode[*s].d-tnode[*s].b+)-tnode[*s].sum; tnode[*s+].flag^=;
tnode[*s+].sum=(tnode[*s+].d-tnode[*s+].b+)-tnode[*s+].sum;
}
if(tnode[s].mid>=d)
insert(b,d,*s);
else if(tnode[s].mid+<=b)
insert(b,d,*s+);
else
{
insert(b,tnode[s].mid,*s);
insert(tnode[s].mid+,d,*s+);
}
tnode[s].sum=tnode[*s].sum+tnode[*s+].sum;//用up,和down就更好理解
} //这样就不需要放回了,更简单了... int find(int b,int d,int s)
{
if(tnode[s].b==b&&tnode[s].d==d)
{
return tnode[s].sum;
}
if(tnode[s].flag!=)
{
tnode[s].flag=;
tnode[*s].flag^=;
tnode[*s].sum=(tnode[*s].d-tnode[*s].b+)-tnode[*s].sum; tnode[*s+].flag^=;
tnode[*s+].sum=(tnode[*s+].d-tnode[*s+].b+)-tnode[*s+].sum;
}
if(tnode[s].mid>=d)
return find(b,d,s*);
else if(tnode[s].mid+<=b)
return find(b,d,*s+);
else return find(b,tnode[s].mid,s*)+find(tnode[s].mid+,d,*s+);
} int main()
{
//freopen("//home//chen//Desktop//ACM//in.text","r",stdin);
//freopen("//home//chen//Desktop//ACM//out.text","w",stdout);
while(scanf("%d%d",&n,&m)!=EOF)
{
cnt=;
id=;
memset(pre,-,sizeof(pre));
for(int i=;i<=n;i++)
{
int tmp;
scanf("%d",&tmp);
add_edge(i,tmp);
add_edge(tmp,i);
}
dfs(,);
///////////////
built(,n,);
for(int i=;i<m;i++)
{
char sel;
int tmp;
cin>>sel;
scanf("%d",&tmp);
if(sel=='o')
{
insert(savex[tmp],savey[tmp],);
}
else
{
printf("%d\n",find(savex[tmp],savey[tmp],));
}
}
printf("\n");
}
return ;
}

zoj3686(线段树的区间更新)的更多相关文章

  1. hdu 1556:Color the ball(线段树,区间更新,经典题)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  2. hdu 1698:Just a Hook(线段树,区间更新)

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. UVA 12436-Rip Van Winkle's Code(线段树的区间更新)

    题意: long long data[250001]; void A( int st, int nd ) { for( int i = st; i \le nd; i++ ) data[i] = da ...

  4. hdu1698线段树的区间更新区间查询

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  5. HDU 1556 Color the ball(线段树:区间更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=1556 题意: N个气球,每次[a,b]之间的气球涂一次色,统计每个气球涂色的次数. 思路: 这道题目用树状数组和 ...

  6. Color the ball (线段树的区间更新问题)

    N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色.但 ...

  7. ZOJ 2301 Color the Ball 线段树(区间更新+离散化)

    Color the Ball Time Limit: 2 Seconds      Memory Limit: 65536 KB There are infinite balls in a line ...

  8. 线段树离散化+区间更新——cf1179C好题

    绝对是很好的题 把问题转化成当第i个询问的答案是数值x时是否可行 要判断值x是否可行,只要再将问题转化成a数组里>=x的值数量是否严格大于b数组里的>=x的值 那么线段树叶子结点维护对于值 ...

  9. hdu 3397 Sequence operation(线段树:区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3397 题意:给你一个长度为n的0,1序列,支持下列五种操作, 操作0(0 a b):将a到b这个区间的 ...

随机推荐

  1. Python 列表 min() 方法

    描述 Python 列表 min() 方法返回列表元素中的最小值. 语法 min() 方法语法: min(L) 参数 L -- 要返回最小值的列表. 返回值 返回列表元素中的最小值. 实例 以下实例展 ...

  2. c#(winform)中自定义ListItem类方便ComboBox添加Item项

    1.定义ListItem类 public class ListItem { private string _key = string.Empty; private string _value = st ...

  3. Spatial Transformer Network

    https://blog.csdn.net/yaoqi_isee/article/details/72784881 Abstract: 作者说明了CNN对于输入的数据缺乏空间变换不变形(lack of ...

  4. ie,cookie,域名与下划线

    前几天为了开发方便,和一个同事将XX.qq.com的测试站点拆成两个站点,我那个叫XX_hanks.qq.com,进行功能开发,在调试 cookie功能的时候,发现IE下cookie没有记住,用htt ...

  5. 连接SQLServer的增删改查方法代码

    在Visual C++中用ADO进行数据库编程 1. 生成应用程序框架并初始化OLE/COM库环境 创建一个标准的MFC AppWizard(exe)应用程序CADOConnection,然后在使用A ...

  6. How To run OAI eNB (No S1) with USRP X310(1)

    How To run OAI eNB (No S1) with USRP X310 1.Things need to be done 1.1 Install Ubuntu 14.04 1.1.1 In ...

  7. linux命令汇总1

    允许非root用户使用“sudo” root身份登录系统,执行“visudo”,根据示例添加新的一个规则(记住输入的密码是当前用户密码,而不是root密码)#不需要密码执行sudo命令hadoop   ...

  8. Redis 学习笔记四 Mysql 与Redis的同步实践

    一.测试环境在Ubuntu kylin 14.04 64bit 已经安装Mysql.Redis.php.lib_mysqludf_json.so.Gearman. 点击这里查看测试数据库及表参考 本文 ...

  9. oracle 快速批量插入复杂数据的内容

    最近迷上一种批量插入的方法,一句sql解决,将需要插入的数据用with as 的方式查出来,不管多么复杂的sql,都可以用临时表的方式查出来,然后直接插入,这样代码更加清晰 流程也简单 insert ...

  10. hdu1978(记忆化搜索)

    #include<iostream> #include<stdio.h> #include<string.h> #include<queue> usin ...