Submission #763754

# Submission time Handle Problem Language Result Execution time Memory
763754 2023-06-22T18:48:26 Z swagchicken Abracadabra (CEOI22_abracadabra) C++14
100 / 100
768 ms 65036 KB
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef vector<int> vi;
typedef vector< vector <int> > vvi;
typedef pair<int, int> pii;
typedef pair < pair < int, int >, int > piii;
typedef pair < pair <int, int > , pair <int, int> > piiii;
typedef pair<ll, ll> pll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
 
#define FOR(i,a,b) for(int i = a; i < b; i ++)
#define RFOR(i,a,b) for(int i = a-1; i >= b; i --)
#define all(a) a.begin(), a.end()
#define endl '\n';
#define sz(x) (int)(x).size()
 
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
 
template <typename T>
void pr(vector<T> &v) {
    FOR(i, 0, sz(v)) cout << v[i] << " ";
    cout << endl;
}
template <typename T>
void pr(vector<vector<T> > &v) {
    FOR(i, 0, sz(v)) { pr(v[i]); }
}
template <typename T>
void re(T &x) {
    cin >> x;
}
template <typename T>
void re(vector<T> &a) {
    FOR(i, 0, sz(a)) re(a[i]);
}
template <class Arg, class... Args>
void re(Arg &first, Args &... rest) {
    re(first);
    re(rest...);
}
template <typename T>
void pr(T x) {
    cout << x << endl;
}
template <class Arg, class... Args>
void pr(const Arg &first, const Args &... rest) {
    cout << first << " ";
    pr(rest...);
    cout << endl;
}
void ps() { cout << endl; }
template<class T, class... Ts>
void ps(const T& t, const Ts&... ts) {
    cout << t; if (sizeof...(ts)) cout << " "; ps(ts...);
}
 
const ll MOD  =  1000000007;
#define inf 1e18;
#define INF INT_MAX;
 
long double PI = 4*atan(1);
long double eps = 1e-8;

int N, q;
int n;

vi a;
vector<array<int, 3>> qs;

struct seg {
    int v, l, r;

    bool operator<(const seg& o) const {
        return v < o.v;
    }
};

map<seg, int> segidx;
set<seg> segs;
vector<seg> allsegs;

int st[2000000] = {};

void update(int p, int l, int r, int idx, int v) {
    if(l == r) {
        st[p] = v;
        return;
    }
    int m = (l + r)/2;
    if(idx <= m) {
        update(2*p, l, m, idx, v);
    } else {
        update(2*p + 1, m+1,r, idx, v);
    }
    st[p] = st[2*p] + st[2*p+1];
    return;
}

int query(int p, int l, int r, int v) {
    if(l == r) {
        return a[allsegs[l].l + v - 1];
    }
    int m = (l+r)/2;
    if(st[2*p] >= v) {
        return query(2*p, l, m, v);
    } 
    return query(2*p + 1, m + 1, r, v - st[2*p]);
}


vi bruteans;

