题目描述
我们用文本处理器来处理一个特殊的文本文件,该文本文件共有N行文本,每一行文本仅包含一个自然数,第一行为1、第二行为2,以此类推至N行为自然数N。
假设对该文本文件执行一次“剪切和粘贴”操作含义如下:首先选定连续的若干行文本,“剪切”操作将选定的文本从文件中剪下,而“粘贴”操作将剪切下来的文本插入到文件中的其他地方。
编写一个程序求出在进行了连续若干次“剪切和粘贴”操作后,文本文件中前十行的内容。
题目大意
将区间内的一段数字移动到另一个地方,求操作后的区间前10个数字。
数据范围
10<=n<=100000 1<=k<=1000
样例输入
13 3
6 12 1
2 9 0
10 13 8
样例输出
6
7
8
9
10
11
12
2
3
4
解题思路
Splay 维护区间,如果要剪切,就先把区间旋转出来,再断边,在把要插入的地方旋转出来,连边。(虽然我并不知道这道题目的标解是什么。。)
Update:最后发现O(nk)的模拟可以直接过QAQ
代码
#include <bits/stdc++.h>
#define Maxn 100005
using namespace std;
inline int Getint(){int x=0,f=1;char ch=getchar();while('0'>ch||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while('0'<=ch&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
int n,q;
struct splay{
int f[Maxn],son[Maxn][2],size[Maxn],vl[Maxn],root;
void PushUp(int v){if(v)size[v]=size[son[v][0]]+size[son[v][1]]+1;}
void Rotate(int x){
int fa=f[x],gr=f[fa],s=son[fa][1]==x,sn=son[x][!s];
son[f[x]=gr][son[gr][1]==fa]=x;
son[f[fa]=x][!s]=fa;
son[f[sn]=fa][s]=sn;
PushUp(sn);
PushUp(fa);
PushUp(x);
}
void Splay(int x,int goal){
if(x==goal)return;
while(f[x]!=goal){
if(f[f[x]]!=goal&&(son[f[f[x]]][1]==f[x])==(son[f[x]][1]==x))Rotate(f[x]);
Rotate(x);
}
if(!goal)root=x;
}
int Select(int pos){
int p=root;
while(size[son[p][0]]+1!=pos){
if(pos<=size[son[p][0]])p=son[p][0];
else pos-=size[son[p][0]]+1,p=son[p][1];
}
return p;
}
void Insert(int pos,int k){
int u=Select(pos+1),v=Select(pos+2);
Splay(u,0);
Splay(v,u);
son[v][0]=k;
f[son[v][0]]=v;
PushUp(v);
PushUp(u);
}
int Delete(int L,int r){
int u=Select(L),v=Select(r+2);
Splay(u,0);
Splay(v,u);
f[son[v][0]]=0;
PushUp(v);
PushUp(u);
int t=son[v][0];
son[v][0]=0;
return t;
}
void Build(int L,int r,int fa){
if(L>r)return;
int mid=(L+r)/2;
if(L!=r)Build(L,mid-1,mid),Build(mid+1,r,mid);
vl[mid]=(mid-1<=n?mid-1:0);
PushUp(mid);
f[mid]=fa;
son[fa][mid>=fa]=mid;
}
void Init(){
Build(1,n+2,0);
root=n+3>>1;
}
}Solver;
int main(){
n=Getint(),q=Getint();
Solver.Init();
while(q--){
int L=Getint(),r=Getint(),k=Getint();
Solver.Insert(k,Solver.Delete(L,r));
}
for(int i=1;i<=10;i++)cout<<Solver.vl[Solver.Select(i+1)]<<"\n";
return 0;
}