题目

可能\(\operatorname{fhq\ treap}\)能做,但是珂朵莉树显然更好写

珂朵莉树是个很玄学的东西啊,就是直接使用\(\operatorname{std::set}\)维护每一段权值相等的连续段,之后暴力这些连续段就好了

在数据随机的意义下且有区间推平操作的时候,连续段的个数是期望\(\log\)的

核心操作是\(split(pos)\),就是把\(pos\)分裂出来,返回一个以\(pos\)为开头的连续段的迭代器

具体实现这样就好了

struct node {
int l,r;
mutable int v;
bool operator<(const node &A) const {if(l==A.l) return r<A.r;return l<A.l;}
};
std::set<node> s;
inline St split(int pos) {
St it=s.lower_bound((node){pos,-1,0});
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;
}

我们操作区间\([l,r]\)的时候,只需要\(x=split(l),y=split(r+1)\),那么对应的\([x,y)\)就是我们要操作的迭代器了

暴力操作这些迭代器即可

一个非常关键的问题,就是我们必须先\(split(r+1)\),之后再\(split(l)\),如果先\(split(l)\)的话可能在\(split(r+1)\)的时候会删除\(l\)所在的区间导致迭代器失效

代码

#include<bits/stdc++.h>
#define re register
#define LL long long
#define St std::set<node>::iterator
inline int read() {
char c=getchar();int x=0;while(c<'0'||c>'9') c=getchar();
while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+c-48,c=getchar();return x;
}
const int mod=1e9+7;
const int maxn=3e5+5;
int lx[maxn],ry[maxn],t[maxn],n,m;
struct node {
int l,r;
mutable int v;
bool operator<(const node &A) const {if(l==A.l) return r<A.r;return l<A.l;}
};
std::set<node> s;
inline St split(int pos) {
St it=s.lower_bound((node){pos,-1,0});
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;
}
inline void del(int l,int r) {
St itr=split(r+1),itl=split(l);
s.erase(itl,itr);
}
inline void pia(int l,int r,int v) {
St itr=split(r+1),it=split(l);
s.erase(it,itr);
s.insert((node){l,r,v});
}
inline void add(int l,int r,int val) {
St itr=split(r+1),it=split(l);
for(;it!=itr;++it) it->v=(it->v+val)%mod;
}
inline int calc(int l,int r) {
int ans=0;
St itr=split(r+1),it=split(l);
for(;it!=itr;++it) ans=(ans+1ll*it->v*(it->r-it->l+1)%mod)%mod;
return ans;
}
inline void rev(int l,int r) {
int tot=0,len=r+l;
St itr=split(r+1),it=split(l);
St a=it;
for(;it!=itr;++it) lx[++tot]=(*it).l,ry[tot]=(*it).r,t[tot]=(*it).v;
s.erase(a,itr);
for(re int i=1;i<=tot;i++)
s.insert((node){len-ry[i],len-lx[i],t[i]});
}
inline void move(int l1,int r1,int l2,int r2) {
int tot=0;
St itr=split(r1+1),it=split(l1);
for(;it!=itr;++it) lx[++tot]=(*it).l,ry[tot]=(*it).r,t[tot]=(*it).v;
itr=split(r2+1),it=split(l2);
s.erase(it,itr);
for(re int i=1;i<=tot;i++)
s.insert((node){lx[i]+l2-l1,ry[i]+r2-r1,t[i]});
}
inline void Swap(int l1,int r1,int l2,int r2) {
int tot=0,cnt=0;
St itr=split(r1+1),it=split(l1);
for(;it!=itr;++it) lx[++tot]=(*it).l,ry[tot]=(*it).r,t[tot]=(*it).v;
cnt=tot;itr=split(r2+1),it=split(l2);
for(;it!=itr;++it) lx[++tot]=(*it).l,ry[tot]=(*it).r,t[tot]=(*it).v;
del(l1,r1),del(l2,r2);
for(re int i=1;i<=cnt;i++)
s.insert((node){lx[i]+l2-l1,ry[i]+r2-r1,t[i]});
for(re int i=cnt+1;i<=tot;i++)
s.insert((node){lx[i]-l2+l1,ry[i]-r2+r1,t[i]}); }
inline void out() {
St it=s.begin();
for(;it!=s.end();++it)
for(re int j=it->l;j<=it->r;++j)
printf("%d ",it->v);
puts("");
}
int main() {
n=read(),m=read();
for(re int i=1;i<=n;i++) s.insert((node){i,i,read()});
int op,l,r,x,y,val;
while(m--) {
op=read(),l=read(),r=read();
if(op==1) printf("%d\n",calc(l,r));
if(op==2||op==3) val=read();
if(op==2) pia(l,r,val);
if(op==3) add(l,r,val);
if(op==6) rev(l,r);
if(op==4||op==5) x=read(),y=read();
if(op==4) move(l,r,x,y);
if(op==5) Swap(l,r,x,y);
}
out();
return 0;
}

