QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#559870 | #8527. Power Divisions | FDUdululu | WA | 41ms | 91860kb | C++14 | 5.0kb | 2024-09-12 10:04:17 | 2024-09-12 10:04:17 |
Judging History
answer
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const ll seed = 31;
const int N = 2e6;
// const ll P = (1ll << 61) - 1;
const ll P = mod;
int n, a[N];
ll coef[N], sum[N];
int pre[N], suf[N];
unordered_map<ll, int> vis;
int sta[N], top;
vector<int> seq[N];
ll f[N];
int flag[N];
void calc1(int l, int r) {
if (l == r) {
seq[r].push_back(l);
return;
}
int mid = (l + r) >> 1;
calc1(l, mid);
calc1(mid + 1, r);
ll sufhash = 0;
for (int i = mid; i >= l; i--) {
int cur = a[i];
while (suf[cur]) {
suf[cur] = 0;
sufhash = (sufhash - coef[cur] + P) % P;
cur++;
}
suf[cur] = 1;
sufhash = (sufhash + coef[cur]) % P;
sta[++top] = cur;
vis[sufhash] = i;
// cout << "$ " << i << " " << mid << " " << sufhash << "\n";
}
while (top)
suf[sta[top--]] = 0;
ll prehash = 0;
int minn = N, maxn = -1;
for (int i = mid + 1; i <= r; i++) {
int cur = a[i];
while (pre[cur]) {
pre[cur] = 0;
prehash = (prehash - coef[cur] + P) % P;
cur++;
}
pre[cur] = 1;
prehash = (prehash + coef[cur]) % P;
sta[++top] = cur;
maxn = max(maxn, cur);
minn = min(minn, cur);
while (!pre[minn])
minn++;
// cout << "#! " << minn << " " << maxn << " " << prehash << "\n";
ll val = sum[maxn];
if (minn)
val = (val - sum[minn - 1] + P) % P;
val = (val + coef[minn]) % P;
val = (val - prehash + P) % P;
// cout << "#@ " << l << " " << mid << " " << r << " " << d << " " << i << " " << val << "\n";
if (vis.count(val)) {
// cout << "? " << l << " " << r << " " << 1 << " | " << vis[val] << " " << i << " \n";
seq[i].push_back(vis[val]); //, cout << vis[val] << " " << i << "\n";
}
}
while (top)
pre[sta[top--]] = 0;
vis.clear();
}
void calc2(int l, int r) {
if (l == r) {
seq[r].push_back(l);
return;
}
int mid = (l + r) >> 1;
calc2(l, mid);
calc2(mid + 1, r);
ll prehash = 0;
for (int i = mid + 1; i <= r; i++) {
int cur = a[i];
while (pre[cur]) {
pre[cur] = 0;
prehash = (prehash - coef[cur] + P) % P;
cur++;
}
pre[cur] = 1;
prehash = (prehash + coef[cur]) % P;
sta[++top] = cur;
vis[prehash] = i;
}
while (top)
pre[sta[top--]] = 0;
ll sufhash = 0;
int minn = N, maxn = -1;
for (int i = mid; i >= l; i--) {
int cur = a[i];
while (suf[cur]) {
suf[cur] = 0;
sufhash = (sufhash - coef[cur] + P) % P;
cur++;
}
suf[cur] = 1;
sufhash = (sufhash + coef[cur]) % P;
sta[++top] = cur;
// cout << "$ " << i << " " << mid << " " << sufhash << "\n";
maxn = max(maxn, cur);
minn = min(minn, cur);
while (!suf[minn])
minn++;
// cout << "#! " << minn << " " << maxn << " " << prehash << "\n";
ll val = sum[maxn];
if (minn)
val = (val - sum[minn - 1] + P) % P;
val = (val + coef[minn]) % P;
val = (val - sufhash + P) % P;
// cout << "#@ " << l << " " << mid << " " << r << " " << d << " " << i << " " << val << "\n";
if (vis.count(val)) {
// cout << "? " << l << " " << r << " " << 2 << " | " << i << " " << vis[val] << " \n";
seq[vis[val]].push_back(i); //, cout << vis[val] << " " << i << "\n";
}
}
while (top)
suf[sta[top--]] = 0;
vis.clear();
}
void solve() {
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
ll mul = 1;
for (int i = 0; i < N; i++)
coef[i] = i;
random_shuffle(coef + 1, coef + N);
for (int i = 0; i < N; i++) {
coef[i] = coef[i] * mul % mod;
sum[i] = coef[i];
if (i)
sum[i] = (sum[i] + sum[i - 1]) % mod;
mul = mul * seed % mod;
}
calc1(1, n);
// reverse(a + 1, a + n + 1);
calc2(1, n);
f[0] = 1;
for (int i = 1; i <= n; i++) {
// cout << "! " << i << "\n";
for (auto j : seq[i]) {
if (flag[j])
continue;
f[i] = (f[i] + f[j - 1]) % mod;
flag[j] = 1;
// cout << j << " ";
}
// cout << "\n";
for (auto j : seq[i])
flag[j] = 0;
}
cout << f[n] << "\n";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T = 1;
// cin >> T;
while (T--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 41ms
memory: 89588kb
input:
5 2 0 0 1 1
output:
6
result:
ok 1 number(s): "6"
Test #2:
score: 0
Accepted
time: 23ms
memory: 89580kb
input:
1 0
output:
1
result:
ok 1 number(s): "1"
Test #3:
score: 0
Accepted
time: 37ms
memory: 89880kb
input:
2 1 1
output:
2
result:
ok 1 number(s): "2"
Test #4:
score: 0
Accepted
time: 36ms
memory: 89592kb
input:
3 2 1 1
output:
3
result:
ok 1 number(s): "3"
Test #5:
score: 0
Accepted
time: 37ms
memory: 91860kb
input:
4 3 2 2 3
output:
4
result:
ok 1 number(s): "4"
Test #6:
score: 0
Accepted
time: 32ms
memory: 89584kb
input:
5 3 4 4 2 4
output:
2
result:
ok 1 number(s): "2"
Test #7:
score: 0
Accepted
time: 32ms
memory: 91696kb
input:
7 3 4 3 5 6 3 4
output:
6
result:
ok 1 number(s): "6"
Test #8:
score: 0
Accepted
time: 33ms
memory: 89656kb
input:
10 8 6 5 6 7 8 6 8 9 9
output:
4
result:
ok 1 number(s): "4"
Test #9:
score: -100
Wrong Answer
time: 33ms
memory: 89688kb
input:
96 5 1 0 2 5 5 2 4 2 4 4 2 3 4 0 2 1 4 3 1 2 0 2 2 3 2 4 5 3 5 2 0 2 2 5 3 0 4 5 3 5 4 4 3 1 2 0 5 4 5 0 2 3 2 4 0 0 4 2 0 2 5 3 3 1 5 5 1 1 1 0 5 0 3 0 2 1 1 0 5 0 3 3 4 4 5 3 0 2 2 0 5 4 5 0 5
output:
414903507
result:
wrong answer 1st numbers differ - expected: '11332014', found: '414903507'