QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#255534 | #7036. Can You Solve the Harder Problem? | supepapupu | WA | 770ms | 28192kb | C++17 | 3.5kb | 2023-11-18 16:14:32 | 2023-11-18 16:14:32 |
Judging History
answer
#include <bits/stdc++.h>
#define x first
#define y second
#define el '\n'
#define debug(x) cout << #x << ": " << x << el
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int N = 2e5 + 10, MAXP = 20;
int n, m;
// sa[i]: 排名第i的后缀, rk[i]: 第i个后缀的排名, height[i]: sa[i]和sa[i - 1]的最长公共前缀
int a[N];
int sa[N], rk[N], height[N];
int c[N], x[N], y[N];
vector<int> nums;
int get(int x) {
return lower_bound(nums.begin(), nums.end(), x) - nums.begin() + 1;
}
void get_sa() {
for (int i = 1; i <= n; ++i) c[i] = 0;
for (int i = 1; i <= n; ++i) ++c[x[i] = get(a[i])];
for (int i = 2; i <= m; ++i) c[i] += c[i - 1];
for (int i = n; i; --i) sa[c[x[i]]--] = i;
for (int k = 1; k <= n; k <<= 1) {
int num = 0;
for (int i = n - k + 1; i <= n; ++i) y[++num] = i;
for (int i = 1; i <= n; ++i)
if (sa[i] > k)
y[++num] = sa[i] - k;
for (int i = 1; i <= m; ++i) c[i] = 0;
for (int i = 1; i <= n; ++i) ++c[x[i]];
for (int i = 2; i <= m; ++i) c[i] += c[i - 1];
for (int i = n; i; --i) sa[c[x[y[i]]]--] = y[i], y[i] = 0;
swap(x, y);
x[sa[1]] = 1, num = 1;
for (int i = 2; i <= n; ++i)
x[sa[i]] = (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k]) ? num : ++num;
if (num == n) break;
m = num;
}
for (int i = 1; i <= n; ++i) rk[sa[i]] = i;
}
void get_height() {
for (int i = 1, k = 0; i <= n; ++i) {
if (rk[i] == 1) continue;
if (k) --k;
int j = sa[rk[i] - 1];
while (i + k <= n && j + k <= n && a[i + k] == a[j + k]) ++k;
height[rk[i]] = k;
}
}
int len[N];
ll s[N];
int logn[N];
int mx[N][MAXP];
void init() {
for (int i = 2; i <= n; ++i) logn[i] = logn[i >> 1] + 1;
for (int j = 0; j < MAXP; ++j)
for (int i = 1; i + (1 << j) - 1 <= n; ++i)
if (!j) mx[i][j] = i;
else {
if (a[mx[i][j - 1]] > a[mx[i + (1 << j - 1)][j - 1]]) mx[i][j] = mx[i][j - 1];
else mx[i][j] = mx[i + (1 << j - 1)][j - 1];
}
}
int query(int l, int r) {
int k = logn[r - l + 1];
if (a[mx[l][k]] > a[mx[r - (1 << k) + 1][k]]) return mx[l][k];
else return mx[r - (1 << k) + 1][k];
}
void solve() {
cin >> n;
nums.clear();
for (int i = 1; i <= n; ++i) {
cin >> a[i], nums.emplace_back(a[i]);
}
sort(nums.begin(), nums.end());
nums.erase(unique(nums.begin(), nums.end()), nums.end());
m = nums.size();
get_sa(), get_height();
init();
for (int i = n; i; --i) {
int l = 1, r = n - i + 1, mid;
while (l < r) {
mid = l + r + 1 >> 1;
if (query(i, i + mid - 1) == i) l = mid;
else r = mid - 1;
}
len[i] = l;
if (i + len[i] == n + 1) s[i] = (ll)len[i] * a[i];
else {
s[i] = s[i + len[i]] + (ll)len[i] * a[i];
}
}
ll ans = 0;
for (int i = 1; i <= n; ++i) {
int x = sa[i];
if (!height[i]) ans += s[x];
else {
int l = x, r = x + height[i] - 1;
int p = query(l, r);
int q = p + len[p];
ans += (ll)a[p] * (q - r - 1) + s[q];
}
}
cout << ans << el;
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int tcase = 1;
cin >> tcase;
while (tcase--) solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 9720kb
input:
2 3 1 2 3 3 2 3 3
output:
14 14
result:
ok 2 lines
Test #2:
score: -100
Wrong Answer
time: 770ms
memory: 28192kb
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 488127755308 494441224927 484296291485 482412939539 34438338505 225764586244 494494743942 492874976740 486797693660 483188558991 341192630235 492749075293 491282417200 9765154255 491746039063 120812820486 487747371061 4725366...
result:
wrong answer 7th lines differ - expected: '488123854116', found: '488127755308'