Codeforces Round #442 (Div. 2) E. Danil and a Part-time Job DFS序+树链剖分+线段树区间^
来源:互联网 发布:三只松鼠 淘宝客 编辑:程序博客网 时间:2024/05/16 11:44
传送门
题意:
给你一棵根为1的树, 每个点的权值是0或1, get x表示求x子树的1的个数, pow x表示把x的子树1变0, 0变1
做法:很容易看出来这是树链剖分,再带上线段树的区间异或就好了- -
/// .-~~~~~~~~~-._ _.-~~~~~~~~~-./// __.' ~. .~ `.__/// .'// \./ \\`./// .'// | \\`./// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`./// .'//.-" `-. | .-' "-.\\`./// .'//______.============-.. \ | / ..-============.______\\`./// .'______________________________\|/______________________________`.#pragma comment(linker, "/STACK:1024000000,1024000000")#include <vector>#include <iostream>#include <string>#include <map>#include <stack>#include <cstring>#include <queue>#include <list>#include <stdio.h>#include <set>#include <algorithm>#include <cstdlib>#include <cmath>#include <iomanip>#include <cctype>#include <sstream>#include <functional>#include <stdlib.h>#include <time.h>#include <bitset>using namespace std;#define pi acos(-1)#define s_1(x) scanf("%d",&x)#define s_2(x,y) scanf("%d%d",&x,&y)#define s_3(x,y,z) scanf("%d%d%d",&x,&y,&z)#define s_4(x,y,z,X) scanf("%d%d%d%d",&x,&y,&z,&X)#define S_1(x) scanf("%I64d",&x)#define S_2(x,y) scanf("%I64d%I64d",&x,&y)#define S_3(x,y,z) scanf("%I64d%I64d%I64d",&x,&y,&z)#define PI acos(-1)#define endl '\n'#define srand() srand(time(0));#define me(x,y) memset(x,y,sizeof(x));#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)#define close() ios::sync_with_stdio(0); cin.tie(0);#define FOR(x,n,i) for(int i=x;i<=n;i++)#define FOr(x,n,i) for(int i=x;i<n;i++)#define fOR(n,x,i) for(int i=n;i>=x;i--)#define fOr(n,x,i) for(int i=n;i>x;i--)#define W while#define sgn(x) ((x) < 0 ? -1 : (x) > 0)#define bug printf("***********\n");#define db double#define ll long long#define mp make_pair#define pb push_backtypedef long long LL;typedef pair <int, int> ii;const int INF=0x3f3f3f3f;const LL LINF=1e19+10;//const int dx[]={-1,0,1,0,1,-1,-1,1};//const int dy[]={0,1,0,-1,-1,1,-1,1};const int maxn=2e3+10;const int maxx=1e6+10;const double EPS=1e-10;const double eps=1e-10;const int mod=1e9+7;template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}void Out(LL a){if(a < 0) { putchar('-'); a = -a; }if(a >= 10) Out(a / 10);putchar(a % 10 + '0');}void print(LL a){ Out(a),puts("");}//freopen( "in.txt" , "r" , stdin );//freopen( "data.txt" , "w" , stdout );//cerr << "run time is " << clock() << endl;/*void readString(string &s){static char str[maxx];scanf("%s", str);s = str;}*/struct node{ int tl,tr,lazy,val; node(int x=0,int y=0,int z=0): lazy(0),tl(x),tr(y),val(z) {}} tree[800020];int a[200005];void pushup(int id){ tree[id].val=tree[id<<1].val+tree[id<<1|1].val;}void build(int id,int tl,int tr){ tree[id]=node(tl,tr,0); if(tl==tr)tree[id].val=a[tl]; else { int tm=(tr+tl)>>1; build(id<<1,tl,tm); build(id<<1|1,tm+1,tr); pushup(id); }}void pushdown(int id){ if(!tree[id].lazy)return; tree[id<<1].lazy^=1,tree[id<<1|1].lazy^=1; tree[id<<1].val=tree[id<<1].tr-tree[id<<1].tl+1-tree[id<<1].val; tree[id<<1|1].val=tree[id<<1|1].tr-tree[id<<1|1].tl+1-tree[id<<1|1].val; tree[id].lazy=0;}void update(int id,int ql,int qr){ int tl=tree[id].tl,tr=tree[id].tr; if(ql>tr || qr<tl)return; if(ql<=tl && tr<=qr) { tree[id].lazy^=1; tree[id].val=tr-tl+1-tree[id].val; return; } pushdown(id); update(id<<1,ql,qr); update(id<<1|1,ql,qr); pushup(id);}int get_sum(int id,int ql,int qr){ int tl=tree[id].tl,tr=tree[id].tr,ans=0; if(ql>tr || qr<tl)return 0; if(ql<=tl && tr<=qr)return tree[id].val; pushdown(id); ans+=get_sum(id<<1,ql,qr); ans+=get_sum(id<<1|1,ql,qr); pushup(id); return ans;}/// dfs序+树链剖分int pre[200005],siz[200005],dep[200005];int top[200005],node_id[200005],heavy[200005];int lson[200005],rson[200005],idx=0;struct{ int nex,nex_node;} edge[200005];int head[200005],cont=0;void add_edge(int now,int nex){ edge[cont].nex=nex; edge[cont].nex_node=head[now]; head[now]=cont++;}int dfs(int x,int fa,int depth){ dep[x]=depth,pre[x]=fa; int res=1,maxx=0; for(int i=head[x]; ~i; i=edge[i].nex_node) { int nex=edge[i].nex; if(nex==fa)continue; int t=dfs(nex,x,depth+1); if(t>maxx)maxx=t,heavy[x]=nex; res+=t; } return siz[x]=res;}void slpf(int x,int fa,int tp){ lson[x]=node_id[x]=++idx,top[x]=tp; if(heavy[x])slpf(heavy[x],x,tp); for(int i=head[x]; ~i; i=edge[i].nex_node) { int nex=edge[i].nex; if(nex==fa)continue; if(nex!=heavy[x])slpf(nex,x,nex); } rson[x]=idx;}int b[200005];int n;void solve(){ W(s_1(n)!=EOF) { me(head,-1); FOR(2,n,i) { int x; s_1(x); add_edge(x,i); } FOR(1,n,i) s_1(b[i]); dfs(1,0,1); slpf(1,0,1); FOR(1,n,i) a[lson[i]]=b[i]; build(1,1,n); int q; s_1(q); W(q--) { int x; char op[10]; scanf("%s%d",op,&x); if(op[0]=='g') printf("%d\n",get_sum(1,lson[x],rson[x])); else update(1,lson[x],rson[x]); } }}int main(){ //freopen( "in.txt" , "r" , stdin ); //freopen( "data.txt" , "w" , stdout ); int t=1; //init(); //s_1(t); for(int cas=1;cas<=t;cas++) { //printf("Case #%d: ",cas); solve(); }}
阅读全文
0 0
- Codeforces Round #442 (Div. 2) E. Danil and a Part-time Job DFS序+树链剖分+线段树区间^
- Codeforces Round #442 (Div. 2)-E-Danil and a Part-time Job(DFS序+线段树区间更新)
- Codeforces Round #442 (Div. 2) E. Danil and a Part-time Job【线段树+dfs序】
- Codeforces Round #442 (Div. 2) Danil and a Part-time Job dfs序+线段树区间反转
- Codeforces Round #442 (Div. 2) 877 E. Danil and a Part-time Job DFS序 线段树
- Codeforces Round #442 (Div. 2) E. Danil and a Part-time Job (dfs序+线段树)
- Codeforces Round #442E-dfs序&线段树的区间更新区间查询-Danil and a Part-time Job
- Codeforces Round #442 (Div. 2) E. Danil and a Part-time Job (dfs序树型转线性 线段树区间修改区间查询)
- codeforces 877 problem E Danil and a Part-time Job 【dfs序 + 线段树区间异或修改】
- Codeforces 877E Danil and a Part-time Job【Dfs序+线段树】
- Codeforces 877 E Danil and a Part-time Job(线段树+dfs序)
- [codeforces] 877E. Danil and a Part-time Job(DFS序+线段树)
- CodeForces 877E Danil and a Part-time Job (dfs序+线段树)
- Codeforces Round #442 (Div. 2) E. Danil and a Part-time Job
- Codeforces-877E:Danil and a Part-time Job(DFS序列+线段树)
- codeforces 877E. Danil and a Part-time Job (DFS序列+线段树)
- Codeforces Round #877 (Div. 2) E. Danil and a Part-time Job
- CF#877 E. Danil and a Part-time Job(DFS序+线段树)
- Runloop 探秘(一)
- Eclipse常用快捷键
- ftpget ftpput指令
- tp5 以及网站架构个人心得
- 2018web前端面试总结
- Codeforces Round #442 (Div. 2) E. Danil and a Part-time Job DFS序+树链剖分+线段树区间^
- Java语言的三大特征
- 带你了解“比特币黄金”和SegWit2x分叉
- 逻辑卷lvm管理
- 查询数据库锁表sql
- android onTouch事件处理机制
- MySQL 使用mysqld_multi部署单机多实例详细过程
- 在慕课学完javascript的入门课程总结
- ue4 renderTexture简单记录