QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#404214 | #7036. Can You Solve the Harder Problem? | shstyle | AC ✓ | 1377ms | 130660kb | C++20 | 3.1kb | 2024-05-03 16:11:33 | 2024-05-03 16:11:50 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
#define int long long
typedef long long ll;
typedef pair<ll,ll> PII;
int n,len1,len2;
char s1[N],s2[N];
int a[N];
int aa[N];
int sa[N],height[N],rnk[N],id[N],oldrnk[N<<1];
int key1[N],cnt[N];
// int st[N][20];
int ST[N][30];
int le[N],ri[N];
bool cmp(int x,int y,int w){
return (oldrnk[x]==oldrnk[y])&&(oldrnk[x+w]==oldrnk[y+w]);
}
void getsa(int s[]){
for(int i=0;i<=(n<<1);i++){
sa[i]=0,height[i]=0,rnk[i]=0,id[i]=0,oldrnk[i]=0,key1[i]=0,cnt[i]=0;
}
int m=n+1,p,w;
for(int i=1;i<=n;i++){
rnk[i]=s[i];
cnt[rnk[i]]++;
}
for(int i=1;i<=m;i++) cnt[i]+=cnt[i-1];
for(int i=n;i>=1;i--){//对长度为1的关键字桶排序
sa[cnt[rnk[i]]--]=i;
}
for(w=1;;w<<=1,m=p){
p=0;
for(int i=n;i>n-w;i--) id[++p]=i;
for(int i=1;i<=n;i++){
if(sa[i]>w) id[++p]=sa[i]-w;
}
for(int i=0;i<=n+1;i++) cnt[i]=0;
for(int i=1;i<=n;i++){
key1[i]=rnk[id[i]];
cnt[key1[i]]++;
}
for(int i=1;i<=m;i++) cnt[i]+=cnt[i-1];
for(int i=n;i>=1;i--){
sa[cnt[key1[i]]--]=id[i];
}
for(int i=1;i<=n;i++) oldrnk[i]=rnk[i];
p=0;
for(int i=1;i<=n;i++){
if(cmp(sa[i],sa[i-1],w)){
rnk[sa[i]]=p;
}else{
rnk[sa[i]]=++p;
}
}
if(p==n) break;
}
for(int i=1,k=0;i<=n;i++){
if(rnk[i]==0) continue;
if(k) k--;
while(s[i+k]==s[sa[rnk[i]-1]+k]) k++;
height[rnk[i]]=k;
}
}
//ll tr[N];
int ne[N];
ll st[N],tt=-1;
ll res[N];
int qr(int l,int r){
int res=1,len=0;
while((res<<1)<r-l+1) res<<=1,len++;
return max(ST[l][len],ST[r-res+1][len]);
}
int gao(int x,int val){
if(x>n||qr(x,n)<=val) return n+1;
int l=x,r=n;
while(l<r){
int mid=l+r>>1;
if(qr(x,mid)>val) r=mid;
else l=mid+1;
}
return l;
}
void __(){
tt=0;
scanf("%lld",&n);
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
a[n+1]=1e9;
vector<int> sg;
for(int i=1;i<=n;i++) sg.push_back(a[i]);
sort(sg.begin(),sg.end());
sg.erase(unique(sg.begin(),sg.end()),sg.end());
for(int i=1;i<=n;i++){
aa[i]=lower_bound(sg.begin(),sg.end(),a[i])-sg.begin()+1;
// cout<<aa[i]<<" ";
}
getsa(aa);
bool flag=0;
tt=0;
st[0]=n+1;
for(int i=1;i<=n+1;i++) ST[i][0]=a[i];
// cout<<(1<<16)<<endl;
for(int i=1;i<20;i++){
for(int j=1;j<=n+1;j++){
ST[j][i]=ST[j][i-1];
if(j+(1<<(i-1))<=n) ST[j][i]=max(ST[j][i],ST[j+(1<<(i-1))][i-1]);
}
}
ll ans=0;
ll tot=0;
st[0]=n+1;
res[n+1]=0;
for(int i=n;i>=1;i--){
int ne=gao(i+1,a[i]);
res[i]=res[ne]+(ne-i)*a[i];
//cout<<res[i]<<" "<<res[st[tt]]<<endl;
}
// cout<<endl;
for(int i=n;i>=1;i--){
ll ht=height[rnk[i]];
if(ht==0){
ans+=res[i];
continue;
}
int mx=qr(i,i+ht-1);
int j=gao(i+ht,mx);
int ii=i+ht;
ans+=(j-ii)*mx+res[j];
}
for(int i=0;i<=(n<<1);i++){
sa[i]=0,height[i]=0,rnk[i]=0,id[i]=0,oldrnk[i]=0,key1[i]=0,cnt[i]=0;
for(int j=0;j<20;j++) ST[i][j]=0;
res[i]=0;
aa[i]=0;
}
printf("%lld\n",ans);
}
signed main(){
int _;
cin>>_;
while(_--){
__();
// if(_==400) cout<<n<<endl;
}
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3908kb
input:
2 3 1 2 3 3 2 3 3
output:
14 14
result:
ok 2 lines
Test #2:
score: 0
Accepted
time: 1237ms
memory: 130616kb
input:
1000 407 205460 200518 200561 199235 198814 198136 198802 206763 198795 200705 205862 209366 204017 209481 206300 198499 197364 200897 208928 196983 205605 206396 205140 201050 199886 207314 196947 207905 204999 203288 200298 198594 198286 197714 206506 203962 197628 201127 206380 199090 200711 2063...
output:
17377263880 484769420621 1469039857 473739194467 490764968536 305434410403 488123854116 494441224927 484272937511 482411215077 34438338505 225764586244 494494400870 492874976740 486779601551 483167680203 341192630235 492747353987 491280077388 9765154255 491745355442 120812820486 487737922489 4725166...
result:
ok 1000 lines
Test #3:
score: 0
Accepted
time: 1377ms
memory: 130572kb
input:
1000 989 507102 732180 353998 101710 542881 118374 431064 689823 413523 117027 629973 750571 190925 749991 433716 360981 112209 548712 201721 316123 391501 242087 419388 181933 456503 655757 417087 739256 442318 78753 243152 491702 215002 645259 568188 663372 389158 380483 792508 586353 739459 90306...
output:
454746757256 28339718425 453884630039 128591729064 448111178045 75284968670 445747071452 279896702194 445704609778 167973786530 371872311621 70407640704 428085383449 147419726815 282581591005 445036870282 198428498846 802046 455258988212 196913576758 21403552866 424994895807 188380097 448477259353 3...
result:
ok 1000 lines
Test #4:
score: 0
Accepted
time: 1175ms
memory: 130660kb
input:
1000 982 590062 597319 244109 393500 687086 311683 519273 222529 165273 443310 33318 251697 850506 177933 423587 60261 567513 185789 315612 389005 173138 466085 358614 249736 451662 287939 154325 674678 302122 631230 72255 586340 454064 425737 424534 726084 45495 607376 248020 509440 122093 701062 5...
output:
428903041682 465564876399 448066404349 437820728634 453686981834 433070617806 187074086885 450111902263 463869202505 125071818152 448570062058 441205083218 456280814880 421177986909 2089294777 279427869076 444963732958 475010632100 450118276001 9994609936 436741023150 456098463889 17896078275738529 ...
result:
ok 1000 lines
Test #5:
score: 0
Accepted
time: 1259ms
memory: 130464kb
input:
1000 985 372606 380156 684655 641468 555433 141926 469854 168775 303961 679291 178219 572619 281395 343523 756186 666170 749512 788286 463868 110160 864845 220706 847501 30848 482661 58326 888652 655505 733551 195467 488714 19609 571337 2327 793288 93419 348702 159592 376399 553701 90541 374944 5081...
output:
450803381706 438582982237 440295491979 449150481542 448335033699 64578055878 465937363239 37665830325 219124838416 465658512161 58052329213 453065801876 444369207459 61516176426 201637236439 194444511404 14273492638 464013523017 43346256749 552428282 3953219456 73155598864 5017635401 38965458377 108...
result:
ok 1000 lines