HDU-4031-Attack(树状数组)

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

Source

The 36th ACM/ICPC Asia Regional
Chengdu Site —— Online Contest

思路:成功攻击的次数=总次数-被防御的次数。用树状数组维护总次数,一个辅助数组记录当前点被攻击之后恢复的时间,另一个数组记录当前点的被防御次数。

#include <stdio.h>

int n,node[20005],ls[20005],rs[20005],pos[20005],c[20005];

int lowbit(int x)
{
    return x & -x;
}

int sum(int x)
{
    int res=0;

    while(x>0)
    {
        res+=node[x];

        x-=lowbit(x);
    }

    return res;
}

void add(int x,int val)
{
    while(x<=n)
    {
        node[x]+=val;

        x+=lowbit(x);
    }
}

int main()
{
    int T,q,t,i,a,b,cnt,cases=1;
    char s[10];

    scanf("%d",&T);

    while(T--)
    {
        scanf("%d%d%d",&n,&q,&t);

        cnt=0;

        for(i=0;i<=n;i++) c[i]=node[i]=pos[i]=0;

        printf("Case %d:\n",cases++);

        while(q--)
        {
            scanf("%s",s);

            if(s[0]=='A')
            {
                scanf("%d%d",&a,&b);

                ls[cnt]=a;
                rs[cnt]=b;

                cnt++;

                add(a,1);
                add(b+1,-1);
            }
            else
            {
                scanf("%d",&a);

                for(i=pos[a];i<cnt;i++)
                {
                    if(a>=ls[i] && a<=rs[i] && i>=pos[a])
                    {
                        pos[a]=i+t;
                        c[a]++;

                        i+=t-1;
                    }
                }

                printf("%d\n",sum(a)-c[a]);
            }
        }
    }
}
时间: 2024-11-03 21:35:53

HDU-4031-Attack(树状数组)的相关文章

hdu 4031 Attack(树状数组区间更新单点求值&amp;暴力)

Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 1890    Accepted Submission(s): 554 Problem Description Today is the 10th Annual of "September 11 attacks", the Al Qaeda is about to

hdu 4031 Attack(树状数组区间更新单点求值&amp;amp;暴力)

Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 1890    Accepted Submission(s): 554 Problem Description Today is the 10th Annual of "September 11 attacks", the Al Qaeda is about to

hdu 4031(树状数组+辅助数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4031 Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 1890    Accepted Submission(s): 554 Problem Description Today is the 10th Annual of "S

HDU Cow Sorting (树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 Cow Sorting Problem Description Sherlock's N (1 ≤ N ≤ 100,000) cows are lined up to be milked in the evening. Each cow has a unique "grumpiness" level in the range 1...100,000. Since grumpy cow

hdu 5193 分块 树状数组 逆序对

题意: 给出n个数,a1,a2,a3,...,an,给出m个修改,每个修改往数组的某个位置后面插入一个数,或者把某个位置上的数移除.求每次修改后逆序对的个数. 限制: 1 <= n,m <= 20000; 1 <= ai <= n 思路: 插入和删除用分块来处理,块与块之间用双向链表来维护,每一块用树状数组来求小于某个数的数有多少个. 外层可以使用分块维护下标,这样添加和删除元素的时候,也很方便,直接暴力.查找权值个数时,使用树状数组比较方便.内层通过树状数组维护权值. 每次更新即

HDU 5493 Queue 树状数组

Queue Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5493 Description N people numbered from 1 to N are waiting in a bank for service. They all stand in a queue, but the queue never moves. It is lunch time now,

hdu 4455 Substrings(树状数组+递推)

题目链接:hdu 4455 Substrings 题目大意:给定一个长度为N的序列,现在有Q次询问,每次给定一个w,表示长度,输出序列中长度为w的连续子序列 的权值和.序列的权值表示序列中不同元素的个数. 解题思路:递推,先预处理处每个位置和前面相同的数据的最短距离P.dp[i]表示说长度为i子序列的权值和,dp[i+1] = dp[i] + v - c.v为[i+1~N]中P值大于i的个数,我们可以看作将长度为i的子序列长度向后增加1,那么v则为增加长度带来 的权值增加值,c则是最后一个长度为

POJ 2352 &amp;&amp; HDU 1541 Stars (树状数组)

一开始想,总感觉是DP,可是最后什么都没想到.还暴力的交了一发. 然后开始写线段树,结果超时.感觉自己线段树的写法有问题.改天再写.先把树状数组的写法贴出来吧. ~~~~~~~~~~~~~~~~~~~~~~~~ 树状数组不懂的去看刘汝佳的大白书,那个图画得很清楚. 题目大意:星星的坐标以y递增的顺序给出,这些点的左下方的点数代表这个点的级数,问0~N-1的级数有多少个?其实y根本木有用. 题目链接:http://poj.org/problem?id=2352 http://acm.hdu.edu

HDU 2689Sort it 树状数组 逆序对

Sort it Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4110    Accepted Submission(s): 2920 Problem Description You want to processe a sequence of n distinct integers by swapping two adjacent s

HDU 2838 (DP+树状数组维护带权排序)

Reference: http://blog.csdn.net/me4546/article/details/6333225 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2838 题目大意:每头牛有个愤怒值,每次交换相邻两个数进行升序排序,$cost=val_{1}+val_{2}$,求$\min \sum cost_{i}$ 解题思路: 按输入顺序DP: 第i的值val的最小cost=当前数的逆序数个数*val+当前数的逆序数和 相当于每次只