1. 程式人生 > >【題解】P3391 文藝平衡樹

【題解】P3391 文藝平衡樹

dig ins back ios del bst 小姐 http 平衡樹

用pb_ds庫中的rope水過去的,忽然發現這玩意能水好多模擬題。

詳見這個博客:背景的小姐姐真的好看

  • 聲明
 #include <ext/rope>
 using namespace __gnu_cxx;
  • 使用
rope<類型>a
a.size()
a.length()
a.substr(pos,x)從pos開始提取x個
a.push_back(x);
a.insert(pos,x);在pos插入x,自然支持整個char數組的一次插入
a.erase(pos,x);從pos開始刪除x個
a.copy(pos,len,x);從pos開始到pos+len為止用x代替
a.replace(pos,x);從pos開始換成x

訪問可直接用數組下標,非常方便


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <ext/rope>
#include <iostream>
using namespace std;
using namespace __gnu_cxx;
int read()
{
    int x=0;char ch;short int neg=0;ch=getchar();
    while(!isdigit(ch)){
        neg|=(ch==‘-‘);ch=getchar();
    }
    while(isdigit(ch)){
        x=x*10+ch-48;ch=getchar();
    }
    return neg?-x:x;
}
int n,m;
int l,r;
rope<int>a,b,tmp;
int main()
{
  int len;
  cin>>n>>m;
  for(int i=0;i<n;i++)
  {
    a.push_back(i+1);b.push_back(n-i);//a[i]=i;b[i]=n-i+1;
  }
  while(m--)
  {
    l=read();r=read();
    l--,r--;
    int x=r-l+1;
    tmp=a.substr(l,r-l+1);
    a=a.substr(0,l)+b.substr(n-r-1,r-l+1)+a.substr(r+1,n-r-1);
    b=b.substr(0,n-r-1)+tmp+b.substr(n-l,l);
  }
  for(register int i=0;i<n;i++)cout<<a[i]<<‘ ‘;
  return 0;
}

【題解】P3391 文藝平衡樹