【LGP5350】序列的更多相关文章

  1. 【夯实PHP基础】UML序列图总结

    原文地址 序列图主要用于展示对象之间交互的顺序. 序列图将交互关系表示为一个二维图.纵向是时间轴,时间沿竖线向下延伸.横向轴代表了在协作中各独立对象的类元角色.类元角色用生命线表示.当对象存在时,角色 ...

  2. Windows10-UWP中设备序列显示不同XAML的三种方式[3]

    阅读目录: 概述 DeviceFamily-Type文件夹 DeviceFamily-Type扩展 InitializeComponent重载 结论 概述 Windows10-UWP(Universa ...

  3. 软件工程里的UML序列图的概念和总结

    俗话说,自己写的代码,6个月后也是别人的代码……复习!复习!复习! 软件工程的一般开发过程:愿景分析.业务建模,需求分析,健壮性设计,关键设计,最终设计,实现…… 时序图也叫序列图(交互图),属于软件 ...

  4. python序列,字典备忘

    初识python备忘: 序列:列表,字符串,元组len(d),d[id],del d[id],data in d函数:cmp(x,y),len(seq),list(seq)根据字符串创建列表,max( ...

  5. BZOJ 1251: 序列终结者 [splay]

    1251: 序列终结者 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 3778  Solved: 1583[Submit][Status][Discu ...

  6. 最长不下降序列nlogn算法

    显然n方算法在比赛中是没有什么用的(不会这么容易就过的),所以nlogn的算法尤为重要. 分析: 开2个数组,一个a记原数,f[k]表示长度为f的不下降子序列末尾元素的最小值,tot表示当前已知的最长 ...

  7. [LeetCode] Sequence Reconstruction 序列重建

    Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...

  8. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  9. [LeetCode] Repeated DNA Sequences 求重复的DNA序列

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

随机推荐

  1. Dubbox框架和Zookeeper 依赖的引入

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...

  2. 1、Go语言的介绍

    一.概念 Go语言(Golang)是云计算时代的C语言,使用Go编译的程序可以媲美C/C++代码的速度,而且更加安全.支持并行进程. 二.Go语言的优势 1.部署简单.可以直接编译成机器码,不依赖其他 ...

  3. bzoj1009题解

    [解题思路] 先KMP出fail数组,再用fail数组求出M[i][j],表示上一次匹配到第i位,这次可以遇到多少种不同的字符,使之转而匹配到第j位. 设集合S=[1,m]∩N 又设f[i][j]表示 ...

  4. NX二次开发-创建直线UF_CURVE_create_line与NXOpen->CreateLine

    NX11+VS2013 #include <uf.h> #include <uf_curve.h> #include <NXOpen/CurveCollection.hx ...

  5. NX二次开发-NXOpen窗口打印NXMessageBox&ListingWindow

    NX9+VS2012 #include <NXOpen/NXObject.hxx> #include <NXOpen/Part.hxx> #include <NXOpen ...

  6. 从[id setValue: forKey:]了解KVC

    <Objective-C基础教程> P224页有详细介绍 下边是apple官网的简单介绍 和一个应用的例子. KVC就是Key-value coding,大意是允许通过一个Key来读写一个 ...

  7. Django中的HttpResponse和JsonResponse

    Django中的HttpResponse和JsonResponse 我们在编写一些借口函数的时候,经常需要给调用者返回json格式的数据,那么如何返回可直接解析的数据呢? 首先第一种方式: from ...

  8. EFCore学习记录笔记

    1:连接slqlocaldb数据库 (1)在CMD下可以输入sqllocaldb info 查看本机安装的所有的localdb实例 (2)数据库连接字符串为:“Server=(localdb)\\MS ...

  9. Spring REST(4)

    REST风格 /user/1    get请求  获取用户 /user/1 post请求   新增用户 /user/1 put请求     更新用户 /user/1 delete请求 删除用户 在Sp ...

  10. springboot中参数处理

    springboot1中处理是这样的 @Configuration public class WebConfig extends WebMvcConfigurerAdapter{ @Autowired ...