【題解】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;
}
相關文章
相關標籤/搜索