HDU 4031 attack (tree array Interval Update Single Point evaluation & brute force)

Source: Internet
Author: User
Attack Time 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 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, random Sive. 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.
 
Inputthe 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, random Sive. 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
 
Outputfor 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
23 7 2Attack 1 2Query 2Attack 2 3Query 2Attack 1 3Query 1Query 39 7 3Attack 5 5Attack 4 6Attack 3 7Attack 2 8Attack 1 9Query 5Query 3
 
Sample output
Case 1:0101Case 2:32
 
Sourcethe 36th ACM/ICPC Asia Regional Chengdu site -- online contest
Recommendlcy | we have carefully selected several similar problems for you: 4035 4037 4036 4033 question:
A wall with a length of N (maximum of 20000. Each unit has a protective cover. Each shield can defend against an attack. However, it takes t's cool-down time to resist the next attack. Ask in the Q group. Attack [L, R]. Or ask the number of successful attacks on a wall. Idea: Use a tree array to maintain the total number of times a wall is attacked. It only involves adding or subtracting one value from the range. And query a vertex value. This can be written in two ways. Then, the number of successful attacks on a wall is equal to the total number of attacks-the number of blocked attacks. The algorithms for the number of blocked attacks are almost violent .. In any case, the analysis time complexity is too high. But there is no such data. For details, see code: Version 1.
# Include <algorithm> # include <iostream> # include <string. h> # include <stdio. h> using namespace STD; const int INF = 0x3f3f3f3f; const int maxn = 100010; typedef long ll; int C [maxn], n, attack [maxn] [2], head [maxn], num [maxn]; int lowbit (int x) {return X &-X;} void Update (int x, int D) {for (INT I = x; I> 0; I-= lowbit (I) c [I] + = D;} int getsum (int x) {int I, sum = 0; for (I = x; I <= N; I + = lowbit (I) sum + = C [I]; return sum;} int main (){ Int t, q, TM, I, j, CT, CAS = 1, a, B; char cmd [20]; scanf ("% d", & T ); while (t --) {scanf ("% d", & N, & Q, & TM); for (I = 1; I <= N; I ++) C [I] = num [I] = 0, head [I] = 0; Ct = 0; printf ("case % d: \ n ", CAS ++); While (Q --) {scanf ("% S % d", CMD, & A); If (CMD [0] = 'A ') {scanf ("% d", & B); attack [cT] [0] = A; attack [cT] [1] = B; CT ++; Update (B, 1); Update (A-1,-1); // similar to line tree. add a lazy. When the calculation is to be done, it will be accumulated by the father.} Else {for (j = head [a]; j <CT; j ++) if (attack [J] [0] <= A & attack [J] [1]> = A) num [a] ++, head [a] = J + TM, J + = TM-1; printf ("% d \ n", getsum (a)-num [a]) ;}} return 0 ;}

Version 2:
# Include <algorithm> # include <iostream> # include <string. h> # include <stdio. h> using namespace STD; const int INF = 0x3f3f3f3f; const int maxn = 100010; typedef long ll; int C [maxn], n, attack [maxn] [2], head [maxn], num [maxn]; int lowbit (int x) {return X &-X;} void Update (int x, int D) {for (INT I = x; I <= N; I + = lowbit (I) c [I] + = D;} int getsum (int x) {int I, sum = 0; for (I = x; I> 0; I-= lowbit (I) sum + = C [I]; return sum;} int main (){ Int t, q, TM, I, j, CT, CAS = 1, a, B; char cmd [20]; scanf ("% d", & T ); while (t --) {scanf ("% d", & N, & Q, & TM); for (I = 1; I <= N; I ++) C [I] = num [I] = head [I] = 0; Ct = 0; printf ("case % d: \ n ", CAS ++); While (Q --) {scanf ("% S % d", CMD, & A); If (CMD [0] = 'A ') {scanf ("% d", & B); attack [cT] [0] = A; attack [cT] [1] = B; CT ++; update (, 1); // use the mark method. The prefix and the value of the current vertex. Update (B + 1,-1);} else {for (j = head [a]; j <CT; j ++) if (attack [J] [0] <= A & attack [J] [1]> = A) num [a] ++, head [a] = J + TM, J + = TM-1; // because J has to add 1 in the for loop, only TM-1 printf ("% d \ n ", getsum (a)-num [a]) ;}} return 0 ;}


HDU 4031 attack (tree array Interval Update Single Point evaluation & brute force)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.