Submission #943777

# Submission time Handle Problem Language Result Execution time Memory
943777 2024-03-11T21:34:59 Z Ahmed57 Abracadabra (CEOI22_abracadabra) C++17
100 / 100
1733 ms 50420 KB
#include "bits/stdc++.h"
 
using namespace std;
#ifdef LOCAL
#include "debug.cpp"
#else
#define debug(...)
#endif
int n;
int bit[1000001];
void add(int e,int v){
    while(e<=n){
        bit[e]+=v;
        e+=e&-e;
    }
}
int sum(int e){
    int su = 0;
    while(e>=1){
        su+=bit[e];
        e-=e&-e;
    }
    return su;
}
signed main() {
    ios_base::sync_with_stdio(false);cin.tie(0);
    int q;
    cin>>n>>q;
    int arr[n+1],pos[n+1];
    for(int i = 1;i<=n;i++){
        cin>>arr[i];
        pos[arr[i]] = i;
    }
    int nxt[n+1];
    stack<int> st;
    for(int i = n;i>=1;i--){
        while(!st.empty()&&arr[i]>=arr[st.top()])st.pop();
        if(st.size())nxt[i] = st.top();
        else nxt[i] = n+1;
        st.push(i);
    }
    int cur = 1;
    set<int> s;
    while(cur<=n){
        s.insert(cur);
        add(arr[cur],nxt[cur]-cur);
        cur = nxt[cur];
    }
    std::vector<array<int,3>> qu;
    for(int i = 1;i<=q;i++){
        int a,b;
        cin>>a>>b;
        a = min(a,n);
        qu.push_back({a,b,i});
    }sort(qu.begin(),qu.end());
    int level = 0;
    int ANS[q+1];
    for(int i = 0;i<qu.size();i++){
        while(level<qu[i][0]){
            int no = n/2+1;
            int l = 1 , r = n , ans = 0;
            while(l<=r){
                int mid = (l+r)/2;
                if(sum(mid)>=no){
                    ans = mid;
                    r = mid-1;
                }else l = mid+1;
            }
            int po = pos[ans]+(no-sum(ans-1)-1);
            no = po;
            auto it = s.lower_bound(no);
            int en = n+1;
            if(it!=s.end()){
                en = (*it);
            }
            if(it==s.end()||(*it)!=no){
                it--;
                add(arr[(*it)],(-(en-(*it))) +(no-(*it)));
            }
            while(no<=n){
                auto it = s.lower_bound(no);
                if(it==s.end()||(*it)!=no){
                    int nx = nxt[no];
                    auto lol = s.lower_bound(no);
                    if(lol!=s.end()){
                        nx = min(nx,(*lol));
                    }
                    s.insert(no);
                    add(arr[no],nx-no);
                    no = nx;
                }else break;
            }
            level++;
        }
        int l = 1 , r = n , ans = 0;
        while(l<=r){
            int mid = (l+r)/2;
            if(sum(mid)>=qu[i][1]){
                ans = mid;
                r = mid-1;
            }else l = mid+1;
        }
        ANS[qu[i][2]] = arr[pos[ans]+(qu[i][1]-sum(ans-1)-1)];
    }
    for(int i = 1;i<=q;i++)cout<<ANS[i]<<endl;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:58:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |     for(int i = 0;i<qu.size();i++){
      |                   ~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1425 ms 21016 KB Output is correct
2 Correct 1412 ms 19984 KB Output is correct
3 Correct 1376 ms 19464 KB Output is correct
4 Correct 1360 ms 19668 KB Output is correct
5 Correct 1398 ms 20188 KB Output is correct
6 Correct 1326 ms 19188 KB Output is correct
7 Correct 1391 ms 20264 KB Output is correct
8 Correct 1310 ms 19460 KB Output is correct
9 Correct 1299 ms 19212 KB Output is correct
10 Correct 1330 ms 19400 KB Output is correct
11 Correct 1368 ms 19708 KB Output is correct
12 Correct 1332 ms 19884 KB Output is correct
13 Correct 1368 ms 20012 KB Output is correct
14 Correct 1359 ms 19888 KB Output is correct
15 Correct 1395 ms 20248 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1321 ms 19756 KB Output is correct
18 Correct 1365 ms 19284 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1580 ms 47368 KB Output is correct
2 Correct 1489 ms 50192 KB Output is correct
3 Correct 1208 ms 41128 KB Output is correct
4 Correct 1255 ms 35220 KB Output is correct
5 Correct 1244 ms 36684 KB Output is correct
6 Correct 1203 ms 33820 KB Output is correct
7 Correct 1497 ms 41492 KB Output is correct
8 Correct 1404 ms 41156 KB Output is correct
9 Correct 1186 ms 36464 KB Output is correct
10 Correct 1391 ms 39432 KB Output is correct
11 Correct 1176 ms 32800 KB Output is correct
12 Correct 1139 ms 33900 KB Output is correct
13 Correct 1380 ms 37952 KB Output is correct
14 Correct 1176 ms 34432 KB Output is correct
15 Correct 1422 ms 38832 KB Output is correct
16 Correct 17 ms 5972 KB Output is correct
17 Correct 1473 ms 45456 KB Output is correct
18 Correct 1160 ms 30512 KB Output is correct
19 Correct 287 ms 11968 KB Output is correct
20 Correct 318 ms 13756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 206 ms 10616 KB Output is correct
2 Correct 209 ms 10832 KB Output is correct
3 Correct 202 ms 10468 KB Output is correct
4 Correct 170 ms 6340 KB Output is correct
5 Correct 195 ms 8068 KB Output is correct
6 Correct 169 ms 6344 KB Output is correct
7 Correct 182 ms 7364 KB Output is correct
8 Correct 213 ms 7008 KB Output is correct
9 Correct 193 ms 7504 KB Output is correct
10 Correct 163 ms 5932 KB Output is correct
11 Correct 159 ms 6088 KB Output is correct
12 Correct 158 ms 5760 KB Output is correct
13 Correct 171 ms 6008 KB Output is correct
14 Correct 166 ms 5956 KB Output is correct
15 Correct 166 ms 5996 KB Output is correct
16 Correct 8 ms 3928 KB Output is correct
17 Correct 187 ms 11140 KB Output is correct
18 Correct 162 ms 5716 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1425 ms 21016 KB Output is correct
2 Correct 1412 ms 19984 KB Output is correct
3 Correct 1376 ms 19464 KB Output is correct
4 Correct 1360 ms 19668 KB Output is correct
5 Correct 1398 ms 20188 KB Output is correct
6 Correct 1326 ms 19188 KB Output is correct
7 Correct 1391 ms 20264 KB Output is correct
8 Correct 1310 ms 19460 KB Output is correct
9 Correct 1299 ms 19212 KB Output is correct
10 Correct 1330 ms 19400 KB Output is correct
11 Correct 1368 ms 19708 KB Output is correct
12 Correct 1332 ms 19884 KB Output is correct
13 Correct 1368 ms 20012 KB Output is correct
14 Correct 1359 ms 19888 KB Output is correct
15 Correct 1395 ms 20248 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1321 ms 19756 KB Output is correct
18 Correct 1365 ms 19284 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1580 ms 47368 KB Output is correct
22 Correct 1489 ms 50192 KB Output is correct
23 Correct 1208 ms 41128 KB Output is correct
24 Correct 1255 ms 35220 KB Output is correct
25 Correct 1244 ms 36684 KB Output is correct
26 Correct 1203 ms 33820 KB Output is correct
27 Correct 1497 ms 41492 KB Output is correct
28 Correct 1404 ms 41156 KB Output is correct
29 Correct 1186 ms 36464 KB Output is correct
30 Correct 1391 ms 39432 KB Output is correct
31 Correct 1176 ms 32800 KB Output is correct
32 Correct 1139 ms 33900 KB Output is correct
33 Correct 1380 ms 37952 KB Output is correct
34 Correct 1176 ms 34432 KB Output is correct
35 Correct 1422 ms 38832 KB Output is correct
36 Correct 17 ms 5972 KB Output is correct
37 Correct 1473 ms 45456 KB Output is correct
38 Correct 1160 ms 30512 KB Output is correct
39 Correct 287 ms 11968 KB Output is correct
40 Correct 318 ms 13756 KB Output is correct
41 Correct 206 ms 10616 KB Output is correct
42 Correct 209 ms 10832 KB Output is correct
43 Correct 202 ms 10468 KB Output is correct
44 Correct 170 ms 6340 KB Output is correct
45 Correct 195 ms 8068 KB Output is correct
46 Correct 169 ms 6344 KB Output is correct
47 Correct 182 ms 7364 KB Output is correct
48 Correct 213 ms 7008 KB Output is correct
49 Correct 193 ms 7504 KB Output is correct
50 Correct 163 ms 5932 KB Output is correct
51 Correct 159 ms 6088 KB Output is correct
52 Correct 158 ms 5760 KB Output is correct
53 Correct 171 ms 6008 KB Output is correct
54 Correct 166 ms 5956 KB Output is correct
55 Correct 166 ms 5996 KB Output is correct
56 Correct 8 ms 3928 KB Output is correct
57 Correct 187 ms 11140 KB Output is correct
58 Correct 162 ms 5716 KB Output is correct
59 Correct 1 ms 344 KB Output is correct
60 Correct 1 ms 344 KB Output is correct
61 Correct 1721 ms 50420 KB Output is correct
62 Correct 1733 ms 50228 KB Output is correct
63 Correct 1650 ms 49700 KB Output is correct
64 Correct 1603 ms 42124 KB Output is correct
65 Correct 1639 ms 43020 KB Output is correct
66 Correct 1566 ms 41960 KB Output is correct
67 Correct 1552 ms 41024 KB Output is correct
68 Correct 1598 ms 40672 KB Output is correct
69 Correct 1524 ms 41012 KB Output is correct
70 Correct 1528 ms 38640 KB Output is correct
71 Correct 1443 ms 37204 KB Output is correct
72 Correct 1500 ms 38216 KB Output is correct
73 Correct 1503 ms 37872 KB Output is correct
74 Correct 1512 ms 39672 KB Output is correct
75 Correct 1556 ms 39236 KB Output is correct
76 Correct 17 ms 5972 KB Output is correct
77 Correct 1502 ms 45900 KB Output is correct
78 Correct 1453 ms 36272 KB Output is correct
79 Correct 0 ms 348 KB Output is correct
80 Correct 0 ms 348 KB Output is correct