Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=4031
AttackTime
limit:5000/3000 MS (java/others) Memory limit:65768/65768 K (java/others)
Total submission (s): 1890 Accepted Submission (s): 554
Problem Descriptiontoday is the 10th annual of "September one attacks", the Al Qaeda is about to attack American again. However, American is protected by a high-wall this time, which can-treating as a segment with length N. Al Qaeda have 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, Inc. Lusive. The shield would defend automatically when it was under attack if it was ready.
During the war, it's 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 are not defended by the shield.
Inputthe beginning of the data is a integer T (t≤20), the number of the test case.
The first line of all test case was three integers, N, Q, T, the length of the wall, the number of attacks and queries, an d 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 has 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
Outputfor The ith case, output one line ' case I: ' At first. Then to each query, output one line containing one integer, the number of time the PTH unit is successfully attacked whe N asked.
Sample Input
7 2Attack 1 2Query 2Attack 2 3Query 2Attack 1 3Query 1Query 7 3Attack 5 5Attack 4 6Attack 3 7Attack 2 8Attack 1 9Que Ry 5Query 3
Sample Output
Case 1:0101case 2:32
Sourcethe 36th ACM/ICPC Asia Regional Chengdu Site--online Contest Thought: This problem is good!. Segment Update for tree arrays + single-point query successfully attacked=attack-defend; & nbsp The total attack is summed by a tree-like array. As for the number of defend to find out ~
#include <iostream> #include <stdio.h> #include <string.h> #include <string> #include < cstdio> #include <cmath>const int n=20200;using namespace Std;int c[n],n,m,t,t;int shield[n],pos[n];struct node{int l,r;} Attack[n];int lowbit (int x) {return x& (-X);} void Update (int x,int d) {while (x<=n) {c[x]+=d; X+=lowbit (x); }}int getsum (int x) {int ans=0; while (x>0) {ans+=c[x]; X-=lowbit (x); } return ans; void Init () {scanf ("%d%d%d", &n,&m,&t); Memset (C,0,sizeof (c)); memset (shield,0,sizeof (shield)); memset (Pos,0,sizeof (POS));} int main () {char s[10]; int test=1; scanf ("%d", &t); while (t--) {Init (); int cnt=0; printf ("Case%d:\n", test++); while (m--) {scanf ("%s", s); if (s[0]== ' A ') {cnt++; int si,ti; scanf ("%d%d", &si,&ti); Attack[cnt].l=si; Attack[cnt].r=ti; Update (si,1); Update (TI+1,-1); } else {int A; scanf ("%d", &a); for (int i=pos[a];i<=cnt;) {if (A>=ATTACK[I].L&&A<=ATTACK[I].R) {pos[a]=i+t; shield[a]++; I=i+t; } else i++; } printf ("%d\n", Getsum (a)-shield[a]); }}} return 0;}
Hdu 4031 (tree array + auxiliary array)