[SHOI2015]脑洞治疗仪(线段树?珂朵莉树)

题面

这道题超级可爱呢,珂朵莉最可爱了,不,小哀才是最可爱的呢

很好的题,可以考虑用线段树维护,hale表示线段树思路很难,而且难打,不如滚去写珂朵莉树哦

对于操作一:直接将set修改插入即可

对于操作三:最大连续子段和(线段树里面是这样叫的吧)维护即可

对于操作二:我们发现可以考虑先将这段区间里面的1 全部取出来,然后暴力合并区间为0,插入会set里面

之后枚举要修改的区间,从左端点开始搞起,一直后搜索,最后加一个判断,是否已经完全ok即可,具体可参见代码

好了,这道题就解决了

我的代码好像luogu会RE四个点,莫名其妙,loj就可以直接AC了,开心

#include<bits/stdc++.h>
#define IT set<node>::iterator
using namespace std;
const int N=2e5+7;
int m,n;
struct node
{
    int l,r;
    mutable int v;
    node(int L,int R=-1,int V=0):l(L),r(R),v(V){}
    bool operator<(const node &x) const
    {
        return l<x.l;
    }
};
set<node> s;
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,V=it->v;
    s.erase(it);
    s.insert(node(L,pos-1,V));
    return s.insert(node(pos,R,V)).first;
}
void del(int l,int r)
{
    IT itr=split(r+1),itl=split(l);
    s.erase(itl,itr);
    s.insert(node(l,r,0));
}
int query(int l,int r)
{
    int ans=0,mx=0;
    IT itr=split(r+1),itl=split(l);
    for (;itl!=itr;itl++)
    if (!itl->v) ans+=itl->r-itl->l+1;
    else mx=max(mx,ans),ans=0;
    return max(mx,ans);
}
void put(int l0,int r0,int l,int r)
{
    IT itr=split(r0+1),itl=split(l0),it=itl;
    int sum=0;
    for (;itl!=itr;itl++)
    if (itl->v) sum+=itl->r-itl->l+1;
    s.erase(it,itr);s.insert(node(l0,r0,0));
    itr=split(r+1);it=itl=split(l);
    int tot=0;
    for (;itl!=itr&&tot<sum;itl++)
    if (!itl->v) tot+=itl->r-itl->l+1;
    if (itl==itr&&tot<=sum) s.erase(it,itr),s.insert(node(l,r,1));
    else
    {
        int pos=itl->l+sum-tot-1;
        IT itp=split(pos+1);
        s.erase(it,itp);
        s.insert(node(l,pos,1));
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    s.insert(node(1,n,1));s.insert(node(n+1,n+1,0));
    for (int i=1;i<=m;i++)
    {
        int op,l,r;
        scanf("%d%d%d",&op,&l,&r);
        if (op==0) del(l,r);
        if (op==2) printf("%d\n",query(l,r));
        if (op==1)
        {
            int l0,r0;scanf("%d%d",&l0,&r0);
            put(l,r,l0,r0);
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Hale522520/p/10837874.html

时间: 2024-08-29 22:32:04

[SHOI2015]脑洞治疗仪(线段树?珂朵莉树)的相关文章

好Van的珂朵莉树

珂朵莉树 珂朵莉树的主要操作是区间覆盖,即将区间\([l,r]\)全部染色为\(c\). EXAMPLE EXAMPLE 1 给出一个长度为\(n\)的序列,一共\(q\)次询问,每次询问给出\(m\)个区间,求这些区间并集的权值和. \(n \leq 10^5,\sum m \leq 10^5\) SOLUTION 1 显然能用珂朵莉树做 珂朵莉树是一种基于std::set的暴力数据结构. 这个set维护若干区间,这些区间没有交集,且按左端点从小到大有序. struct node { int

Solution: 题解 CF896C Willem, Chtholly and Seniorious(线段树解珂朵莉树)

Intro: 珂朵莉树模板题 怎么所有题解都是珂朵莉树啊啊啊啊 于是本蒟蒻决定来一发中(feng)规(kuang)中(luan)矩(gao)的线段树 首先这棵线段树只维护懒标记 来一发定义 线段树节点\(u\)维护区间\([l_u,r_u]\)的内容 懒标记\(t_u\):当\(t_u\not=0\)时表示区间\([l_u,r_u]\)全是\(t_u\),\(t_u=0\)就是没有懒标记 建立线段树 在建立时顺便处理\(l_u,r_u\),只要当\(l_u=r_u\)时就打上标记 P.s \(L

P2787 语文1(chin1)- 理理思维(珂朵莉树)

珂朵莉树模板,区间排序就暴力地取二十六个字母出来并且计数,然后重新从小到大插入即可 代码: #include <bits/stdc++.h> #define int long long #define sc(a) scanf("%lld",&a) #define scc(a,b) scanf("%lld %lld",&a,&b) #define sccc(a,b,c) scanf("%lld %lld %lld"

模板—珂朵莉树

其实本质上是优化暴力. 网上都说构造的数据可以卡掉珂朵莉树,是因为在修改的时候要遍历set导致很容易卡掉,所以珂朵莉树可能比较有局限性. 但是如果用来维护区间用于求交求并,复杂度是严格的log的,常数好像稍大,但是还是非常有用的. 放个板子: 1 #include<iostream> 2 #include<cstdio> 3 #include<set> 4 #define re register 5 #define co const 6 #define cor co r

CF896C Willem, Chtholly and Seniorious 珂朵莉树

问题描述 CF896C LG-CF896C 题解 我expect就是T飞,从这里跳下去,也不碰和珂朵莉相关的任何东西. 珂朵莉树真好使. 珂朵莉树模板. \(\mathrm{Code}\) #include<bits/stdc++.h> using namespace std; #define int long long #define IT set<node>::iterator template <typename Tp> void read(Tp &x){

LG4979 矿洞:坍塌 珂朵莉树

问题描述 LG4979 题解 珂朵莉树+O2简直就是绝配 对于操作 A ,直接 \(\mathrm{assign}\) 推平就完事了. 对于操作 B ,如果它左右端点有在边界上的,直接把区间 \([l,r]\)撕出来判断就完了,如果不在边界上,先把单点 \({l-1,r+1}\) 撕出来判,如果符合条件,再撕 \([l,r]\) 出来判. \(\mathrm{Code}\) #include<bits/stdc++.h> using namespace std; #define IT set&

ODT (Old Driver Tree)珂朵莉树

1 #include <bits/stdc++.h> 2 using namespace std; 3 4 typedef long long LL; 5 const int MOD7 = 1e9 + 7; 6 const int MOD9 = 1e9 + 9; 7 const int imax_n = 1e5 + 7; 8 LL add(LL a, LL b, LL mod) 9 { 10 LL res = 0; 11 LL ans = a; 12 while (b) 13 { 14 if

珂朵莉树

将一段区间的值全变成c 区间加 区间第k小的数 区间幂次和 传送门 模板 #include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <set> #include <algorithm> #define IT set<node>::iterator using namespace std; const int max

[Codeforces896C] Willem, Chtholly and Seniorious (ODT-珂朵莉树)

无聊学了一下珂朵莉树 珂朵莉树好哇,是可以维护区间x次方和查询的高效数据结构. 思想大致就是一个暴力(相对而言)的树形数据结构 lxl毒瘤太强了,发明了ODT算法(Old Driver Tree老司机算法) 这里有一个大佬ACDreamer的题解 附上链接https://www.luogu.org/blog/ACdreamer/solution-cf896c 还有一个B站的讲解视频 附上链接https://www.bilibili.com/video/av21651173 我不会用珂朵莉树,但是