Submission #176067

# Submission time Handle Problem Language Result Execution time Memory
176067 2020-01-07T19:47:06 Z stefdasca Cake (CEOI14_cake) C++14
0 / 100
546 ms 10764 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 = max(1, n - 9); 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)
                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 248 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 388 ms 5348 KB Output isn't correct
2 Correct 295 ms 5352 KB Output is correct
3 Incorrect 320 ms 5380 KB Output isn't correct
4 Correct 223 ms 5348 KB Output is correct
5 Incorrect 411 ms 5756 KB Output isn't correct
6 Incorrect 371 ms 5780 KB Output isn't correct
7 Incorrect 343 ms 5736 KB Output isn't correct
8 Correct 232 ms 5748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 76 ms 4216 KB Output isn't correct
2 Incorrect 68 ms 4204 KB Output isn't correct
3 Incorrect 61 ms 4088 KB Output isn't correct
4 Incorrect 2 ms 376 KB Output isn't correct
5 Incorrect 112 ms 7544 KB Output isn't correct
6 Incorrect 101 ms 7544 KB Output isn't correct
7 Correct 85 ms 7288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 42 ms 1284 KB Output isn't correct
2 Incorrect 36 ms 1324 KB Output isn't correct
3 Incorrect 65 ms 3068 KB Output isn't correct
4 Incorrect 76 ms 3064 KB Output isn't correct
5 Incorrect 113 ms 2364 KB Output isn't correct
6 Incorrect 124 ms 3916 KB Output isn't correct
7 Incorrect 132 ms 2548 KB Output isn't correct
8 Incorrect 164 ms 5868 KB Output isn't correct
9 Incorrect 546 ms 10764 KB Output isn't correct
10 Incorrect 366 ms 4176 KB Output isn't correct
11 Incorrect 430 ms 4968 KB Output isn't correct
12 Incorrect 544 ms 10244 KB Output isn't correct
13 Incorrect 457 ms 10624 KB Output isn't correct