给一个有根树,1e5个节点,每个节点有权值0/.1,
1e5操作:
1.将一个点的子树上所有点权值取反
2.查询一个点的子树的权值和
 
题解:
先深搜整颗树,用dfs序建立每个点对应的区间,
等于把树拍扁成一个数列,每次操作从就对点变成了对区间
然后就是裸线段树
注意拍扁后的节点标号和原来的树节点标号是不等价的,要映射一下
#include <bits/stdc++.h>
#define endl '\n'
#define ll long long
#define fi first
#define se second
#define pii pair<int,int>
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define rep(ii,a,b) for(int ii=a;ii<=b;++ii)
using namespace std;
const int maxn=1e6+7;
int casn,n,m,k;
vector<int>g[maxn];
pii seg[maxn];
int vis[maxn];
int a[maxn];
int dfn;
void dfs(int now){
seg[now].fi=++dfn;
vis[dfn]=now;
for(auto i:g[now]) dfs(i);
seg[now].se=dfn;
}
class segtree{
#define nd node[now]
#define ndl node[now<<1]
#define ndr node[now<<1|1]
public:
struct segnode {
int l,r;int sum,tag;
int mid(){return (r+l)>>1;}
int len(){return r-l+1;}
void update(){sum=len()-sum;tag^=1;}
};
vector<segnode> node;
int cnt;
segtree(int n) {node.resize(n<<2|3);maketree(1,n);}
void pushup(int now){nd.sum=ndl.sum+ndr.sum;}
void pushdown(int now){
if(nd.tag){
ndl.update();
ndr.update();
nd.tag=0;
}
}
void maketree(int s,int t,int now=1){
nd={s,t,0,0};
if(s==t){
nd.sum=a[vis[s]];
return ;
}
maketree(s,nd.mid(),now<<1);
maketree(nd.mid()+1,t,now<<1|1);
pushup(now);
}
void update(int s,int t,int now=1){
if(s>nd.r||t<nd.l) return ;
if(s<=nd.l&&t>=nd.r){nd.update();return ;}
pushdown(now);
update(s,t,now<<1); update(s,t,now<<1|1);
pushup(now);
}
int query(int s,int t,int now=1){
if(s>nd.r||t<nd.l) return 0;
if(s<=nd.l&&t>=nd.r) return nd.sum;
pushdown(now);
return query(s,t,now<<1)+query(s,t,now<<1|1);
}
};
int main() {
IO;
cin>>n;
rep(i,2,n){
int a;cin>>a;
g[a].push_back(i);
}
dfs(1);
rep(i,1,n) cin>>a[i];
segtree tree(n);
cin>>m;
string s;
int x;
while(m--){
cin>>s>>x;
if(s=="get")cout<<tree.query(seg[x].fi,seg[x].se)<<endl;
else tree.update(seg[x].fi,seg[x].se);
}
return 0;
}

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

  1. Codeforces 571D - Campus(并查集+线段树+DFS 序,hot tea)

    Codeforces 题目传送门 & 洛谷题目传送门 看到集合的合并,可以本能地想到并查集. 不过这题的操作与传统意义上的并查集不太一样,传统意义上的并查集一般是用来判断连通性的,而此题还需支 ...

  2. Codeforces 343D WaterTree - 线段树, DFS序

    Description Translated by @Nishikino_Maki from Luogu 行吧是我翻的 Mad scientist Mike has constructed a roo ...

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

    877E - Danil and a Part-time Job 思路:dfs序+线段树 dfs序:http://blog.csdn.net/qq_24489717/article/details/5 ...

  4. codeforces 877 E. Danil and a Part-time Job(线段树(dfs序))

    题目链接:http://codeforces.com/contest/877/problem/E 题解:显然一看就感觉要么树链剖分要么线段树+dfs序,题目要求的操作显然用线段树+dfs序就可以实现. ...

  5. [Codeforces 464E] The Classic Problem(可持久化线段树)

    [Codeforces 464E] The Classic Problem(可持久化线段树) 题面 给出一个带权无向图,每条边的边权是\(2^{x_i}(x_i<10^5)\),求s到t的最短路 ...

  6. 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 ...

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

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

  8. [Codeforces 877E] Danil and a Part-time Job

    [题目链接] https://codeforces.com/contest/877/problem/E [算法] 首先求出这棵树的DFS序 一棵子树的DFS序为连续的一段 , 根据这个性质 , 用线段 ...

  9. Codeforces Round #244 (Div. 2) B. Prison Transfer 线段树rmq

    B. Prison Transfer Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/pro ...

随机推荐

  1. Spring Cloud:多环境配置、eureka 安全认证、容器宿主机IP注册

    记录一下搭建 Spring Cloud 过程中踩过的一些坑,测试的东西断断续续已经弄了好多了,一直没有时间整理搭建过程,时间啊~时间~ Spring 版本 Spring Boot:2.0.6.RELE ...

  2. Golang 入门系列(九) 如何读取YAML,JSON,INI等配置文件

    实际项目中,读取相关的系统配置文件是很常见的事情.今天就来说一说,Golang 是如何读取YAML,JSON,INI等配置文件的. 1. json使用 JSON 应该比较熟悉,它是一种轻量级的数据交换 ...

  3. 静态代理与JDK动态代理

    demo地址: https://github.com/ZbLeaning/leaning 代理: 为其他对象提供一种代理以控制对这个对象的访问.分为静态代理和动态代理.代理模式的目的就是为真实业务对象 ...

  4. Android——图片轮播

    Android技术——轮播功能 轮播需要什么? 答:实现图片与广告语展示.循环播发以及手动切换.支持加载本地与网络图片. 性能优化? 答:多张图片与指示器展示.自动与定时.循环播发.滑动流畅并且无卡顿 ...

  5. Visual Studio Code常用设置及快捷键

    1. Visual Studio Code常用设置 { // 控制是否显示 minimap(缩略图) "editor.minimap.enabled": false, // 控制折 ...

  6. AngularJS 1.x系列:AngularJS控制器(3)

    1. 控制器(Controller)定义 控制器(Controller)在AngularJS中作用是增强视图(View),AngularJS控制器是一个构造方法,用来向视图(View)中添加额外功能. ...

  7. BZOJ4621 Tc605(动态规划)

    容易发现最终序列所有数字的相对顺序不变,一个数字可能的覆盖范围由两边第一个比它大的数决定,且若不考虑次数限制所有这样的序列都可以变换得到.对于一个序列,其需要的最少变换次数显然就是覆盖了别的位置的数的 ...

  8. 小程序运行报错:errMsg: "request:fail url not in domain list"

    错误原因: 报错提示说请求的url不在域名列表里,应该是还没有配置服务器域名 解决方法: 可点击开发者工具右上角 详情-项目设置-不校验合法域名.web-view(业务域名).TLS 版本以及 HTT ...

  9. jvm学习笔记二(减少GC开销的建议)

    一:触发主GC(Garbage Collector)的条件 JVM进行次GC的频率很高,但因为这种GC占用时间极短,所以对系统产生的影响不大.更值得关注的是主GC的触发条件,因为它对系统影响很明显.总 ...

  10. 通过crontab调度java -jar任务提示"nohup: failed to run command `java': No such file or directory"

    通过crontab无法运行,提示如标题的信息: 但直接在终端控制台执行sh和java -jar都可以: 网上给的提示解决方法,在.sh文件开始上面加上 source /etc/profile 然后cr ...