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

题目链接:http://www.sdutacm.org/sdutoj/problem.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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值