877E - Danil and a Part-time Job

思路:dfs序+线段树

dfs序:http://blog.csdn.net/qq_24489717/article/details/50569644

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ls rt<<1,l,m
#define rs rt<<1|1,m+1,r
#define mem(a,b) memset(a,b,sizeof(a)) const int N=2e5+;
int tree[*N];
int lazy[*N];
int a[N];
int in[N];
int out[N];
vector<int>g[N];
void dfs(int o,int u,int &x)
{
in[u]=x;
for(int i=;i<g[u].size();i++)if(g[u][i]!=o)dfs(u,g[u][i],++x);
out[u]=x;
}
void push_up(int rt)
{
tree[rt]=tree[rt<<]+tree[rt<<|];
}
void push_down(int rt,int len)
{
lazy[rt<<]^=lazy[rt];//对lazy数组很巧妙地运用,两次变换相当于没变
lazy[rt<<|]^=lazy[rt];
tree[rt<<]=(len-(len>>))-tree[rt<<];
tree[rt<<|]=(len>>)-tree[rt<<|];
lazy[rt]=;
}
void build(int rt,int l,int r)
{
lazy[rt]=;
if(l==r)
{
tree[rt]=a[l];
return ;
}
int m=(l+r)>>;
build(ls);
build(rs);
push_up(rt);
}
void Update(int L,int R,int rt,int l,int r)
{
if(L<=l&&r<=R)
{
lazy[rt]^=;
tree[rt]=r-l+-tree[rt];
return ;
}
if(lazy[rt])push_down(rt,r-l+);
int m=(l+r)>>;
if(L<=m)Update(L,R,ls);
if(R>m)Update(L,R,rs);
push_up(rt);
}
int query(int L,int R,int rt,int l,int r)
{
if(L<=l&&r<=R)return tree[rt];
if(lazy[rt])push_down(rt,r-l+);
int m=(l+r)>>;
int ans=;
if(L<=m)ans+=query(L,R,ls);
if(R>m)ans+=query(L,R,rs);
return ans;
} int main()
{
ios::sync_with_stdio(false);
cin.tie();
int n,p,q,t;
string s;
cin>>n;
for(int i=;i<=n;i++)
{
cin>>p;
g[p].pb(i);
g[i].pb(p);
}
int x=;
dfs(,,x);
for(int i=;i<=n;i++)cin>>a[in[i]];
build(,,n);
cin>>q;
while(q--)
{
cin>>s>>t;
if(s=="get")cout<<query(in[t],out[t],,,n)<<endl;
else Update(in[t],out[t],,,n);
}
return ;
}

Codeforces 877E - Danil and a Part-time Job(dfs序+线段树)的更多相关文章

  1. Codeforces 877E Danil and a Part-time Job(dfs序 + 线段树)

    题目链接   Danil and a Part-time Job 题意    给出一系列询问或者修改操作 $pow$ $x$表示把以$x$为根的子树的所有结点的状态取反($0$变$1$,$1$变$0$ ...

  2. Codeforces Round #225 (Div. 2) E. Propagating tree dfs序+-线段树

    题目链接:点击传送 E. Propagating tree time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  3. CodeForces 877E Danil and a Part-time Job(dfs序+线段树)

    Danil decided to earn some money, so he had found a part-time job. The interview have went well, so ...

  4. CodeForces 877E DFS序+线段树

    CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...

  5. Codeforces Round #442 (Div. 2)A,B,C,D,E(STL,dp,贪心,bfs,dfs序+线段树)

    A. Alex and broken contest time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  6. Codeforces 838B - Diverging Directions - [DFS序+线段树]

    题目链接:http://codeforces.com/problemset/problem/838/B You are given a directed weighted graph with n n ...

  7. Educational Codeforces Round 6 E dfs序+线段树

    题意:给出一颗有根树的构造和一开始每个点的颜色 有两种操作 1 : 给定点的子树群体涂色 2 : 求给定点的子树中有多少种颜色 比较容易想到dfs序+线段树去做 dfs序是很久以前看的bilibili ...

  8. Codeforces 343D Water Tree(DFS序 + 线段树)

    题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...

  9. Codeforces 620E New Year Tree(DFS序 + 线段树)

    题目大概说给一棵树,树上结点都有颜色(1到60),进行下面两个操作:把某结点为根的子树染成某一颜色.询问某结点为根的子树有多少种颜色. 子树,显然DFS序,把子树结点映射到连续的区间.而注意到颜色60 ...

随机推荐

  1. css+div table

    div+css table表格样式 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...

  2. yii2之增加省市字段

    第一步,利用数据库迁移文件改表 修改一下迁移文件: https://bitbucket.org/ysxy/zijiu.git

  3. 20154312 曾林 EXP7 网络欺诈防范

    目录 1.基础问题回答 ----1.1.通常在什么场景下容易受到DNS spoof攻击 ----1.2.在日常生活工作中如何防范以上两攻击方法 2.实践总结与体会 3.实践过程记录 ----3.1.简 ...

  4. Swift Optional

    拆包和解包的原因: 其实所谓的 nil 就是 Optional.None, 非 nil 就是Optional.Some, 然后会通过Some(T)包装(wrap)原始值,这也是为什么在使用 Optio ...

  5. jstack生成的Thread Dump日志结构解析

    1 第一部分:Full thread dump identifier 2 第二部分:Java EE middleware, third party & custom application T ...

  6. linux常用命令:iconv 命令

    iconv命令是linux下用于文件转编码的常用命令,对于同时使用windows系统和linux系统的同学来说文件转编码也是经常遇到的操作. 1.命令格式: iconv [选项...] [文件...] ...

  7. python webdriver 测试框架-行为驱动例子

    安装行为驱动模块lettuce(卷心菜)模块 pip install lettuce Successfully installed argparse-1.4.0 colorama-0.3.9 extr ...

  8. CSS前叙

    1 css是什么?层叠样式表,修饰网页结构2 如何去使用css?a.在html网页中,加入一个style标签,在这个style标签里面写css代码b.可以直接把style里面的代码放到一个单独的文件中 ...

  9. redis 哨兵机制环境搭建

    Redis哨兵机制,一主二从 注:Redis哨兵切换,建议一主多从 一.一主二从 教程步骤:https://www.cnblogs.com/zwcry/p/9046207.html 二.哨兵配置(se ...

  10. SNMP学习笔记之SNMP介绍,OID及MIB库

    1.1.    SNMP概览 SNMP的基本知识介绍简单网络管理协议(SNMP-Simple Network Management Protocol)是一个与网络设备交互的简单方法.该规范是由IETF ...