Devour Magic
Time Limit: 2000ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
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 ≤ 10^5). The the next m line each line contains a instruction t l r.(1 ≤ t ≤ 10^5, 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
提示
来源
2014年山东省第五届ACM大学生程序设计竞赛
在1到n这n个单位,每秒都会恢复一滴血,在第t分钟时,从l到r的这几个单位血值变为0,求最后变为0的有多少。
基本线段树。
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define M 100007
#define inf 0x3f3f3f3f
using namespace std;
long long sum;
long long r,c,m;
struct T
{
long long left,right,sum;
long long add,set;
}tree[M<<4];
void build(long long l,long long r,long long i)
{
tree[i].left=l;tree[i].right=r;tree[i].set=-1;
tree[i].add=tree[i].sum=0;
if(l==r)return;
long long mid=l+(r-l)/2;
build(l,mid,i<<1);
build(mid+1,r,i<<1|1);
}
void pushdown(long long i)
{
if(tree[i].left>=tree[i].right)return;
if(tree[i].set!=-1)
{
tree[i<<1].set=tree[i<<1|1].set=tree[i].set;
tree[i<<1].add=tree[i<<1|1].add=0;
tree[i<<1].sum=(tree[i<<1].right-tree[i<<1].left+1)*tree[i].set;
tree[i<<1|1].sum=(tree[i<<1|1].right-tree[i<<1|1].left+1)*tree[i].set;
}
if(tree[i].add>0)
{
long long add=tree[i].add;
tree[i<<1].add+=add;tree[i<<1|1].add+=add;
tree[i<<1].sum+=add*(tree[i<<1].right-tree[i<<1].left+1);
tree[i<<1|1].sum+=add*(tree[i<<1|1].right-tree[i<<1|1].left+1);
}
}
void maintain(long long i)
{
if(tree[i].left>=tree[i].right)return;
tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum;
}
void update_add(long long l,long long r,long long val,long long i)
{
if(tree[i].left==l&&tree[i].right==r)
{
tree[i].add+=val;
tree[i].sum+=(tree[i].right-tree[i].left+1)*val;
return;
}
pushdown(i);
tree[i].set=-1;
tree[i].add=0;
long long mid=tree[i].left+(tree[i].right-tree[i].left)/2;
if(r<=mid)update_add(l,r,val,i<<1);
else if(l>mid)update_add(l,r,val,i<<1|1);
else
{
update_add(l,mid,val,i<<1);
update_add(mid+1,r,val,i<<1|1);
}
maintain(i);
}
void update_set(long long l,long long r,long long val,long long i)
{
if(tree[i].left==l&&tree[i].right==r)
{
tree[i].set=val;
tree[i].add=0;
tree[i].sum=(tree[i].right-tree[i].left+1)*val;
return;
}
pushdown(i);
tree[i].set=-1;
tree[i].add=0;
long long mid=tree[i].left+(tree[i].right-tree[i].left)/2;
if(r<=mid)update_set(l,r,val,i<<1);
else if(l>mid)update_set(l,r,val,i<<1|1);
else
{
update_set(l,mid,val,i<<1);
update_set(mid+1,r,val,i<<1|1);
}
maintain(i);
}
void query(long long l,long long r,long long i)
{
if(tree[i].left==l&&tree[i].right==r)
{
sum+=tree[i].sum;
return;
}
pushdown(i);
tree[i].set=-1;tree[i].add=0;
long long mid=tree[i].left+(tree[i].right-tree[i].left)/2;
if(r<=mid)query(l,r,i<<1);
else if(l>mid)query(l,r,i<<1|1);
else
{
query(l,mid,i<<1);
query(mid+1,r,i<<1|1);
}
maintain(i);
}
int main()
{
long long t;
scanf("%lld",&t);
while(t--)
{
long long n,m;
scanf("%lld%lld",&n,&m);
build(1,n,1);
long long a,b,c,time=0,summ=0;
while(m--)
{
sum=0;
scanf("%lld%lld%lld",&c,&a,&b);
update_add(1,n,c-time,1);
time=c;
query(a,b,1);
summ+=sum;
update_set(a,b,0,1);
}
printf("%lld\n",summ);
}
return 0;
}