QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#247927#7627. Phonyucup-team088#WA 5ms12168kbC++1711.1kb2023-11-11 16:36:412023-11-11 16:36:41

Judging History

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

  • [2023-11-11 16:36:41]
  • 评测
  • 测评结果:WA
  • 用时:5ms
  • 内存:12168kb
  • [2023-11-11 16:36:41]
  • 提交

answer

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
#include<chrono>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
//ll mod = 1;
constexpr ll mod = 998244353;
//constexpr ll mod = 1000000007;
const int mod17 = 1000000007;
const ll INF = (ll)mod17 * mod17;
typedef pair<int, int>P;

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;

using ld = double;
typedef pair<ld, ld> LDP;
const ld eps = 1e-10;
const ld pi = acosl(-1.0);

template<typename T>
void chmin(T& a, T b) {
    a = min(a, b);
}
template<typename T>
void chmax(T& a, T b) {
    a = max(a, b);
}
template<typename T>
vector<T> vmerge(vector<T>& a, vector<T>& b) {
    vector<T> res;
    int ida = 0, idb = 0;
    while (ida < a.size() || idb < b.size()) {
        if (idb == b.size()) {
            res.push_back(a[ida]); ida++;
        }
        else if (ida == a.size()) {
            res.push_back(b[idb]); idb++;
        }
        else {
            if (a[ida] < b[idb]) {
                res.push_back(a[ida]); ida++;
            }
            else {
                res.push_back(b[idb]); idb++;
            }
        }
    }
    return res;
}
template<typename T>
void cinarray(vector<T>& v) {
    rep(i, v.size())cin >> v[i];
}
template<typename T>
void coutarray(vector<T>& v) {
    rep(i, v.size()) {
        if (i > 0)cout << " "; cout << v[i];
    }
    cout << "\n";
}
ll mod_pow(ll x, ll n, ll m = mod) {
    if (n < 0) {
        ll res = mod_pow(x, -n, m);
        return mod_pow(res, m - 2, m);
    }
    if (abs(x) >= m)x %= m;
    if (x < 0)x += m;
    //if (x == 0)return 0;
    ll res = 1;
    while (n) {
        if (n & 1)res = res * x % m;
        x = x * x % m; n >>= 1;
    }
    return res;
}
//mod should be <2^31
struct modint {
    int n;
    modint() :n(0) { ; }
    modint(ll m) {
        if (m < 0 || mod <= m) {
            m %= mod; if (m < 0)m += mod;
        }
        n = m;
    }
    operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
bool operator<(modint a, modint b) { return a.n < b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= (int)mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += (int)mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
    if (n == 0)return modint(1);
    modint res = (a * a) ^ (n / 2);
    if (n % 2)res = res * a;
    return res;
}

ll inv(ll a, ll p) {
    return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f() {
    fact[0] = modint(1);
    for (int i = 0; i < max_n - 1; i++) {
        fact[i + 1] = fact[i] * modint(i + 1);
    }
    factinv[max_n - 1] = modint(1) / fact[max_n - 1];
    for (int i = max_n - 2; i >= 0; i--) {
        factinv[i] = factinv[i + 1] * modint(i + 1);
    }
}
modint comb(int a, int b) {
    if (a < 0 || b < 0 || a < b)return 0;
    return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
    if (a < 0 || b < 0 || a < b)return 0;
    return fact[a] * factinv[a - b];
}

ll gcd(ll a, ll b) {
    a = abs(a); b = abs(b);
    if (a < b)swap(a, b);
    while (b) {
        ll r = a % b; a = b; b = r;
    }
    return a;
}
template<typename T>
void addv(vector<T>& v, int loc, T val) {
    if (loc >= v.size())v.resize(loc + 1, 0);
    v[loc] += val;
}
/*const int mn = 2000005;
bool isp[mn];
vector<int> ps;
void init() {
    fill(isp + 2, isp + mn, true);
    for (int i = 2; i < mn; i++) {
        if (!isp[i])continue;
        ps.push_back(i);
        for (int j = 2 * i; j < mn; j += i) {
            isp[j] = false;
        }
    }
}*/

//[,val)
template<typename T>
auto prev_itr(set<T>& st, T val) {
    auto res = st.lower_bound(val);
    if (res == st.begin())return st.end();
    res--; return res;
}

//[val,)
template<typename T>
auto next_itr(set<T>& st, T val) {
    auto res = st.lower_bound(val);
    return res;
}
using mP = pair<modint, modint>;
mP operator+(mP a, mP b) {
    return { a.first + b.first,a.second + b.second };
}
mP operator+=(mP& a, mP b) {
    a = a + b; return a;
}
mP operator-(mP a, mP b) {
    return { a.first - b.first,a.second - b.second };
}
mP operator-=(mP& a, mP b) {
    a = a - b; return a;
}
LP operator+(LP a, LP b) {
    return { a.first + b.first,a.second + b.second };
}
LP operator+=(LP& a, LP b) {
    a = a + b; return a;
}
LP operator-(LP a, LP b) {
    return { a.first - b.first,a.second - b.second };
}
LP operator-=(LP& a, LP b) {
    a = a - b; return a;
}

mt19937 mt(time(0));

const string drul = "DRUL";
string senw = "SENW";
//DRUL,or SENW
//int dx[4] = { 1,0,-1,0 };
//int dy[4] = { 0,1,0,-1 };

//------------------------------------
template<typename T>
struct BIT {
private:
    vector<T> node; int n;
public:
    BIT(int n_) {
        n = n_; node.resize(n, 0);
    }
    //0-indexed
    void add(int a, T w) {
        for (int i = a; i < n; i |= i + 1)node[i] += w;
    }
    //[0,a)
    T sum(int a) {
        T ret = 0;
        for (int i = a - 1; i >= 0; i = (i & (i + 1)) - 1)ret += node[i];
        return ret;
    }
    //[a,b)
    T sum(int a, int b) {
        return sum(b) - sum(a);
    }
};

using T = __int128;
void solve() {
    int n, q; ll k; cin >> n >> q >> k;
    vector<ll> a(n);
    rep(i, n)cin >> a[i];
    sort(all(a));
    vector<LP> rs;
    rep(i, n)rs.push_back({ a[i] % k,i });
    sort(all(rs));
    vector<int> locs(n);
    rep(i, n)locs[rs[i].second] = i;
    BIT<int> bt(n);
    BIT<int> bttmp(n);


    vector<pair<T, int>> qs;
    T sumq = 0;
    rep(i, q) {
        char typ; cin >> typ;
        if (typ == 'A') {
            int x; cin >> x;
            qs.push_back({ sumq,x });
        }
        else {
            ll x; cin >> x;
            sumq += x;
        }
    }

    T sum = 0;

    vector < pair<ll, P>> vp;
    rep(i, n) {
        int le = i;
        while (i + 1 < n&&a[i] / k == a[i + 1] / k)i++;
        vp.push_back({ a[i] / k,{le,i + 1} });
    }
    int loc = 0;

    auto getxs = [&](int x) {
        int cl = -1, cr = n;
        while (cr - cl > 1) {
            int mid = (cl + cr) / 2;
            int c = bt.sum(0,mid);
            if (c > x)cr = mid;
            else cl = mid;
        }
        return cl;
    };
    vector<ll> ans;
    Rep(j, vp.back().second.first, vp.back().second.second) {
        bt.add(locs[j], 1);
    }
    int cnt = 0;
    per(i, vp.size()) {
        if (i > 0) {
            int le = vp[i-1].second.first;
            int ri = vp[i-1].second.second;
            Rep(j, le, ri) {
                bttmp.add(locs[j], 1);
            }
        }
        cnt += vp[i].second.second - vp[i].second.first;
        ll dif = vp[i].first;
        if (i > 0)dif -= vp[i - 1].first;
        else {
            dif += INF;
        }
        while (loc < qs.size() && qs[loc].first <= sum + (T)(dif - 1) * cnt) {
            T dif = qs[loc].first - sum;
            //cout << "?? " <<loc<<" "<< dif << "\n";
            ll dd = dif / cnt;
            ll r = dif % cnt;
            int x = qs[loc].second;
            ll ansval;
            if (x > cnt) {
                ansval = a[n - x];
            }
            else if (x <= cnt - r) {
                int v = getxs(cnt - r - x);
                ansval = (vp[i].first - dd) * k + rs[v].first;
            }
            else {
                x -= cnt - r;
                int v = getxs(cnt-x);
                ansval = (vp[i].first - dd - 1) * k + rs[v].first;
            }
            ans.push_back(ansval);
            loc++;
        }
        while (loc < qs.size() && qs[loc].first < sum + (T)dif * cnt) {
            T dif = qs[loc].first - sum;
            ll dd = dif / cnt;
            ll r = dif % cnt;
            int x = qs[loc].second;
            ll ansval;
            if (x <= cnt - r) {
                int v = getxs(cnt - r - x);
                ansval = (vp[i].first - dd) * k + rs[v].first;
            }
            else {
                x -= cnt - r;
                int vv = getxs(cnt - r);
                //cout << "?? " << vv << "\n";
                int cl = -1, cr = n;
                while (cr - cl > 1) {
                    int mid = (cl + cr) / 2;
                    int num = 0;
                    num += bt.sum(max(mid, vv), n);
                    num += bttmp.sum(mid, n);
                    if (num >= x) {
                        cl = mid;
                    }
                    else {
                        cr = mid;
                    }
                }
                //cout << "?? " << loc<<" "<<i<<" "<<cl << "\n";
                //cout << vp[i].first - dd <<" "<<dif<< "\n";
                ansval = (vp[i].first - dd - 1) * k + rs[cl].first;
            }
            ans.push_back(ansval);
            loc++;
        }
        //cout << loc << ans.size() << "\n";
        if (i > 0) {
            int le = vp[i - 1].second.first;
            int ri = vp[i - 1].second.second;
            Rep(j, le, ri) {
                bttmp.add(locs[j], -1);
                bt.add(locs[j], 1);
            }
        } 
        sum += (T)dif * cnt;
    }
    assert(loc == qs.size());
    //cout << qs.size() << "\n";
    for (auto v : ans)cout << v << "\n";

    /*ll dr = 0;
    ll dd = 0;
    
    rep(i, q) {
        char typ; cin >> typ;
        if (typ == 'A') {
            int x; cin >> x; x--;
            ll ans;
            if (loc == vp.size()) {
                ans = a[n - x];
            }
            else {

            }
        }
        else {
            ll x; cin >> x;
            
        }
    }*/
}



signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    //cout << fixed<<setprecision(10);
    //init_f();
    //init();
    //init2();
    //while(true)
    //expr();
    //int t; cin >> t; rep(i, t)
    solve();
    return 0;
}


详细

Test #1:

score: 100
Accepted
time: 5ms
memory: 11832kb

input:

3 5 5
7 3 9
A 3
C 1
A 2
C 2
A 3

output:

3
4
-1

result:

ok 3 lines

Test #2:

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

input:

5 8 8
294 928 293 392 719
A 4
C 200
A 5
C 10
A 2
C 120
A 1
A 3

output:

294
200
191
0
-2

result:

ok 5 lines

Test #3:

score: -100
Wrong Answer
time: 5ms
memory: 12168kb

input:

100 100 233
5101 8001 6561 6329 6305 7745 4321 811 49 1121 3953 8054 8415 9876 6701 4097 6817 6081 495 5521 2389 2042 4721 8119 7441 7840 8001 5756 5561 129 1 5981 4801 7201 8465 7251 6945 5201 5626 3361 5741 3650 7901 2513 8637 3841 5621 9377 101 3661 5105 4241 5137 7501 5561 3581 4901 561 8721 811...

output:

8854
9161
8854
8200
7223
7647
7531
7223
7223
6990
6990
6990
6990
4097
7187
7218
7035
7018
6757
6752
6524
6683
6114
6135
5825
5825
5825
5957
5393
5126
5303
5171
5205
4893
5084
4660
4660
4591
4816
4586
4705
4427
4535
4454
4603
4435
4194
4345
3961
3961
4284
3961
4274
3961
4326
-17552

result:

wrong answer 1st lines differ - expected: '6881', found: '8854'