QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#665984#5541. Substring Sortkai824TL 1045ms18836kbC++173.4kb2024-10-22 16:08:212024-10-22 16:08:29

Judging History

你现在查看的是最新测评结果

  • [2024-10-22 16:08:29]
  • 评测
  • 测评结果:TL
  • 用时:1045ms
  • 内存:18836kb
  • [2024-10-22 16:08:21]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#pragma O3
// #define int long long
typedef long long ll;
#define eb emplace_back
#define pb push_back
#define mp make_pair
typedef pair<int,int> pii;
#define f first
#define s second
#define rep(i,a,b) for(int i=a;i<(b);i++)

ll poww[100005];
ll mod=1e9+7,K=31;

struct Node{
  Node *l=0, *r=0;
  ll val,hash;
  int y,c=1;
  Node(int val): val(val), y(rand()) {};
  void recalc();
  void dfs(){
    if(l)l->dfs();
    cout<<(char)('a'+val);
    if(r)r->dfs();
  }
};
int cnt(Node *n){return n? n->c:0;}
void Node::recalc(){
  int cl=cnt(l),cr=cnt(r);
  c=cl+cr+1;
  //TODO: get hash
  hash=0;
  if(cl){
    hash+=l->hash;
  }
  hash+=(poww[cl+1]*val)%mod;
  hash%=mod;
  if(cr){
    hash+=(poww[cl+1]*r->hash)%mod;
    hash%=mod;
  }
}
pair<Node*,Node*>split(Node* n,int k){
  if(!n)return {};
  if(cnt(n->l)>=k){
    auto pa = split(n->l,k);
    n->l = pa.second;
    n->recalc();
    return {pa.first,n};
  }else{
    auto pa=split(n->r,k-cnt(n->l)-1);
    n->r=pa.first;
    n->recalc();
    return {n,pa.second};
  }
}

Node* merge(Node* l, Node* r){
  if(!l)return r;
  if(!r)return l;
  if(l->y > r->y){
    l->r = merge(l->r,r);
    l->recalc();
    return l;
  }else {
    r->l = merge(l, r->l);
    r->recalc();
    return r;
  }
}

Node* parse(string s){
  Node* tmp=new Node(s[0]-'a');
  for(int i=1;i<s.length();i++){
    tmp = merge(tmp,new Node(s[i]-'a') );
  }
  return tmp;
}

//return if (a<=b)
bool compare(Node* a,Node *b){
  if(a->hash == b->hash)return 1;
  Node *al,*ar;
  Node *bl,*br;
  Node *tmp;
  int lo,hi,mid;
  lo=1,hi=cnt(a);//bs for 1-index of first different ch
  while(lo<hi){
    mid=lo+(hi-lo)/2;
    tie(al,ar) = split(a,mid);
    tie(bl,br) = split(b,mid);
    if(al->hash == bl->hash){
      lo=mid+1;
    }else{
      hi=mid;
    }
    a=merge(al,ar);
    b=merge(bl,br);
  }
  // cout<<"100 "<<lo<<'\n';

  tie(al,ar)=split(a,lo-1);
  tie(bl,br)=split(b,lo-1);

  int a_ch;
  int b_ch;
  tie(tmp,ar) = split(ar,1);
  a_ch = tmp->val;
  ar=merge(tmp,ar);
  tie(tmp,br) = split(br,1);
  b_ch = tmp->val;
  br=merge(tmp,br);

  a=merge(al,ar);
  b=merge(bl,br);

  // a->dfs();cout<<' ';b->dfs();cout<<' ';cout<<a_ch<<' '<<b_ch<<'\n';

  return a_ch<=b_ch;
}

int32_t main(){
  ios_base::sync_with_stdio(false);cin.tie(0);
  int n,q;
  cin>>n>>q;
  poww[0]=1;
  for(int i=1;i<=n;i++){
    poww[i]=(poww[i-1]*K)%mod;
  }
  string a,b,c;
  cin>>a>>b>>c;
  Node *A,*B,*C;
  A=parse(a);
  B=parse(b);
  C=parse(c);


  // A->dfs();cout<<'\n';
  // B->dfs();cout<<'\n';
  // C->dfs();cout<<'\n';


  while(q--){
    int l,r;
    cin>>l>>r;

    Node* al,*am,*ar;
    tie(al,am) = split(A,l-1);
    tie(am,ar) = split(am, r-l+1);
    Node* bl,*bm,*br;
    tie(bl,bm) = split(B,l-1);
    tie(bm,br) = split(bm, r-l+1);
    Node* cl,*cm,*cr;
    tie(cl,cm) = split(C,l-1);
    tie(cm,cr) = split(cm, r-l+1);
    if(!compare(am,bm)){
      swap(am,bm);
    }
    if(!compare(bm,cm)){
      swap(bm,cm);
    }
    if(!compare(am,bm)){
      swap(am,bm);
    }
    A = merge(al,merge(am,ar));
    B = merge(bl,merge(bm,br));
    C = merge(cl,merge(cm,cr));


    // A->dfs();cout<<'\n';
    // B->dfs();cout<<'\n';
    // C->dfs();cout<<'\n';
    // cout<<'\n';
  }
  A->dfs();cout<<'\n';
  B->dfs();cout<<'\n';
  C->dfs();cout<<'\n';
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3476kb

input:

5 2
icpca
siaja
karta
2 4
1 5

output:

iarta
kiaja
scpca

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 0ms
memory: 3572kb

input:

6 6
aabbcc
bcacab
cbcaba
1 1
2 2
3 3
4 4
5 5
6 6

output:

aaaaaa
bbbbbb
cccccc

result:

ok 3 lines

Test #3:

score: 0
Accepted
time: 0ms
memory: 3456kb

input:

3 1
aba
aab
aac
1 3

output:

aab
aac
aba

result:

ok 3 lines

Test #4:

score: 0
Accepted
time: 0ms
memory: 3808kb

input:

1 1
z
y
x
1 1

output:

x
y
z

result:

ok 3 lines

Test #5:

score: 0
Accepted
time: 1045ms
memory: 18836kb

input:

100000 100000
llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll...

output:

llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll...

result:

ok 3 lines

Test #6:

score: -100
Time Limit Exceeded

input:

100000 100000
ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttottttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt...

output:


result: