hdu 4107

Gangster

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3123    Accepted Submission(s): 762

Problem Description

There
are two groups of gangsters fighting with each other. The first group
stands in a line, but the other group has a magic gun that can shoot a
range [a, b], and everyone in that range will take a damage of c points.
When a gangster is taking damage, if he has already taken at least P
point of damage, then the damage will be doubled. You are required to
calculate the damage that each gangster in the first group toke.
To
simplify the problem, you are given an array A of length N and a magic
number P. Initially, all the elements in this array are 0.
Now, you
have to perform a sequence of operation. Each operation is represented
as (a, b, c), which means: For each A[i] (a <= i <= b), if A[i]
< P, then A[i] will be A[i] + c, else A[i] will be A[i] + c * 2.
Compute all the elements in this array when all the operations finish.

Input

The input consists several testcases.
The
first line contains three integers n, m, P (1 <= n, m, P <=
200000), denoting the size of the array, the number of operations and
the magic number.
Next m lines represent the operations. Each
operation consists of three integers a; b and c (1 <= a <= b <=
n, 1 <= c <= 20).

Output

Print A[1] to A[n] in one line. All the numbers are separated by a space.

Sample Input

3 2 1
1 2 1
2 3 1

Sample Output

1 3 1

Source

2011 Alibaba-Cup Campus Contest

Recommend

lcy   |   We have carefully selected several similar problems for you:  4104 4102 4103 4105 4106

题意不想说 ,妈的 什么破题目 ,一样的代码第一次交就过,第二次就wa

时间卡,这段时间杭电也卡,我快晕了

线段树,用c++交

  1 #include<iostream>
  2 #include<cstdio>
  3 using namespace std;
  4 #define N 200004
  5 int  n,m,p;
  6 struct node
  7 {
  8     int left,right;
  9     int max,min;
 10     int num;
 11 }tree[N*4];
 12 void build(int l,int r,int pos)
 13 {
 14     int mid=(l+r)>>1;
 15     tree[pos].left=l;
 16     tree[pos].right=r;
 17     tree[pos].max=0;
 18     tree[pos].min=0;
 19     tree[pos].num=0;
 20     if(l==r)
 21     {
 22         return ;
 23     }
 24     build(l,mid,pos<<1);
 25     build(mid+1,r,pos<<1|1);
 26 }
 27 void update(int l,int r,int pos,int v)
 28 {
 29     int mid=(tree[pos].left+tree[pos].right)>>1;
 30     if(tree[pos].left==l&&tree[pos].right==r)
 31     {
 32         if(tree[pos].min>=p)
 33         {
 34             tree[pos].min+=v<<1;
 35             tree[pos].max+=v<<1;
 36             tree[pos].num+=v<<1;
 37             return ;
 38         }
 39         if(tree[pos].max<p)
 40         {
 41             tree[pos].min+=v;
 42             tree[pos].max+=v;
 43             tree[pos].num+=v;
 44             return ;
 45         }
 46     }
 47     if(tree[pos].num!=0)
 48     {
 49         tree[pos<<1].num+=tree[pos].num;
 50         tree[pos<<1].min+=tree[pos].num;
 51         tree[pos<<1].max+=tree[pos].num;
 52         tree[pos<<1|1].num+=tree[pos].num;
 53         tree[pos<<1|1].min+=tree[pos].num;
 54         tree[pos<<1|1].max+=tree[pos].num;
 55         tree[pos].num=0;
 56     }
 57     if(r<=mid)
 58         update(l,r,pos<<1,v);
 59     else if(l>mid)
 60         update(l,r,pos<<1|1,v);
 61     else
 62     {
 63         update(l,mid,pos<<1,v);
 64         update(mid+1,r,pos<<1|1,v);
 65     }
 66     if(tree[pos*2].max>tree[pos<<1|1].max)
 67         tree[pos].max=tree[pos<<1].max;
 68     else
 69         tree[pos].max=tree[pos<<1|1].max;
 70     if(tree[pos*2].min>tree[pos<<1|1].min)
 71         tree[pos].min=tree[pos<<1|1].min;
 72     else
 73         tree[pos].min=tree[pos<<1].min;
 74 }
 75 void query(int pos)
 76 {
 77     if(tree[pos].left==tree[pos].right)
 78     {
 79         if(tree[pos].left!=1)
 80             printf(" ");
 81         printf("%d",tree[pos].num);
 82         return;
 83     }
 84     if(tree[pos].num!=0)
 85     {
 86         tree[pos<<1].num+=tree[pos].num;
 87         tree[pos<<1|1].num+=tree[pos].num;
 88         tree[pos].num=0;
 89     }
 90     query(pos<<1);
 91     query(pos<<1|1);
 92 }
 93 int main()
 94 {
 95     while(scanf("%d%d%d",&n,&m,&p)>0)
 96     {
 97         build(1,n,1);
 98         while(m--)
 99         {
100             int a,b,c;
101             scanf("%d%d%d",&a,&b,&c);
102             update(a,b,1,c);
103         }
104         query(1);
105         printf("\n");
106     }
107     return 0;
108 }
时间: 2024-07-31 14:33:48

hdu 4107的相关文章

HDU 4107 Gangster Segment Tree线段树

这道题也有点新意,就是需要记录最小值段和最大值段,然后成段更新这个段,而不用没点去更新,达到提高速度的目的. 本题过的人很少,因为大部分都超时了,我严格按照线段树的方法去写,一开始居然也超时. 然后修补了两个地方就过了,具体修改的地方请参看程序. 知道最大值段和最小值段,然后修补一下就能过了.不是特别难的题目. #include <stdio.h> #include <string> #include <algorithm> using namespace std; c

hdu 4107卡时线段树

核心思想就是节点上记录最大值和最小值,如果max<p或min>=p时,只在节点改变add值,不用往子树遍历:否则就往子树进行递归. #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<vector> using namespace std; const int maxn = 2

hdu 4107 Gangster 线段树(成段更新)

维护每个区间的最小值和最大值,update的时候判断low[rt]与up[rt]和p的大小关系,进行更新操作. 卡时卡得很紧. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 usin

hdu 4107当卡段树

其核心思想是记录最大的节点值和最低值,假设max<p要么min>=p时间,在节点只变化add值,不要子树遍历:否则,就往子树递归. #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<vector> using namespace std; const int maxn = 2

HDU 4107 线段树

给出N个节点,M次操作,和p 每次操作 对l-r区间的每一个节点+c,若节点值>=p,则加2*c: 结点存当前区间伤害最小值,最大值,以及lazy操作.更新到假设最小值大于等于P,或者最大值小于P为止. #include "stdio.h" #include "string.h" struct node { int l,r,Min,Max,lazy; } data[800010]; int p; int Max(int a,int b) { if (a<

转载:hdu 题目分类 (侵删)

转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029. 1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093. 1094.1095.1096.1097.1098.1106.1108.1157.116

HDU 6203 ping ping ping [LCA,贪心,DFS序,BIT(树状数组)]

题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=6203] 题意 :给出一棵树,如果(a,b)路径上有坏点,那么(a,b)之间不联通,给出一些不联通的点对,然后判断最少有多少个坏点. 题解 :求每个点对的LCA,然后根据LCA的深度排序.从LCA最深的点对开始,如果a或者b点已经有点被标记了,那么continue,否者标记(a,b)LCA的子树每个顶点加1. #include<Bits/stdc++.h> using namespace std;

HDU 5542 The Battle of Chibi dp+树状数组

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5542 题意:给你n个数,求其中上升子序列长度为m的个数 可以考虑用dp[i][j]表示以a[i]结尾的长度为j的上升子序列有多少 裸的dp是o(n2m) 所以需要优化 我们可以发现dp的第3维是找比它小的数,那么就可以用树状数组来找 这样就可以降低复杂度 #include<iostream> #include<cstdio> #include<cstring> #include

hdu 1207 汉诺塔II (DP+递推)

汉诺塔II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4529    Accepted Submission(s): 2231 Problem Description 经典的汉诺塔问题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故.汉诺塔来源于印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往