- Problem Link:
- https://codeforces.com/contest/343/problem/D
Mike wants to do the following operations with the tree:
- Fill vertex v with water. Then v and all its children are filled with water.
- Empty vertex v. Then v and all its ancestors are emptied.
- Determine whether vertex v is filled with water at the moment.
For each type 3 operation print 1 on a separate line if the vertex is full, and 0 if the vertex is empty. Print the answers to queries in the order in which the queries are given in the input.
Input:
5
1 2
5 1
2 3
4 2
12
1 1
2 3
3 1
3 2
3 3
3 4
1 2
2 4
3 1
3 3
3 4
3 5
Output:
0
0
0
1
0
1
0
1
- ///...................SUBHASHIS MOLLICK...................///
- ///.....DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING....///
- ///.............ISLAMIC UNIVERSITY,BANGLADESH.............///
- ///....................SESSION-(14-15)....................///
- #include<bits/stdc++.h>
- using namespace std;
- #define sf(a) scanf("%d",&a)
- #define sf2(a,b) scanf("%d %d",&a,&b)
- #define sf3(a,b,c) scanf("%d %d %d",&a,&b,&c)
- #define pf(a) printf("%d",a)
- #define pf2(a,b) printf("%d %d",a,b)
- #define pf3(a,b,c) printf("%d %d %d",a,b,c)
- #define sfl(a) scanf("%lld",&a)
- #define sfl2(a,b) scanf("%lld %lld",&a,&b)
- #define sfl3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
- #define pfl(a) printf("%lld",a)
- #define pfl2(a,b) printf("%lld %lld",a,b)
- #define pfl3(a,b,c) printf("%lld %lld %lld",a,b,c)
- #define nl printf("\n")
- #define timesave ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
- #define ll long long
- #define pb push_back
- #define MPI map<int,int>mp;
- #define fr(i,n) for(i=0;i<n;i++)
- #define fr1(i,n) for(i=1;i<=n;i++)
- #define frl(i,a,b) for(i=a;i<=b;i++)
- #define tz 100000
- /*primes in range 1 - n
- 1 - 100(1e2) -> 25 pimes
- 1 - 1000(1e3) -> 168 primes
- 1 - 10000(1e4) -> 1229 primes
- 1 - 100000(1e5) -> 9592 primes
- 1 - 1000000(1e6) -> 78498 primes
- 1 - 10000000(1e7) -> 664579 primes
- large primes ->
- 104729 1299709 15485863 179424673 2147483647 32416190071 112272535095293 48112959837082048697
- */
- //freopen("Input.txt","r",stdin);
- //freopen("Output.txt","w",stdout);
- //const int fx[]={+1,-1,+0,+0};
- //const int fy[]={+0,+0,+1,-1};
- //const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1}; // Kings Move
- //const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1}; // Kings Move
- //const int fx[]={-2, -2, -1, -1, 1, 1, 2, 2}; // Knights Move
- //const int fy[]={-1, 1, -2, 2, -2, 2, -1, 1}; // Knights Move
- bool tree[1050000];
- int start[500050],End[500050],cnt=0,pr[500050];
- vector<int>vec[500050];
- int ss,ee;
- void dfs(int u,int par)
- {
- pr[u]=par;
- start[u]=++cnt; /// start kokhon hoece
- for(int i=0; i<vec[u].size(); i++)
- {
- int v=vec[u][i];
- if(v!=par)
- {
- dfs(v,u);
- }
- }
- End[u]=cnt; /// ending kokhon hoece
- }
- void init(int s,int e,int node)
- {
- if(s==e)
- {
- tree[node]=true;
- return;
- }
- int lft=node*2;
- int right=node*2+1;
- int mid=(s+e)/2;
- init(s,mid,lft);
- init(mid+1,e,right);
- tree[node]=true;
- return;
- }
- bool Fill(int s,int e,int node)
- {
- if(s>ee||ss>e) /// baire porce
- return false;
- if(s==e)
- {
- tree[node]=false;
- return true;
- }
- int lft=node*2;
- int right=node*2+1;
- int mid=(s+e)/2;
- bool ret=false;
- if(tree[lft])
- ret|=Fill(s,mid,lft);
- if(tree[right])
- ret|=Fill(mid+1,e,right);
- tree[node]=tree[lft]|tree[right];
- return ret;
- }
- void Empty(int s,int e,int node)
- {
- if(s==e)
- {
- tree[node]=true;
- return;
- }
- int lft=node*2;
- int right=node*2+1;
- int mid=(s+e)/2;
- if(ss<=mid)
- Empty(s,mid,lft);
- else
- Empty(mid+1,e,right);
- tree[node]=tree[lft]|tree[right];
- return;
- }
- bool qry(int s,int e,int node)
- {
- if(ss>e||s>ee) /// baire porce
- return false;
- if(ss<=s&&e<=ee)
- return tree[node];
- int lft=node*2;
- int right=node*2+1;
- int mid=(s+e)/2;
- return qry(s,mid,lft)|qry(mid+1,e,right);
- }
- main()
- {
- timesave;
- int n,u,v,i,dir;
- cin>>n;
- for(i=1; i<n; i++)
- {
- cin>>u>>v;
- vec[u].push_back(v);
- vec[v].push_back(u);
- }
- dfs(1,-1);
- init(1,n,1);/// fill up with 1--- parameter hocce first position,last position,node no
- // for(i=1;i<=n;i++)
- // cout<<i<<" "<<tree[i]<<endl;
- int q;
- cin>>q;
- for(i=1; i<=q; i++)
- {
- cin>>dir>>v;
- ss=start[v],ee=End[v];
- if(dir==1)
- {
- if(Fill(1,n,1))
- {
- if(pr[v]!=-1)
- {
- ss=start[pr[v]];
- Empty(1,n,1);
- }
- }
- }
- else if(dir==2)
- {
- Empty(1,n,1);
- }
- else
- {
- if(qry(1,n,1))
- printf("0\n");
- else
- printf("1\n");
- }
- }
- }