Codeforces Round #442 (Div. 2) Danil and a Part-time Job
http://codeforces.com/contest/877/problem/E
真的菜的不行,自己敲一个模板,到处都是问题。哎
- #include <bits/stdc++.h>
- using namespace std;
- const int maxn = 2e5+;
- #define lson (q<<1)
- #define rson ((q<<1)|1)
- struct node
- {
- int l,r,mid;
- int v,lazy;
- }tree[maxn*];
- int L[maxn],R[maxn],index;
- int vis[maxn] = {};
- int a[maxn];
- vector<int> g[maxn];
- void dfs(int x)
- {
- L[x] = index;
- for(int i=;i<g[x].size();i++)
- {
- int v = g[x][i];
- if(!vis[v])
- {
- vis[v] = ;
- index++;
- dfs(v);
- }
- }
- R[x] = index;
- }
- void push_up(int q)
- {
- tree[q].v = tree[lson].v+tree[rson].v;
- }
- void build(int l,int r,int q)
- {
- tree[q].l = l,tree[q].r = r,tree[q].mid = (l+r)/;
- tree[q].lazy = ;
- if(l==r)
- {
- tree[q].v = a[l];
- //printf("%d %d\n",l,tree[q].v);
- return;
- }
- build(l,tree[q].mid,lson);
- build(tree[q].mid+,r,rson);
- push_up(q);
- }
- void push_down(int q)
- {
- if(tree[q].lazy)
- {
- tree[lson].v = (tree[lson].r-tree[lson].l+)-tree[lson].v;
- tree[rson].v = (tree[rson].r-tree[rson].l+)-tree[rson].v;
- tree[q].lazy ^= ;
- tree[lson].lazy ^= ;
- tree[rson].lazy ^= ;
- }
- }
- void update(int l,int r,int q)
- {
- if(tree[q].l>=l&&tree[q].r<=r)
- {
- tree[q].lazy ^= ;
- tree[q].v = (tree[q].r-tree[q].l+)-tree[q].v;
- return;
- }
- push_down(q);
- if(l<=tree[q].mid) update(l,r,lson);
- if(r>=tree[q].mid+) update(l,r,rson);
- push_up(q); ///v值由下往上更新
- }
- int query(int l,int r,int q) ///由上及下
- {
- if(tree[q].l>=l&&tree[q].r<=r)
- {
- // printf("%d %d %d %d\n",tree[q].l,tree[q].r,tree[q].v,tree[q].lazy);
- return tree[q].v;
- }
- push_down(q);
- int sum = ;
- if(l<=tree[q].mid) sum += query(l,r,lson);
- if(r>=tree[q].mid+) sum += query(l,r,rson);
- return sum;
- }
- int main()
- {
- int n;scanf("%d",&n);
- for(int i=;i<=n;i++)
- {
- int x;scanf("%d",&x);
- g[x].push_back(i);
- g[i].push_back(x);
- }
- index = ;vis[] = ;
- dfs();
- for(int i=;i<=n;i++)
- {
- int cc;
- scanf("%d",&cc);
- a[L[i]] = cc; ///按dfs序来赋值
- }
- build(,n,);
- int q;scanf("%d",&q);
- while(q--)
- {
- char s[];
- int v;
- scanf("%s %d",s,&v);
- if(s[]=='g')
- {
- printf("%d\n",query(L[v],R[v],));
- }
- else
- {
- update(L[v],R[v],);
- }
- }
- return ;
- }
- /*
- 10
- 1 2 3 3 5 5 7 7 8
- 0 0 0 0 1 1 1 1 0 0
- 10
- pow 3
- get 3
- */
Codeforces Round #442 (Div. 2) Danil and a Part-time Job的更多相关文章
- Codeforces Round #442 (Div. 2) E Danil and a Part-time Job (dfs序加上一个线段树区间修改查询)
题意: 给出一个具有N个点的树,现在给出两种操作: 1.get x,表示询问以x作为根的子树中,1的个数. 2.pow x,表示将以x作为根的子树全部翻转(0变1,1变0). 思路:dfs序加上一个线 ...
- Codeforces Round #442 Div.2 A B C D E
A. Alex and broken contest 题意 判断一个字符串内出现五个给定的子串多少次. Code #include <bits/stdc++.h> char s[110]; ...
- Codeforces Round #442 (Div. 2)
A. Alex and broken contest time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- 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 ...
- 【Codeforces Round #442 (Div. 2) A】Alex and broken contest
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 注意是所有的名字里面,只出现了其中某一个名字一次. [代码] #include <bits/stdc++.h> usin ...
- 【Codeforces Round #442 (Div. 2) D】Olya and Energy Drinks
[链接] 我是链接,点我呀:) [题意] 给一张二维点格图,其中有一些点可以走,一些不可以走,你每次可以走1..k步,问你起点到终点的最短路. [题解] 不能之前访问过那个点就不访问了.->即k ...
- 【Codeforces Round #442 (Div. 2) C】Slava and tanks
[链接] 我是链接,点我呀:) [题意] 有n个位置,每个位置都可能有不定数量的tank; 你每次可以选择一个位置投掷炸弹. 并且,这个位置上的所有tank都会受到你的攻击. 并且失去一点体力. 然后 ...
- 【Codeforces Round #442 (Div. 2) B】Nikita and string
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 枚举中间那一段从哪里开始.哪里结束就好 注意为空的话,就全是a. 用前缀和优化一下. [代码] #include <bits/ ...
- Codeforces Round #442 (Div. 2) B题【一道模拟题QAQ】
B. Nikita and string One day Nikita found the string containing letters "a" and "b&qu ...
随机推荐
- Django与多个数据库交互
定义数据库 在Django中使用多个数据库的第一步是告诉Django您将要使用的数据库服务器. 数据库可以有您选择的任何别名.但是,别名 default 有着特殊的意义.Django使用别名为 def ...
- Head First Python (二)
if...else... 1 movies = ["The Holy Grail",1975,"Terry Jones & Terry Gilliam" ...
- LeetCode(228) Summary Ranges
题目 Given a sorted integer array without duplicates, return the summary of its ranges. For example, g ...
- ACM-ICPC 2018 徐州赛区网络预赛 I. Characters with Hash
Mur loves hash algorithm, and he sometimes encrypt another one's name, and call him with that encryp ...
- Monkey与MonkeyRunner之间的区别
为了支持黑盒自动化测试的场景,Android SDK提供了monkey和monkeyrunner两个测试工具,这两个测试工具除了名字类似外,还都可以向待测应用发送按键等消息,往往容易产生混淆,以下是他 ...
- CodeForces 379F 树的直径 New Year Tree
题意:每次操作新加两个叶子节点,每次操作完以后询问树的直径. 维护树的直径的两个端点U,V,每次计算一下新加进来的叶子节点到U,V两点的距离,如果有更长的就更新. 因为根据树的直径的求法,若出现新的直 ...
- Ubuntu简单指令和热键的学习
Ubuntu查看本机版本的方法 sudo lsb_release -a即可 注销linux: 输入:exit 注意,离开系统不是关机,基本上,linux本身已经有相当多的工作进行,所以你离开时,这次这 ...
- Leetcode 421.数组中两数的最大异或值
数组中两数的最大异或值 给定一个非空数组,数组中元素为 a0, a1, a2, … , an-1,其中 0 ≤ ai < 231 . 找到 ai 和aj 最大的异或 (XOR) 运算结果,其中0 ...
- Game on Tree
D - Game on Tree Time limit : 2sec / Memory limit : 256MB Score : 1100 points Problem Statement Ther ...
- 面向对象编程(四)继承,概念及super关键字,final关键字,Object类常见方法
继承 概念: ① 继承背后的思想就是基于已存在的类来构建新类; ② 当从已存在类继承时,就重用了它的方法和属性,还可以添加新的方法和属性来定制新类以应对需求; ③ 当从其它类导出的类叫作子 ...