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

编程入门 行业动态 更新时间:2024-10-25 04:23:43

1444: Devour Magic(<a href=https://www.elefans.com/category/jswz/34/1769188.html style=线段树区间更新)"/>

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了很久,看似不起眼的预定义其实可以节约很多时间,所以要多多使用预定义

更多推荐

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

本文发布于:2024-02-27 11:41:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1706387.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线段   区间   Devour   Magic

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!