Skip to content

Latest commit

 

History

History
133 lines (131 loc) · 3.31 KB

Tree_Queries.md

File metadata and controls

133 lines (131 loc) · 3.31 KB

Algorithm: Divide and Conquer

Problem: Tree Queries

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vi vector<int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vvi vector<vi>
#define vll vector<ll>
#define vvll vector<vector<ll>>
#define vpii vector<pair<int,int>>
#define vpll vector<pair<ll,ll>>
#define vb vector<bool>
#define vs vector<string>
///............x...........///
#define all(a) a.begin(), a.end()
#define allr(a) a.rbegin(), a.rend()
#define mp(a, b) make_pair(a, b)
#define pb push_back
#define ff first
#define ss second
#define bg begin()
#define UNIQUE(X) (X).erase(unique(all(X)), (X).end())
#define ft cout << "for test"<<endl;
#define read(v, a, n) for (int i = a; i<n; i++)cin>>v[i];
#define print(v) for (auto it : v)cout << it<<' ';cout << endl;
#define PI acos(-1.0)
#define yes cout <<"Yes"<< endl
#define no cout<<"No"<<endl
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define t_c int test, cs = 1;cin>>test;while (test--)
#define casep cout<<"Case "<< cs++<<": ";
///................function.....................///

#define D(a) (double)(a)
#define sqr(a) (a * a)
#define siz(s) (int)(s.size())
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define Min(a) *min_element(all(a))
///#define mod 1000000007
///.........Graph.........///
int X[] = {1, -1, 0, 0};
int Y[] = {0, 0, 1, -1};
///........constant........///
const ll N = 1e6+5;
#define lft nd*2
#define rgt nd*2+1
#define mid (b+e)/2
class C{
    public:
    int n,m,Time=0;
    vvi g;
    vi in,out,val,mapping;
    vvi tree;
    C(){
        cin>>n>>m;
        g.resize(n+1);in.resize(n+1);out.resize(n+1);val.resize(n+1);
        tree.resize(4*n);mapping.resize(n+1);
        for(int i=2; i<=n; i++){
            int a;
            cin>>a;g[a].pb(i);
        }
        dfs(1,1);
        for(int i=1; i<=n; i++){
            cin>>val[i];
            mapping[in[i]]=i;
        }
        build(1,1,Time);
        for(int i=0; i<m; i++){
            int a,b;
            cin>>a>>b;
            cout<<query(1,1,Time,in[a],out[a],b)<<endl;
        }
    }
    void dfs(int nd, int p){
        Time++;
        in[nd]=Time;
        for(auto to:g[nd]){
            if(to!=p){
                dfs(to,nd);
            }
        }
        out[nd]=Time;
    }
    void build(int nd, int b, int e){
        if(b>e)return;
        //cout<<nd<<" "<<b<<" "<<e<<endl;
        if(b==e){
            tree[nd].pb(val[mapping[b]]);
            return;
        }
        build(lft,b,mid);
        build(rgt,mid+1,e);
        merge(tree[lft].begin(),tree[lft].end(),tree[rgt].begin(),tree[rgt].end(),back_inserter(tree[nd]));
        /**cout<<" left tree ";print(tree[lft]);
        cout<<" right tree ";print(tree[rgt]);
        cout<<" node tree ";print(tree[nd]);**/
    }
    int query(int nd, int b ,int e , int l, int r, int x){
        if(e<b or r<b or e<l)return 0;
        if(l<=b and e<=r){
            int cnt = lower_bound(all(tree[nd]),x)-tree[nd].begin();
            return tree[nd].size()-cnt;
        }
        int p1 = query(lft,b,mid,l,r,x);
        int p2 = query(rgt,mid+1,e,l,r,x);
        return p1+p2;
    }
};
int main(){
    FIO;
    C c;
    /**
7 10
1 1 2 2 3 3 
5 3 1 5 7 4 8
3 3
2 3
1 4
6 3
6 4
6 5
1 10
4 5
2 5
1 1
**/
}