本文主要是介绍HDU 4031 Attack(树状数组修改区间查询点),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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.
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
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
Case 1: 0 1 0 1 Case 2: 3 2
题意:
长度为n的城墙,进行q次询问,护盾冷却时间为cd,每次询问包含Attack和Query,Attack的时候是过一秒城墙l,r段被攻击,Query是询问输出城墙某一点到这个时间被攻击到的次数。
思路:
树状数组,通常树状数组是更新点询问区间,而这题是要更新区间询问点,对于更新区间,其实可以转化为更新点:对于区间[l,r],我们更新点l为正,
点r+1为负,这样一来等效于更新了区间。然后在询问点的时候,对于我们这个保存方式,保存的是城墙被攻击的次数,所以要转化一下,
被攻击到的次数=总攻击次数-成功保护的次数。每次询问到一个点的时候,记录下从开始到当前时间这个点一共成功保护了多少次。
代码:
#include <stdio.h>
#include <string.h>const int N = 20005;int t, n, q, cd, bit[N], pro[N], time, pt[N], rt[N], lt[N];void Add(int x, int v) {while (x <= n) {bit[x] += v;x += (x&(-x));}
}int Sum(int x) {int ans = 0;while (x > 0) {ans += bit[x];x -= (x&(-x));}return ans;
}void init() {time = 0;memset(bit, 0, sizeof(bit));memset(pro, 0, sizeof(pro));memset(lt, 0, sizeof(lt));memset(rt, 0, sizeof(rt));memset(pt, 0, sizeof(pt));scanf("%d%d%d", &n, &q, &cd);
}void solve() {char Q[10];for (int i = 0; i < q; i++) {scanf("%s", Q);if (Q[0] == 'A') {int l, r;scanf("%d%d", &l, &r);Add(l, 1);Add(r + 1, -1);lt[time] = l;rt[time] = r;time++;}else {int num; scanf("%d", &num);for (int j = pt[num]; j < time; j++) {if (num >= lt[j] && num <= rt[j]) {pt[num] = j + cd;pro[num]++;j += cd - 1;}}printf("%d\n", Sum(num) - pro[num]);}}
}int main() {int cas = 0;scanf("%d", &t);while (t--) {printf("Case %d:\n", ++cas);init();solve();}return 0;
}
这篇关于HDU 4031 Attack(树状数组修改区间查询点)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!