Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- typedef long double ld;
- typedef long long int ll;
- typedef pair<int,int> pi;
- typedef pair<long long,long long> pll;
- #define endl '\n'
- #define ff first
- #define ss second
- #define pb push_back
- #define int long long
- #define sz(v) (int)v.size()
- #define inf 2147483647
- #define llinf 9223372036854775807
- #define all(v) v.begin(),v.end()
- #define bp(n) __builtin_popcountll(n)
- #define f(i,l,r) for(long long i=l;i<=r;i++)
- #define rf(i,r,l) for(long long i=r;i>=l;i--)
- #define fast ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
- template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }
- template<typename T, size_t size> ostream& operator<<(ostream &os, const array<T, size> &arr) { os << '{'; string sep; for (const auto &x : arr) os << sep << x, sep = ", "; return os << '}'; }
- template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
- void dbg_out() { cerr << endl; }
- template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
- const int N=5e5+5,mod=1e9+7,bit=61;
- template<typename T>
- class fenwick
- {
- public:
- vector<T> fen;
- int n;
- fenwick() {}
- fenwick(int _n,T val=T())
- {
- n=_n;
- fen=vector<T> (n+1,val);
- }
- void reset()
- {
- for(int i=0;i<(int)fen.size();i++)
- {
- fen[i]=(T)0;
- }
- }
- T merge(T a,T b)
- {
- return a+b;
- }
- void update(int idx,T val)
- {
- while(idx<=n)
- {
- fen[idx]=merge(fen[idx],val);
- idx+=(idx & -idx);
- }
- }
- T query(int idx)
- {
- T ans{};
- while(idx>0)
- {
- ans=merge(ans,fen[idx]);
- idx-=(idx & -idx);
- }
- return ans;
- }
- T from(int l,int r)
- {
- return query(r)-query(l-1);
- }
- };
- vector<int> adj[N],mid[N],my[N];
- int tin[N],tout[N],dep[N],req[N],F[N],X[N],D[N],l[N],r[N];
- int n,m,q,tim=0;
- fenwick<int> obj1,obj2;
- void dfs(int u,int par,int d)
- {
- tin[u]=++tim;
- dep[u]=d;
- for(auto &v:adj[u])
- {
- if(v!=par)
- {
- dfs(v,u,d+1);
- }
- }
- tout[u]=tim;
- }
- void apply(int id)
- {
- int s=tin[F[id]];
- int e=tout[F[id]];
- obj1.update(s,X[id] - dep[F[id]] * D[id]);
- obj1.update(e+1,-X[id] + dep[F[id]] * D[id]);
- obj2.update(s,D[id]);
- obj2.update(e+1,-D[id]);
- }
- signed main()
- {
- fast;
- cin>>n>>m;
- obj1 = fenwick<int> (n);
- obj2 = fenwick<int> (n);
- f(i,2,n)
- {
- int p;
- cin>>p;
- adj[p].pb(i);
- }
- f(i,1,n)
- {
- int ow;
- cin>>ow;
- my[ow].pb(i);
- }
- dfs(1,0,1);
- f(i,1,m)
- {
- cin>>req[i];
- }
- cin>>q;
- f(i,1,q)
- {
- cin>>F[i]>>X[i]>>D[i];
- }
- f(i,1,m)
- {
- l[i]=1;
- r[i]=q+1;
- }
- bool done=0;
- while(!done)
- {
- done=1;
- obj1.reset();
- obj2.reset();
- f(i,1,m)
- {
- if(l[i]!=r[i])
- {
- done=0;
- int c_mid = (l[i]+r[i])>>1;
- mid[c_mid].pb(i);
- }
- }
- f(i,1,q)
- {
- apply(i);
- while(!mid[i].empty())
- {
- int par=mid[i].back();
- bool sat=0;
- int sum=0;
- for(auto &v:my[par])
- {
- int add=obj1.query(tin[v]);
- add+=(dep[v] * obj2.query(tin[v]));
- sum+=add;
- if(sum >= req[par])
- {
- sat=1;
- break;
- }
- }
- if(sat)
- {
- r[par]=i;
- }
- else
- {
- l[par]=i+1;
- }
- mid[i].pop_back();
- }
- }
- }
- f(i,1,m)
- {
- assert(l[i] == r[i]);
- if(r[i] <= q)
- {
- cout<<r[i]<<endl;
- }
- else
- {
- cout<<"rekt"<<endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement