1444: Devour Magic(线段树区间更新)

题目描述
In Warcraft III, Destroyer is a large flying unit that must consume magic to sustain its mana. Breaking free of the obsidian stone that holds them, these monstrous creatures roar into battle, swallowing magic to feed their insatiable hunger as they move between battles and rain destruction down upon their foes. Has Spell Immunity. Attacks land and air units.



The core skill of the Destroyer is so called Devour Magic, it takes all mana from all units in a area and gives it to the Destroyer.

Now to simplify the problem, assume you have n units in a line, all unit start with 0 mana and can increase to infinity maximum mana. All unit except the Destroyer have mana regeneration 1 in per unit time.

The Destroyer have m instructions t l r, it means, in time t, the Destroyer use Devour Magic on unit from l to r. We give you all m instructions in time order, count how many mana the Destroyer have devour altogether.

输入
The first line contains one integer T, indicating the test case. For each test case, the first contains two integer n, m(1 ≤ n, m ≤ 105). The the next m line each line contains a instruction t lr.(1 ≤ t ≤ 105, 1 ≤ l ≤ r ≤ n)

输出
For each test case, output the conrespornding result.

样例输入
1
10 5
1 1 10
2 3 10
3 5 10
4 7 10
5 9 10
样例输出

30

<span style="font-size:14px;">#include<string.h>
#include<iostream>
#include<algorithm>
#include<stdio.h>
#define L(n) 2*n+1
#define R(n) 2*n+2
using namespace std;
#define Max 100010
int m,n;
long long sum;
struct node
{
	int l,r,data,flag;
	long long sum;
}tree[4*Max];
struct seg
{
	int l,r,t;
}Seg[Max];
bool cmp(seg a,seg b)
{
	return a.t<b.t;
}
void build(int l,int r,int nd)
{
	tree[nd].l=l,tree[nd].r=r,tree[nd].sum=0,tree[nd].data=0;
	tree[nd].flag=1;
	if(l==r) 
	{
		return;
	}
	int mid=(l+r)/2;
	build(l,mid,L(nd));
	build(mid+1,r,R(nd));
	//tree[nd].sum=tree[L(nd)].sum+tree[R(nd)].sum;
}
void update(int l,int r,int nd,int data)
{
	if(tree[nd].l==l&&tree[nd].r==r)
	{
		if(data!=-1)
		{
			tree[nd].sum+=(r-l+1)*data;
			tree[nd].data+=data;
		}
		else if(data==-1)
		{
			tree[nd].sum=0;
			tree[nd].data=0;
			tree[nd].flag=-1;
		}
		return;
	}
	if(tree[nd].flag==-1)
	{
		tree[L(nd)].data=0;
		tree[L(nd)].sum=0;
		tree[L(nd)].flag=-1;
		tree[R(nd)].data=0;
		tree[R(nd)].sum=0;
		tree[R(nd)].flag=-1;
		tree[nd].flag=1;
	}
	if(tree[nd].data)
	{
		tree[L(nd)].data+=tree[nd].data;
		tree[L(nd)].sum+=tree[nd].data*(tree[L(nd)].r-tree[L(nd)].l+1);
		tree[R(nd)].data+=tree[nd].data;
		tree[R(nd)].sum+=tree[nd].data*(tree[R(nd)].r-tree[R(nd)].l+1);
		tree[nd].data=0;
	}
	int mid=(tree[nd].l+tree[nd].r)/2;
	if(l<=mid)
	{
		if(r>=mid+1)
		{
			update(l,mid,L(nd),data);
			update(mid+1,r,R(nd),data);
		}
		else update(l,r,L(nd),data);
	}
	else update(l,r,R(nd),data);
	tree[nd].sum=tree[L(nd)].sum+tree[R(nd)].sum;
}
long long query(int l,int r,int nd)
{
	if(tree[nd].l==l&&tree[nd].r==r)
		return tree[nd].sum;
	int mid=(tree[nd].l+tree[nd].r)/2;
	if(tree[nd].flag==-1)
	{
		tree[L(nd)].data=0;
		tree[L(nd)].sum=0;
		tree[L(nd)].flag=-1;
		tree[R(nd)].data=0;
		tree[R(nd)].sum=0;
		tree[R(nd)].flag=-1;
		tree[nd].flag=1;
	}
	if(tree[nd].data)
	{
		tree[L(nd)].data+=tree[nd].data;
		tree[L(nd)].sum+=tree[nd].data*(tree[L(nd)].r-tree[L(nd)].l+1);
		tree[R(nd)].data+=tree[nd].data;
		tree[R(nd)].sum+=tree[nd].data*(tree[R(nd)].r-tree[R(nd)].l+1);
		tree[nd].data=0;
	}
	if(l<=mid)
	{
		if(r>=mid+1)
			return query(l,mid,L(nd))+query(mid+1,r,R(nd));
		else return query(l,r,L(nd));
	}
	else return query(l,r,R(nd));
}
int main()
{
	//freopen("b.txt","r",stdin);
	int b,t,i;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d %d",&n,&m);
		build(1,n,0);
		b=0;
		sum=0;
		for(i=0;i<m;i++)
			scanf("%d %d %d",&Seg[i].t,&Seg[i].l,&Seg[i].r);
		sort(Seg,Seg+m,cmp);
		for(i=0;i<m;i++)
		{
			if(Seg[i].t>b)
			{
				update(1,n,0,1);
					b=Seg[i].t;
				
			}
			    sum+=query(Seg[i].l,Seg[i].r,0);
				update(Seg[i].l,Seg[i].r,0,-1);
		}
		printf("%lld\n",sum);
	}
	return 0;
}
</span>
//TLE了很久,看似不起眼的预定义其实可以节约很多时间,所以要多多使用预定义

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值