QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#440014#6753. Medianssuibian_xiaozhao#AC ✓953ms134136kbC++177.7kb2024-06-12 23:17:112024-06-12 23:17:12

Judging History

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

  • [2024-06-12 23:17:12]
  • 评测
  • 测评结果:AC
  • 用时:953ms
  • 内存:134136kb
  • [2024-06-12 23:17:11]
  • 提交

answer

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <cassert>
#include <cctype>
#include <ciso646>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <chrono>
#include <random>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#endif

#define FAST ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);cout<<fixed<<setprecision(15)
#define Tsolve() int T; cin >> T; while (T --) solve()
#define endl '\n'
#define em(x) (x.empty())
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define Fill(x,a) memset(x,a,sizeof(x))
#define cpy(a,b) memcpy(a,b,sizeof(a))
#define pbk push_back
#define mpr make_pair
#define lb lower_bound
#define ub upper_bound
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define fi first
#define se second
#define complete_unique(a) a.erase(unique(a.begin(),a.end()),a.end())
//-----------------------precompiler--------------------
using ll = long long;
using i64 = long long;
using pii = std::pair<int,int>;
using VI = std::vector<int>;
using namespace std;
template<typename typC,typename typD> istream &operator>>(istream &cin,pair<typC,typD> &a) { return cin>>a.first>>a.second; }
template<typename typC> istream &operator>>(istream &cin,vector<typC> &a) { for (auto &x:a) cin>>x; return cin; }
template<typename typC,typename typD> ostream &operator<<(ostream &cout,const pair<typC,typD> &a) { return cout<<a.first<<' '<<a.second; }
template<typename typC,typename typD> ostream &operator<<(ostream &cout,const vector<pair<typC,typD>> &a) { for (auto &x:a) cout<<x<<'\n'; return cout; }
template<typename typC> ostream &operator<<(ostream &cout,const vector<typC> &a) { int n=a.size(); if (!n) return cout; cout<<a[0]; for (int i=1; i<n; i++) cout<<' '<<a[i]; return cout; }
//-----------------------debug-----------------------
#ifndef ONLINE_JUDGE
#include "D:/OneDrive-Personal/OneDrive/my-acm-template/my_header/debug.h"
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define dbg(...) {}
#endif
//-----------------------constant-----------------------
const int maxn = 2e5+10;
const int inf = 0x3f3f3f3f;
const ll inf2 = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-8;
const int mod = 1e9+7;
const int mod2 = 998244353;
ll gcd(ll a,ll b) { return b?gcd(b, a%b):a;}
//-----------------------variable-----------------------
int n;

//-----------------------function-----------------------

template<class T>
constexpr T power(T a, i64 b) {
    T res = 1;
    for (; b; b /= 2, a *= a) {
        if (b % 2) {
            res *= a;
        }
    }
    return res;
}
template<int P>
struct MInt {
    int x;
    constexpr MInt() : x{} {}
    constexpr MInt(i64 x) : x{norm(x % getMod())} {}
    
