Submission #176071

# Submission time Handle Problem Language Result Execution time Memory
176071 2020-01-07T20:00:12 Z stefdasca Cake (CEOI14_cake) C++14
0 / 100
568 ms 12368 KB
#include<bits/stdc++.h>
#define god dimasi5eks
#pragma GCC optimize("O3")
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define mod 1000000007
#define dancila 3.14159265359
#define eps 1e-9

// #define fisier 1

using namespace std;

typedef long long ll;

int n, a, v[250002], whr[250002];

int q;

int aint[1000002], poz[1000002];

vector<pair<int, int> >top;

void build(int nod, int st, int dr)
{
    if(st == dr)
    {
        aint[nod] = v[st];
        poz[nod] = st;
        return;
    }
    int mid = (st + dr) / 2;
    build(nod << 1, st, mid);
    build(nod << 1|1, mid+1, dr);
    if(st >= a)
    {
        if(aint[nod << 1] >= aint[nod << 1|1])
            aint[nod] = aint[nod << 1], poz[nod] = poz[nod << 1];
        else
            aint[nod] = aint[nod << 1|1], poz[nod] = poz[nod << 1|1];
    }
    else
        if(aint[nod << 1] > aint[nod << 1|1])
            aint[nod] = aint[nod << 1], poz[nod] = poz[nod << 1];
        else
            aint[nod] = aint[nod << 1|1], poz[nod] = poz[nod << 1|1];
}

void upd(int nod, int st, int dr, int pz, int val)
{
    if(st == dr)
    {
        aint[nod] = val;
        poz[nod] = st;
        return;
    }
    int mid = (st + dr) / 2;
    if(pz <= mid)
        upd(nod << 1, st, mid, pz, val);
    else
        upd(nod << 1|1, mid+1, dr, pz, val);
    if(st >= a)
    {
        if(aint[nod << 1] >= aint[nod << 1|1])
            aint[nod] = aint[nod << 1], poz[nod] = poz[nod << 1];
        else
            aint[nod] = aint[nod << 1|1], poz[nod] = poz[nod << 1|1];
    }
    else
        if(aint[nod << 1] > aint[nod << 1|1])
            aint[nod] = aint[nod << 1], poz[nod] = poz[nod << 1];
        else
            aint[nod] = aint[nod << 1|1], poz[nod] = poz[nod << 1|1];
}

int qumx(int nod, int st, int dr, int L, int R)
{
    if(dr < L || st > R)
        return 0;
    if(L <= st && dr <= R)
        return aint[nod];
    int mid = (st + dr) / 2;
    return max(qumx(nod << 1, st, mid, L, R), qumx(nod << 1|1, mid+1, dr, L, R));
}
int mxval;
int query1(int nod, int st, int dr, int L, int R)
{
    if(dr < L || st > R)
        return -1;
    if(aint[nod] < mxval)
        return -1;
    if(L <= st && dr <= R)
        return poz[nod];
    int mid = (st + dr) / 2;
    int ans = query1(nod << 1|1, mid+1, dr, L, R);
    if(ans != -1)
        return ans;
    return query1(nod << 1, st, mid, L, R);
}

int query2(int nod, int st, int dr, int L, int R)
{
    if(dr < L || st > R)
        return -1;
    if(aint[nod] < mxval)
        return -1;
    if(L <= st && dr <= R)
        return poz[nod];
    int mid = (st + dr) / 2;
    int ans = query1(nod << 1, st, mid, L, R);
    if(ans != -1)
        return ans;
    return query1(nod << 1|1, mid+1, dr, L, R);
}
int main()
{

    #ifdef fisier
        ifstream f("input.in");
        ofstream g("output.out");
    #endif

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n >> a;
    for(int i = 1; i <= n; ++i)
        cin >> v[i], whr[v[i]] = i;
    for(int i = 1; i <= n; ++i)
        top.pb({i, whr[i]});
    build(1, 1, n);
    cin >> q;
    for(int i = 1; i <= q; ++i)
    {
        char tip;
        cin >> tip;
        if(tip == 'E')
        {
            int poz, plz;
            cin >> poz >> plz;
            deque<pair<int, int> >vals;
            while(vals.size() + 1 < plz)
            {
                if(top.back().se == poz)
                    top.pop_back();
                else
                    vals.push_front(top.back()), top.pop_back();
            }
            upd(1, 1, n, poz, top.back().fi + 1);
            top.pb({top.back().fi + 1, poz});
            while(!vals.empty())
            {
                upd(1, 1, n, vals.front().se, top.back().fi + 1);
                top.pb({top.back().fi + 1, vals.front().se});
                vals.pop_front();
            }
        }
        else
        {
            int poz;
            cin >> poz;
            if(poz == a)
                cout << 0 << '\n';
            else
            {
                if(a == 1 || a == n)
                    cout << abs(poz - a) << '\n';
                else
                    if(poz > a)
                    {
                        int ans = poz - a;
                        mxval = qumx(1, 1, n, a + 1, poz);
                        int p2 = query1(1, 1, n, 1, a - 1);
                        if(p2 == -1)
                            p2 = 0;
                        ans += a - 1 - p2;
                        cout << ans << '\n';
                    }
                    else
                    {
                        int ans = a - poz;
                        mxval = qumx(1, 1, n, poz, a - 1);
                        int p2 = query2(1, 1, n, a + 1, n);
                        if(p2 == -1)
                            p2 = n + 1;
                        ans += p2 - a - 1;
                        cout << ans << '\n';
                    }
            }
        }
    }
    return 0;
}

Compilation message

cake.cpp: In function 'int main()':
cake.cpp:144:35: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             while(vals.size() + 1 < plz)
                   ~~~~~~~~~~~~~~~~^~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Incorrect 2 ms 376 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 385 ms 4968 KB Output isn't correct
2 Correct 297 ms 5016 KB Output is correct
3 Incorrect 320 ms 4940 KB Output isn't correct
4 Correct 228 ms 5028 KB Output is correct
5 Incorrect 413 ms 5436 KB Output isn't correct
6 Incorrect 373 ms 9488 KB Output isn't correct
7 Incorrect 342 ms 9444 KB Output isn't correct
8 Correct 238 ms 9500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 75 ms 4720 KB Output isn't correct
2 Incorrect 65 ms 4724 KB Output isn't correct
3 Incorrect 60 ms 4720 KB Output isn't correct
4 Incorrect 2 ms 380 KB Output isn't correct
5 Incorrect 108 ms 9220 KB Output isn't correct
6 Incorrect 106 ms 9192 KB Output isn't correct
7 Correct 96 ms 8936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 43 ms 888 KB Output isn't correct
2 Incorrect 35 ms 1016 KB Output isn't correct
3 Incorrect 66 ms 3060 KB Output isn't correct
4 Incorrect 82 ms 3060 KB Output isn't correct
5 Incorrect 113 ms 1996 KB Output isn't correct
6 Incorrect 125 ms 4464 KB Output isn't correct
7 Incorrect 132 ms 2184 KB Output isn't correct
8 Incorrect 173 ms 7400 KB Output isn't correct
9 Incorrect 568 ms 12368 KB Output isn't correct
10 Incorrect 369 ms 3844 KB Output isn't correct
11 Incorrect 436 ms 6816 KB Output isn't correct
12 Incorrect 552 ms 11296 KB Output isn't correct
13 Incorrect 465 ms 12268 KB Output isn't correct