QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#665966 | #5541. Substring Sort | kai824# | WA | 0ms | 3732kb | C++17 | 3.4kb | 2024-10-22 16:03:27 | 2024-10-22 16:03:38 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
#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++)
int poww[100005];
int mod=1e9+7,K=31;
struct Node{
Node *l=0, *r=0;
int 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(a->hash == b->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: 3524kb
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: 3732kb
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: -100
Wrong Answer
time: 0ms
memory: 3532kb
input:
3 1 aba aab aac 1 3
output:
aba aab aac
result:
wrong answer 1st lines differ - expected: 'aab', found: 'aba'