QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#143218#6753. MediansKidding_MaAC ✓2795ms130852kbC++206.2kb2023-08-20 21:56:212023-08-20 21:56:24

Judging History

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

  • [2023-08-20 21:56:24]
  • 评测
  • 测评结果:AC
  • 用时:2795ms
  • 内存:130852kb
  • [2023-08-20 21:56:21]
  • 提交

answer

#include "bits/stdc++.h"

using namespace std;
using i64 = long long;
using i128 = __int128;

namespace cpstd {
#define range(a) begin(a), end(a)

template <class T>
T max(const vector<T> &a) {
    return *std::max_element(a.begin(), a.end());
}

template <class T>
T min(const vector<T> &a) {
    return *std::min_element(a.begin(), a.end());
}

template <class T>
struct Fenwick {
    int n;
    std::vector<T> a;
    Fenwick(const int &n = 0) : n(n), a(n, T()) {}
    void modify(int i, T x) {
        for (i++; i <= n; i += i & -i) {
            a[i - 1] += x;
        }
    }
    T get(int i) {
        T res = T();
        for (; i > 0; i -= i & -i) {
            res += a[i - 1];
        }
        return res;
    }
    T sum(int l, int r) { // [l, r)
        return get(r) - get(l);
    }
    T kth(T k) {
        int x = 0;
        for (int i = 1 << std::__lg(n); i; i >>= 1) {
            if (x + i <= n && k >= a[x + i - 1]) {
                x += i;
                k -= a[x - 1];
            }
        }
        return x;
    }
};

template <class T>
T exgcd(T a, T b, T &x, T &y) {
    if (!b) {
        x = 1, y = 0;
        return a;
    }
    T g = exgcd(b, a % b, y, x);
    y -= a / b * x;
    return g;
}

template <class T>
T floor_sum(T n, T m, T a, T b) {
    T ans = 0;
    if (a < 0) {
        T a2 = (a % m + m) % m;
        ans -= n * (n - 1) / 2 * ((a2 - a) / m);
        a = a2;
    }
    if (b < 0) {
        T b2 = (b % m + m) % m;
        ans -= n * ((b2 - b) / m);
        b = b2;
    }
    while (1) {
        if (a >= m) {
            ans += n * (n - 1) / 2 * (a / m);
            a %= m;
        }
        if (b >= m) {
            ans += n * (b / m);
            b %= m;
        }
        T y_max = a * n + b;
        if (y_max < m) {
            break;
        }
        n = y_max / m;
        b = y_max % m;
        swap(m, a);
    }
    return ans;
}

template <class T, class U1, class U2>
T power(T a, U1 b, U2 p) {
    T res = 1;
    for (; b; b >>= 1, a = a * a % p) {
        if (b & 1) {
            res = res * a % p;
        }
    }
    return res;
}

template <class T, class U>
T power(T a, U b) {
    T res = 1;
    for (; b; b >>= 1, a = a * a) {
        if (b & 1) {
            res = res * a;
        }
    }
    return res;
}

__int128 abs(const __int128 &v) {
    return (v < 0 ? -v : v);
}

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

istream &operator>>(istream &is, __int128 &v) {
    std::string s;
    is >> s;
    v = 0;
    for (auto &si : s) {
        v = v * 10 + si - '0';
    }
    return is;
}

ostream &operator<<(ostream &os, const __int128 &v) {
    if (v <= 1000000000000000000) {
        return os << (long long) (v);
    }
    return os << (long long) (v / 1000000000000000000) << std::setw(18) << std::setfill('0') << (long long) (v % 1000000000000000000);
}

template <class T>
T crt(const vector<T> &r, const vector<T> &m) {
    int n = r.size();
    T r0 = 0, m0 = 1;
    for (int i = 0; i < n; i++) {
        T m1 = m[i];
        T r1 = r[i] % m1;
        if (r1 < 0) {
            r1 += m1;
        }
        if (m0 < m1) {
            std::swap(r0, r1);
            std::swap(m0, m1);
        }
        if (!(m0 % m1)) {
            if (r0 % m1 != r1) {
                return -1;
            }
            continue;
        }
        T x, y;
        T g = exgcd(m0, m1, x, y);
        if ((r1 - r0) % g) {
            return -1;
        }
        T u1 = m1 / g;
        r0 += (r1 - r0) / g % u1 * x % u1 * m0;
        m0 *= u1;
        if (r0 < 0) {
            r0 += m0;
        }
    }
    return r0;
}

constexpr int B = 777;
constexpr long long P = 100000000000031;

long long *p;

void initHash(int N) {
    p = new long long [N + 1];
    for (int i = 0; i <= N; i++) {
        p[i] = 0;
    }
    p[0] = 1;
    for (int i = 1; i <= N; i++) {
        p[i] = p[i - 1] * B % P;
    }
}

struct StringHash {
    std::vector<long long> h;
    StringHash() : h(1) {}
    void push_back(char ch) {
        h.push_back((h.back() * B + ch) % P);
    }
    long long get(int l, int r) { // [l, r)
        return (h[r] + __int128(h[l]) * (P - p[r - l])) % P;
    }
};

struct UnionFind {
    int n;
    std::vector<int> f, sz;
    UnionFind(const int &n = 0) : n(n), f(n), sz(n, 1) {
        std::iota(f.begin(), f.end(), 0);
    }
    int get(int x) {
        while (x != f[x]) {
            x = f[x] = f[f[x]];
        }
        return x;
    }
    bool unite(int x, int y) {
        x = get(x), y = get(y);
        if (x != y) {
            f[y] = x;
            sz[x] += sz[y];
            return 1;
        }
        return 0;
    }
    bool united(int x, int y) {
        return get(x) == get(y);
    }
    int size(int x) {
        x = get(x);
        return sz[x];
    }
};
}

