zoukankan      html  css  js  c++  java
  • BZOJ4003: [JLOI2015]城池攻占

    4003: [JLOI2015]城池攻占

    Time Limit: 20 Sec  Memory Limit: 128 MB
    Submit: 1356  Solved: 510
    [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 <= fi<i; 1 <= ci <= n; -10^18 <= hi,vi,si <= 10^18;ai等于1或者2;当 ai =1 时,vi > 0;保证任何时候骑士战斗力值的绝对值不超过 10^18。


    Source

    【题解】

    吉林省选好水啊,这题让我一眼秒了(逃)

    但是。。

    调不出来的呀!

    没有用longlong的呀!

    用了longlong没用lld的呀!

    用了longlong,INF设置小了呀!

    了呀!

    呀!!

    呀!!!!

    这点破错废了我一个下午的呀!

    太裸了不想写题解,价格标记就好

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstdlib>
      4 #include <cstring>
      5 
      6 inline void swap(long long &a, long long &b)
      7 {
      8     long long tmp = a;
      9     a = b;b = tmp;
     10 }
     11 
     12 inline void read(long long &x)
     13 {
     14     x = 0;char ch = getchar(), c = ch;
     15     while(ch < '0' || ch > '9')c = ch, ch = getchar();
     16     while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar();
     17     if(c == '-')x = -x;
     18 }
     19 
     20 const long long MAXN = 310000 + 10;
     21 const long long MAXM = 310000 + 10;
     22 const long long INF = 0x3f3f3f3f3f3f3f3f;
     23 
     24 long long n,m,h[MAXN],fa[MAXN],a[MAXN],v[MAXN],root[MAXN];
     25 
     26 long long ans;
     27 
     28 struct Heap
     29 {
     30     long long l,r,fa,dist,s,add,mul,lazy,ans;
     31 }heap[MAXM];
     32 
     33 inline void pushdown(long long a)
     34 {
     35     long long l = heap[a].l, r = heap[a].r;
     36     if(heap[a].lazy)
     37     {
     38         heap[l].lazy += heap[a].lazy;
     39         heap[l].ans += heap[a].lazy;
     40         heap[r].lazy += heap[a].lazy;
     41         heap[r].ans += heap[a].lazy;
     42     }
     43     if(heap[a].mul > 1)
     44     {
     45         heap[l].s *= heap[a].mul;
     46         heap[l].add *= heap[a].mul;
     47         heap[l].mul *=  heap[a].mul;
     48         heap[r].s *= heap[a].mul;
     49         heap[r].add *= heap[a].mul;
     50         heap[r].mul *=  heap[a].mul;
     51     }
     52     if(heap[a].add)
     53     {
     54         heap[l].s += heap[a].add;
     55         heap[l].add += heap[a].add;
     56         heap[r].s += heap[a].add;
     57         heap[r].add += heap[a].add;
     58     }
     59     heap[a].add = heap[a].lazy = 0;
     60     heap[a].mul = 1;
     61 }
     62 
     63 long long merge(long long a, long long b)
     64 {
     65     if(!a)return b;
     66     if(!b)return a;
     67     if(heap[a].s > heap[b].s)swap(a, b);
     68     pushdown(a);pushdown(b);
     69     heap[a].r = merge(heap[a].r, b);
     70     heap[heap[a].r].fa = a;
     71     if(heap[heap[a].l].dist < heap[heap[a].r].dist)swap(heap[a].l, heap[a].r);
     72     if(heap[a].r == 0)heap[a].dist = 0;
     73     else heap[a].dist = heap[heap[a].r].dist + 1;
     74     return a;
     75 }
     76 
     77 long long pop(long long a)
     78 {
     79     pushdown(a);
     80     long long l = heap[a].l, r = heap[a].r;
     81     heap[a].l = heap[a].r = heap[a].dist = heap[l].fa = heap[r].fa = 0;
     82     heap[a].s = INF;
     83     return merge(l, r);
     84 }
     85 
     86 struct Edge
     87 {
     88     long long u,v,next;
     89     Edge(long long _u, long long _v, long long _next){u = _u;v = _v;next = _next;}
     90     Edge(){}
     91 }edge[MAXN];
     92 
     93 long long head[MAXN], cnt;
     94 
     95 void insert(long long a, long long b)
     96 {
     97     edge[++cnt] = Edge(a,b,head[a]);
     98     head[a] = cnt;
     99 }
    100 
    101 long long ansqi[MAXN];
    102 
    103 void dfs(long long u)
    104 {
    105     for(register long long pos = head[u];pos;pos = edge[pos].next)
    106     {
    107         dfs(edge[pos].v);
    108         root[u] = merge(root[u], root[edge[pos].v]);
    109     }
    110     while(heap[root[u]].s < h[u] && root[u])
    111     {
    112         ++ ansqi[u];
    113         root[u] = pop(root[u]);
    114     }
    115     ++ heap[root[u]].lazy;
    116     ++ heap[root[u]].ans;  
    117     if(!root[u])return;
    118     if(a[u])
    119     {
    120         heap[root[u]].s *= v[u];
    121         heap[root[u]].add *= v[u];
    122         heap[root[u]].mul *= v[u];
    123     }
    124     else
    125     {
    126         heap[root[u]].s += v[u];
    127         heap[root[u]].add += v[u];
    128     }
    129 }
    130 
    131 void dfs2(long long u)
    132 {
    133     if(!u)return;
    134     pushdown(u);
    135     dfs2(heap[u].l);
    136     dfs2(heap[u].r);
    137 }
    138 
    139 int main()
    140 {
    141     read(n), read(m);
    142     for(register long long i = 1;i <= n;++ i)
    143         read(h[i]);
    144     for(register long long i = 2;i <= n;++ i)
    145     {
    146         read(fa[i]), read(a[i]), read(v[i]);
    147         insert(fa[i], i);
    148     }
    149     register long long tmp;
    150     heap[0].s = INF;
    151     for(register long long i = 1;i <= m;++ i)
    152     {
    153         read(heap[i].s), read(tmp);
    154         heap[i].mul = 1;
    155         if(!root[tmp])root[tmp] = i;
    156         else root[tmp] = merge(root[tmp], i);
    157     }
    158     dfs(1);
    159     for(register long long i = 1;i <= n;++ i)printf("%lld
    ", ansqi[i]);
    160     dfs2(root[1]);
    161     for(register long long i = 1;i <= m;++ i)printf("%lld
    ", heap[i].ans);
    162     return 0;
    163 } 
    BZOJ4003
  • 相关阅读:
    jquery 筛选元素(1)
    jquery操作元素的位置
    jquery 操作css 选择器
    jquery 操作css 尺寸
    jquery 标签中的属性操作
    jquery基本选择器
    jquery表单属性筛选元素
    jquery属性值选择器
    jquery 层级选择器
    jquery的基本选择器
  • 原文地址:https://www.cnblogs.com/huibixiaoxing/p/7481371.html
Copyright © 2011-2022 走看看