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. JQuery表单元素过滤选择器

    此选择器主要是对所选择的表单元素进行过滤: 选择器 描述 返回 enabled 选择所有的可用的元素 集合元素 disabled 选择所有的不可用的元素 集合元素 checked 选择所有被选中的元素 ...

  2. 百度知道里关于C++的讨论

    1.把C++当成一门新的语言学习(和C没啥关系!真的.): 2.看<Thinking In C++>,不要看<C++变成死相>: 3.看<The C++ Programm ...

  3. Excel脱拽或者下拉公式时, 保持公式里单元格数字不变

    绝对引用 可以选中B1 用F4快捷键自己就给加绝对引用符号了 然后回车 复制或者拖拽

  4. vim 常用命令小结

    1.打开多个窗口 split   上下打开窗口 vsplit     左右开打窗口 ctrl + ww  窗口之间切换 ctrl + wq  退出当前窗口 2.移动光标:     数字 0 : 将光标 ...

  5. MIPSsim使用说明

    MIPSsim下载:https://files.cnblogs.com/files/jiangxinnju/MIPSsim.zip 启动模拟器 双击MIPSsim.exe,即可启动该模拟器.MIPSs ...

  6. Ubuntu系统下Jenkins的本地构建基本方法

    上一篇文章介绍了,jenkins的安装和系统配置之后,配置登录成功后,就可以新建jenkins构建项目,用于自动化构建. 1.项目名称和项目描述 点击左上角的 新建任务,输入项目名称,选择 构建一个自 ...

  7. Django框架----Web框架本质

    Web框架本质 我们可以这样理解:所有的Web应用本质上就是一个socket服务端,而用户的浏览器就是一个socket客户端. 这样我们就可以自己实现Web框架了. 半成品自定义web框架 impor ...

  8. http协议/获得请求/中文参数处理/访问数据库

    # 1. http协议(了解)## (1)什么是http协议?一种网络应用层协议,规定了浏览器与web服务器之间如何通信以及相应的的数据包的结构.注:tcp/ip协议:保证数据可靠的传递.(UDP不可 ...

  9. ”MySQL查询优化“学习总结

    查询优化有几种方法,下面分别介绍. 切分查询 一条大的语句(涉及很多行)一次会锁住很多数据(不利于高并发). 占满整个事务日志,耗尽系统资源.阻塞很多小的但很重要的查询. 分解关联查询 关联查询分解方 ...

  10. Ubuntu Linux系统环境变量配置文件

    Ubuntu Linux系统环境变量配置文件: /etc/profile : 在登录时,操作系统定制用户环境时使用的第一个文件 ,此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行. ...