HDU 4031 Attack

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   io   os   java   ar   

AttackTime Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 1904    Accepted Submission(s): 560


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, 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.
 
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, 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
 
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解題思路:被攻擊次數=總攻擊次數-防禦成功次數,總攻擊次數用線段樹或者樹狀數組,防禦次數暴力更新,pre數組記錄上次成功防禦的時間點
#include<iostream>#include<cstdio>#include<cstring>#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define Max 20005int N,Q,t;struct{    int atk;    int cover;}tree[Max<<2];void push_up(int rt){    tree[rt].atk=tree[rt<<1].atk+tree[rt<<1|1].atk;}void push_down(int rt,int m){    if(tree[rt].cover)    {        tree[rt<<1].cover+=tree[rt].cover;        tree[rt<<1|1].cover+=tree[rt].cover;        tree[rt<<1].atk+=(m-(m>>1))*tree[rt].cover;        tree[rt<<1|1].atk+=(m>>1)*tree[rt].cover;        tree[rt].cover=0;    }}void update(int L,int R,int l,int r,int rt){    if(L<=l&&R>=r)    {        tree[rt].cover++;        tree[rt].atk+=r-l+1;        return ;    }    push_down(rt,r-l+1);    int m=(l+r)>>1;    if(L<=m)        update(L,R,lson);    if(R>m)        update(L,R,rson);    push_up(rt);}int query(int pos,int l,int r,int rt){    if(l==r)        return tree[rt].atk;    push_down(rt,r-l+1);    int m=(l+r)>>1;    if(pos<=m)        return query(pos,lson);    else        return query(pos,rson);}int main(){    int i,T,time,a,b,atk[Max][2],pre[Max],def[Max],ncase=1;    char op[10];    scanf("%d",&T);    while(T--)    {        time=0;        memset(tree,0,sizeof(tree));        scanf("%d%d%d",&N,&Q,&t);        memset(atk,0,sizeof(atk));        memset(def,0,sizeof(def));        for(i=1;i<=N;i++)            pre[i]=1;        printf("Case %d:\n",ncase++);        while(Q--)        {            scanf("%s",op);            if(op[0]=='A')            {                scanf("%d%d",&a,&b);                atk[++time][0]=a;atk[time][1]=b;                update(a,b,1,N,1);            }            else            {                scanf("%d",&a);                if(t==0)                {                    printf("0\n");                    continue;                }                for(i=pre[a];i<=time;i++)                {                    if(atk[i][0]<=a&&atk[i][1]>=a)                    {                        def[a]++;                        pre[a]=i+t;                        i+=t-1;                    }                }                printf("%d\n",query(a,1,N,1)-def[a]);            }        }    }    return 0;}


 

HDU 4031 Attack

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.