    static int Mod;
    constexpr static int getMod() {
        if (P > 0) {
            return P;
        } else {
            return Mod;
        }
    }
    constexpr static void setMod(int Mod_) {
        Mod = Mod_;
    }
    constexpr int norm(int x) const {
        if (x < 0) {
            x += getMod();
        }
        if (x >= getMod()) {
            x -= getMod();
        }
        return x;
    }
    constexpr int val() const {
        return x;
    }
    explicit constexpr operator int() const {
        return x;
    }
    constexpr MInt operator-() const {
        MInt res;
        res.x = norm(getMod() - x);
        return res;
    }
    constexpr MInt inv() const {
        assert(x != 0);
        return power(*this, getMod() - 2);
    }
    constexpr MInt &operator*=(MInt rhs) & {
        x = 1LL * x * rhs.x % getMod();
        return *this;
    }
    constexpr MInt &operator+=(MInt rhs) & {
        x = norm(x + rhs.x);
        return *this;
    }
    constexpr MInt &operator-=(MInt rhs) & {
        x = norm(x - rhs.x);
        return *this;
    }
    constexpr MInt &operator/=(MInt rhs) & {
        return *this *= rhs.inv();
    }
    friend constexpr MInt operator*(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res *= rhs;
        return res;
    }
    friend constexpr MInt operator+(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res += rhs;
        return res;
    }
    friend constexpr MInt operator-(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res -= rhs;
        return res;
    }
    friend constexpr MInt operator/(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res /= rhs;
        return res;
    }
    friend constexpr std::istream &operator>>(std::istream &is, MInt &a) {
        i64 v;
        is >> v;
        a = MInt(v);
        return is;
    }
    friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) {
        return os << a.val();
    }
    friend constexpr bool operator==(MInt lhs, MInt rhs) {
        return lhs.val() == rhs.val();
    }
    friend constexpr bool operator!=(MInt lhs, MInt rhs) {
        return lhs.val() != rhs.val();
    }
};
 
template<>
int MInt<0>::Mod = 1;
 
template<int V, int P>
constexpr MInt<P> CInv = MInt<P>(V).inv();
 
constexpr int P = 998244353;
using Z = MInt<P>;

template<typename T, typename _Compare = less<>>
struct MySet {
    priority_queue<T, vector<T>, _Compare> p{};
    priority_queue<T, vector<T>, _Compare> ep{};
    int sum;
    MySet() {
        sum = 0;
    }
    void insert(int x) {
        p.emplace(x);
        sum += x;
    }
    void erase(int x) {
        ep.emplace(x);
        sum -= x;
    }
    bool empty() {
        return size() == 0;
    }
    int top() {
        while (!p.empty() && !ep.empty() && p.top() == ep.top()) {
            p.pop();
            ep.pop();
        }
        return p.top();
    }
    void pop() {
        erase(p.top());
    }
    int size() {
        return p.size() - ep.size();
    }
};

struct MidNum {
    MySet<int, greater<>> r{};
    MySet<int> l{};


    void adjust() {
        // debug(r.size(), l.size());
        while (r.size() > l.size()) {
            l.insert(r.top());
            r.pop();
        }
        while (!l.empty() && l.size() - 1 > r.size()) {
            r.insert(l.top());
            l.pop();
        }
    }

    void insert(int x) {
        if (l.empty()||l.top() > x)
            l.insert(x);
        else {
            r.insert(x);
        }
        adjust();
    }

    void erase(int x) {
        if (l.top() >= x) {
            l.erase(x);
        }
        else {
            r.erase(x);
        }

        adjust();
    }

    int opsum() {
        int sums = r.sum - l.sum;
        if (l.size() > r.size()) sums += l.top();
        return sums;
    }
};

