QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#559526#8527. Power DivisionsFDUdululuWA 23ms87792kbC++143.3kb2024-09-11 22:53:442024-09-11 22:53:44

Judging History

你现在查看的是最新测评结果

  • [2024-09-11 22:53:44]
  • 评测
  • 测评结果:WA
  • 用时:23ms
  • 内存:87792kb
  • [2024-09-11 22:53:44]
  • 提交

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;

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 calc(int l, int r, int d) {
    if (l == r) {
        seq[r].push_back(l);
        return;
    }
    int mid = (l + r) >> 1;
    calc(l, mid, d);
    calc(mid + 1, r, d);

    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;
    }
    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 << " " << r << " " << d << " " << i << " " << val << "\n";
        if (vis.count(val)) {
            // cout << "? " << l << " " << r << " " << d << " " << i << " " << vis[val] << " | ";
            if (d)
                seq[n - vis[val] + 1].push_back(n - i + 1);  //, cout << n - i + 1 << " " << n - vis[val] + 1 << "\n";
            else
                seq[i].push_back(vis[val]);  //, cout << vis[val] << " " << i << "\n";
        }
    }
    while (top)
        pre[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;
    }

    calc(1, n, 0);
    reverse(a + 1, a + n + 1);
    calc(1, n, 1);

    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: 23ms
memory: 86548kb

input:

5
2 0 0 1 1

output:

6

result:

ok 1 number(s): "6"

Test #2:

score: 0
Accepted
time: 21ms
memory: 84776kb

input:

1
0

output:

1

result:

ok 1 number(s): "1"

Test #3:

score: 0
Accepted
time: 19ms
memory: 86356kb

input:

2
1 1

output:

2

result:

ok 1 number(s): "2"

Test #4:

score: 0
Accepted
time: 23ms
memory: 87792kb

input:

3
2 1 1

output:

3

result:

ok 1 number(s): "3"

Test #5:

score: 0
Accepted
time: 12ms
memory: 87536kb

input:

4
3 2 2 3

output:

4

result:

ok 1 number(s): "4"

Test #6:

score: 0
Accepted
time: 16ms
memory: 87376kb

input:

5
3 4 4 2 4

output:

2

result:

ok 1 number(s): "2"

Test #7:

score: -100
Wrong Answer
time: 23ms
memory: 87544kb

input:

7
3 4 3 5 6 3 4

output:

5

result:

wrong answer 1st numbers differ - expected: '6', found: '5'