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

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. MySQL-Transfer2.3发布

    Transfer 2.3发布,下载地址 此版本除了升级based版本外 *优化了无索引表的同步性能 *优化了slave模式下超大事务内存消耗问题 *Transfer模式相关的功能改动较多 *修复tra ...

  2. C复杂声明举例

    首先,一些国外的研究成果: 一个用英语解析复杂声明的网站:http://cdecl.org 图表说明复杂声明(英):http://c-faq.com/decl/spiral.anderson.html ...

  3. [svc]influxdb+grafana实战-各省份api访问成功率统计

    简单说下需求: 统计各个省份的 3大运营商的接口访问成功率,绘图展示 数据格式 {"mobile" : "15812345608", "provinc ...

  4. android.animation(2) - ValueAnimator的 Interpolator 和 Evaluator

    一.插值器 插值器,也叫加速器:有关插值器的知识,我在<Animation动画详解(二)——Interpolator插值器>中专门讲过,大家可以先看看这篇文章中各个加速器的效果.这里再讲一 ...

  5. 关于HashMap初始化容量问题

    使用阿里云代码规范插件扫描后出现以下提示: hashmap should set a size when initalizing,即hashmap应该在初始化时设置一个大小 在网上搜到一篇讲解(htt ...

  6. 使用libjpeg 压缩yuv420到jpg (内存方式)

    #include <Windows.h> #include <stdio.h> extern "C" { #include <jpeglib.h> ...

  7. SAP ERP 6.0 EHP7 SR2(WINDOWS MSSQL版)安装说明

    原文 by 枫竹丹青 ⋅ 1.安装准备 1.1.版本说明 本文是描述在一个Windows虚拟机.SQL Server数据库环境下,安装SAP ERP 6.0 EHP7 SR2服务器,安装完成虚拟机文件 ...

  8. XML异步请求实例

    其实还是很格式化的: <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type&qu ...

  9. [Application]Ctrl+C终止程序代码

    代码如下: #include <stdio.h> #include <stdlib.h> #include <iostream> #include <sign ...

  10. selenuim爬虫实战 (下)

    SuperLOFTERDownloader7.java package test; import java.io.IOException; import java.util.ArrayList; im ...