In 2014, the 5th ACM College Student Programming Contest in Shandong Province, Devour Magic (Line Segment Tree)

Simply write a line segment tree, and no query is required. Also int timed out. . .

Each time the values ​​of l and r are updated to the current input t.

#include <bits/stdc++.h>
using namespace std;
long long tree[400009];
long long lazy[400009];
void pushback(long long root,long long l,long long r)
{
    if(lazy[root])
    {
    long long mid=(r+l)/2;
    tree[root<<1]=(mid-l+1)*lazy[root];
    lazy[root<<1]=lazy[root];
    tree[root<<1|1]=(r-mid)*lazy[root];
    lazy[root<<1|1]=lazy[root];
    lazy[root]=0;
    }
}
void update(long long l,long long r,long long L,long long R,long long val,long long root)
{
    if(L>R)return;
    if(L==R){tree[root]=val;return ;}
    pushback(root,L,R);
    if(l<=L&&r>=R)
    {
        tree[root]=val*(R-L+1);
        lazy[root]=val;
        return;
    }
    long long mid=(L+R)/2;
    if(mid>=r)update(l,r,L,mid,val,root<<1);
    else if(mid<l)update(l,r,mid+1,R,val,root<<1|1);
    else
    {
        update(l,r,L,mid,val,root<<1);
        update(l,r,mid+1,R,val,root<<1|1);
    }
    tree[root]=tree[root<<1]+tree[root<<1|1];
}
intmain()
{
   long long t,n,m;
   scanf("%lld",&t);
   while(t--)
   {
       memset(tree,0,sizeof(tree));
       memset(lazy,0,sizeof(lazy));
       long long now,l,r;
       scanf("%lld%lld",&n,&m);
    while(m--)
    {
    scanf("%lld%lld%lld",&now,&l,&r);
    update(l,r,1,n,now,1);
    }
    long long ans=0;
   ans=tree[1];
   printf("%lld\n",ans);
   }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325520001&siteId=291194637