【题解】Luogu CF915E Physical Education Lessons

原题传送门:CF915E Physical Education Lessons

前置芝士:珂朵莉树

窝博客里对珂朵莉树的介绍

没什么好说的自己看看吧

这道题很简单啊

每个操作就是区间赋值,顺带把总和修改一下,这样会快多了,所以我又成了洛咕最优解第二(好像比23forever dalao快,玄学???)

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define IT set<node>::iterator
using namespace std;
inline int read()
{
    register int x=0,f=1;register char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
struct node
{
    int l,r;
    mutable bool v;
    node(int L, int R=-1, bool V=0):l(L), r(R), v(V) {}
    bool operator<(const node& o) const
    {
        return l < o.l;
    }
};
set<node> s;
int sum=0;
IT split(int pos)
{
    IT it = s.lower_bound(node(pos));
    if (it != s.end() && it->l == pos) 
        return it;
    --it;
    int L = it->l, R = it->r;
    bool V = it->v;
    s.erase(it);
    s.insert(node(L, pos-1, V));
    return s.insert(node(pos, R, V)).first;
}
void assign_val(int l,int r,bool val)
{
    IT itr = split(r+1), itl = split(l), it = itl;
    for( ;itl != itr; ++itl)
        sum-=itl->v*(itl->r-itl->l+1);
    s.erase(it,itr);
    s.insert(node(l,r,val));
    sum+=val*(r-l+1);
}
int main()
{
	int n=read(),m=read();
	s.insert(node(1,n,1));
	sum=n;
	while(m--)
	{
		int l=read(),r=read(),op=read();
		if(op==1)
			assign_val(l,r,0);
		else
			assign_val(l,r,1);
		printf("%d\n",sum);
	}
	return 0;
}
posted @ 2018-10-11 21:38  JSOI爆零珂学家yzhang  阅读(332)  评论(2编辑  收藏  举报