题目描述
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
之前不知道区间的覆盖与更新有什么区别,现在才知道覆盖时要将节点和子树的延迟标记清零
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cstdio>
//#include <set>
#include <vector>
#include <iomanip>
#include <stack>
#include <map>
#include <queue>
#define MAXN 100010
#define mod 9973
#define INF 0x3f3f3f3f
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
long long sum[MAXN<<2];
long long add[MAXN<<2];
long long set[MAXN<<4];
void PushUp(int rt)
{
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void PushDown(int rt,int m)
{
if(set[rt]!=-1)
{
add[rt<<1]=add[rt<<1|1]=0;
set[rt<<1]=set[rt<<1|1]=set[rt];
sum[rt<<1]=set[rt]*(m-(m>>1));
sum[rt<<1|1]=set[rt]*(m>>1);
set[rt]=-1;
}
if(add[rt])
{
add[rt<<1]+=add[rt];
add[rt<<1|1]+=add[rt];
sum[rt<<1]+=add[rt]*(m-(m>>1));
sum[rt<<1|1]+=add[rt]*(m>>1);
add[rt]=0;
}
}
void Build(int l,int r,int rt)
{
add[rt]=0;
if(l==r)
{
sum[rt]=0;
return;
}
int m=(l+r)>>1;
Build(lson);
Build(rson);
PushUp(rt);
}
void update(int L,int R,long long c,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
add[rt]+=c;
sum[rt]+=(long long)c*(r-l+1);
return;
}
PushDown(rt,r-l+1);
int m=(l+r)>>1;
if(L<=m)
update(L,R,c,lson);
if(m<R)
update(L,R,c,rson);
PushUp(rt);
}
void Set(int L,int R,long long c,int l,int r,int rt)
{
if (L <= l && r <= R)
{
set[rt]=c;
add[rt]=0;
sum[rt]=c*(r-l+1);
return ;
}
PushDown(rt, r - l + 1);
int m = (l + r) >> 1;
if (L <= m)
Set(L, R, c, lson);
if (R > m)
Set(L, R, c, rson);
PushUp(rt);
}
long long query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
return sum[rt];
}
PushDown(rt,r-l+1);
int m=(l+r)>>1;
long long ret=0;
if(L<=m)
ret+=query(L,R,lson);
if(m<R)
ret+=query(L,R,rson);
PushUp(rt);
return ret;
}
int main()
{
ios::sync_with_stdio(false);
int t,n,m;
int l,r,t1,t2;
cin>>t;
while(t--)
{
cin>>n>>m;
Build(1,n,1);
t1=0;
long long ans=0;
while(m--)
{
cin>>t2>>l>>r;
update(1,n,t2-t1,1,n,1);
ans+=query(l,r,1,n,1);
Set(l,r,0,1,n,1);
t1=t2;
}
cout<<ans<<endl;
}
return 0;
}