codeforces 877 E. Danil and a Part-time Job(线段树(dfs序))
题目链接:http://codeforces.com/contest/877/problem/E
题解:显然一看就感觉要么树链剖分要么线段树+dfs序,题目要求的操作显然用线段树+dfs序就可以实现。然后就敲一下线段树+dfs序就行挺简单的只要dfs一遍记录当前节点的下表然后再加一个leng数组来存子树最多到达几然后更新或者求值的时候只要查询(pos[x],leng[x])即可。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#define lson (i << 1)
#define rson ((i << 1) | 1)
using namespace std;
const int M = 2e5 + ;
int a[M] , pos[M] , leng[M] , pre[M] , cnt;
vector<int> vc[M];
struct TnT {
int l , r , sum , lazy;
}T[M << ];
void push_up(int i) {
T[i].sum = T[lson].sum + T[rson].sum;
}
void push_down(int i) {
if(T[i].lazy) {
T[lson].sum = T[lson].r - T[lson].l + - T[lson].sum;
T[rson].sum = T[rson].r - T[rson].l + - T[rson].sum;
T[lson].lazy ^= ;
T[rson].lazy ^= ;
T[i].lazy = ;
}
}
void build(int l , int r , int i) {
int mid = (l + r) >> ;
T[i].l = l , T[i].r = r , T[i].sum = , T[i].lazy = ;
if(l == r) {
T[i].sum = a[pre[l]];
return ;
}
push_down(i);
build(l , mid , lson);
build(mid + , r , rson);
push_up(i);
}
void update(int l , int r , int i) {
int mid = (T[i].l + T[i].r) >> ;
if(T[i].l == l && T[i].r == r) {
T[i].sum = (T[i].r - T[i].l + ) - T[i].sum;
T[i].lazy ^= ;
return ;
}
push_down(i);
if(mid < l) {
update(l , r , rson);
}
else if(mid >= r) {
update(l , r , lson);
}
else {
update(l , mid , lson) , update(mid + , r , rson);
}
push_up(i);
}
int query(int l , int r , int i) {
int mid = (T[i].l + T[i].r) >> ;
if(T[i].l == l && T[i].r == r) {
return T[i].sum;
}
push_down(i);
if(mid < l) {
return query(l , r , rson);
}
else if(mid >= r) {
return query(l , r , lson);
}
else {
return query(l , mid , lson) + query(mid + , r , rson);
}
}
void dfs(int u) {
int len = vc[u].size();
for(int i = ; i < len ; i++) {
int v = vc[u][i];
pos[v] = ++cnt;
pre[cnt] = v;
dfs(v);
leng[v] = cnt;
}
}
int main() {
int n , x , q;
char s[];
scanf("%d" , &n);
for(int i = ; i <= n ; i++) {
scanf("%d" , &x);
vc[x].push_back(i);
}
for(int i = ; i <= n ; i++) {
scanf("%d" , &a[i]);
}
scanf("%d" , &q);
cnt = ;
pos[] = ++cnt;
pre[cnt] = ;
leng[] = n;
dfs();
build( , n , );
while(q--) {
scanf("%s" , s);
if(s[] == 'g') {
scanf("%d" , &x);
printf("%d\n" , query(pos[x] , leng[x] , ));
}
else {
scanf("%d" , &x);
update(pos[x] , leng[x] , );
}
}
return ;
}
codeforces 877 E. Danil and a Part-time Job(线段树(dfs序))的更多相关文章
- Codeforces 571D - Campus(并查集+线段树+DFS 序,hot tea)
Codeforces 题目传送门 & 洛谷题目传送门 看到集合的合并,可以本能地想到并查集. 不过这题的操作与传统意义上的并查集不太一样,传统意义上的并查集一般是用来判断连通性的,而此题还需支 ...
- Codeforces 877E - Danil and a Part-time Job 线段树+dfs序
给一个有根树,1e5个节点,每个节点有权值0/.1,1e5操作:1.将一个点的子树上所有点权值取反2.查询一个点的子树的权值和 题解: 先深搜整颗树,用dfs序建立每个点对应的区间,等于把树拍扁成 ...
- Codeforces 343D WaterTree - 线段树, DFS序
Description Translated by @Nishikino_Maki from Luogu 行吧是我翻的 Mad scientist Mike has constructed a roo ...
- Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树)
Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树) 题目链接 题意 给定一个nm的矩阵,每行取2k的矩阵,求总 ...
- Codeforces Round #292 (Div. 1) C. Drazil and Park 线段树
C. Drazil and Park 题目连接: http://codeforces.com/contest/516/problem/C Description Drazil is a monkey. ...
- Codeforces Round #254 (Div. 1) C. DZY Loves Colors 线段树
题目链接: http://codeforces.com/problemset/problem/444/C J. DZY Loves Colors time limit per test:2 secon ...
- Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树扫描线
D. Vika and Segments 题目连接: http://www.codeforces.com/contest/610/problem/D Description Vika has an i ...
- Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)
题目链接:http://codeforces.com/contest/610/problem/D 就是给你宽度为1的n个线段,然你求总共有多少单位的长度. 相当于用线段树求面积并,只不过宽为1,注意y ...
- Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)
题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...
随机推荐
- 【Sublime】设置显示编码格式
Mac 上的 Sublime 显示编码格式,设置方法: 右下角显示的 UTF-8 就是当前的编码格式. 添加如下代码: { "font_size": 18, // Display ...
- 从windows平台转战ubuntu
说到ubuntu,可能很多人会有些陌生,但对于有些人很熟悉.ubuntu是linux里面最为流行的一版,以下来自百度百科. Ubuntu(乌班图)是基于Debian GNU/Linux,支 ...
- Django settings.py 配置文件详解
settings.py 配置文件 import os BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) #引 ...
- Fork 多进程 模拟并行访问web service获取响应时间差
#include <ros/ros.h> #include <iostream> #include <string> #include <cstring> ...
- win10和浏览器快捷键
1. Win10快捷键[Win+↑/↓/←/→] 将当前窗口按比例固定到屏幕的四个边角,如左上.右上.左下.右下.[Win+1/2/3…] 按顺序打开任务栏上的已固定程序(不包括第一个“任务视图”按钮 ...
- HTML/CSS:div居中和div内部元素垂直居中(1)
div居中 div水平和垂直居中,text-align和vertical-align不起作用,因为标签div没有这两个属性,所以再css中设置这两个值不能居中的效果 1. div水平居中:设置marg ...
- 【0728 | 预习】第三篇 Python基础
第三篇 Python基础预习 Part 1 变量 一.什么是变量? 二.为什么要有变量? 三.定义变量 四.变量的组成 五.变量名的命名规范 六.变量名的两种风格 Part 2 常量 Part 3 P ...
- 带你剖析WebGis的世界奥秘----瓦片式加载地图
WebGIS应用程序的页面能够通过HTML.JSP.ASP或任何任何类型的Web页文件构成,其特殊之处在于,它的请求提交的方法并不是通过常用的 "超链接"形式,而是使用鼠标与Web ...
- abap简单实现form递归
需求:根据物料号查询下层物料清单 DATA LV_MATNR LIKE ZMARA_TEST-MATNR VALUE '000000000000000001'. DATA: LT_MAT LIKE T ...
- 迁移学习(Transformer),面试看这些就够了!(附代码)
1. 什么是迁移学习 迁移学习(Transformer Learning)是一种机器学习方法,就是把为任务 A 开发的模型作为初始点,重新使用在为任务 B 开发模型的过程中.迁移学习是通过从已学习的相 ...