码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces 915E. Physical Education Lessons(动态开点线段树)

时间:2019-01-25 11:31:58      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:font   防止   \n   com   线段树   return   int   force   题目   

E. Physical Education Lessons

题目:一段长度为n的区间初始全为1,每次成段赋值0或1,求每次操作后的区间总和。(n<=1e9,q<=3e5)
题意:用线段树做的话,没什么思维量,主要是空间复杂度的问题。因此采用动态开点的办法,即需要用到的节点,在使用前分配内存,没有用到的就虚置。这样每次操作新增的节点约logn个。则q次修改需要的空间大约是qlogn。但是,在这个数量级上尽可能开的再大一些,防止RE。

#include<bits/stdc++.h>
#define dd(x) cout<<#x<<" = "<<x<<" "
#define de(x) cout<<#x<<" = "<<x<<"\n"
#define sz(x) int(x.size())
#define All(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef priority_queue<int> BQ;
typedef priority_queue<int,vector<int>,greater<int> > SQ;
const int maxn=1e7+5e6+10,mod=1e9+7,INF=0x3f3f3f3f;
int sum[maxn],ls[maxn],rs[maxn],lazy[maxn],id,root;
void push_dw(int l,int r,int rt)
{
    if (lazy[rt]!=-1)
    {
        if (!ls[rt])
            ls[rt]=++id;
        if (!rs[rt])
            rs[rt]=++id;
        int lson=ls[rt],rson=rs[rt];
        int m=(l+r)>>1;
        lazy[lson]=lazy[rson]=lazy[rt];
        sum[lson]=lazy[rt]*(m-l+1);
        sum[rson]=lazy[rt]*(r-m);
        lazy[rt]=-1;
    }
}
void upd(int L,int R,int c,int l,int r,int& rt)
{
    if (!rt)
        rt=++id;
    if (L<=l&&r<=R)
    {
        lazy[rt]=c;
        sum[rt]=(r-l+1)*c;
        return;
    }
    push_dw(l,r,rt);
    int m=(l+r)>>1;
    if (L<=m)
        upd(L,R,c,l,m,ls[rt]);
    if (R>m)
        upd(L,R,c,m+1,r,rs[rt]);
    sum[rt]=sum[ls[rt]]+sum[rs[rt]];
}
int main()
{
    int n,q;
    cin>>n>>q;
    memset(lazy,-1,sizeof(lazy));
    for (int i=0;i<q;++i)
    {
        int l,r,ty;
        scanf("%d%d%d",&l,&r,&ty);
        upd(l,r,ty==1?1:0,1,n,root);
        printf("%d\n",n-sum[1]);
    }
    return 0;
}

Codeforces 915E. Physical Education Lessons(动态开点线段树)

标签:font   防止   \n   com   线段树   return   int   force   题目   

原文地址:https://www.cnblogs.com/orangee/p/10317743.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!