QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#657804 | #7036. Can You Solve the Harder Problem? | veg# | WA | 2092ms | 70064kb | C++17 | 2.5kb | 2024-10-19 15:29:28 | 2024-10-19 15:29:29 |
Judging History
answer
#include <bits/stdc++.h>
#define rint int
using namespace std;
const int maxn = 1000005;
int LOG[maxn];
int sa[maxn];
int bar[maxn];
int fir[maxn];
int sec[maxn<<1];
int rk[maxn];
int hei[maxn];
int s[maxn];
int pre[maxn];
int lsh[maxn];
int nxt[maxn];
int sta[maxn];
int n,m, sss[maxn];
int top, stk[maxn];
long long sum[maxn];
int st[maxn][21];
void qsort()
{
memset(bar,0,m+1<<2);
for(rint i=1;i<=n;i++) bar[fir[i]]++;
for(rint i=1;i<=m;i++) bar[i]+=bar[i-1];
for(rint i=n;i>=1;i--) sa[bar[fir[sec[i]]]--]=sec[i];
}
void get_sa()
{
for(rint i=1;i<=n;i++) fir[i]=s[i],sec[i]=i;
qsort();
for(rint k=1;;k<<=1)
{
rint num=0;
for(rint i=1;i<=k;i++) sec[++num]=n-k+i;
for(rint i=1;i<=n;i++) if(sa[i]>k) sec[++num]=sa[i]-k;
qsort();
memcpy(sec,fir,sizeof(fir));
fir[sa[1]]=m=1;
for(rint i=2;i<=n;i++)
fir[sa[i]]=sec[sa[i]]==sec[sa[i-1]]&&sec[sa[i]+k]==sec[sa[i-1]+k]?m:++m;
if(m==n) return;
}
}
void get_hei()
{
for(rint i=1;i<=n;i++) rk[sa[i]]=i;
for(rint i=1,k=0;i<=n;i++)
{
if(k) k--;
rint j=sa[rk[i]-1];
while(s[i+k]==s[j+k]) k++;
hei[rk[i]]=k;
}
for (int i = 1; i <= n; ++i)
st[i][0] = hei[i];
for (int j = 1; j <= 20; ++j)
for (int i = 1; i <= n; ++i)
st[i][j] = min(st[i][j - 1], st[i + (1 << j - 1)][j - 1]);
}
int get_lcp(int x, int y) {
int len = LOG[y - x + 1];
return min(st[x][len], st[y - (1 << len) + 1][len]);
}
int main() {
LOG[0] = -1;
for (int i = 1; i <= 1000000; ++i) LOG[i] = LOG[i >> 1] + 1;
int T;
cin >> T;
while (T--) {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &s[i]);
lsh[i]=s[i];
sss[i] = s[i];
}
sort(lsh+1,lsh+n+1);
m=unique(lsh+1,lsh+n+1)-lsh-1;
for(rint i=1;i<=n;i++)
s[i]=lower_bound(lsh+1,lsh+m+1,s[i])-lsh;
get_sa(),get_hei();
for (int i = 1; i <= n; ++i) s[i] = sss[i];
set<int> ss;
long long ans = 0;
int top = 0;
stk[0] = n + 1;
for (int i = n; i >= 1; --i) {
while (top && s[i] >= s[stk[top]]) --top;
stk[++top] = i;
sum[top] = sum[top - 1] + 1ll * s[i] * (stk[top - 1] - i);
int lcp = 0;
auto up = ss.upper_bound(rk[i]);
if (up != ss.end()) lcp = max(lcp, get_lcp(rk[i] + 1, *up));
if (up != ss.begin()) --up, lcp = max(lcp, get_lcp(*up + 1, rk[i]));
ss.insert(rk[i]);
int idx = lower_bound(stk + 1, stk + top + 1, i + lcp, greater<int>()) - stk;
ans += sum[idx - 1] + 1ll * s[i] * (stk[idx - 1] - i - lcp);
}
cout << ans << endl;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 35572kb
input:
2 3 1 2 3 3 2 3 3
output:
14 14
result:
ok 2 lines
Test #2:
score: -100
Wrong Answer
time: 2092ms
memory: 70064kb
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:
17377191496 484028407812 1469039857 473738852141 488918871992 305434410403 486113781267 492677947133 480937654493 480626723547 34438338505 225763899284 492865597618 483469889212 485195159959 481278694257 341190824044 491752583456 487005053159 9765137023 490649783980 120812601996 486619980906 4668715...
result:
wrong answer 1st lines differ - expected: '17377263880', found: '17377191496'