vi brute() {
    vi b = a;
    int t = 0;

    vi c = b;
    int qidx = 0;
    vi ans(q);
    while(true) {
        
        while(qidx < q && qs[qidx][0] == t) {
            ans[qs[qidx][2]] = b[qs[qidx][1] - 1];
            qidx++;
        }

        c.clear();
        int lp = 0;
        int rp = 0;
        while(lp < n && rp < n) {
            if(b[lp] < b[n + rp]) {
                c.pb(b[lp]); lp++;
            } else {
                c.pb(b[n + rp]); rp++;
            }
        }
        while(lp < n) {c.pb(b[lp]); lp++;}
        while(rp < n) {c.pb(b[n + rp]); rp++;}

        if(c == b) break;
        b = c;
        t++;
    }

    
    while(qidx < q) {
        ans[qs[qidx][2]] = b[qs[qidx][1] - 1];
        qidx++;
    }
    
    return ans;
    
}
int main() {
    //auto start = chrono::high_resolution_clock::now();
    ios_base::sync_with_stdio(0);cin.tie(0);
    // freopen("promote.in", "r", stdin);
    // freopen("promote.out", "w", stdout);
    #ifdef DEBUG
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
    #endif 
    
    
    cin >> N >> q;
    n = N/2;

    a.resize(N); re(a);

    
    FOR(i,0,q) {
        int t,id; cin >> t >> id;
        qs.pb({t, id, i});
    }

    sort(all(qs));

    
    // vi bruteans = brute();

    stack<int> stk;
    vi nxt(N);
    FOR(i,0,N) {
        while(!stk.empty() && a[i] > a[stk.top()]) {
            nxt[stk.top()] = i - 1;
            stk.pop();
        }
        stk.push(i);
    }
    while(!stk.empty()) {
        int c = stk.top(); stk.pop();
        nxt[c] = N-1;
    }

    

    for(int i = 0; i < N; i = nxt[i] + 1) {
        segs.insert({a[i], i, nxt[i]});
        allsegs.pb({a[i], i, nxt[i]});
    }

    int len = N;
    while(len > n) {
        auto p = segs.rbegin();
        int clen = (*p).r - (*p).l + 1;
        while(len - clen >= n) {
            segs.erase(*p);
            len -= clen;
            if(!segs.empty()) p = segs.rbegin();
            clen = (*p).r - (*p).l + 1;
        }

        if(len > n) {
            int l = (*p).l; 
            int r = (*p).r;
            segs.erase(*p);
            
            int r1 = l + n - (len - clen) - 1;
            allsegs.pb({a[l], l, r1});
            segs.insert({a[l], l , r1});
            r1++;
            while(r1 <= r) {
                int r2 = min(nxt[r1], r);
                allsegs.pb({a[r1], r1, r2});
                segs.insert({a[r1], r1, r2});
                r1 = r2 + 1;
            }
        } 
    }

    sort(all(allsegs));
    int m = allsegs.size();
    FOR(i,0,m) segidx[allsegs[i]] = i;

    segs.clear();
    
    vi ans(q);
    int t = 0;
    int qidx = 0;
    
    for(int i = 0; i < N; i = nxt[i] + 1) {
        segs.insert({a[i], i, nxt[i]});
        int segid = segidx[{a[i], i, nxt[i]}];
        update(1, 0, m, segid, nxt[i] - i + 1);
    }

    while(qidx < q && qs[qidx][0] == t) {
        ans[qs[qidx][2]] = query(1, 0, m, qs[qidx][1]);
        qidx++;
    }

    len = N;
    while(len > n) {
        auto p = segs.rbegin();
        int clen = (*p).r - (*p).l + 1;
        while(len - clen >= n) {
            int segid = segidx[*p];
         //   update(1, 0, m, segid, 0);
            segs.erase(*p);
            len -= clen;
            if(!segs.empty()) p = segs.rbegin();
            clen = (*p).r - (*p).l + 1;
        }

        if(len > n) {
            int l = (*p).l; 
            int r = (*p).r;
            int segid = segidx[*p];
            update(1, 0, m, segid, 0);

            segs.erase(*p);
            
            
            int r1 = l + n - (len - clen) - 1;
            segs.insert({a[l], l , r1});

            segid = segidx[{a[l], l , r1}];
            update(1, 0, m, segid, r1 - l + 1);

            r1++;
            while(r1 <= r) {
                int r2 = min(nxt[r1], r);
                segs.insert({a[r1], r1, r2});

                segid = segidx[{a[r1], r1, r2}];
                update(1, 0, m, segid, r2 - r1 + 1);

                r1 = r2 + 1;
            }
        }
        
        t++;

        while(qidx < q && qs[qidx][0] == t) {
            ans[qs[qidx][2]] = query(1, 0, m, qs[qidx][1]);
            qidx++;
        }
    }

    while(qidx < q) {
        ans[qs[qidx][2]] = query(1, 0, m, qs[qidx][1]);
        qidx++;
    }

    FOR(i,0,q) {
        cout << ans[i] << endl;
    }


    





    

    
    // auto stop = chrono::high_resolution_clock::now();
    // auto duration = chrono::duration_cast<chrono::microseconds>(stop - start);
    // cout << duration.count() << endl;
    //cin.close();
    //cout.close();
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:266:17: warning: unused variable 'segid' [-Wunused-variable]
  266 |             int segid = segidx[*p];
      |                 ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 318 ms 27628 KB Output is correct
2 Correct 330 ms 27284 KB Output is correct
3 Correct 307 ms 26448 KB Output is correct
4 Correct 305 ms 25068 KB Output is correct
5 Correct 310 ms 27028 KB Output is correct
6 Correct 297 ms 25412 KB Output is correct
7 Correct 309 ms 27032 KB Output is correct
8 Correct 299 ms 25600 KB Output is correct
9 Correct 298 ms 25180 KB Output is correct
10 Correct 300 ms 25728 KB Output is correct
11 Correct 322 ms 25560 KB Output is correct
12 Correct 296 ms 24604 KB Output is correct
13 Correct 300 ms 25388 KB Output is correct
14 Correct 306 ms 26272 KB Output is correct
15 Correct 313 ms 26132 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 286 ms 24924 KB Output is correct
18 Correct 303 ms 24648 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 604 ms 65036 KB Output is correct
2 Correct 546 ms 63912 KB Output is correct
3 Correct 548 ms 55472 KB Output is correct
4 Correct 364 ms 37896 KB Output is correct
5 Correct 385 ms 39268 KB Output is correct
6 Correct 402 ms 37440 KB Output is correct
7 Correct 337 ms 41388 KB Output is correct
8 Correct 346 ms 41552 KB Output is correct
9 Correct 343 ms 37404 KB Output is correct
10 Correct 297 ms 35904 KB Output is correct
11 Correct 280 ms 28952 KB Output is correct
12 Correct 278 ms 30176 KB Output is correct
13 Correct 312 ms 34528 KB Output is correct
14 Correct 281 ms 30512 KB Output is correct
15 Correct 304 ms 36316 KB Output is correct
16 Correct 16 ms 3284 KB Output is correct
17 Correct 448 ms 60552 KB Output is correct
18 Correct 248 ms 27212 KB Output is correct
19 Correct 61 ms 9128 KB Output is correct
20 Correct 88 ms 11204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 176 ms 18212 KB Output is correct
2 Correct 145 ms 18144 KB Output is correct
3 Correct 173 ms 17652 KB Output is correct
4 Correct 51 ms 6464 KB Output is correct
5 Correct 118 ms 12192 KB Output is correct
6 Correct 56 ms 6976 KB Output is correct
7 Correct 88 ms 9916 KB Output is correct
8 Correct 70 ms 8668 KB Output is correct
9 Correct 89 ms 10396 KB Output is correct
10 Correct 39 ms 4920 KB Output is correct
11 Correct 42 ms 5392 KB Output is correct
12 Correct 38 ms 4828 KB Output is correct
13 Correct 41 ms 5312 KB Output is correct
14 Correct 40 ms 5204 KB Output is correct
15 Correct 38 ms 4748 KB Output is correct
16 Correct 10 ms 2000 KB Output is correct
17 Correct 124 ms 18736 KB Output is correct
18 Correct 34 ms 4780 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 318 ms 27628 KB Output is correct
2 Correct 330 ms 27284 KB Output is correct
3 Correct 307 ms 26448 KB Output is correct
4 Correct 305 ms 25068 KB Output is correct
5 Correct 310 ms 27028 KB Output is correct
6 Correct 297 ms 25412 KB Output is correct
7 Correct 309 ms 27032 KB Output is correct
8 Correct 299 ms 25600 KB Output is correct
9 Correct 298 ms 25180 KB Output is correct
10 Correct 300 ms 25728 KB Output is correct
11 Correct 322 ms 25560 KB Output is correct
12 Correct 296 ms 24604 KB Output is correct
13 Correct 300 ms 25388 KB Output is correct
14 Correct 306 ms 26272 KB Output is correct
15 Correct 313 ms 26132 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 286 ms 24924 KB Output is correct
18 Correct 303 ms 24648 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 604 ms 65036 KB Output is correct
22 Correct 546 ms 63912 KB Output is correct
23 Correct 548 ms 55472 KB Output is correct
24 Correct 364 ms 37896 KB Output is correct
25 Correct 385 ms 39268 KB Output is correct
26 Correct 402 ms 37440 KB Output is correct
27 Correct 337 ms 41388 KB Output is correct
28 Correct 346 ms 41552 KB Output is correct
29 Correct 343 ms 37404 KB Output is correct
30 Correct 297 ms 35904 KB Output is correct
31 Correct 280 ms 28952 KB Output is correct
32 Correct 278 ms 30176 KB Output is correct
33 Correct 312 ms 34528 KB Output is correct
34 Correct 281 ms 30512 KB Output is correct
35 Correct 304 ms 36316 KB Output is correct
36 Correct 16 ms 3284 KB Output is correct
37 Correct 448 ms 60552 KB Output is correct
38 Correct 248 ms 27212 KB Output is correct
39 Correct 61 ms 9128 KB Output is correct
40 Correct 88 ms 11204 KB Output is correct
41 Correct 176 ms 18212 KB Output is correct
42 Correct 145 ms 18144 KB Output is correct
43 Correct 173 ms 17652 KB Output is correct
44 Correct 51 ms 6464 KB Output is correct
45 Correct 118 ms 12192 KB Output is correct
46 Correct 56 ms 6976 KB Output is correct
47 Correct 88 ms 9916 KB Output is correct
48 Correct 70 ms 8668 KB Output is correct
49 Correct 89 ms 10396 KB Output is correct
50 Correct 39 ms 4920 KB Output is correct
51 Correct 42 ms 5392 KB Output is correct
52 Correct 38 ms 4828 KB Output is correct
53 Correct 41 ms 5312 KB Output is correct
54 Correct 40 ms 5204 KB Output is correct
55 Correct 38 ms 4748 KB Output is correct
56 Correct 10 ms 2000 KB Output is correct
57 Correct 124 ms 18736 KB Output is correct
58 Correct 34 ms 4780 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Correct 747 ms 64548 KB Output is correct
62 Correct 704 ms 63688 KB Output is correct
63 Correct 768 ms 61560 KB Output is correct
64 Correct 438 ms 43600 KB Output is correct
65 Correct 541 ms 45860 KB Output is correct
66 Correct 453 ms 43540 KB Output is correct
67 Correct 417 ms 40800 KB Output is correct
68 Correct 415 ms 41192 KB Output is correct
69 Correct 455 ms 42416 KB Output is correct
70 Correct 383 ms 36268 KB Output is correct
71 Correct 352 ms 34388 KB Output is correct
72 Correct 365 ms 36364 KB Output is correct
73 Correct 349 ms 35232 KB Output is correct
74 Correct 359 ms 36540 KB Output is correct
75 Correct 364 ms 36476 KB Output is correct
76 Correct 17 ms 3284 KB Output is correct
77 Correct 515 ms 60744 KB Output is correct
78 Correct 327 ms 32932 KB Output is correct
79 Correct 1 ms 212 KB Output is correct
80 Correct 1 ms 212 KB Output is correct