QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#214029 | #6556. Text Editor | ucup-team087# | 0 | 0ms | 0kb | C++20 | 23.6kb | 2023-10-14 16:57:41 | 2023-10-14 16:57:41 |
answer
#ifndef LOCAL
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#endif
#include <bits/stdc++.h>
using namespace std;
//fast IO by yosupo
//sc.read(string) だと append される
struct Scanner {
FILE* fp = nullptr;
char line[(1 << 15) + 1];
size_t st = 0, ed = 0;
void reread() {
memmove(line, line + st, ed - st);
ed -= st;
st = 0;
ed += fread(line + ed, 1, (1 << 15) - ed, fp);
line[ed] = '\0';
}
bool succ() {
while (true) {
if (st == ed) {
reread();
if (st == ed) return false;
}
while (st != ed && isspace(line[st])) st++;
if (st != ed) break;
}
if (ed - st <= 50) reread();
return true;
}
template <class T, enable_if_t<is_same<T, string>::value, int> = 0>
bool read_single(T& ref) {
if (!succ()) return false;
ref.clear();
while (true) {
size_t sz = 0;
while (st + sz < ed && !isspace(line[st + sz])) sz++;
ref.append(line + st, sz);
st += sz;
if (!sz || st != ed) break;
reread();
}
return true;
}
template <class T, enable_if_t<is_integral<T>::value, int> = 0>
bool read_single(T& ref) {
if (!succ()) return false;
bool neg = false;
if (line[st] == '-') {
neg = true;
st++;
}
ref = T(0);
while (isdigit(line[st])) {
ref = 10 * ref + (line[st++] - '0');
}
if (neg) ref = -ref;
return true;
}
template <class T> bool read_single(vector<T>& ref) {
for (auto& d : ref) {
if (!read_single(d)) return false;
}
return true;
}
void read() {}
template <class H, class... T> void read(H& h, T&... t) {
bool f = read_single(h);
assert(f);
read(t...);
}
Scanner(FILE* _fp) : fp(_fp) {}
};
struct Printer {
public:
template <bool F = false> void write() {}
template <bool F = false, class H, class... T>
void write(const H& h, const T&... t) {
if (F) write_single(' ');
write_single(h);
write<true>(t...);
}
template <class... T> void writeln(const T&... t) {
write(t...);
write_single('\n');
}
Printer(FILE* _fp) : fp(_fp) {}
~Printer() { flush(); }
private:
static constexpr size_t SIZE = 1 << 15;
FILE* fp;
char line[SIZE], small[50];
size_t pos = 0;
void flush() {
fwrite(line, 1, pos, fp);
pos = 0;
}
void write_single(const char& val) {
if (pos == SIZE) flush();
line[pos++] = val;
}
template <class T, enable_if_t<is_integral<T>::value, int> = 0>
void write_single(T val) {
if (pos > (1 << 15) - 50) flush();
if (val == 0) {
write_single('0');
return;
}
if (val < 0) {
write_single('-');
val = -val; // todo min
}
size_t len = 0;
while (val) {
small[len++] = char('0' + (val % 10));
val /= 10;
}
for (size_t i = 0; i < len; i++) {
line[pos + i] = small[len - 1 - i];
}
pos += len;
}
void write_single(const string& s) {
for (char c : s) write_single(c);
}
void write_single(const char* s) {
size_t len = strlen(s);
for (size_t i = 0; i < len; i++) write_single(s[i]);
}
template <class T> void write_single(const vector<T>& val) {
auto n = val.size();
for (size_t i = 0; i < n; i++) {
if (i) write_single(' ');
write_single(val[i]);
}
}
void write_single(long double d){
{
long long v=d;
write_single(v);
d-=v;
}
write_single('.');
for(int _=0;_<8;_++){
d*=10;
long long v=d;
write_single(v);
d-=v;
}
}
};
Scanner sc(stdin);
Printer pr(stdout);
using ll=long long;
#define int ll
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define pb push_back
#define eb emplace_back
#define a first
#define b second
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
#define si(x) int(x.size())
#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif
template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}
template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}
template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;
using pi=pair<int,int>;
using vi=vc<int>;
template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
return os<<"{"<<p.a<<","<<p.b<<"}";
}
template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
os<<"{";
for(auto e:v)os<<e<<",";
return os<<"}";
}
#define mp make_pair
#define mt make_tuple
#define one(x) memset(x,-1,sizeof(x))
#define zero(x) memset(x,0,sizeof(x))
#ifdef LOCAL
void dmpr(ostream&os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
os<<t<<" ";
dmpr(os,args...);
}
#define dmp2(...) dmpr(cerr,__LINE__,##__VA_ARGS__)
#else
#define dmp2(...) void(0)
#endif
using uint=unsigned;
using ull=unsigned long long;
template<class t,size_t n>
ostream& operator<<(ostream&os,const array<t,n>&a){
return os<<vc<t>(all(a));
}
template<int i,class T>
void print_tuple(ostream&,const T&){
}
template<int i,class T,class H,class ...Args>
void print_tuple(ostream&os,const T&t){
if(i)os<<",";
os<<get<i>(t);
print_tuple<i+1,T,Args...>(os,t);
}
template<class ...Args>
ostream& operator<<(ostream&os,const tuple<Args...>&t){
os<<"{";
print_tuple<0,tuple<Args...>,Args...>(os,t);
return os<<"}";
}
ll read(){
ll i;
cin>>i;
return i;
}
vi readvi(int n,int off=0){
vi v(n);
rep(i,n)v[i]=read()+off;
return v;
}
pi readpi(int off=0){
int a,b;cin>>a>>b;
return pi(a+off,b+off);
}
template<class t>
void print_single(t x,int suc=1){
cout<<x;
if(suc==1)
cout<<"\n";
if(suc==2)
cout<<" ";
}
template<class t,class u>
void print_single(const pair<t,u>&p,int suc=1){
print_single(p.a,2);
print_single(p.b,suc);
}
template<class T>
void print_single(const vector<T>&v,int suc=1){
rep(i,v.size())
print_single(v[i],i==int(v.size())-1?suc:2);
}
template<class T>
void print_offset(const vector<T>&v,ll off,int suc=1){
rep(i,v.size())
print_single(v[i]+off,i==int(v.size())-1?suc:2);
}
template<class T,size_t N>
void print_single(const array<T,N>&v,int suc=1){
rep(i,N)
print_single(v[i],i==int(N)-1?suc:2);
}
template<class T>
void print(const T&t){
print_single(t);
}
template<class T,class ...Args>
void print(const T&t,const Args&...args){
print_single(t,2);
print(args...);
}
string readString(){
string s;
cin>>s;
return s;
}
template<class T>
T sq(const T& t){
return t*t;
}
void YES(bool ex=true){
cout<<"YES\n";
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void NO(bool ex=true){
cout<<"NO\n";
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void Yes(bool ex=true){
cout<<"Yes\n";
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void No(bool ex=true){
cout<<"No\n";
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
//#define CAPITAL
/*
void yes(bool ex=true){
#ifdef CAPITAL
cout<<"YES"<<"\n";
#else
cout<<"Yes"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void no(bool ex=true){
#ifdef CAPITAL
cout<<"NO"<<"\n";
#else
cout<<"No"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}*/
void possible(bool ex=true){
#ifdef CAPITAL
cout<<"POSSIBLE"<<"\n";
#else
cout<<"Possible"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void impossible(bool ex=true){
#ifdef CAPITAL
cout<<"IMPOSSIBLE"<<"\n";
#else
cout<<"Impossible"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
constexpr ll ten(int n){
return n==0?1:ten(n-1)*10;
}
const ll infLL=LLONG_MAX/3;
#ifdef int
const int inf=infLL;
#else
const int inf=INT_MAX/2-100;
#endif
int topbit(signed t){
return t==0?-1:31-__builtin_clz(t);
}
int topbit(ll t){
return t==0?-1:63-__builtin_clzll(t);
}
int topbit(ull t){
return t==0?-1:63-__builtin_clzll(t);
}
int botbit(signed a){
return a==0?32:__builtin_ctz(a);
}
int botbit(ll a){
return a==0?64:__builtin_ctzll(a);
}
int botbit(ull a){
return a==0?64:__builtin_ctzll(a);
}
int popcount(signed t){
return __builtin_popcount(t);
}
int popcount(ll t){
return __builtin_popcountll(t);
}
int popcount(ull t){
return __builtin_popcountll(t);
}
int bitparity(ll t){
return __builtin_parityll(t);
}
bool ispow2(int i){
return i&&(i&-i)==i;
}
ll mask(int i){
return (ll(1)<<i)-1;
}
ull umask(int i){
return (ull(1)<<i)-1;
}
ll minp2(ll n){
if(n<=1)return 1;
else return ll(1)<<(topbit(n-1)+1);
}
bool inc(int a,int b,int c){
return a<=b&&b<=c;
}
template<class t> void mkuni(vc<t>&v){
sort(all(v));
v.erase(unique(all(v)),v.ed);
}
ll rand_int(ll l, ll r) { //[l, r]
//#ifdef LOCAL
static mt19937_64 gen;
/*#else
static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
#endif*/
return uniform_int_distribution<ll>(l, r)(gen);
}
ll rand_int(ll k){ //[0,k)
return rand_int(0,k-1);
}
template<class t>
void myshuffle(vc<t>&a){
rep(i,si(a))swap(a[i],a[rand_int(0,i)]);
}
template<class t>
int lwb(const vc<t>&v,const t&a){
return lower_bound(all(v),a)-v.bg;
}
template<class t>
bool bis(const vc<t>&v,const t&a){
return binary_search(all(v),a);
}
vvc<int> readGraph(int n,int m){
vvc<int> g(n);
rep(i,m){
int a,b;
cin>>a>>b;
//sc.read(a,b);
a--;b--;
g[a].pb(b);
g[b].pb(a);
}
return g;
}
vvc<int> readTree(int n){
return readGraph(n,n-1);
}
template<class t>
vc<t> presum(const vc<t>&a){
vc<t> s(si(a)+1);
rep(i,si(a))s[i+1]=s[i]+a[i];
return s;
}
vc<ll> presum(const vi&a){
vc<ll> s(si(a)+1);
rep(i,si(a))s[i+1]=s[i]+a[i];
return s;
}
//BIT で数列を管理するときに使う (CF850C)
template<class t>
vc<t> predif(vc<t> a){
gnr(i,1,si(a))a[i]-=a[i-1];
return a;
}
template<class t>
vvc<ll> imos(const vvc<t>&a){
int n=si(a),m=si(a[0]);
vvc<ll> b(n+1,vc<ll>(m+1));
rep(i,n)rep(j,m)
b[i+1][j+1]=b[i+1][j]+b[i][j+1]-b[i][j]+a[i][j];
return b;
}
//verify してないや
void transvvc(int&n,int&m){
swap(n,m);
}
template<class t,class... Args>
void transvvc(int&n,int&m,vvc<t>&a,Args&...args){
assert(si(a)==n);
vvc<t> b(m,vi(n));
rep(i,n){
assert(si(a[i])==m);
rep(j,m)b[j][i]=a[i][j];
}
a.swap(b);
transvvc(n,m,args...);
}
//CF854E
void rotvvc(int&n,int&m){
swap(n,m);
}
template<class t,class... Args>
void rotvvc(int&n,int&m,vvc<t>&a,Args&...args){
assert(si(a)==n);
vvc<t> b(m,vi(n));
rep(i,n){
assert(si(a[i])==m);
rep(j,m)b[m-1-j][i]=a[i][j];
}
a.swap(b);
rotvvc(n,m,args...);
}
//ソートして i 番目が idx[i]
//CF850C
template<class t>
vi sortidx(const vc<t>&a){
int n=si(a);
vi idx(n);iota(all(idx),0);
sort(all(idx),[&](int i,int j){return a[i]<a[j];});
return idx;
}
//vs[i]=a[idx[i]]
//例えば sortidx で得た idx を使えば単にソート列になって返ってくる
//CF850C
template<class t>
vc<t> a_idx(const vc<t>&a,const vi&idx){
int n=si(a);
assert(si(idx)==n);
vc<t> vs(n);
rep(i,n)vs[i]=a[idx[i]];
return vs;
}
//CF850C
vi invperm(const vi&p){
int n=si(p);
vi q(n);
rep(i,n)q[p[i]]=i;
return q;
}
template<class t,class s=t>
s SUM(const vc<t>&a){
return accumulate(all(a),s(0));
}
template<class t>
t MAX(const vc<t>&a){
return *max_element(all(a));
}
template<class t>
pair<t,int> MAXi(const vc<t>&a){
auto itr=max_element(all(a));
return mp(*itr,itr-a.bg);
}
template<class t>
t MIN(const vc<t>&a){
return *min_element(all(a));
}
template<class t>
pair<t,int> MINi(const vc<t>&a){
auto itr=min_element(all(a));
return mp(*itr,itr-a.bg);
}
vi vid(int n){
vi res(n);iota(all(res),0);
return res;
}
template<class S>
void soin(S&s){
sort(all(s));
}
template<class S>
S soout(S s){
soin(s);
return s;
}
template<class S>
void rein(S&s){
reverse(all(s));
}
template<class S>
S reout(S s){
rein(s);
return s;
}
template<class t,class u>
pair<t,u>&operator+=(pair<t,u>&a,pair<t,u> b){
a.a+=b.a;a.b+=b.b;return a;}
template<class t,class u>
pair<t,u>&operator-=(pair<t,u>&a,pair<t,u> b){
a.a-=b.a;a.b-=b.b;return a;}
template<class t,class u>
pair<t,u> operator+(pair<t,u> a,pair<t,u> b){return mp(a.a+b.a,a.b+b.b);}
template<class t,class u>
pair<t,u> operator-(pair<t,u> a,pair<t,u> b){return mp(a.a-b.a,a.b-b.b);}
template<class t>
t gpp(vc<t>&vs){
assert(si(vs));
t res=move(vs.back());
vs.pop_back();
return res;
}
template<class t>
void pb(vc<t>&a,const vc<t>&b){
a.insert(a.ed,all(b));
}
template<class t>
vc<t> cat(vc<t> a,const vc<t>&b){
pb(a,b);
return a;
}
template<class t,class u>
vc<t>& operator+=(vc<t>&a,u x){
for(auto&v:a)v+=x;
return a;
}
template<class t,class u>
vc<t> operator+(vc<t> a,u x){
return a+=x;
}
template<class t,class u>
vc<t>& operator-=(vc<t>&a,u x){
for(auto&v:a)v-=x;
return a;
}
template<class t,class u>
vc<t>& operator-(vc<t> a,u x){
return a-=x;
}
//#define PAVLLAZY
//S_ bytes のバッファを確保
//ARC030D
//Copy and Paste
//FHC 2022 Round3 E2
//https://qiita.com/QCFium/items/3cf26a6dc2d49ef490d7
template<class N,int S_>
struct pavl{
struct N2{
using np=N2*;
N v;
np l,r;
int h;
//h は葉までの最長パスのノード数
//null node で 0
//v has no lazy data
np upd(){
v.single();
h=0;
if(l){
v.updatel(l->v);
chmax(h,l->h);
}
if(r){
v.updater(r->v);
chmax(h,r->h);
}
h++;
return this;
}
np L(np x){l=x;return upd();}
np R(np x){r=x;return upd();}
np LR(np x,np y){l=x;r=y;return upd();}
};
static constexpr int S=S_/sizeof(N2);
N2 buf[S];
using np=N2*;
int fix=0,used=0;
np nn(N v){
assert(used<S);
buf[used]=N2{v,0,0,1};
return buf+used++;
}
//a is not null
np clone(np&a){
if(a-buf<fix){
assert(used<S);
buf[used]=*a;
a=buf+used++;
}
return a;
}
//a is not null
np push(np&x){
clone(x);
#ifdef PAVLLAZY
if(x->l)x->v.push(clone(x->l)->v);
if(x->r)x->v.push(clone(x->r)->v);
x->v.clear();
#endif
return x;
}
int hei(np x){return x?x->h:0;}
N val(np x){return x?x->v:N();}
int ok(int a,int b){return abs(a-b)<=1;}
//x is already cloned
//x needs upd
//hei(res)==max(hei(x->l),hei(x->r))+(0 or 1)
//O(|hei(x->r)-hei(x->r)|)
np balance(np x){
np a=x->l;
np b=x->r;
int dif=hei(a)-hei(b);
if(inc(-1,dif,1)){
return x->upd();
}else if(dif<0){
push(b);
if(dif<-2){
return Lb(b,Rb(x,b->l));
}else if(ok(hei(b->l)+1,hei(b->r))){
return b->L(x->R(b->l));
}else{
np y=push(b->l);
return y->LR(x->R(y->l),b->L(y->r));
}
}else if(dif>0){
push(a);
if(dif>2){
return Rb(a,Lb(x,a->r));
}else if(ok(hei(a->l),hei(a->r)+1)){
return a->R(x->L(a->r));
}else{
np y=push(a->r);
return y->LR(a->R(y->l),x->L(y->r));
}
}else assert(false);
}
np Lb(np x,np a){x->l=a;return balance(x);}
np Rb(np x,np a){x->r=a;return balance(x);}
//hei(res)==max(hei(a),hei(b))+(0 or 1)
//O(hei(a)+hei(b))
np mergedfs(np a,np b){
if(a==0)return b;
if(b==0)return a;
if(hei(a)<hei(b)){
push(b);
return Lb(b,mergedfs(a,b->l));
}else{
push(a);
return Rb(a,mergedfs(a->r,b));
}
}
template<class...Args>
np merge(Args...args){
fix=used;
np res=0;
for(auto x:{args...})res=mergedfs(res,x);
return res;
}
template <class F,class... Args>
pair<np,np> max_right(np x,N cur,F f,Args&&... args){
if(x==0)return mp(np(0),np(0));
push(x);
x->v.single();
N nx=cur;
nx.updater(val(x->l));
nx.updater(val(x));
if((nx.*f)(forward<Args>(args)...)){
auto [a,b]=max_right(x->r,nx,f,forward<Args>(args)...);
return mp(Rb(x,a),b);
}else{
auto [a,b]=max_right(x->l,cur,f,forward<Args>(args)...);
return mp(a,Lb(x,b));
}
}
template <class F,class... Args>
pair<np,np> max_right(np x,F f,Args&&... args){
fix=used;
return max_right(x,N(),f,forward<Args>(args)...);
}
//f(v,args)=true が左,false が右
//not verified
/*template <class F,class... Args>
pair<np,np> split_by_cmp(np x,F f,Args&&... args){
if(x==0)return mp(np(0),np(0));
push(x);
x->v.single();
if(f(x->v,forward<Args>(args)...)){
auto [a,b]=split_by_cmp(x->r,f,forward<Args>(args)...);
return mp(Rb(x,a),b);
}else{
auto [a,b]=split_by_cmp(x->l,f,forward<Args>(args)...);
return mp(a,Lb(x,b));
}
}*/
//lower_bound の位置に insert
//FHC 2022 Round3 E2 (without LAZY)
template <class F>
np insertdfs(np x,F f,const N&v){
if(x==0)return nn(v);
push(x);
x->v.single();
if(f(x->v,v)){
return Rb(x,insertdfs(x->r,f,v));
}else{
return Lb(x,insertdfs(x->l,f,v));
}
}
template <class F>
np insert_by_cmp(np x,F f,const N&v){
fix=used;
return insertdfs(x,f,v);
}
//lower_bound の位置を erase
//erase が実際に走ったかどうかが bool で返る
//FHC 2022 Round3 E2 (without LAZY, all true)
template <class F>
pair<bool,np> erasedfs(np x,F f,const N&v){
if(x==0)return mp(false,np(0));
push(x);
x->v.single();
if(f(x->v,v)){
auto [done,y]=erasedfs(x->r,f,v);
return mp(done,Rb(x,y));
}else{
auto [done,y]=erasedfs(x->l,f,v);
if(done)return mp(true,Lb(x,y));
else return mp(true,merge(y,x->r));
}
}
template <class F>
pair<bool,np> erase_by_cmp(np x,F f,const N&v){
fix=used;
return erasedfs(x,f,v);
}
//lower_bound が存在するかどうかを bool で返す
//存在するならノード情報も返す
//FHC 2022 Round3 E2 (without LAZY)
template <class F>
pair<bool,N> lower_bound_by_cmp(np x,F f,N v){
fix=used;
np res=0;
while(x){
push(x);
x->v.single();
if(f(x->v,v)){
x=x->r;
}else{
res=x;
x=x->l;
}
}
used=fix;
if(res)return mp(true,res->v);
else return mp(false,N());
}
np insert(np x,const N&v){return insert_by_cmp(x,less<N>(),v);}
pair<bool,np> erase(np x,const N&v){return erase_by_cmp(x,less<N>(),v);}
pair<bool,N> lower_bound(np x,const N&v){return lower_bound_by_cmp(x,less<N>(),v);}
void clear(){fix=used=0;}
template<class T>
np build(const T&rw){
fix=used;
auto dfs=[&](auto self,int l,int r)->np{
if(l==r)return 0;
int m=(l+r)/2;
np x=nn(rw[m]);
return x->LR(self(self,l,m),self(self,m+1,r));
};
return dfs(dfs,0,si(rw));
}
vc<N> listup(np root){
fix=used;
vc<N> res;
auto dfs=[&](auto self,np x)->void{
if(x==0)return;
push(x);
self(self,x->l);
res.pb(x->v);
self(self,x->r);
};
dfs(dfs,root);
return res;
}
void rebuild_if_needed(np&x,int maxlen){
if(used+1000+maxlen>S){
auto ls=listup(x);
clear();
x=build(ls);
}
}
};
//デフォルトコンストラクターが null node になっているべき (例えば len=0)
//reverse なし
//N::push
//副作用なし,1個の子にpush
//N::clear
//lazy tagを消去
//push/lazy は PAVLAZY がdefineされてなければ呼ばれない(これ定数倍変わる?)
//N::single
//ノード単体の情報を元に部分木情報を初期化
//N::updatel,updater
//N::ok
//max_right のノリで split をする
//ノード同士の比較関数でも split 可能(TODO)
//TODO 細かい実装の違いによる定数倍を検証
//Copy and Paste
template<class t>
struct SingleElement{
t val;
int len;
SingleElement():val(0),len(0){}
SingleElement(t v):val(v),len(1){}
void single(){
len=1;
}
void upd(const SingleElement&x){
len+=x.len;
}
void updatel(const SingleElement&x){upd(x);}
void updater(const SingleElement&x){upd(x);}
bool ok_len(int x){
return len<=x;
}
};
using N=SingleElement<char>;
pavl<N,1400*ten(6)> t;
using np=decltype(t)::np;
//https://stackoverflow.com/a/49091681
template<class Tuple>
auto tuple_pop_front(Tuple tuple) {
return apply([](auto a,auto...rest){return mp(a,mt(rest...));},tuple);
}
tuple<np> cut_by_index(np a){
return a;
}
template<class...Args>
auto cut_by_index(np x,int pos,Args...args){
assert((!x&&pos==0)||inc(0,pos,x->v.len));
auto [ab,c]=tuple_pop_front(cut_by_index(x,args...));
return tuple_cat(t.max_right(ab,&N::ok_len,pos),c);
}
bool dbg=false;
np slv(string raw,bool isder){
vc<np> roots{nullptr};
int cur=0;
np clip=nullptr;
stringstream ss(raw);
int n;ss>>n;
auto insert=[&](){
np root=roots[cur];
int p;ss>>p;
string s;ss>>s;
auto [x,y]=cut_by_index(root,p);
root=t.merge(x,t.build(s),y);
roots.resize(++cur);
roots.pb(root);
};
auto erase=[&](){
np root=roots[cur];
int l,r;ss>>l>>r;
auto [x,y,z]=cut_by_index(root,l,r);
root=t.merge(x,z);
roots.resize(++cur);
roots.pb(root);
};
auto copy=[&](){
np root=roots[cur];
int l,r;ss>>l>>r;
auto [x,y,z]=cut_by_index(root,l,r);
clip=y;
};
auto cut=[&](){
np root=roots[cur];
int l,r;ss>>l>>r;
auto [x,y,z]=cut_by_index(root,l,r);
root=t.merge(x,z);
clip=y;
roots.resize(++cur);
roots.pb(root);
};
auto paste=[&](){
np root=roots[cur];
int p;ss>>p;
auto [x,y]=cut_by_index(root,p);
root=t.merge(x,clip,y);
roots.resize(++cur);
roots.pb(root);
};
auto print_ans=[&](){
np root=roots[cur];
int l,r;ss>>l>>r;
if(isder)return;
auto [x,y,z]=cut_by_index(root,l,r);
auto ans=t.listup(y);
for(auto c:ans)pr.write(c.val);
pr.write("\n");
};
auto serialize=[&](){
if(isder)return;
//!0->space
//!1->!
string ser;
for(auto c:raw){
if(c==' '){
ser+='!';
ser+='0';
}else if(c=='!'){
ser+='!';
ser+='1';
}else{
ser+=c;
}
}
assert(si(ser)<=ten(7));
pr.writeln(ser);
};
auto deserialize=[&](){
string en;ss>>en;
if(isder)return;
string nx;
for(int i=0;i<si(en);){
char c=en[i];
if(c=='!'){
i++;
if(en[i]=='0')nx+=' ';
else if(en[i]=='1')nx+='!';
else assert(0);
}else{
nx+=c;
}
i++;
}
np root=slv(nx,true);
roots.resize(++cur);
roots.pb(root);
};
rep(phase,n){
string type;ss>>type;
if(type=="insert"){
insert();
}else if(type=="erase"){
erase();
}else if(type=="print"){
print_ans();
}else if(type=="copy"){
copy();
}else if(type=="cut"){
cut();
}else if(type=="paste"){
paste();
}else if(type=="undo"){
if(cur>0)cur--;
}else if(type=="redo"){
if(cur+1<si(roots))cur++;
}else if(type=="serialize"){
serialize();
}else if(type=="deserialize"){
deserialize();
}else{
dmp(type);
//exit(0);
assert(false);
}
}
return roots[cur];
}
signed main(){
cin.tie(0);
ios::sync_with_stdio(0);
cout<<fixed<<setprecision(20);
if(dbg){
//while(1)slv();
}else{
//int t;cin>>t;rep(_,t)
string raw;
while(1){
string s;
if(sc.read_single(s)){
raw+=" ";
raw+=s;
}
else break;
}
slv(raw,false);
}
}
详细
Test #1:
score: 0
Stage 1: Program answer Memory Limit Exceeded
First Run Input
17 insert 0 abcdef print 0 6 erase 4 5 print 0 5 copy 0 3 paste 1 print 0 8 cut 2 4 print 0 6 undo print 0 8 paste 6 print 0 10 redo redo print 0 10 serialize
First Run Output
abcdef abcdf aabcbcdf aabcdf aabcbcdf aabcbcbcdf aabcbcbcdf !017!0insert!00!0abcdef!0print!00!06!0erase!04!05!0print!00!05!0copy!00!03!0paste!01!0print!00!08!0cut!02!04!0print!00!06!0undo!0print!00!08!0paste!06!0print!00!010!0redo!0redo!0print!00!010!0serialize