SDUT2880 devour Magic segment tree (set+add marker)

Source: Internet
Author: User
Tags new set

Devour Magic Time limit:2000ms Memory limit:65536k have questions? Dot here ^_^ Title Description in Warcraft III, Destroyer are a large flying unit that must consume magic to sustain its mana. Breaking free of the obsidian stone/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 has n units in a line, all unit start with 0 mana and can increase to infinity MA Ximum Mana. All unit except the Destroyer has Mana Regeneration 1 in per unit time. The Destroyer has 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, and count how many mana the destroyer has devour altogether. Enter the first line Co Ntains One integer T, indicating the test case. For each test case, the first contains n,?m (1?≤?N,?M?≤?10^5). The next m line contains a instruction T L R. (1?≤?t?≤?10^5,?1?≤?l?≤?r?≤?n) outputs the for each test case, output the C onrespornding result. Example input
110 51 1 102 3 103 5 104 7 105 9 10
Sample output
30
title Link: http://www.sdutacm.org/sdutoj/problem.php?action=showproblem&problemid=2880Line tree: set+add operation, good question. Note the order of the two, set first, add in, this should be understandable.
/* Line Tree Conclusion: As long as it is set, it is directly assigned, and as long as it is add, it is added on the original basis; When set, add 0 is always updated with the sum value so that it remains correct. */#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;        Here is assigned value, then the subtree add is useless set[lc]=set[rc]=set[k]; sum[lc]=set[k]* (m-l+1);        The sum here is the direct assignment, because the previous value is no longer present, but instead uses the current new set value to calculate the 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);        When the add is processed, sum cannot be directly assigned, but is added sum[rc]+=add[k]* (r-m) on the original basis;    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;    Notice there's 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 is wrong .....        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;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

SDUT2880 devour Magic segment tree (set+add marker)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.