HDU 4031 Attack (tree array interval Update single point evaluation & violence)

Source: Internet
Author: User
Tags cas

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
Recommendlcy | We have carefully selected several similar problems for you:4035 4037 4036 4033 4038Test Instructions:
A wall with a length of n (20000 maximum). There is one protective cover per unit length. Each shield can withstand a single attack. But the cooldown of T is the ability to withstand the next attack. Want to ask in the Q Group.

Attack [L,r]. Or ask the number of times a unit wall has been successfully attacked.

Ideas:the total number of times the wall was attacked with a tree-like array. Only the interval is involved and a value is reduced.

and query a point value. There are two ways to do this. Then the number of successful attacks on a wall is equal to the total number of attacks-the number of attacks being resisted. The algorithm for the number of attacks being resisted is almost violent.

。 Anyway, the analysis of the complexity of the time will be super.

But there is no such data.

See the code for details:version Number one
#include <algorithm> #include <iostream> #include <string.h> #include <stdio.h>using namespace std;const int Inf=0x3f3f3f3f;const int maxn=100010;typedef long 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%d%d", &AMP;N,&AMP;Q,&AMP;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);//And lineChishu similar. Add a lazy.            When it's time to count, keep up with your father. } else {for (j=head[a];j<ct;j++) if (attack[j][0]<=a&&amp                ; attack[j][1]>=a) num[a]++,head[a]=j+tm,j+=tm-1;            printf ("%d\n", Getsum (a)-num[a]); }}} return 0;}

Version number two:
#include <algorithm> #include <iostream> #include <string.h> #include <stdio.h>using namespace std;const int Inf=0x3f3f3f3f;const int maxn=100010;typedef long 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%d%d", &AMP;N,&AMP;Q,&AMP;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 (a,1);//Use the marking method.

The prefix and is the value of the current point. 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 adds 1 to the For loop, it can only add tm-1 printf ( "%d\n", Getsum (a)-num[a]); }}} return 0;}



HDU 4031 Attack (tree array interval Update single point evaluation &amp; violence)

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.