using namespace cpstd;

void solve() {
    
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, a0;
    cin >> n >> a0;
    vector<int> a(n + 1);
    a[0] = a0;
    for (int i = 1; i <= n; i++) {
        a[i] = (998244353LL * a[i - 1] + 1000000007) % 1000000009;
    }
    vector<int> p(n + 1);
    iota(p.begin(), p.end(), 0);
    for (int i = 1; i <= n; i++) {
        swap(p[i], p[(a[i] % i) + 1]);
    }
    i64 pw = 19;
    i64 ans = 0;
    priority_queue<int> q1;
    priority_queue<int, vector<int>, greater<>> q2;
    for (int i = 1; i <= n; i++) {
        q1.push(p[i]);
        while ((int) q1.size() > (int) q2.size() + 1) {
            auto cur = q1.top();
            q1.pop();
            q2.push(cur);
        }
        while (!q1.empty() && !q2.empty()) {
            auto cur1 = q1.top();
            auto cur2 = q2.top();
            q1.pop();
            q2.pop();
            if (cur1 > cur2) {
                q1.push(cur2);
                q2.push(cur1);
            } else {
                q1.push(cur1);
                q2.push(cur2);
                break;
            }
        }
        ans += 1LL * q1.top() * pw % 998244353;
        ans %= 998244353;
        pw *= 19;
        pw %= 998244353;
    }
    cout << ans << '\n';
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3592kb

input:

5 0

output:

7703113

result:

ok 1 number(s): "7703113"

Test #2:

score: 0
Accepted
time: 1ms
memory: 3532kb

input:

5 1

output:

7840977

result:

ok 1 number(s): "7840977"

Test #3:

score: 0
Accepted
time: 1ms
memory: 3572kb

input:

2 1361955

output:

399

result:

ok 1 number(s): "399"

Test #4:

score: 0
Accepted
time: 1ms
memory: 3472kb

input:

4 207579012

output:

274740

result:

ok 1 number(s): "274740"

Test #5:

score: 0
Accepted
time: 1ms
memory: 3524kb

input:

8 628145516

output:

783389330

result:

ok 1 number(s): "783389330"

Test #6:

score: 0
Accepted
time: 1ms
memory: 3588kb

input:

16 376140462

output:

772072366

result:

ok 1 number(s): "772072366"

Test #7:

score: 0
Accepted
time: 1ms
memory: 3524kb

input:

32 883515280

output:

822906393

result:

ok 1 number(s): "822906393"

Test #8:

score: 0
Accepted
time: 1ms
memory: 3544kb

input:

64 186969585

output:

536948870

result:

ok 1 number(s): "536948870"

Test #9:

score: 0
Accepted
time: 1ms
memory: 3524kb

input:

128 762888635

output:

914896632

result:

ok 1 number(s): "914896632"

Test #10:

score: 0
Accepted
time: 1ms
memory: 3528kb

input:

256 326402539

output:

816864808

result:

ok 1 number(s): "816864808"

Test #11:

score: 0
Accepted
time: 0ms
memory: 3532kb

input:

512 98152102

output:

792934555

result:

ok 1 number(s): "792934555"

Test #12:

score: 0
Accepted
time: 1ms
memory: 3484kb

input:

1024 158176572

output:

187304261

result:

ok 1 number(s): "187304261"

Test #13:

score: 0
Accepted
time: 1ms
memory: 3564kb

input:

2048 61402883

output:

881629018

result:

ok 1 number(s): "881629018"

Test #14:

score: 0
Accepted
time: 2ms
memory: 3664kb

input:

4096 127860889

output:

926052991

result:

ok 1 number(s): "926052991"

Test #15:

score: 0
Accepted
time: 3ms
memory: 3668kb

input:

8192 9580638

output:

18767865

result:

ok 1 number(s): "18767865"

Test #16:

score: 0
Accepted
time: 2ms
memory: 3728kb

input:

16384 570870044

output:

676635475

result:

ok 1 number(s): "676635475"

Test #17:

score: 0
Accepted
time: 5ms
memory: 3844kb

input:

32768 646139319

output:

121314798

result:

ok 1 number(s): "121314798"

Test #18:

score: 0
Accepted
time: 15ms
memory: 4232kb

input:

65536 178509022

output:

518784793

result:

ok 1 number(s): "518784793"

Test #19:

score: 0
Accepted
time: 30ms
memory: 5260kb

input:

131072 484027666

output:

783563468

result:

ok 1 number(s): "783563468"

Test #20:

score: 0
Accepted
time: 60ms
memory: 6860kb

input:

262144 61263304

output:

560815556

result:

ok 1 number(s): "560815556"

Test #21:

score: 0
Accepted
time: 122ms
memory: 10468kb

input:

524288 841082555

output:

478037004

result:

ok 1 number(s): "478037004"

Test #22:

score: 0
Accepted
time: 254ms
memory: 18660kb

input:

1048576 558212774

output:

145045199

result:

ok 1 number(s): "145045199"

Test #23:

score: 0
Accepted
time: 536ms
memory: 32200kb

input:

2097152 940563715

output:

267114566

result:

ok 1 number(s): "267114566"

Test #24:

score: 0
Accepted
time: 1107ms
memory: 60772kb

input:

4194304 26389620

output:

535216368

result:

ok 1 number(s): "535216368"

Test #25:

score: 0
Accepted
time: 2323ms
memory: 118196kb

input:

8388608 579113528

output:

926081338

result:

ok 1 number(s): "926081338"

Test #26:

score: 0
Accepted
time: 2783ms
memory: 130844kb

input:

10000000 496147999

output:

872799419

result:

ok 1 number(s): "872799419"

Test #27:

score: 0
Accepted
time: 2789ms
memory: 130840kb

input:

10000000 925801172

output:

676521567

result:

ok 1 number(s): "676521567"

Test #28:

score: 0
Accepted
time: 2771ms
memory: 130732kb

input:

10000000 837151740

output:

617759049

result:

ok 1 number(s): "617759049"

Test #29:

score: 0
Accepted
time: 2784ms
memory: 130852kb

input:

10000000 70301164

output:

413391579

result:

ok 1 number(s): "413391579"

Test #30:

score: 0
Accepted
time: 2785ms
memory: 130756kb

input:

10000000 656585275

output:

505441893

result:

ok 1 number(s): "505441893"

Test #31:

score: 0
Accepted
time: 2776ms
memory: 130800kb

input:

10000000 285845005

output:

465986348

result:

ok 1 number(s): "465986348"

Test #32:

score: 0
Accepted
time: 2795ms
memory: 130760kb

input:

10000000 902071050

output:

964328151

result:

ok 1 number(s): "964328151"