Submission #304634

# Submission time Handle Problem Language Result Execution time Memory
304634 2020-09-21T15:59:32 Z HynDuf Cake (CEOI14_cake) C++11
100 / 100
906 ms 5240 KB
#include <bits/stdc++.h>

#define task "C"
#define all(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define Rep(i, r, l) for (int i = (r); i >= (l); --i)
#define DB(X) { cerr << #X << " = " << (X) << '\n'; }
#define DB1(A, _) { cerr << #A << "[" << _ << "] = " << (A[_]) << '\n'; }
#define DB2(A, _, __) { cerr << #A << "[" << _ << "][" << __ << "] = " << (A[_][__]) << '\n'; }
#define DB3(A, _, __, ___) { cerr << #A << "[" << _ << "][" << __ << "][" << ___ << "] = " << (A[_][__][___]) << '\n'; }
#define PR(A, l, r) { cerr << '\n'; rep(_, l, r) DB1(A, _); cerr << '\n';}
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
#define pf push_front
#define F first
#define S second
#define by(x) [](const auto& a, const auto& b) { return a.x < b.x; } // sort(arr, arr + N, by(a));
#define next ___next
#define prev ___prev
#define y1 ___y1
#define left ___left
#define right ___right
#define y0 ___y0
#define div ___div
#define j0 ___j0
#define jn ___jn

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using namespace std;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vl;
const int N = 250002;
int read(){int s=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){s=(s<<3)+(s<<1)+ch-'0',ch=getchar();}return s*f;}
char readch() {char c; do c = getchar(); while (c == '\n' || c == ' '); return c;}
template <typename T> inline void wrip(T x) {if (x > 9) wrip(x / 10); putchar(x%10 + 48); }
template <typename T> inline void writeln(T x) {if (x < 0) putchar('-'), x = -x; wrip(x); putchar('\n');}
int n, A, q, a[N];
set<ii> mxa;
int it[4 * N];
void build(int id, int l, int r)
{
    if (l == r)
    {
        it[id] = a[l];
        return;
    }
    int m = (l + r) >> 1;
    build(id << 1, l, m);
    build((id << 1) | 1, m + 1, r);
    it[id] = max(it[id << 1], it[(id << 1) | 1]);
}
void update(int id, int l, int r, int p)
{
    if (l > p || r < p) return;
    if (l == r)
    {
        it[id] = a[p];
        return;
    }
    int m = (l + r) >> 1;
    update(id << 1, l, m, p);
    update((id << 1) | 1, m + 1, r, p);
    it[id] = max(it[id << 1], it[(id << 1) | 1]);
}
int query(int id, int l, int r, int L, int R)
{
    if (l > R || r < L) return -1e9;
    if (L <= l && r <= R) return it[id];
    int m = (l + r) >> 1;
    return max(query(id << 1, l, m, L, R), query((id << 1) | 1, m + 1, r, L, R));
}
int queryL(int id, int l, int r, int L, int R, int x)
{
    if (l > R || r < L || L > R || it[id] <= x) return -1e9;
    if (l == r) return l;
    int m = (l + r) >> 1;
    int res = queryL((id << 1) | 1, m + 1, r, L, R, x);
    if (res != -1e9) return res;
    return queryL(id << 1, l, m, L, R, x);
}
int queryR(int id, int l, int r, int L, int R, int x)
{
    if (l > R || r < L || L > R || it[id] <= x) return -1e9;
    if (l == r) return l;
    int m = (l + r) >> 1;
    int res = queryR(id << 1, l, m, L, R, x);
    if (res != -1e9) return res;
    return queryR((id << 1) | 1, m + 1, r, L, R, x);
}
int main()
{
#ifdef HynDuf
    freopen(task".in", "r", stdin);
    //freopen(task".out", "w", stdout);
#else
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
#endif
    //cin >> n >> A;
    n = read(), A = read();
    rep(i, 1, n)
    {
        a[i] = read();
        mxa.insert({a[i], i});
        if (SZ(mxa) > 10) mxa.erase(mxa.begin());
    }
    build(1, 1, n);
    q = read();
    while (q--)
    {
        char t;
        int x;
        t = readch();
        x = read();
        if (t == 'E')
        {
            int r;
            r = read();
            vii change;
            rep(i, 1, r - 1)
            {
                auto it = mxa.end(); it--;
                if (it->S != x)
                {
                    change.pb({it->F + 1, it->S});
                    a[it->S]++;
                    update(1, 1, n, it->S);
                } else i--;
                mxa.erase(it);
            }
            auto it = mxa.lower_bound({a[x], 0});
            if (it != mxa.end() && it->F == a[x]) mxa.erase(it);
            a[x] = (change.empty() ? (mxa.rbegin()->F + 1) : (change.back().F - 1));
            change.pb({a[x], x});
            update(1, 1, n, x);

            for (ii v : change) mxa.insert(v);
            if (SZ(mxa) > 10) mxa.erase(mxa.begin());

        } else if (x > A)
        {
            int mx = query(1, 1, n, A + 1, x);
            int l = queryL(1, 1, n, 1, A - 1, mx);
            if (l == -1e9) l = 0;
            writeln(x - l - 1);
        } else if (x < A)
        {
            int mx = query(1, 1, n, x, A - 1);
            int r = queryR(1, 1, n, A + 1, n, mx);
            if (r == -1e9) r = n + 1;
            writeln(r - x - 1);
        } else puts("0");
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 6 ms 384 KB Output is correct
5 Correct 15 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 622 ms 632 KB Output is correct
2 Correct 336 ms 512 KB Output is correct
3 Correct 401 ms 512 KB Output is correct
4 Correct 189 ms 512 KB Output is correct
5 Correct 727 ms 768 KB Output is correct
6 Correct 578 ms 752 KB Output is correct
7 Correct 444 ms 752 KB Output is correct
8 Correct 216 ms 756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 81 ms 2296 KB Output is correct
2 Correct 71 ms 2296 KB Output is correct
3 Correct 60 ms 2168 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 106 ms 4084 KB Output is correct
6 Correct 125 ms 4088 KB Output is correct
7 Correct 88 ms 3832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 62 ms 528 KB Output is correct
2 Correct 44 ms 512 KB Output is correct
3 Correct 73 ms 1272 KB Output is correct
4 Correct 117 ms 1400 KB Output is correct
5 Correct 171 ms 760 KB Output is correct
6 Correct 140 ms 1528 KB Output is correct
7 Correct 138 ms 1016 KB Output is correct
8 Correct 219 ms 1792 KB Output is correct
9 Correct 886 ms 5220 KB Output is correct
10 Correct 540 ms 1528 KB Output is correct
11 Correct 685 ms 2168 KB Output is correct
12 Correct 906 ms 4984 KB Output is correct
13 Correct 748 ms 5240 KB Output is correct