Description
Xiaomingming recently got a new board game, in which m knights are needed to occupy n cities.
These n cities are represented by integers from 1 to n. In addition to city 1, city i will be under the jurisdiction of another city fi,
Of which fi
Input
The first line contains two positive integers, n;m, representing the number of cities and the number of knights.
The second line contains n integers, where the number i is hi, indicating the defense value of the city i.
Lines 3 to n +1, each containing three integers. Where the three numbers of the i +1 line are fi;ai;vi, respectively indicating jurisdiction
The number of the city and two combat power change parameters of the city.
Lines n +2 to n + m +1, each containing two integers. Where the two numbers of the n + i row are si;ci, respectively
Indicates the initial combat power and the first city to attack.
Output
Output n + m lines, each containing a non negative integer. The first n lines represent the knights who died in the city 1 to n respectively
The last m lines indicate the number of cities occupied by Knight 1 to m respectively.
Sample Input
5 5
50 20 10 10 30
1 1 2
2 0 5
2 0 -10
1 0 10
20 2
10 3
40 4
20 4
35 5
Sample Output
2
2
0
0
0
1
1
3
1
1
HINT
For 100% data, 1 < = n; m < = 300000; 1 < = fi
Solution
Left skew tree
code
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N=300005;
int n,m,num[N],ans[N],dep[N],cnt,last[N],c[N],a[N],root[N];
LL h[N],v[N];
struct edge{int to,next;}e[N];
struct tree{int l,r,dis;LL val,tag1,tag2;}t[N];
void addedge(int u,int v)
{
e[++cnt].to=v;e[cnt].next=last[u];last[u]=cnt;
}
void pushdown(int d)
{
LL u=t[d].tag1,v=t[d].tag2;t[d].tag1=1;t[d].tag2=0;
if (t[d].l) t[t[d].l].val=t[t[d].l].val*u+v,t[t[d].l].tag1*=u,t[t[d].l].tag2=t[t[d].l].tag2*u+v;
if (t[d].r) t[t[d].r].val=t[t[d].r].val*u+v,t[t[d].r].tag1*=u,t[t[d].r].tag2=t[t[d].r].tag2*u+v;
}
int merge(int x,int y)
{
if (!x||!y) return x+y;
pushdown(x);pushdown(y);
if (t[x].val>t[y].val) swap(x,y);
t[x].r=merge(t[x].r,y);
if (t[t[x].l].dis<t[t[x].r].dis) swap(t[x].l,t[x].r);
t[x].dis=t[t[x].l].dis+1;
return x;
}
void get_dep(int x,int fa)
{
dep[x]=dep[fa]+1;
for (int i=last[x];i;i=e[i].next) get_dep(e[i].to,x);
}
void solve(int x)
{
for (int i=last[x];i;i=e[i].next)
{
solve(e[i].to);
if (a[e[i].to]==0) t[root[e[i].to]].val+=v[e[i].to],t[root[e[i].to]].tag2+=v[e[i].to];
else t[root[e[i].to]].val*=v[e[i].to],t[root[e[i].to]].tag1*=v[e[i].to],t[root[e[i].to]].tag2*=v[e[i].to];
root[x]=merge(root[x],root[e[i].to]);
}
while (root[x]&&t[root[x]].val<h[x]) ans[root[x]]=x,pushdown(root[x]),root[x]=merge(t[root[x]].l,t[root[x]].r),num[x]++;
}
int main()
{
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++) scanf("%lld",&h[i]);
for (int i=2;i<=n;i++)
{
int x;
scanf("%d%lld%lld",&x,&a[i],&v[i]);
addedge(x,i);
}
for (int i=1;i<=m;i++)
{
LL s;
scanf("%lld%d",&s,&c[i]);
t[i].val=s;t[i].tag1=1;
root[c[i]]=merge(root[c[i]],i);
}
get_dep(1,0);
solve(1);
for (int i=1;i<=n;i++) printf("%d\n",num[i]);
for (int i=1;i<=m;i++) printf("%d\n",dep[c[i]]-dep[ans[i]]);
return 0;
}