QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#445614#8527. Power Divisionsucup-team3099#TL 887ms20772kbC++238.0kb2024-06-16 06:35:422024-06-16 06:35:43

Judging History

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

  • [2024-06-16 06:35:43]
  • 评测
  • 测评结果:TL
  • 用时:887ms
  • 内存:20772kb
  • [2024-06-16 06:35:42]
  • 提交

answer

#include <iostream>
#include <vector>
#include <chrono>
#include <random>
#include <cassert>
#include <set>

std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());

template <class T, class E>
constexpr T fexp(T x, E e) {
    T ans(1);
    for(; e > 0; e >>= 1) {
        if(e & 1) ans = ans * x;
        x = x * x;
    }
    return ans;
}

template <class LOW, class HIGH, const LOW mod>
struct modBase {
    using mint = modBase<LOW, HIGH, mod>;
    constexpr modBase() : val(0) {}
    // be careful of negative numbers!
    constexpr modBase(const LOW v) : val((v % mod + mod) % mod) {} 
    LOW val;

    #define add(a, b) a + b >= mod ? a + b - mod : a + b
    #define sub(a, b) a < b ? a + mod - b : a - b

    constexpr mint &operator += (const mint &o) { return val = add(val, o.val), *this; }
    constexpr mint &operator -= (const mint &o) { return val = sub(val, o.val), *this; }
    constexpr mint &operator *= (const mint &o) { return val = (LOW) ((HIGH) val * o.val % mod), *this; }
    constexpr mint &operator /= (const mint &o) { return *this *= o.inverse(); }

    constexpr mint operator + (const mint &b) const { return mint(*this) += b; }
    constexpr mint operator - (const mint &b) const { return mint(*this) -= b; }
    constexpr mint operator * (const mint &b) const { return mint(*this) *= b; }
    constexpr mint operator / (const mint &b) const { return mint(*this) /= b; }

    constexpr mint operator - () const { return mint() - mint(*this); }
    constexpr bool operator == (const mint &b) const { return val == b.val; }
    constexpr bool operator != (const mint &b) const { return val != b.val; }

    template<class E> constexpr mint pow (E e) const { return fexp(*this, e); }
    constexpr mint inverse() const { return pow(mod - 2); }
    constexpr LOW get() const { return val; }
    static constexpr LOW getMod() { return mod; }

    friend std::ostream& operator << (std::ostream &os, const mint &p) { return os << p.val; }
    friend std::istream& operator >> (std::istream &is, mint &p) { return is >> p.val; }
};

struct Info {
    int pos;
    int potL, potR;
    int L, R;
};

std::vector<Info> genInfo(int n, const std::vector<int> &a) {
    std::set<int> st;
    st.insert(-1);
    st.insert(n);
    std::vector<int> p(n);
    for(int i = 0; i < n; i++) {
        p[i] = i;
    }
    std::sort(p.begin(), p.end(), [&](int v0, int v1) { return a[v0] > a[v1]; });
    std::vector<Info> ans;
    for(auto x : p) {
        auto itL = st.lower_bound(x);
        auto itR = itL;
        itL--;
        int l = *itL + 1, r = *itR - 1;
        if(x - l < r - x) {
            for(int i = l; i <= x; i++) {
                ans.push_back({i, a[x], a[x] + 20, x+1, r+1});
            }
        } else {
            for(int i = x; i <= r; i++) {
                ans.push_back({i+1, a[x], a[x] + 20, l, x});
            }
        }
        st.insert(x);
    }
    return ans;
}

template<const int MOD>
std::vector<std::pair<int, int>> solve(int n, const std::vector<int> &a, const std::vector<Info> &info) {
    using mint = modBase<int, long long, MOD>;
    std::vector<std::pair<int, int>> ans;
    const int ms = 1001000;
    std::vector<mint> b(n+1, 0), pot(ms, 1);
    for(int i = 1; i < ms; i++) {
        pot[i] = pot[i-1] * 2;
    }
    for(int i = 0; i < n; i++) {
        b[i+1] = b[i] + pot[a[i]];
    }
    std::vector<std::pair<int, int>> st;
    for(int i = 0; i <= n; i++) {
        //std::cout << b[i] << (i == n ? '\n' : ' ');
        st.push_back({b[i].val, i});
    }
    std::sort(st.begin(), st.end());
    for(auto [pos, potL, potR, l, r] : info) {
        for(int i = potL; i <= potR; i++) {
            int target = (pos <= l ? (b[pos] + pot[i]).val : (b[pos] - pot[i]).val);
            //std::cout << "pos " << pos << ", pot " << i << " range [" << l << ", " << r << ") got target " << target << '\n';
            int first = (int)(std::lower_bound(st.begin(), st.end(), std::pair<int, int>(target, l)) - st.begin());
            for(int j = first; j <= n && st[j].first == target && st[j].second <= r; j++) {
                ans.emplace_back(std::min(pos, st[j].second), std::max(pos, st[j].second));
                //std::cout << "found " << ans.back().first << ", " << ans.back().second << '\n';
            }
        }
    }
    std::sort(ans.begin(), ans.end());
    return ans;
}

