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 播放器按钮实现

    效果图 html代码 //播放按钮 <div id="playBtn" class="circle" style="margin: 20px 0 ...

  2. STA分析(一) setup and hold

    timing check可以分为Dynamic Timing Analysis(Post_sim)和Static Timing Analysis STA:可以分析的很全面:仿真速度也很快:可以分析控制 ...

  3. Object-C-NSString

    NSString *info=@"Hello world"; NSString *info=[[NSString alloc]initWithFormat:@"my na ...

  4. Android (Android Studio)无法启动adb 解决方案

    打开cmd 输入:     netstat -aon|findstr "5037"      回车  taskkill /pid xxxx /f     ps:xxxx为占用端口 ...

  5. Python: 去掉字符串开头、结尾或者中间不想要的字符

    ①Strip()方法用于删除开始或结尾的字符.lstrip()|rstirp()分别从左右执行删除操作.默认情况下会删除空白或者换行符,也可以指定其他字符. ②如果想处理中间的空格,需要求助其他技术 ...

  6. Python Web学习笔记之SOCK_STREAM和SOCK_DGRAM

    SOCK_STREAM 数据流 一般是tcp/ip协议的编程 有保障的(即能保证数据正确传送到对方)面向连接的SOCKET,多用于资料(如文件)传送 SOCK_DGRAM 数据包 udp协议网络编程 ...

  7. 根据wsdl文件,soupUI生成webservice客户端代码

    根据wsdl文件,soupUI生成webservice客户端代码 功能介绍: 对于面向WebServie接口开发时,当我们已经获取到WSDL文件后,可以使用soapUI工具生成对应的客户端和服务端代码 ...

  8. Android 深入理解Activity 页面Intent跳转

  9. Linux 环境 HTTP 服务器

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <netinet/in ...

  10. CentOS 7 怎样自动连接网络

    https://jingyan.baidu.com/article/19192ad8f7c320e53e570728.html