【BZOJ 4003】 [JLOI2015]城池攻佔

4003: [JLOI2015]城池攻佔

Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 206 Solved: 89
[Submit][Status][Discuss]
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 <= ci <= n; -10^18 <= hi,vi,si <= 10^18;ai等於1或者2;當 ai =1 時,vi > 0;保證任何時候騎士戰鬥力值的絕對值不超過 10^18。

思路題+左偏樹+標記傳遞。

可以發現修改操作並不會改變騎士戰鬥力之間的大小關係。

那麼我們可以從葉子開始處理,對每一個節點合併他的兒子以及從他開始出發的騎士(建立小根堆),剔除掉戰鬥力 這個節點防禦值的人(死亡);然後打上標記表示經過這個節點之後的戰鬥力變化情況。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#define pb push_back
#define M 300005
#define LL long long
using namespace std;
int C;
struct qishi
{
    int ans;
    LL s;
}q[M];
struct cq
{
    int f,a,ans,root;
    LL h,v;
}c[M];
int n,m,tot=0,h[M];
struct edge
{
    int y,ne;
}e[M*3];
struct Ltree
{
    LL add,mul,v;
    int aa,ans,l,r,dis,root,id;
}a[M];
void read(int &tmp)
{
    tmp=0;
    char ch=getchar();
    int fu=1;
    for (;ch<'0'||ch>'9';ch=getchar())
        if (ch=='-') fu=-1;
    for (;ch>='0'&&ch<='9';ch=getchar())
        tmp=tmp*10+ch-'0';
    tmp*=fu;
}
void Read(LL &tmp)
{
    tmp=0;
    char ch=getchar();
    LL fu=1;
    for (;ch<'0'||ch>'9';ch=getchar())
        if (ch=='-') fu=-1;
    for (;ch>='0'&&ch<='9';ch=getchar())
        tmp=tmp*10LL+ch-'0';
    tmp*=fu;
}
void Addedge(int x,int y)
{
    e[++tot].y=y;
    e[tot].ne=h[x];
    h[x]=tot;
}
void Modify(int x,int aa,LL m,LL d)
{
    if (!aa&&!d&&m==1) return;
    a[x].aa+=aa,a[x].ans+=aa;
    a[x].v=a[x].v*m+d;
    a[x].add=a[x].add*m+d;
    a[x].mul*=m;
}
void Push_down(int x)
{
    int l=a[x].l,r=a[x].r;
    Modify(l,a[x].aa,a[x].mul,a[x].add);
    Modify(r,a[x].aa,a[x].mul,a[x].add);
    a[x].aa=a[x].add=0,a[x].mul=1;
}
int Merge(int x,int y)
{
    if (!x||!y) return x+y;
    Push_down(x),Push_down(y);
    if (a[x].v>a[y].v)
        swap(x,y);
    a[x].r=Merge(a[x].r,y);
    if (a[a[x].l].dis<a[a[x].r].dis)
        swap(a[x].l,a[x].r);
    a[x].dis=a[a[x].r].dis+1;
    return x;
}
int Del(int x)
{
    return Merge(a[x].l,a[x].r);
}
void dfs(int x,int fa)
{
    for (int i=h[x];i;i=e[i].ne)
    {
        int y=e[i].y;
        dfs(y,x);
        c[x].root=Merge(c[x].root,c[y].root);
    }
    while (c[x].root)
    {
        Push_down(c[x].root);
        if (a[c[x].root].v<c[x].h)
        {
            c[x].ans++;
            q[a[c[x].root].id].ans=a[c[x].root].ans;
            c[x].root=Del(c[x].root);
        }
        else break;
    }
    int r=c[x].root;
    if (c[x].a) a[r].v*=c[x].v,a[r].mul*=c[x].v,a[r].add*=c[x].v;
    else a[r].v+=c[x].v,a[r].add+=c[x].v;
    a[r].ans++,a[r].aa++;
}
int main()
{
    read(n),read(m);
    for (int i=1;i<=n;i++)
        Read(c[i].h);
    for (int i=2;i<=n;i++)
    {
        read(c[i].f),read(c[i].a),Read(c[i].v);
        Addedge(c[i].f,i);
    }
    for (int i=1;i<=m;i++)
    {
        Read(q[i].s),read(C);
        a[i].root=i,a[i].v=q[i].s,a[i].mul=1,a[i].add=0;
        a[i].aa=0,a[i].ans=0,a[i].id=i;
        if (c[C].root) c[C].root=Merge(c[C].root,i);
        else c[C].root=i;
    }
    dfs(1,0);
    for (int i=1;i<=n;i++)
        printf("%d\n",c[i].ans);
    while (c[1].root)
    {
        Push_down(c[1].root);
        q[a[c[1].root].id].ans=a[c[1].root].ans;
        c[1].root=Del(c[1].root);
    }
    for (int i=1;i<=m;i++)
        printf("%d\n",q[i].ans);
    /*if (n<=3000&&m<=3000)  下面是暴力
    {
        for (int i=1;i<=m;i++)
        {
            int now=q[i].c;
            while (now)
            {
                if (q[i].s<c[now].h)
                {
                    c[now].ans++;
                    break;
                }
                else 
                {
                    if (c[now].a) q[i].s*=c[now].v;
                    else q[i].s+=c[now].v;
                    now=c[now].f;
                    q[i].ans++;
                }
            }
        }
        for (int i=1;i<=n;i++)
            printf("%d\n",c[i].ans);
        for (int i=1;i<=m;i++)
            printf("%d\n",q[i].ans);
        return 0;
    }*/
    return 0;
}

這裏寫圖片描述

發佈了315 篇原創文章 · 獲贊 26 · 訪問量 37萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章