constexpr int MOD[] = {(int) 1e9 + 7, (int) 1e9 + 9, 998244353};

int main() {
    std::ios_base::sync_with_stdio(false); std::cin.tie(NULL);
    int n;
    std::cin >> n;
    std::vector<int> a(n, 0);
    for(int i = 0; i < n; i++) {
        std::cin >> a[i];
    }
    auto info = genInfo(n, a);
    std::vector<std::pair<int, int>> transitions = solve<MOD[0]>(n, a, info);
    {
        auto cur = solve<MOD[1]>(n, a, info);
        int sz = 0;
        for(int i = 0, j = 0; i < (int) transitions.size(); i++) {
            while(j+1 < (int) cur.size() && cur[j] < transitions[i]) j++;
            if(cur[j] == transitions[i]) {
                transitions[sz++] = transitions[i];
            }
        }
        transitions.resize(sz);
    }
    {
        auto cur = solve<MOD[2]>(n, a, info);
        int sz = 0;
        for(int i = 0, j = 0; i < (int) transitions.size(); i++) {
            while(j+1 < (int) cur.size() && cur[j] < transitions[i]) j++;
            if(cur[j] == transitions[i]) {
                transitions[sz++] = transitions[i];
            }
        }
        transitions.resize(sz);
    }
    using mint = modBase<int, long long, MOD[0]>;
    std::vector<mint> dp(n+1, 0);
    dp[0] = 1;
    std::vector<std::vector<int>> trans(n+1);
    for(auto [l, r] : transitions) {
        //std::cout << "[" << l << ", " << r << ")" << std::endl;
        trans[l].push_back(r);
    }
    for(int i = 0; i < n; i++) for(auto j : trans[i]) {
        dp[j] += dp[i];
    }
    std::cout << dp.back() << '\n';
}

