Attack

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 2496    Accepted Submission(s): 788

Problem Description
Today is the 10th Annual of “September 11 attacks”, the Al Qaeda is about to attack American again. However, American is protected by a high wall this time, which can be treating as a segment with length N. Al Qaeda has a super weapon, every second it can attack a continuous range of the wall. American deployed N energy shield. Each one defends one unit length of the wall. However, after the shield defends one attack, it needs t seconds to cool down. If the shield defends an attack at kth second, it can’t defend any attack between (k+1)th second and (k+t-1)th second, inclusive. The shield will defend automatically when it is under attack if it is ready.

During the war, it is very important to understand the situation of both self and the enemy. So the commanders of American want to know how much time some part of the wall is successfully attacked. Successfully attacked means that the attack is not defended by the shield.

 
Input
The beginning of the data is an integer T (T ≤ 20), the number of test case.
The first line of each test case is three integers, N, Q, t, the length of the wall, the number of attacks and queries, and the time each shield needs to cool down.
The next Q lines each describe one attack or one query. It may be one of the following formats
1. Attack si ti
  Al Qaeda attack the wall from si to ti, inclusive. 1 ≤ si ≤ ti ≤ N
2. Query p
  How many times the pth unit have been successfully attacked. 1 ≤ p ≤ N
The kth attack happened at the kth second. Queries don’t take time.
1 ≤ N, Q ≤ 20000
1 ≤ t ≤ 50
 
Output
For the ith case, output one line “Case i: ” at first. Then for each query, output one line containing one integer, the number of time the pth unit was successfully attacked when asked.
 
Sample Input
2
3 7 2
Attack 1 2
Query 2
Attack 2 3
Query 2
Attack 1 3
Query 1
Query 3
9 7 3
Attack 5 5
Attack 4 6
Attack 3 7
Attack 2 8
Attack 1 9
Query 5
Query 3
 
Sample Output
Case 1:
0
1
0
1
Case 2:
3
2
 
  1. /*
  2. hdu 4031 attack 线段树区间更新
  3.  
  4. problem:
  5. 每个位置有一个防御塔,每次敌人会对一个区间进行攻击。防御塔有一个冷却时间t。
  6. 问某个时间时,某个位置被成功攻击的次数(没有被防御)
  7.  
  8. solve:
  9. 主要是怎么处理这个冷却值的问题,区间攻击都可以使用 线段树区间更新来解决。
  10. 最开始考虑的是通过 更新来维护每个点,那每次就要更新[1,n],感觉应该会超时吧
  11. 然后想到是 通过每次-1来体现时间的变化,但是无法知道每个点攻击成功的情况
  12.  
  13. 所以直接暴力搞了- -
  14. 通过线段树记录被攻击了多少次,然后通过记录可以知道防御成功了多少次
  15. 于是就能得出答案。
  16.  
  17. hhh-2016-08-05 21:16:55
  18. */
  19. #include <cstdio>
  20. #include <cstring>
  21. #include <iostream>
  22. #include <algorithm>
  23. #include <functional>
  24. #include <map>
  25. #include <queue>
  26. #include <vector>
  27. #include <set>
  28. #pragma comment(linker, "/STACK:1024000000,1024000000")
  29. #define lson (i<<1)
  30. #define rson ((i<<1)|1)
  31. using namespace std;
  32. typedef long long ll;
  33. const int maxn=100000 + 10;
  34. const int INF=0x3f3f3f3f;
  35. const int mod = 1e9+7;
  36. int n;
  37.  
  38. struct node
  39. {
  40. int l,r;
  41. int val;
  42. int len ;
  43. int mid()
  44. {
  45. return (l+r)>> 1;
  46. }
  47. } tree[maxn<<2];
  48.  
  49. void push_up(int i)
  50. {
  51. }
  52.  
  53. void build(int i,int l,int r)
  54. {
  55. tree[i].l = l;
  56. tree[i].r = r;
  57. tree[i].val = tree[i].len = 0;
  58. if(l == r)
  59. {
  60. return ;
  61. }
  62. int mid = tree[i].mid();
  63. build(lson,l,mid);
  64. build(rson,mid+1,r);
  65. push_up(i);
  66. }
  67.  
  68. void push_down(int i)
  69. {
  70. if(tree[i].val)
  71. {
  72. tree[lson].val += tree[i].val;
  73. tree[rson].val += tree[i].val;
  74. tree[i].val = 0;
  75. }
  76. }
  77.  
  78. void Insert(int i,int l,int r,int val)
  79. {
  80. if(tree[i].l >= l && tree[i].r <= r)
  81. {
  82. tree[i].val += val;
  83. push_up(i);
  84. return ;
  85. }
  86. push_down(i);
  87. int mid = tree[i].mid();
  88. if(l <= mid)
  89. Insert(lson,l,r,val);
  90. if(r > mid)
  91. Insert(rson,l,r,val);
  92. push_up(i);
  93. }
  94.  
  95. int query(int i,int k)
  96. {
  97. if(tree[i].l == tree[i].r && tree[i].l == k)
  98. {
  99. return tree[i].val;
  100. }
  101. push_down(i);
  102. int mid = tree[i].mid();
  103. if(k <= mid)
  104. return query(lson,k);
  105. else
  106. return query(rson,k);
  107. }
  108. struct Point
  109. {
  110. int l,r;
  111. Point()
  112. {
  113.  
  114. }
  115. Point(int a,int b)
  116. {
  117. l = a,r = b;
  118. }
  119. };
  120. Point pt[maxn];
  121. char op[5];
  122.  
  123. int main()
  124. {
  125. int T,n,m,k;
  126. int cas = 1;
  127. int a,b;
  128. //freopen("in.txt","r",stdin);
  129. scanf("%d",&T);
  130. while(T--)
  131. {
  132. scanf("%d%d%d",&n,&m,&k);
  133. int cnt = 0;
  134. build(1,1,n);
  135. printf("Case %d:\n",cas++);
  136. for(int i = 1;i <= m;i++)
  137. {
  138. scanf("%s",op);
  139. if(op[0] == 'Q')
  140. {
  141. int num = 0;
  142. scanf("%d",&a);
  143. for(int i = 0;i < cnt;)
  144. {
  145. if( a>= pt[i].l && a <= pt[i].r)
  146. {
  147. num++;
  148. i += k;
  149. }
  150. else
  151. {
  152. i++;
  153. }
  154. }
  155. printf("%d\n",query(1,a) - num);
  156. }
  157. else
  158. {
  159. scanf("%d%d",&a,&b);
  160. Insert(1,a,b,1);
  161. pt[cnt++] = Point(a,b);
  162. }
  163. }
  164. }
  165. return 0;
  166. }

  

hdu 4031 attack 线段树区间更新的更多相关文章

  1. hdu 4031 Attack 线段树

    题目链接 Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total ...

  2. HDU 5861 Road 线段树区间更新单点查询

    题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5861 Road Time Limit: 12000/6000 MS (Java/Othe ...

  3. HDU 3308 LCIS 线段树区间更新

    最近开始线段树一段时间了,也发现了不少大牛的博客比如HH大牛  ,小媛姐.这个题目是我在看HH大牛的线段树专题是给出的习题,(可以去他博客找找,真心推荐)原本例题是POJ3667 Hotel 这个题目 ...

  4. hdu 1698(线段树区间更新)

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  5. HDU 1556【线段树区间更新】

    这篇lazy讲的很棒: https://www.douban.com/note/273509745/ if(tree[rt].l == l && r == tree[rt].r) 这里 ...

  6. HDU 5023 A Corrupt Mayor's Performance Art(线段树区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 解题报告:一面墙长度为n,有N个单元,每个单元编号从1到n,墙的初始的颜色是2,一共有30种颜色 ...

  7. HDU 4902 Nice boat 2014杭电多校训练赛第四场F题(线段树区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4902 解题报告:输入一个序列,然后有q次操作,操作有两种,第一种是把区间 (l,r) 变成x,第二种是 ...

  8. hdu 3966(树链剖分+线段树区间更新)

    传送门:Problem 3966 https://www.cnblogs.com/violet-acmer/p/9711441.html 学习资料: [1]线段树区间更新:https://blog.c ...

  9. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

随机推荐

  1. python实现线性回归

    参考:<机器学习实战>- Machine Learning in Action 一. 必备的包 一般而言,这几个包是比较常见的: • matplotlib,用于绘图 • numpy,数组处 ...

  2. Alpha冲刺Day2

    Alpha冲刺Day2 一:站立式会议 今日安排: 首先完善前一天的剩余安排工作量,其次我们把项目大体分为四个模块:数据管理员.企业人员.第三方机构.政府人员.数据管理员这一模块,数据管理员又可细分为 ...

  3. 冲刺No.3

    Alpha冲刺第三天 站立式会议 项目进展 今日团队对CSS与JS的基础知识进行了应用,并对网站的UI设计进行了讨论,对数据库设计进行了进一步的探讨,基本确立了各个表单的结构和内容.分割出项目基本模块 ...

  4. centos7 下通过nginx+uwsgi部署django应用

    1. 安装python3.6 1. 获取 wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz tar -xzvf Python- ...

  5. 利用python实现简单邮件功能

    #!/usr/bin/env python # -*- coding:utf-8 -*- import smtplib from email.utils import formataddr from ...

  6. LR回放https协议脚本失败: 错误 -27778: 在尝试与主机“www.baidu.com”connect 时发生 SSL 协议错误

    今天用LR录制脚本协议为https协议,回放脚本时出现报错: Action.c(14): 错误 -27778: 在尝试与主机"www.baidu.com"connect 时发生 S ...

  7. 深入浅出 SSL 管理配置实战

    我们生活在一个信息大爆炸的时代,几乎每天都在和互联网打交道,购物.网银转账.支付宝付款.搜索信息.查看邮件.观看视频.微信聊天.上网冲浪.阅读新闻等,无不时时刻刻在和网络打交道.那如何保护网络安全就相 ...

  8. 用‘+=’拼接字符串,打印时总会出现一个undefined

    var str; for(var i = 0; i < 5; i++){ str += String(i); } console.log(str); 他喵的,打印的结果竟然是"unde ...

  9. 阿里云API网关(16)客户端请求的https支持

    网关指南: https://help.aliyun.com/document_detail/29487.html?spm=5176.doc48835.6.550.23Oqbl 网关控制台: https ...

  10. python入门(8)数据类型和变量

    python入门(8)数据类型和变量 数据类型 在Python中,能够直接处理的数据类型有以下几种: 整数 Python可以处理任意大小的整数,当然包括负整数,在程序中的表示方法和数学上的写法一模一样 ...