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. cc150 --链表中倒数第k个节点

    题目描述 输入一个链表,输出该链表中倒数第k个结点.   快指针先走K步,然后快慢同时走,快走到末尾时,慢指针就是倒数第个.     public class Solution { public Li ...

  2. FAFU 1136 最长递增子序列

    http://acm.fafu.edu.cn/problem.php?id=1136 根据dp建边,建边的时候记得判断如果原本数的大小就ok了 好久没在自家OJ上刷了 #include <ios ...

  3. 公司里面用的iTextSharp(教程)---关于PDF的属性设置

    下面介绍下怎么添加属性,代码如下: protected void Button1_Click(object sender, EventArgs e) {   Document doc = new Do ...

  4. linux常用命令:shutdown 命令

    shutdown以一种安全的方式关闭系统. 1.命令格式: shutdown [参数] [时间] 2.命令功能:     功能:  系统关机命令,shutdown指令可以关闭所有程序,并依用户的需要, ...

  5. python webdriver 从无到有搭建数据驱动自动化测试框架的步骤和总结

    一步一步搭建数据驱动测试框架的过程和总结 跟吴老学了搭建自动化数据驱动的框架后,我在自己练习的时候,尝试从简单的程序进行一点一点的扩展和优化,到实现这个数据驱动的框架. 先说一下搭建自动化测试框架的目 ...

  6. Linux服务器---apache支持SSL

    Apache支持ssl 1.检测是否安装ssl模块,如果没有就安装 [root@localhost cgi-bin]# rpm -qa | grep mod_ssl           //查看是否安 ...

  7. python之路----TCP与UDP

    TCP import socket #tcp协议 sk = socket.socket() # 买手机 创建一个socket对象 sk.bind(('127.0.0.1',8080)) # 给serv ...

  8. Linux下Oracle常用命令

    1. 备份表 exp database_user/pass tables='(table1,table2)' file=filename.dmp(例如:exp ismrenbao/iflytek ta ...

  9. Python入门之Python引用模块和查找模块路径

    #这篇文章主要介绍了Python引用模块和Python查找模块路径的相关资料,需要的朋友可以参考下 模块间相互独立相互引用是任何一种编程语言的基础能力.对于“模块”这个词在各种编程语言中或许是不同的, ...

  10. Python3.x(windows系统)安装libxml2库

    Python3.x(windows系统)安装libxml2库 cmd安装命令: pip install lxml 执行结果: 再执行命令: pip install virtualenv 执行结果: