Submission #285886

# Submission time Handle Problem Language Result Execution time Memory
285886 2020-08-29T17:20:35 Z caoash Cake (CEOI14_cake) C++14
0 / 100
2000 ms 25336 KB
#include <bits/stdc++.h> 
using namespace std;

using ll = long long;

using vi = vector<int>;
#define pb push_back
#define rsz resize
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()

using pi = pair<int,int>;
#define f first
#define s second
#define mp make_pair

template<class T, int SZ> struct Seg{
    T tree[4*SZ];
    T merge(T a, T b){
        return max(a, b);
    }
    
    void update(int v, int l, int r, int i, T x) {
        if (i > r || i < l) {
            return;
        } else if (l == r) {
            tree[v] = x;
        } else {
            int m = (l + r) / 2;
            update(2 * v + 1, l, m, i, x);
            update(2 * v + 2, m + 1, r, i, x);
            tree[v] = merge(tree[2 * v + 1], tree[2 * v + 2]);
        }
    }

    T query(int v, int l, int r, int ql, int qr) {
        if (l > qr || r < ql) {
            return 0;
        } else if (l >= ql && r <= qr) {
            return tree[v];
        } else {
            int m = (l + r) / 2;
            T a = query(2 * v + 1, l, m, ql, qr);
            T b = query(2 * v + 2, m + 1, r, ql, qr);
            return merge(a,b);
        }
    }
};

const int MX = 200005;

map<int, int> pos;

Seg<int, MX> vals; 

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, a; cin >> n >> a;
    a--;
    vi d(n);
    for (int i = 0; i < n; i++) {
        cin >> d[i]; 
        pos[d[i]] = i;
        vals.update(0, 0, n - 1, i, d[i]);
    }
    /*
    cout << "vals: " << '\n';
    for (int i = 0; i < n; i++) {
        cout << vals.query(0, 0, n - 1, i, i) << " ";
    }
    cout << '\n';
    */ 
    int q; cin >> q;
    vi top;
    for (int i = max(1, n - 9); i <= n; i++) {
        top.pb(i);
    }
    while(q--) {
        char qt; cin >> qt; 
        if (qt == 'E') {
            int i, e; cin >> i >> e;
            i--;
            vi order;
            /*
            cout << "TOP: " << endl;
            for (int x : top) {
                cout << x << " ";
            }
            */ 
            for (int i = 0; i < e - 1; i++) {
                order.pb(top.back() + 1);
                vals.update(0, 0, n - 1, pos[top.back()], top.back() + 1);
                d[pos[top.back()]] = top.back() + 1;
                pos[top.back() + 1] = pos[top.back()];
                top.pop_back();
            }
            int prev = d[i];
            // cout << "prev: " << prev << endl;
            d[i] = top.back() + 1;
            vals.update(0, 0, n - 1, i, top.back() + 1);
            order.pb(top.back() + 1);
            pos[top.back() + 1] = i;
            while (!top.empty()) {
                if(top.back() != prev) order.pb(top.back());
                top.pop_back(); 
            }
            reverse(all(order));
            for (int x : order) {
                top.pb(x);
            }
            /*
            cout << "vals: " << '\n';
            for (int i = 0; i < n; i++) {
                cout << d[i] << " ";
            }
            cout << '\n';
            */
        }
        else {
            int x; cin >> x;
            x--;
            int best = vals.query(0, 0, n - 1, min(x, a), max(x, a));
            if (x == a) { 
                cout << 0 << '\n';
            }
            else if (x < a) {
                int lo = a + 1; int hi = n - 1;
                int ans = n;
                while (lo <= hi) {
                    int mid = (lo + hi) / 2;
                    if (vals.query(0, 0, n - 1, a, mid) > best) {
                        ans = mid;
                        hi = mid - 1;
                    }
                    else {
                        lo = mid + 1;
                    }
                }
                cout << (ans - x - 1) << '\n';
            }
            else {
                int lo = 0; int hi = a - 1;
                int ans = -1;
                while (lo <= hi) {
                    int mid = (lo + hi) / 2;
                    if (vals.query(0, 0, n - 1, mid, a) > best) {
                        ans = mid;
                        lo = mid + 1;
                    }
                    else {
                        hi = mid - 1;
                    }
                }
                cout << (x - ans - 1) << '\n';
            }
        }
    }
}

# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Incorrect 1 ms 384 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2068 ms 3764 KB Time limit exceeded
2 Execution timed out 2063 ms 8872 KB Time limit exceeded
3 Execution timed out 2069 ms 4640 KB Time limit exceeded
4 Correct 428 ms 24568 KB Output is correct
5 Execution timed out 2081 ms 4604 KB Time limit exceeded
6 Execution timed out 2048 ms 5260 KB Time limit exceeded
7 Execution timed out 2024 ms 5408 KB Time limit exceeded
8 Correct 409 ms 25336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 414 ms 7160 KB Output is correct
2 Incorrect 282 ms 7032 KB Output isn't correct
3 Incorrect 262 ms 7160 KB Output isn't correct
4 Incorrect 0 ms 384 KB Output isn't correct
5 Correct 589 ms 16048 KB Output is correct
6 Correct 667 ms 15980 KB Output is correct
7 Incorrect 509 ms 15876 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 634 ms 1912 KB Output isn't correct
2 Incorrect 846 ms 2120 KB Output isn't correct
3 Execution timed out 2064 ms 5212 KB Time limit exceeded
4 Execution timed out 2051 ms 5120 KB Time limit exceeded
5 Incorrect 1500 ms 4340 KB Output isn't correct
6 Execution timed out 2080 ms 5868 KB Time limit exceeded
7 Execution timed out 2066 ms 3280 KB Time limit exceeded
8 Execution timed out 2066 ms 8964 KB Time limit exceeded
9 Execution timed out 2075 ms 16712 KB Time limit exceeded
10 Execution timed out 2078 ms 6008 KB Time limit exceeded
11 Execution timed out 2048 ms 3360 KB Time limit exceeded
12 Execution timed out 2078 ms 14152 KB Time limit exceeded
13 Execution timed out 2068 ms 17924 KB Time limit exceeded