[luogu3261 JLOI2015] 城池攻占 (左偏树+标记)

传送门

Description

小铭铭最近获得了一副新的桌游,游戏中需要用 m 个骑士攻占 n 个城池。这 n 个城池用 1 到 n 的整数表示。除 1 号城池外,城池 i 会受到另一座城池 fi 的管辖,其中 fi <i。也就是说,所有城池构成了一棵有根树。这 m 个骑士用 1 到 m 的整数表示,其中第 i 个骑士的初始战斗力为 si,第一个攻击的城池为 ci。

每个城池有一个防御值 hi,如果一个骑士的战斗力大于等于城池的生命值,那么骑士就可以占领这座城池;否则占领失败,骑士将在这座城池牺牲。占领一个城池以后,骑士的战斗力将发生变化,然后继续攻击管辖这座城池的城池,直到占领 1 号城池,或牺牲为止。

除 1 号城池外,每个城池 i 会给出一个战斗力变化参数 ai;vi。若 ai =0,攻占城池 i 以后骑士战斗力会增加 vi;若 ai =1,攻占城池 i 以后,战斗力会乘以 vi。注意每个骑士是单独计算的。也就是说一个骑士攻击一座城池,不管结果如何,均不会影响其他骑士攻击这座城池的结果。

现在的问题是,对于每个城池,输出有多少个骑士在这里牺牲;对于每个骑士,输出他攻占的城池数量。

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<i; 1 <= ci <= n; -10^18 <= hi,vi,si <= 10^18; ai等于1或者0;
当 ai =1 时,vi > 0;保证任何时候骑士战斗力值的绝对值不超过 10^18。

Solution

把每个骑士视为一个点,给每个城市建左偏树维护战斗力小的在上比较这个值和城市生命值
每个城市做之前先把所有子树合并
需要模仿线段树一样打标记(不打标记20分)

Code

//By Menteur_Hxy
#include<cstdio>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define F(i,a,b) for(register int i=(a);i<=(b);i++)
using namespace std;
typedef long long LL;

LL read() {
    LL x=0,f=1; char c=getchar();
    while(!isdigit(c)) {if(c=='-')f=-f; c=getchar();}
    while(isdigit(c)) x=(x<<1)+(x<<3)+c-48,c=getchar();
    return x*f;
}

const int N=300010;
bool fla[N];
int n,m;
int rt[N],fa[N],nd[N][2],dep[N],bl[N];
LL de[N],wi[N],po[N],laz[N],dis[N],mul[N],add[N],ans1[N],ans2[N];
vector <int> V[N];

void upd(int x,LL mu,LL ad) {
    if(!x) return ;
    po[x]*=mu,po[x]+=ad;
    mul[x]*=mu;add[x]*=mu;add[x]+=ad;
}

#define ls nd[x][0]
#define rs nd[x][1]
void pushdown(int x) {
    upd(ls,mul[x],add[x]);
    upd(rs,mul[x],add[x]);
    mul[x]=1,add[x]=0;
}

int merge(int x,int y) {
    if(!x||!y) return x+y;
    pushdown(x),pushdown(y);
    if(po[x]>po[y]||(po[x]==po[y]&&x>y)) swap(x,y);
    fa[rs=merge(rs,y)]=x;
    if(dis[ls]<dis[rs]) swap(ls,rs);
    dis[x]=dis[rs]+1;
    return x;
}

void dfs(int u) {
    int siz=V[u].size();
    F(i,0,siz-1) { int v=V[u][i];
        dep[v]=dep[u]+1;
        dfs(v);
        if(rt[u]&&rt[v]) rt[u]=merge(rt[u],rt[v]);
        else rt[u]=rt[v]+rt[u];
    }
    while(rt[u]&&po[rt[u]]<de[u]) {
        pushdown(rt[u]);po[rt[u]]=-1;
        ans2[rt[u]]=dep[bl[rt[u]]]-dep[u];
        rt[u]=merge(nd[rt[u]][0],nd[rt[u]][1]);
        ans1[u]++;
    }
    if(fla[u]) upd(rt[u],wi[u],0);
    else upd(rt[u],1,wi[u]);
}

int main() {
    // freopen("rand.txt","r",stdin);
    // freopen("2.txt","w",stdout);
    n=read(),m=read();
    F(i,1,n) de[i]=read();
    F(i,1,n-1) {
        int father=read();
        V[father].push_back(i+1);
        fla[i+1]=read(); wi[i+1]=read();
    }
    F(i,1,m) {
        po[i]=read();
        bl[i]=read();mul[i]=1;
        rt[bl[i]]=merge(rt[bl[i]],i);
    }
    dep[1]=1;
    dfs(1);
    F(i,1,n) printf("%lld\n",ans1[i]);
    F(i,1,m) {
        if(po[i]!=-1) ans2[i]=dep[bl[i]];
        printf("%lld\n",ans2[i]);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Menteur-Hxy/p/9358318.html