线段树 HDU 4031 attack

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4031

代码风格:http://www.notonlysuccess.com/index.php/segment-tree-complete/


题目大意:Attack——攻击区间【a,b】, 每个位置的防御盾用过一次在 t 时间内不能再次防御,没有启动防御盾表示successful attack,W——该位置有多少次successful attack

算法:线段树

思路:统计该点所有被攻击的次数 减去- 该点启动了多少次防御盾

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

#define lson l, m, rt << 1 
#define rson m+1, r, rt << 1 | 1
#define mid int m = (r+l) >> 1

struct nod
{
	int l, r;
}p[565656];
int cnt[565656];

void update(int L, int R, int l, int r, int rt)
{
	if(L <= l && r <= R)
	{
		cnt[rt] ++;
		return ;
	}
	mid ;
	if(L <= m)
		update(L, R, lson);
	if(m < R)
		update(L, R, rson);
}

int query(int d, int l, int r, int rt)
{
	if(l == r)
		return cnt[rt];
	mid ;
	if(d <= m)
		return cnt[rt] + query(d, lson);
	else return cnt[rt] + query(d, rson);
}


int main()
{
	int n, T, m, t;
	char op[15];
	int a, b;
	int ica = 1;
	scanf("%d", &T);
	while(T --)
	{
		printf("Case %d:\n", ica ++);
		scanf("%d%d%d", &n, &m, &t);
		memset(cnt, 0, sizeof(cnt));
			int tot = 0;
		while(m --)
		{
			scanf("%s%d", op, &a);
			if(op[0] == 'A')
			{
				scanf("%d", &b);
				p[tot].l = a;
				p[tot].r = b;
				tot ++;
				update(a, b, 1, n, 1);
			}
			else 
			{
				int sum = query(a, 1, n, 1);//一共被攻打了多少次
				int temp = 0;
				for(int i = 0; i < tot; i ++)
				{
	//				printf("l = %d, r = %d\n", p[i].l, p[i].r);
					if(p[i].l <= a && a <= p[i].r)
					{
						temp ++;
						i += t-1;
					}
				}
	//			printf("sum = %d, temp = %d\n", sum, temp);
				printf("%d\n", sum - temp);
			}
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值