首页 > 代码库 > 山东省赛题 NEU OJ 1444 线段树双标记

山东省赛题 NEU OJ 1444 线段树双标记

http://acm.neu.edu.cn/hustoj/problem.php?id=1444

<marquee id="broadcast" style="height:50px; position:static; visibility:visible; width:1360.607666015625px; z-index:4">OJ问题论坛发帖http://t.cn/zjBp4jd FAQ http://t.cn/zjHKbmN Linux问题看http://t.cn/aWnP1n</marquee>

1444: Devour Magic

时间限制: 1 Sec  内存限制: 256 MB
提交: 129  解决: 21
[提交][状态][讨论版]

题目描述

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

提示

来源

山东省赛



写了几次,改了几次,然后最后确定了一种方法:
双标记类的,需要考虑:
1、多个变量标记不同的修改

2、不同的标记修改的先后次序,我就是这道题,清零和add操作的标记在pushdown的时候次序错误到时一直WA,,,

似乎还是有点疑问,不过对于这道题,每次查询的之前的操作都必然是Add的操作,所以先清零后Add,但是其他题呢??

回头在总结

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#include <cmath>
#include <map>
#include <queue>
using namespace std;

#define ls(rt) rt*2
#define rs(rt) rt*2+1
#define ll long long
#define rep(i,s,e) for(int i=s;i<e;i++)
#define repe(i,s,e) for(int i=s;i<=e;i++)
#define CL(a,b) memset(a,b,sizeof(a))
#define IN(s) freopen(s,"r",stdin)
#define OUT(s) freopen(s,"w",stdin)

const int MAXN = 100000+10;

struct Node{
    int l,r;
    int add;//标记增量
    int v;
    ll sum;
    int len(){return r-l+1;}
}nodes[MAXN*4];

void build(int rt, int l, int r)
{
    ////////////
    //printf("#build#rt=%d l=%d r=%d\n",rt,l,r);
    ////////////
    nodes[rt].l=l;
    nodes[rt].r=r;
    nodes[rt].sum=nodes[rt].add=0;
    nodes[rt].v=0;
    if(l == r)return;
    int mid=(l+r)/2;
    build(ls(rt),l,mid);
    build(rs(rt),mid+1,r);
    nodes[rt].sum=nodes[ls(rt)].sum+nodes[rs(rt)].sum;
}

void pushdown(int rt)
{
    ///////////
    //printf("#push# rt=%d l=%d r=%d sum=%lld add=%d v=%d\n",rt,nodes[rt].l,nodes[rt].r,nodes[rt].sum,nodes[rt].add,nodes[rt].v);
    if(nodes[rt].v)//放后面因为这个优先级高
    {
        nodes[ls(rt)].add=nodes[rs(rt)].add=0;
        nodes[rs(rt)].sum=nodes[ls(rt)].sum=0;
        nodes[ls(rt)].v=nodes[rs(rt)].v=1;
        nodes[rt].v=0;
    }
    if(nodes[rt].add)
    {
        nodes[ls(rt)].add+=nodes[rt].add;
        nodes[rs(rt)].add+=nodes[rt].add;
        nodes[ls(rt)].sum+=nodes[rt].add*nodes[ls(rt)].len();
        nodes[rs(rt)].sum+=nodes[rt].add*nodes[rs(rt)].len();
        nodes[rt].add=0;
    }

}

//rt的更新在pushdown之前做完,pushdown仅仅更新子节点
void update(int rt, int l, int r, int op)
{
        if(l==nodes[rt].l && nodes[rt].r==r)
        {
            if(op==1)//increase
            {

                nodes[rt].add++;
                nodes[rt].sum+=nodes[rt].len();////////
                //printf("#update# rt=%d l=%d r=%d sum=%lld add=%d\n",rt,l,r,nodes[rt].sum,nodes[rt].add);

            }
            if(op==2)  //clear
            {
                nodes[rt].add=nodes[rt].sum=0;
                nodes[rt].v=1;
            }
            return;
        }
        pushdown(rt);
        int mid=(nodes[rt].l+nodes[rt].r)/2;
        /*if(l<=mid)update(ls(rt),l,r,op);
        if(r>mid)update(rs(rt),l,r,op);*/
        if(r<=mid)update(ls(rt),l,r,op);
        else
        {
            if(l>mid)update(rs(rt),l,r,op);
            else
            {
                update(ls(rt),l,mid,op);
                update(rs(rt),mid+1,r,op);
            }
        }
        nodes[rt].sum=nodes[rs(rt)].sum+nodes[ls(rt)].sum;
}

ll query(int rt, int l, int r)
{
    if(l==nodes[rt].l && nodes[rt].r==r)
    {
        //printf("rt=%d l=%d r=%d sum=%lld\n",rt,l,r,nodes[rt].sum);
        return nodes[rt].sum;
    }
    pushdown(rt);
    int mid=(nodes[rt].l+nodes[rt].r)/2;
    ll tmp1=0;
    /*if(l<=mid)tmp1=query(ls(rt),l,r);
    if(r>mid)tmp2=query(rs(rt),l,r);
    nodes[rt].sum=nodes[rs(rt)].sum+nodes[ls(rt)].sum;*/
    if(r<=mid)tmp1=query(ls(rt),l,r);
    else
    {
        if(l>mid)
            tmp1=query(rs(rt),l,r);
        else
        {
            tmp1=query(ls(rt),l,mid)+query(rs(rt),mid+1,r);
        }
    }
    nodes[rt].sum=nodes[rs(rt)].sum+nodes[ls(rt)].sum;
    return tmp1;
}

struct edge
{
    int l, r;
    int t;
}e[MAXN];

bool cmp(const edge &a, const edge &b)
{
    return a.t<b.t;
}

int main()
{
    int ncase,n,m,l,r,t;
    ll ans;
    scanf("%d",&ncase);
    while(ncase--)
    {
        scanf("%d%d",&n,&m);
        build(1,1,n);

        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&e[i].t,&e[i].l,&e[i].r);
        }
        sort(e,e+m,cmp);
        int time=0;
        ans=0;
        for(int i=0;i<m;i++)
        {
            if(e[i].t>time)
            {
                update(1,1,n,1);
                time=e[i].t;
            }
            ans+=query(1,e[i].l,e[i].r);
            update(1,e[i].l,e[i].r,2);
        }
        printf("%lld\n",ans);
    }
    return 0;
}