【LGP5350】序列
可能\(\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】序列的更多相关文章
- 【夯实PHP基础】UML序列图总结
原文地址 序列图主要用于展示对象之间交互的顺序. 序列图将交互关系表示为一个二维图.纵向是时间轴,时间沿竖线向下延伸.横向轴代表了在协作中各独立对象的类元角色.类元角色用生命线表示.当对象存在时,角色 ...
- Windows10-UWP中设备序列显示不同XAML的三种方式[3]
阅读目录: 概述 DeviceFamily-Type文件夹 DeviceFamily-Type扩展 InitializeComponent重载 结论 概述 Windows10-UWP(Universa ...
- 软件工程里的UML序列图的概念和总结
俗话说,自己写的代码,6个月后也是别人的代码……复习!复习!复习! 软件工程的一般开发过程:愿景分析.业务建模,需求分析,健壮性设计,关键设计,最终设计,实现…… 时序图也叫序列图(交互图),属于软件 ...
- python序列,字典备忘
初识python备忘: 序列:列表,字符串,元组len(d),d[id],del d[id],data in d函数:cmp(x,y),len(seq),list(seq)根据字符串创建列表,max( ...
- BZOJ 1251: 序列终结者 [splay]
1251: 序列终结者 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 3778 Solved: 1583[Submit][Status][Discu ...
- 最长不下降序列nlogn算法
显然n方算法在比赛中是没有什么用的(不会这么容易就过的),所以nlogn的算法尤为重要. 分析: 开2个数组,一个a记原数,f[k]表示长度为f的不下降子序列末尾元素的最小值,tot表示当前已知的最长 ...
- [LeetCode] Sequence Reconstruction 序列重建
Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Repeated DNA Sequences 求重复的DNA序列
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
随机推荐
- 针对list<object>中的对象数据的一些简单处理
一 首先创建一个实体类(PersonData ): package hello; public class PersonData { String Id; String Name; String ...
- chkconfig 与 systemctl
chkconfig命令 主要用来更新(启动或停止)和查询系统服务(service)的运行级信息,用于维护/etc/rc[0-6].d目录的命令行工具. chkconfig -–add httpd 增加 ...
- [Grt]一篇简单概括XML
一.XML基础 XML主要用途(我认为就这三点): XML 把数据从 HTML 分离 XML 简化数据共享 XML 简化数据传输 XML 语法规则: XML 文档必须有根元素 XML 文档必须有关闭标 ...
- JavaScript学习笔记之CSS-DOM
HTML负责结构层,网页的结构层由HTML或者XHTML之类的标记语言负责构建 CSS负责表示层,描述页面内容应该如何呈现. JavaScript负责行为层,负责内容应该如何响应事件这一问题. 能利用 ...
- delphi 压缩
DELPHI 通过ZLib来压缩文件夹 unit Unit1; interface uses ZLib, Windows, Messages, SysUtils, Variants, Classes, ...
- Css实现Div在页面上垂直居中显示
方法一 <html> <head> <title>垂直居中</title> <style type=" ...
- Java-javaFx库运用-自动弹跳的球
(1)定义一个名为BallPane的类,用于显示一个弹动的球: (2)定义一个名为BounceBallControl的类,用来使用鼠标动作控制弹球,当鼠标按下的时候动画暂停,当鼠标释放的时候动画恢复执 ...
- Python3 From Zero——{最初的意识:000~Initial consciousness}
http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000 a.编码 默认情况下,Python ...
- L1正则化可以解决过拟合问题(稀疏解)
损失函数最小,也就是求极值点,也就是损失函数导数为0.上面也说了,如果d0+λ和d0-λ为异号的时候刚好为极值点,损失函数导数为0(w为0是条件).而对于L2正则化,在w=0时并不一定是极值点而是d0 ...
- Quartz2作业监听
在本教程中,我们将展示/介绍如何创建一个JobListener,跟踪运行工作状态在作业完成等. P.S 这个例子是Quartz 2.1.5 1. Quartz 作业 作业 - 用于打印一个简单的信息, ...