标签:ever tor cat mem int content arch exce get
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5032 Accepted Submission(s): 1966
#include<iostream> #include<vector> #include<cstdio> #include<cstring> using namespace std; const int maxn = 5e5+8; int First[maxn<<1],LAST[maxn<<1],wa; vector<int>u[maxn]; bool book[maxn]; struct node { int l,r; int num; bool lazy; }tree[maxn<<2]; void dfs(int t) { int siz=u[t].size(); First[t]=++wa; for(int i=0;i<siz;i++){ dfs(u[t][i]); } LAST[t]=++wa; } void build(int t,int l,int r) { tree[t].l=l;tree[t].r=r; tree[t].num=-1;tree[t].lazy=false; if(l==r){return;} int mid=(l+r)>>1; build(t<<1,l,mid); build((t<<1)|1,mid+1,r); } void push_down(int t) { tree[t<<1].num=tree[t].num; if(tree[t<<1].l!=tree[t<<1].r){tree[t<<1].lazy=true;} tree[(t<<1)|1].num=tree[t].num; if(tree[(t<<1)|1].l!=tree[(t<<1)|1].r){tree[(t<<1)|1].lazy=true;} tree[t].lazy=false; } void update(int t,int l,int r,int x) { if(tree[t].lazy)push_down(t); if(tree[t].l==l&&tree[t].r==r){ if(l!=r)tree[t].lazy=true; tree[t].num=x;return ; } int mid=(tree[t].l+tree[t].r)>>1; if(r<=mid){update(t<<1,l,r,x);} else if(l>mid){update((t<<1)|1,l,r,x);} else { update((t<<1),l,mid,x); update((t<<1)|1,mid+1,r,x); } } int query(int t,int x) { // cout<<t<<" "<<x<<" "<<tree[t].lazy<<endl; if(tree[t].lazy)push_down(t); if(tree[t].l==tree[t].r){ if(tree[t].l!=x){return -1;} return tree[t].num; } int mid=(tree[t].l+tree[t].r)>>1; if(x<=mid){return query(t<<1,x);} else if(x>mid){return query((t<<1)|1,x);} } int main() { int T,n; scanf("%d",&T); int re=0; while(T--){ re++; printf("Case #%d:\n",re); wa=0; memset(book,0,sizeof(book)); memset(First,0,sizeof(First)); memset(LAST,0,sizeof(LAST)); scanf("%d",&n); for(int i=0;i<=n+1;i++){ u[i].clear(); } int x,y; for(int i=1;i<n;i++){ scanf("%d%d",&x,&y); u[y].push_back(x); book[x]++; } for(int i=1;i<=n;i++){ if(!book[i]){dfs(i);} } build(1,1,n*2); scanf("%d",&n); char p[5]; for(int i=1;i<=n;i++){ getchar(); scanf("%s",p); if(p[0]==‘C‘){ scanf("%d",&x); printf("%d\n",query(1,First[x])); } else if(p[0]==‘T‘){ scanf("%d%d",&x,&y); update(1,First[x],LAST[x],y); } } } return 0; }
标签:ever tor cat mem int content arch exce get
原文地址:https://www.cnblogs.com/ZGQblogs/p/9368286.html