题目

可能\(\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. 脚本启动SpringBoot(jar)

    #!/bin/sh RESOURCE_NAME=springbsit-api.jar tpid=`ps -ef|grep $RESOURCE_NAME|grep -v grep|grep -v kil ...

  2. delphi 安卓开发常用

    delphi 安卓开发有几个常用的pas: FMX.Helpers.Android, Androidapi.JNI.Net, Androidapi.JNI.GraphicsContentViewTex ...

  3. Delphi全面控制Windows任务栏

    使用Windows95/NT/98操作系统的用户知道:Windows正常启动后,在电脑屏幕下方出现一块 任务栏.从系统功能角度而言,整个任务栏包括几个不同的子区域,从左至右依次是:开始 按钮.应用程序 ...

  4. CommandLineToArgvW调EXE传入参数【转载】

    #include <afxwin.h>  // TODO: add your code here LPWSTR *szArglist = NULL; ; szArglist = Comma ...

  5. 笨办法学Python记录--习题15-17 开始读写文件啦

    习题15 - 17 打开并读流程: from sys import argv script,filename = argv txt = open(filename) print "Here' ...

  6. oracle一体机(exdata)创建ACFS文件系统

    ACFS是一个支持多个平台,可扩展的,基于ORACLE ASM的集群文件系统,可以用来存储数据库和各种应用的文件,包括数据库的trace文件,alert日志文件和配置文件等等,也可以用来存储视频,音频 ...

  7. topjui.core.js

    var defaultConfig = { pageLoadComplete: false, config: { ctx: "", mainPage: false, pkName: ...

  8. PAT_A1079#Total Sales of Supply Chain

    Source: PAT A1079 Total Sales of Supply Chain (25 分) Description: A supply chain is a network of ret ...

  9. LeetCode刷题笔记-回溯法-组合总和问题

    题目描述: <组合总和问题>给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. cand ...

  10. POJ Evacuation /// 二分图最大匹配

    题目大意: 在一个n*m的房间中 ‘X’为墙 ‘D’为门 ‘.’为人 门只存在与外围 人每秒钟只能向四连通区域走一步 门比较狭窄 每秒钟只能通过一个人 求所有人逃脱的最短时间 如果不可能则输出impo ...