/*
NEVER FORGET TO:
    Look at the problem's constraints before coding.
How to cheese cf:
    Find a lower bound or upper bound for the problem. Have faith that it is the answer of the problem.
    If it isn't the answer, have more faith or change to another bound god by looking for a better bound.

    Trust guesses. Who has time to think? If people in div2 AC the problem it requires no proof since people don't prove things.

    You must draw cases. Thinking gets you nowhere, so draw cases and reach illogical conclusions from them.
    Sometimes drawing cases is bad because it takes too much time. Faster is to not think at all and just code a bruteforce solution.
    This is called "law of small numbers". If something works for small numbers, surely it works for big numbers.
    https://en.wikipedia.org/wiki/Faulty_generalization#Hasty_generalization don't mind the "faulty" part of it, in competitive programming mistakes are lightly punished
    Don't think about them being right or not, cf is a battle of intuition only.

    Be as stupid as possible in implementation. Trying to be smart is an easy way to get WA.

    Think about 2x2 cases for matrix problems and hope that everything works for the general case.

    Find a necessary condition and trust it to be sufficient. They're basically the same thing.

    Heuristics might speed up your code. Forget about complexity, it's only about ACing and not proving that your solution is good.

    For paths in a grid starting at (1, i) or something like that, assume that they never cross and do D&C

    Consider doing problems in reverse order of queries/updates

    For combinatorics problems, consider symmetry

General strategy (MUST DO):
    Try to solve the problem with more restricted constraints.

About testing:
    Test n=1, a[i]=1, a[i]=n, etc. Basically, test low values. No need to test if pretests are strong, but if you get WA it's good.

This isn't a joke. Do it if you get stuck. It's shit practice in my opinion, but do it if you want AC.
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 10ms
memory: 7600kb

input:

5
2 0 0 1 1

output:

6

result:

ok 1 number(s): "6"

Test #2:

score: 0
Accepted
time: 10ms
memory: 7540kb

input:

1
0

output:

1

result:

ok 1 number(s): "1"

Test #3:

score: 0
Accepted
time: 13ms
memory: 7480kb

input:

2
1 1

output:

2

result:

ok 1 number(s): "2"

Test #4:

score: 0
Accepted
time: 10ms
memory: 7508kb

input:

3
2 1 1

output:

3

result:

ok 1 number(s): "3"

Test #5:

score: 0
Accepted
time: 13ms
memory: 7712kb

input:

4
3 2 2 3

output:

4

result:

ok 1 number(s): "4"

Test #6:

score: 0
Accepted
time: 14ms
memory: 7576kb

input:

5
3 4 4 2 4

output:

2

result:

ok 1 number(s): "2"

Test #7:

score: 0
Accepted
time: 13ms
memory: 7520kb

input:

7
3 4 3 5 6 3 4

output:

6

result:

ok 1 number(s): "6"

Test #8:

score: 0
Accepted
time: 14ms
memory: 7524kb

input:

10
8 6 5 6 7 8 6 8 9 9

output:

4

result:

ok 1 number(s): "4"

Test #9:

score: 0
Accepted
time: 11ms
memory: 7592kb

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:

11332014

result:

ok 1 number(s): "11332014"

Test #10:

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

input:

480
2 0 4 4 1 0 0 3 1 1 4 2 5 5 4 2 1 2 4 4 1 3 4 3 0 5 2 0 2 5 1 0 5 0 0 5 5 0 2 5 2 2 3 1 4 3 5 4 5 2 4 4 4 4 1 4 0 3 4 3 4 1 0 4 3 4 5 4 3 5 0 2 2 0 1 5 4 4 2 0 3 3 3 4 3 0 5 5 3 1 5 1 0 1 0 4 3 0 5 1 4 1 4 3 0 1 3 5 0 3 3 1 0 4 1 1 2 0 1 2 0 3 5 2 0 5 5 5 5 3 5 1 0 2 5 2 2 0 2 0 2 3 5 1 2 1 5 4 ...

output:

506782981

result:

ok 1 number(s): "506782981"

Test #11:

score: 0
Accepted
time: 31ms
memory: 7736kb

input:

2400
0 2 2 0 5 4 3 2 3 2 5 4 5 4 4 5 2 2 4 2 2 0 1 0 5 0 4 4 0 0 5 0 4 0 1 3 4 5 0 3 1 0 4 0 2 5 0 3 3 3 3 1 0 5 5 3 1 3 5 2 4 0 5 0 4 5 4 2 2 1 5 2 2 4 1 0 5 1 5 0 1 2 0 0 3 5 4 0 0 1 1 1 4 2 0 5 1 3 3 5 0 4 4 1 5 5 3 4 4 4 0 2 4 0 5 1 3 1 5 0 5 5 1 3 0 3 1 2 0 1 1 3 5 2 3 4 0 3 0 5 4 0 4 3 5 0 5 2...

output:

586570528

result:

ok 1 number(s): "586570528"

Test #12:

score: 0
Accepted
time: 127ms
memory: 9328kb

input:

12000
2 2 1 2 0 2 5 3 2 0 1 3 2 5 4 0 0 5 3 2 0 2 3 4 3 2 1 4 3 0 3 5 4 1 0 2 4 1 3 2 3 5 0 3 0 0 4 0 4 5 1 0 4 1 1 1 5 4 3 0 3 5 4 5 2 5 0 1 2 3 5 5 2 5 4 2 0 4 4 3 0 0 2 5 0 3 4 2 5 4 2 1 4 5 1 1 2 3 0 3 3 3 3 4 0 5 3 4 0 3 0 2 0 0 2 0 3 4 2 2 0 1 0 5 3 0 2 0 2 2 1 0 5 3 5 4 5 5 0 4 0 4 1 4 4 3 2 ...

output:

201653965

result:

ok 1 number(s): "201653965"

Test #13:

score: 0
Accepted
time: 887ms
memory: 20772kb

input:

60000
2 5 0 3 2 3 5 3 5 5 4 1 1 5 3 0 1 1 2 5 5 5 0 3 2 0 3 2 3 3 0 0 1 4 3 1 4 2 3 3 0 5 1 0 1 1 5 5 4 0 5 4 1 3 1 3 5 3 2 4 4 4 5 4 3 2 3 2 4 5 2 0 4 5 1 2 0 4 0 5 1 3 4 1 2 4 1 1 3 3 0 1 1 3 0 0 2 3 3 2 1 4 1 2 4 3 3 5 2 5 3 4 3 0 2 1 1 1 5 1 2 4 2 3 1 2 1 0 2 0 1 1 5 5 3 4 2 5 2 4 5 3 0 5 1 4 2 ...

output:

592751350

result:

ok 1 number(s): "592751350"

Test #14:

score: -100
Time Limit Exceeded

input:

300000
0 5 1 5 5 4 5 3 0 5 0 5 1 4 1 2 2 2 3 0 1 5 4 0 3 1 4 5 2 1 0 3 2 1 2 5 0 2 4 5 0 1 2 1 1 0 0 5 3 0 0 3 4 5 0 2 1 1 1 2 5 1 4 3 1 0 2 0 0 4 3 3 2 5 3 3 1 5 2 0 2 4 3 1 0 3 4 1 3 3 1 0 0 1 1 1 3 1 2 3 5 3 3 2 0 3 0 0 5 5 0 0 0 0 1 4 3 3 4 3 4 5 3 3 5 1 1 4 2 2 1 3 2 1 1 0 0 5 5 0 0 3 2 4 5 5 2...

output:

842503795

result: