SDUT2880 Devour Magic 线段树(set+add标记)

编程入门 行业动态 更新时间:2024-10-25 02:25:09

SDUT2880 Devour Magic <a href=https://www.elefans.com/category/jswz/34/1769188.html style=线段树(set+add标记)"/>

SDUT2880 Devour Magic 线段树(set+add标记)

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

题目链接:.php?action=showproblem&problemid=2880

线段树:set+add操作,不错的题。

注意一下两者的顺序,set优先,add在后,这个都应该能理解。
/*
线段树
结论:只要是set的,就是直接赋值的;只要是add的,就是在原基础上加的;set时把add置0
还有就是时刻更新sum值,使其保持正确.
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
typedef long long ll;const int maxn=100000+10;
ll sum[maxn*4],add[maxn*4],set[maxn*4];void pushUp(int k){sum[k]=sum[k*2]+sum[k*2+1];
}void pushDown(int k,int l,int r){int lc=k*2,rc=k*2+1,m=(l+r)/2;if(set[k]!=-1){add[lc]=add[rc]=0;      //这里被赋值,那么子树的add就没用了set[lc]=set[rc]=set[k];sum[lc]=set[k]*(m-l+1); //这里的sum就是直接赋值,因为之前的值不复存在,而是用现在的新的set值计算sum[rc]=set[k]*(r-m);set[k]=-1;}if(add[k]){add[lc]+=add[k];add[rc]+=add[k];sum[lc]+=add[k]*(m-l+1);    //处理add时,sum就不能直接赋值,而是在原来的基础上加sum[rc]+=add[k]*(r-m);add[k]=0;}
}void Set(int a,int b,ll v,int k,int l,int r){if(a<=l && r<=b){set[k]=v;add[k]=0;sum[k]=v*(r-l+1);return ; //注意这里有add[k]=0!!!!!!!!!!!!!!!!!!}pushDown(k,l,r);int m=(l+r)/2;if(a<=m)Set(a,b,v,k*2,l,m);if(b>m)Set(a,b,v,k*2+1,m+1,r);pushUp(k);
}void Add(int a,int b,ll v,int k,int l,int r){if(a<=l && r<=b){add[k]+=v;sum[k]+=v*(r-l+1);return ;}pushDown(k,l,r);int m=(l+r)/2;if(a<=m)Add(a,b,v,k*2,l,m);if(b>m)Add(a,b,v,k*2+1,m+1,r);pushUp(k);
}ll ask(int a,int b,int k,int l,int r){if(a<=l && r<=b){return sum[k];}pushDown(k,l,r);int m=(l+r)/2;ll res=0;if(a<=m)res+=ask(a,b,k*2,l,m);if(b>m)res+=ask(a,b,k*2+1,m+1,r);pushUp(k);return res;
}int main()
{int i,T,n,m,a,b,t;scanf("%d",&T);while(T--){scanf("%d%d",&n,&m);memset(add,0,sizeof(add));Set(1,n,(ll)0,1,1,n);       //set[1]=0是不对的。。。。。ll res=0;t=0;while(m--){int t1;scanf("%d%d%d",&t1,&a,&b);Add(1,n,(ll)t1-t,1,1,n);res+=ask(a,b,1,1,n);Set(a,b,(ll)0,1,1,n);t=t1;}printf("%lld\n",res);}return 0;
}


更多推荐

SDUT2880 Devour Magic 线段树(set+add标记)

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

发布评论

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

>www.elefans.com

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