山东省第五届ACM省赛题——Devour Magic(线段树区间覆盖+更新)

编程入门 行业动态 更新时间:2024-10-24 12:28:07

山东省第五届ACM省赛题——Devour Magic(<a href=https://www.elefans.com/category/jswz/34/1769188.html style=线段树区间覆盖+更新)"/>

山东省第五届ACM省赛题——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 ≤ 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;
}

更多推荐

山东省第五届ACM省赛题——Devour Magic(线段树区间覆盖+更新)

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

发布评论

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

>www.elefans.com

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