QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#515101 | #8527. Power Divisions | ngpin04 | WA | 10ms | 42144kb | C++14 | 4.5kb | 2024-08-11 15:12:31 | 2024-08-11 15:12:33 |
Judging History
answer
#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define bit(n) (1LL << (n))
#define getbit(x, i) (((x) >> (i)) & 1)
#define pii pair<int, int>
#define ALL(x) (x).begin(), (x).end()
#define sz(x) ((int) (x).size())
using namespace std;
const int M = 5e5 + 5;
const int mod = 1e9 + 7;
const int oo = 1e9;
const long long ooo = 1e18;
const double pi = acos(-1);
template<typename T1, typename T2> bool mini(T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi(T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}
mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
typedef unsigned long long ull;
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef LOCAL
freopen("file.inp", "r",stdin);
#endif
int n; cin >> n;
vector<int> a(n);
for (int &x : a) {
cin >> x;
x++;
}
vector<vector<int>> cand(n);
vector<int> dp(n + 1);
const int N = 2e6 + 5;
vector<int> cnt(N, 0);
vector<ull> hasher(N, 0), sum(N, 0);
for (int i = 1; i < N; i++) {
hasher[i] = rd();
sum[i] = hasher[i] + sum[i - 1];
}
auto getsum = [&](int l, int r) -> ull {
return sum[r] - sum[l - 1];
};
function<void(int, int)> solve = [&](int l, int r) -> void {
if (l == r) {
cand[r].push_back(r + 1);
return;
}
int mid = (l + r) >> 1;
int mx = -oo, mn = oo;
vector<int> vis;
set<pair<ull, int>> posl, posr;
ull cur = 0;
// cerr << l << " " << r << " " << mid << "\n";
for (int i = mid; i >= l; i--) {
maxi(mx, a[i]);
mini(mn, a[i]);
vis.push_back(a[i]);
cnt[a[i]]++;
cur += hasher[a[i]];
maxi(mx, a[i]);
int j = a[i];
while (cnt[j] > 1) {
if (j == mn) {
mn++;
maxi(mx, mn);
}
cnt[j] = 0;
cur -= 2 * hasher[j];
cnt[j + 1]++;
cur += hasher[j + 1];
vis.push_back(j + 1);
j++;
}
// cerr << "at " << i << ": " << cur << " " << getsum(mn, mx) - cur + hasher[mn] << " " << mn << " " << mx << "\n";
ull rev = getsum(mn, mx) - cur + hasher[mn];
posl.insert({cur, i});
posr.insert({rev, i});
}
for (int x : vis) {
cnt[x] = 0;
}
vis.clear();
mn = oo, mx = -oo;
cur = 0;
for (int i = mid + 1; i <= r; i++) {
maxi(mx, a[i]);
mini(mn, a[i]);
vis.push_back(a[i]);
cnt[a[i]]++;
cur += hasher[a[i]];
maxi(mx, a[i]);
int j = a[i];
while (cnt[j] > 1) {
if (j == mn) {
mn++;
maxi(mx, mn);
}
cnt[j] = 0;
cur -= 2 * hasher[j];
cnt[j + 1]++;
cur += hasher[j + 1];
vis.push_back(j + 1);
j++;
}
// cerr << "at " << i << ": " << cur << " " << getsum(mn, mx) - cur + hasher[mn] << " " << mn << " " << mx << "\n";
for (auto it = posr.lower_bound({cur, -oo}); it != posr.end() && it->fi == cur; it++) {
cand[it->se].push_back(i + 1);
}
ull rev = getsum(mn, mx) - cur + hasher[mn];
if (mn < mx) {
for (auto it = posr.lower_bound({rev, -oo}); it != posr.end() && it->fi == rev; it++) {
cand[it->se].push_back(i + 1);
}
}
}
for (int x : vis) {
cnt[x] = 0;
}
solve(l, mid);
solve(mid + 1, r);
};
solve(0, n - 1);
auto add = [&](int &a, int b) -> void {
a += b;
if (a >= mod)
a -= mod;
};
dp[0] = 1;
for (int i = 0; i < n; i++) {
for (int j : cand[i]) {
// cerr << i << " " << j - 1 << "\n";
add(dp[j], dp[i]);
}
}
cout << dp[n] << "\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 5ms
memory: 42128kb
input:
5 2 0 0 1 1
output:
6
result:
ok 1 number(s): "6"
Test #2:
score: 0
Accepted
time: 10ms
memory: 42140kb
input:
1 0
output:
1
result:
ok 1 number(s): "1"
Test #3:
score: 0
Accepted
time: 4ms
memory: 42124kb
input:
2 1 1
output:
2
result:
ok 1 number(s): "2"
Test #4:
score: 0
Accepted
time: 7ms
memory: 42144kb
input:
3 2 1 1
output:
3
result:
ok 1 number(s): "3"
Test #5:
score: -100
Wrong Answer
time: 7ms
memory: 42124kb
input:
4 3 2 2 3
output:
5
result:
wrong answer 1st numbers differ - expected: '4', found: '5'