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 “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

思路:这题出的好!。树状数组的成段更新+单点查询

successfully attacked=attack-defend;

总的attack 由树状数组求和就可以。至于defend数得想办法求出~

#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;
}
时间: 2024-11-08 20:19:53

hdu 4031(树状数组+辅助数组)的相关文章

HDU 1394 树状数组(逆序数)

Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10100    Accepted Submission(s): 6192 Problem Description The inversion number of a given number sequence a1, a2, ..., an

hdu 3333 树状数组+离线处理

http://acm.hdu.edu.cn/showproblem.php?pid=3333 不错的题,想了很久不知道怎么处理,而且答案没看懂,然后找个例子模拟下别人的代码马上懂了---以后看不懂的话就拿个例子模拟下别人的代码 举个例子:1 3 3 5 3 5 查询 a, 2 4 b, 2 5 最初是这么想的:对于a查询,倘若把第二个数第三个数变成1个3,那么到b查询,又出现了两个3,再做处理似乎还是O(n),而且如果先出现2,5查询,后出现2,4查询,那么还需要把删除的数补回来.....o(╯

Hdu 3887树状数组+模拟栈

题目链接 Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1757    Accepted Submission(s): 582 Problem Description You are given a tree, it’s root is p, and the node is numbered fr

HDU 1754 树状数组 解法

mnesia在频繁操作数据的过程可能会报错:** WARNING ** Mnesia is overloaded: {dump_log, write_threshold},可以看出,mnesia应该是过载了.这个警告在mnesia dump操作会发生这个问题,表类型为disc_only_copies .disc_copies都可能会发生. 如何重现这个问题,例子的场景是多个进程同时在不断地mnesia:dirty_write/2 mnesia过载分析 1.抛出警告是在mnesia 增加dump

HDU 1166(树状数组)

用树状数组把HDU1166再写了一次  感觉树状数组简洁 1 #include <cstdio> 2 #include <iostream> 3 #include <string.h> 4 using namespace std; 5 int c[50002],lv[50002],n; 6 int lowbit(int x){return x&(-x);} 7 int sum(int b){ 8 int sum=0; 9 while(b>0){ 10 su

hdu 4368 树状数组 离线维护

http://acm.hdu.edu.cn/showproblem.php?pid=4638 Problem Description There are n men ,every man has an ID(1..n).their ID is unique. Whose ID is i and i-1 are friends, Whose ID is i and i+1 are friends. These n men stand in line. Now we select an interv

hdu 4630 树状数组+离线操作+GCD

http://acm.hdu.edu.cn/showproblem.php?pid=4630 重新认识了树状数组. 首先要记住那个树形的图,然后+或-lowbit(i)是自己根据具体问题设定的,不要死于+或者-, 树状数组的特点: 1.+lowbit(i)可以到达包含结点i的上一层父节点    所以用于值的更改 2.-lowbit(i)可以到达不包含i所代表区间的上一层父节点  所以用于值的求和---每个不相交的段加起来 3.C[i]的含义也是根据具体问题去做设定的,但是c[i]覆盖了a[i-2

hdu 4970 树状数组区间更新 思维题

http://acm.hdu.edu.cn/showproblem.php?pid=4970 好像还没有用树状数组写过区间更新,但是树状数组的确比线段树快很多,不知道跟ZKW线段树比效率怎么样: 先贴个模板: #include <cstdio> const int MAXN = 1024; int B[MAXN], C[MAXN]; #define LOWBIT(x) ((x)&(-(x))) void bit_update(int *a, int p, int d) { for (

hdu 2492(树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Ping pong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4011    Accepted Submission(s): 1482 Problem Description N(3<=N<=20000) ping pong