void solve() {
    int n;
    cin >> n;
    vector<int> a(n + 1), p(n + 1);
    cin >> a[0];
    for (int i = 1; i <= n; i ++) {
        a[i] = ((ll)a[i - 1] * 998244353 + 1000000007) % 1000000009;
        p[i] = i;
    }
    
    MidNum mn;
    for (int i = 1; i <= n; i ++) {
        swap(p[i], p[a[i] % i + 1]);
    }
    Z res = 0, pow = 1;
    for (int i = 1; i <= n; i ++) {
        mn.insert(p[i]);
        pow *= 19;
        Z ans(mn.l.top());
        ans *= pow;
        res += ans;
    }
    cout << res << endl;
}
int main() {
    FAST;
    // Tsolve();
    solve();

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

5 0

output:

7703113

result:

ok 1 number(s): "7703113"

Test #2:

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

input:

5 1

output:

7840977

result:

ok 1 number(s): "7840977"

Test #3:

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

input:

2 1361955

output:

399

result:

ok 1 number(s): "399"

Test #4:

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

input:

4 207579012

output:

274740

result:

ok 1 number(s): "274740"

Test #5:

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

input:

8 628145516

output:

783389330

result:

ok 1 number(s): "783389330"

Test #6:

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

input:

16 376140462

output:

772072366

result:

ok 1 number(s): "772072366"

Test #7:

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

input:

32 883515280

output:

822906393

result:

ok 1 number(s): "822906393"

Test #8:

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

input:

64 186969585

output:

536948870

result:

ok 1 number(s): "536948870"

Test #9:

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

input:

128 762888635

output:

914896632

result:

ok 1 number(s): "914896632"

Test #10:

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

input:

256 326402539

output:

816864808

result:

ok 1 number(s): "816864808"

Test #11:

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

input:

512 98152102

output:

792934555

result:

ok 1 number(s): "792934555"

Test #12:

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

input:

1024 158176572

output:

187304261

result:

ok 1 number(s): "187304261"

Test #13:

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

input:

2048 61402883

output:

881629018

result:

ok 1 number(s): "881629018"

Test #14:

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

input:

4096 127860889

output:

926052991

result:

ok 1 number(s): "926052991"

Test #15:

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

input:

8192 9580638

output:

18767865

result:

ok 1 number(s): "18767865"

Test #16:

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

input:

16384 570870044

output:

676635475

result:

ok 1 number(s): "676635475"

Test #17:

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

input:

32768 646139319

output:

121314798

result:

ok 1 number(s): "121314798"

Test #18:

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

input:

65536 178509022

output:

518784793

result:

ok 1 number(s): "518784793"

Test #19:

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

input:

131072 484027666

output:

783563468

result:

ok 1 number(s): "783563468"

Test #20:

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

input:

262144 61263304

output:

560815556

result:

ok 1 number(s): "560815556"

Test #21:

score: 0
Accepted
time: 43ms
memory: 10496kb

input:

524288 841082555

output:

478037004

result:

ok 1 number(s): "478037004"

Test #22:

score: 0
Accepted
time: 80ms
memory: 18636kb

input:

1048576 558212774

output:

145045199

result:

ok 1 number(s): "145045199"

Test #23:

score: 0
Accepted
time: 183ms
memory: 35560kb

input:

2097152 940563715

output:

267114566

result:

ok 1 number(s): "267114566"

Test #24:

score: 0
Accepted
time: 367ms
memory: 61556kb

input:

4194304 26389620

output:

535216368

result:

ok 1 number(s): "535216368"

Test #25:

score: 0
Accepted
time: 785ms
memory: 121016kb

input:

8388608 579113528

output:

926081338

result:

ok 1 number(s): "926081338"

Test #26:

score: 0
Accepted
time: 936ms
memory: 133888kb

input:

10000000 496147999

output:

872799419

result:

ok 1 number(s): "872799419"

Test #27:

score: 0
Accepted
time: 953ms
memory: 133912kb

input:

10000000 925801172

output:

676521567

result:

ok 1 number(s): "676521567"

Test #28:

score: 0
Accepted
time: 928ms
memory: 131476kb

input:

10000000 837151740

output:

617759049

result:

ok 1 number(s): "617759049"

Test #29:

score: 0
Accepted
time: 937ms
memory: 130924kb

input:

10000000 70301164

output:

413391579

result:

ok 1 number(s): "413391579"

Test #30:

score: 0
Accepted
time: 914ms
memory: 133056kb

input:

10000000 656585275

output:

505441893

result:

ok 1 number(s): "505441893"

Test #31:

score: 0
Accepted
time: 940ms
memory: 132304kb

input:

10000000 285845005

output:

465986348

result:

ok 1 number(s): "465986348"

Test #32:

score: 0
Accepted
time: 943ms
memory: 134136kb

input:

10000000 902071050

output:

964328151

result:

ok 1 number(s): "964328151"