QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#300257 | #7901. Basic Substring Structure | defyers | WA | 23ms | 7952kb | C++17 | 5.1kb | 2024-01-07 22:00:34 | 2024-01-07 22:00:34 |
Judging History
answer
#include <bits/stdc++.h>
#define all(a) (a).begin(), (a).end()
#define sz(a) (int) (a).size()
#define forn(i, n) for(int i = 0; i < (n); i++)
#define int long long
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
const ULL A = 525523;
const int N = 2e5 + 11;
ULL h[N], pw[N], a[N];
ULL f(int l, int sz){
return h[l + sz] - h[l] * pw[sz];
}
bool in(int l, int r, int x){
return l <= x && x <= r;
}
struct update{
int pos, i, type;
int a, b, c;
};
vector<update> U;
void add(int i, int pos, int type, int a, int b = 0, int c = 0){
#ifdef LOCAL
cout << "ADD " << i << ' ' << pos << ' ' << type << ' ' << a << ' ' << b << ' ' << c << endl;
#endif
U.push_back({pos, i, type, a, b, c});
}
struct state{
int t, a, b;
} ST[N];
int n;
int LCS(int a, int b){
int ans = 0;
for(int j = 19; j >= 0; j--){
if(max(a, b) + (1 << j) > n) continue;
if(f(a, 1 << j) == f(b, 1 << j))
ans += (1 << j), a += (1 << j), b += (1 << j);
}
return ans;
}
void solve(){
cin >> n;
for(int i = 0; i < n; i++){
cin >> a[i];
}
fill(ST, ST + n + 1, state{0, 0});
U.clear();
h[0] = 0; for(int i = 0; i < n; i++) h[i + 1] = h[i] * A + a[i];
for(int i = 1; i < n; i++){
int L = 0, R = 0;
for(int j = 19; j >= 0; j--){
if(i + L + (1 << j) <= n && f(L, 1 << j) == f(i + L, 1 << j))
L += (1 << j);
}
for(int j = 19; j >= 0; j--){
if(i + L + 1 + R + (1 << j) <= n && f(L + 1 + R, 1 << j) == f(i + L + 1 + R, 1 << j))
R += (1 << j);
}
vector<int> sp {0, L, L + 1, L + 1 + R, i, i + L, i + L + 1, i + L + R + 1};
sort(sp.begin(), sp.end());
sp.resize(unique(all(sp)) - sp.begin());
while(!sp.empty() && sp.back() >= n) sp.pop_back();
for(auto pos : sp){
int pA = -1, pB = -1;
if(in(0, L - 1, pos)) pA = 0;
else if(in(L, L, pos)) pA = 1;
else if(in(L + 1, L + R, pos)) pA = 2;
if(in(i + 0, i + L - 1, pos)) pB = 0;
else if(in(i + L, i + L, pos)) pB = 1;
else if(in(i + L + 1, i + L + R, pos)) pB = 2;
if((pA & 2) && (pB & 2)){
add(i, pos, 1, L, 0);
}else if(pA == -1){
if(pB == 0){
add(i, pos, 1, pos - i, 1);
}else if(pB == 1){
// cout << pos << ' ' << L + 1 + R << ' ' << a[pos] << ' ' << a[i + pos] << endl;
if(pos != L + 1 + R || i + pos >= n || a[L] != a[i + pos]){
add(i, pos, 2, a[L], L + 1 + R, L);
}else{
// cout << "???" << endl;
add(i, pos, 2, a[L], L + 1 + R + 1 + LCS(L + 1 + R + 1, i + L + 1 + R + 1), L);
// cout << "SPECIAL!" << endl;
// cout << pos << ' ' << L << ' ' << 1 + R + 1 << ' ' << LCS(L + 1 + R + 1, i + L + 1 + R + 1) << endl;
}
}
}else if(pB == -1){
if(pA == 0){
add(i, pos, 1, pos, 1);
}else if(pA == 1){
add(i, pos, 2, a[i + L], L + 1 + R, L);
}
}else if(pB == 0){
add(i, pos, 1, pos - i, 1);
}else if(pA == 2 && pB == 1){
add(i, pos, 2, a[L], pos, L);
}else{
assert(false);
}
}
// cout << i << ": " << L << ' ' << R << endl;
}
sort(all(U), [](auto& x, auto& y){
return x.pos < y.pos;
});
int uid = 0;
int x = 0, dx = 0;
int xor_sum = 0;
for(int pos = 0; pos < n; pos++){
unordered_map<int, int> mp;
#ifdef LOCAL
cout << "POS = " << pos << endl;
#endif
while(uid < sz(U) && U[uid].pos == pos){
auto& u = U[uid++];
x -= ST[u.i].a + ST[u.i].b * (pos - ST[u.i].t), dx -= ST[u.i].b;
ST[u.i] = state{0, 0, 0};
if(u.type == 1){
// cout << "GRADIENT " << u.i << ' ' << u.a << ' ' << u.b << endl;
x += u.a; dx += u.b;
ST[u.i] = state{pos, u.a, u.b};
}else{
// cout << "CHANGE " << u.i << ' ' << u.a << ' ' << u.b << ' ' << u.c << endl;
x += u.c;
ST[u.i] = state{pos, u.c, 0};
mp[u.a] += u.b - u.c;
}
}
int ans = n + x, mx = 0;
for(auto [a, b] : mp){
if(a == 0) continue;
// cout << a << " => " << b << endl;
mx = max(mx, b);
}
ans += mx;
xor_sum += (ans ^ (pos + 1));
x += dx;
#ifdef LOCAL
cout << "POS = " << pos << ", " << "ANS = " << ans << '\n';
#endif
}
cout << xor_sum << '\n';
}
int32_t main(){
pw[0] = 1; for(int i = 1; i < N; i++) pw[i] = pw[i - 1] * A;
cin.tie(0)->sync_with_stdio(false);
int t; cin >> t;
while(t--){
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 7952kb
input:
2 4 2 1 1 2 12 1 1 4 5 1 4 1 9 1 9 8 10
output:
15 217
result:
ok 2 lines
Test #2:
score: -100
Wrong Answer
time: 23ms
memory: 7640kb
input:
10000 8 2 1 2 1 1 1 2 2 9 2 2 1 2 1 2 1 2 1 15 2 1 2 1 1 1 1 2 2 1 2 1 2 2 1 2 1 1 10 2 1 1 1 2 2 1 1 2 2 3 2 1 2 11 1 2 2 1 1 2 1 2 2 1 1 14 2 1 1 1 1 2 1 1 1 2 2 1 2 1 12 2 2 2 1 2 2 2 1 1 2 1 2 4 2 1 1 2 8 1 2 2 2 1 2 1 1 8 1 1 2 1 2 1 1 1 6 2 1 1 1 2 2 14 2 2 1 1 1 1 2 2 2 1 2 2 1 1 10 1 2 2 1 1...
output:
94 128 347 3 208 9 259 363 278 16 92 113 58 348 225 3 335 364 377 316 3 19 122 66 16 97 36 258 11 63 28 91 85 103 253 191 21 48 303 63 101 20 24 68 316 362 266 309 355 281 326 281 231 312 3 330 59 328 3 68 32 147 322 40 343 95 242 3 165 346 245 20 152 3 404 393 392 82 270 360 20 59 21 272 3 18 351 3...
result:
wrong answer 5th lines differ - expected: '211', found: '208'