bzoj 4003: [JLOI2015]城池攻占

Description

小铭铭最近获得了一副新的桌游,游戏中需要用 m 个骑士攻占 n 个城池。

这 n 个城池用 1 到 n 的整数表示。除 1 号城池外,城池 i 会受到另一座城池 fi 的管辖,
其中 fi

Input

第 1 行包含两个正整数 n;m,表示城池的数量和骑士的数量。

第 2 行包含 n 个整数,其中第 i 个数为 hi,表示城池 i 的防御值。
第 3 到 n +1 行,每行包含三个整数。其中第 i +1 行的三个数为 fi;ai;vi,分别表示管辖
这座城池的城池编号和两个战斗力变化参数。
第 n +2 到 n + m +1 行,每行包含两个整数。其中第 n + i 行的两个数为 si;ci,分别表
示初始战斗力和第一个攻击的城池。

Output

输出 n + m 行,每行包含一个非负整数。其中前 n 行分别表示在城池 1 到 n 牺牲的骑士

数量,后 m 行分别表示骑士 1 到 m 攻占的城池数量。

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

对于 100% 的数据,1 <= n;m <= 300000; 1 <= fi

Solution

左偏树

code

 #include
#include
#include
#include
#include
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].dis1;
    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]].valint 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;
}

你可能感兴趣的:(左偏树)