답안 #824368

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
824368 2023-08-14T04:33:53 Z kwongweng Abracadabra (CEOI22_abracadabra) C++17
100 / 100
738 ms 45772 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long double ld;
typedef pair<ll, ll> pll;
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define ROF(i, a, b) for(int i = a; i >= b; i--)
#define ms memset
#define pb push_back
#define fi first
#define se second

/*Decompose the array into blocks such that:
Leftmost value of block is max in the block
the values of leftmost of each block is strictly increasing
Each swap simply decompose the block containing position n/2, n/2+1
(i.e. a "cut" between n/2 and n/2+1)
Then the blocks will be sorted based on leftmost value.
The structure of the blocks can be maintained using segtree*/

/*segtree used to find the position & maintain the blocks
sm[v] (with tl==tr) stores the number of elements in the block with leftmost value tl.

*/ 
struct segtree{
  int sz; vi sm;
  void init(int n){
    sz=n; sm.assign(4*n,0);
  }
  void update(int v, int tl, int tr, int pos, int val){
    if (tl==tr) {sm[v]=val; return;}
    int tm=(tl+tr)/2;
    if (pos<=tm) update(2*v,tl,tm,pos,val);
    else update(2*v+1,tm+1,tr,pos,val);
    sm[v]=sm[2*v]+sm[2*v+1];
  }
  void update(int pos, int val) {update(1,0,sz-1,pos,val);}
  ii get(int v, int tl, int tr, int val){
    if (tl==tr) {return {tl,val};}
    int tm=(tl+tr)/2;
    if (sm[2*v]<val) return get(2*v+1,tm+1,tr,val-sm[2*v]);
    return get(2*v,tl,tm,val);
  }
  ii get(int val){return get(1,0,sz-1,val);}
};

//segtree2 is range maximum, returns index with max value.
struct segtree2{
  int sz; vi mx, a;
  void init(int n, vi A){
    sz=n; mx.assign(4*n,-1); a=A;
  }
  void build(int v, int tl, int tr){
    if (tl==tr){mx[v]=tl; return;}
    int tm=(tl+tr)/2;
    build(2*v,tl,tm); build(2*v+1,tm+1,tr);
    if (a[mx[2*v]]>a[mx[2*v+1]]) mx[v]=mx[2*v];
    else mx[v]=mx[2*v+1];
  }
  int get(int v, int tl, int tr, int l, int r){
    if (tl==l && tr==r) return mx[v];
    if (l>r) return -1;
    int tm=(tl+tr)/2;
    int L=get(2*v,tl,tm,l,min(r,tm)); int R=get(2*v+1,tm+1,tr,max(l,tm+1),r);
    if (L==-1) return R;
    if (R==-1) return L;
    if (a[L]<a[R]) return R;
    return L;
  }
  int get(int l, int r){return get(1,0,sz-1,l,r);}
};

//offline query, sort the query in increasing order of t-value (# swaps)
struct que{
  int id, pos, t;
};

void solve(){
  int n,q; cin>>n>>q;
  vi a(n); FOR(i,0,n){ cin>>a[i]; a[i]--;}
  segtree2 st2; st2.init(n,a); st2.build(1,0,n-1);
  vi pos(n); FOR(i,0,n) pos[a[i]]=i;
  vi sz(n);
  vector<que> Q[n]; vi ans(q);
  FOR(i,0,q){
    que qr; cin>>qr.t>>qr.pos; qr.id=i; qr.pos--;
    if (qr.t>=n) qr.t=n-1;
    Q[qr.t].pb(qr);
  }
  segtree st; st.init(n);
  vi used(n); // decompose the array
  ROF(i,n-1,0){
    if (used[i]) continue;
    int L = pos[i]; int cnt=0;
    while (L < n){
      if (used[a[L]]) break;
      cnt++; used[a[L]]=1; L++;
    }
    st.update(i,cnt); sz[i]=cnt;
    //cout<<i<<" "<<cnt<<'\n';
  }
  FOR(i,0,n){
    for (que qr : Q[i]){
      ii val = st.get(qr.pos+1);
      ans[qr.id] = a[pos[val.fi]+val.se-1];
    }
    ii val = st.get(n/2+1);
    if (val.se == 1) continue;
    // there is block containing the middle 2 positions, decompose this block.
    int L=pos[val.fi]+val.se-1, R=pos[val.fi]+sz[val.fi]-1;
    while(st2.get(L,R) != -1){
      int mx = st2.get(L,R); 
      sz[a[mx]]=R-mx+1; st.update(a[mx],sz[a[mx]]);
      R=mx-1; 
    }
    sz[val.fi]=val.se-1; st.update(val.fi, sz[val.fi]);
  }
  FOR(i,0,q) cout<<ans[i]+1<<"\n";
}

int main(){
	//MOD=MOD1;
    ios::sync_with_stdio(false);
    if (fopen("input.txt", "r")) {
		  freopen("input.txt", "r", stdin);
    	freopen("output.txt", "w", stdout);
	  }
    int TC = 1;
    //cin >> TC; 
    FOR(i, 1, TC+1){
        //cout << "Case #" << i << ": ";
        solve();
    }
	
	return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:128:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  128 |     freopen("input.txt", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:129:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  129 |      freopen("output.txt", "w", stdout);
      |      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 205 ms 23364 KB Output is correct
2 Correct 213 ms 21656 KB Output is correct
3 Correct 203 ms 21304 KB Output is correct
4 Correct 169 ms 19640 KB Output is correct
5 Correct 193 ms 23800 KB Output is correct
6 Correct 198 ms 24892 KB Output is correct
7 Correct 206 ms 25108 KB Output is correct
8 Correct 261 ms 23172 KB Output is correct
9 Correct 188 ms 21804 KB Output is correct
10 Correct 196 ms 20736 KB Output is correct
11 Correct 204 ms 21416 KB Output is correct
12 Correct 177 ms 19160 KB Output is correct
13 Correct 185 ms 19976 KB Output is correct
14 Correct 191 ms 23756 KB Output is correct
15 Correct 183 ms 20196 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 209 ms 19440 KB Output is correct
18 Correct 162 ms 19312 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 311 ms 37280 KB Output is correct
2 Correct 284 ms 37220 KB Output is correct
3 Correct 235 ms 31916 KB Output is correct
4 Correct 214 ms 32052 KB Output is correct
5 Correct 222 ms 32792 KB Output is correct
6 Correct 206 ms 31620 KB Output is correct
7 Correct 250 ms 37108 KB Output is correct
8 Correct 246 ms 35568 KB Output is correct
9 Correct 215 ms 32668 KB Output is correct
10 Correct 227 ms 35392 KB Output is correct
11 Correct 193 ms 32448 KB Output is correct
12 Correct 192 ms 31484 KB Output is correct
13 Correct 234 ms 35760 KB Output is correct
14 Correct 196 ms 32448 KB Output is correct
15 Correct 304 ms 36352 KB Output is correct
16 Correct 27 ms 15044 KB Output is correct
17 Correct 284 ms 35808 KB Output is correct
18 Correct 171 ms 31740 KB Output is correct
19 Correct 66 ms 19208 KB Output is correct
20 Correct 72 ms 18704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 81 ms 10932 KB Output is correct
2 Correct 76 ms 10704 KB Output is correct
3 Correct 85 ms 10380 KB Output is correct
4 Correct 62 ms 10504 KB Output is correct
5 Correct 58 ms 10700 KB Output is correct
6 Correct 43 ms 10276 KB Output is correct
7 Correct 73 ms 10644 KB Output is correct
8 Correct 51 ms 10264 KB Output is correct
9 Correct 80 ms 10492 KB Output is correct
10 Correct 44 ms 10160 KB Output is correct
11 Correct 58 ms 10700 KB Output is correct
12 Correct 43 ms 10276 KB Output is correct
13 Correct 37 ms 9968 KB Output is correct
14 Correct 40 ms 10488 KB Output is correct
15 Correct 35 ms 10328 KB Output is correct
16 Correct 19 ms 7632 KB Output is correct
17 Correct 44 ms 9520 KB Output is correct
18 Correct 45 ms 9508 KB Output is correct
19 Correct 1 ms 256 KB Output is correct
20 Correct 1 ms 260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 205 ms 23364 KB Output is correct
2 Correct 213 ms 21656 KB Output is correct
3 Correct 203 ms 21304 KB Output is correct
4 Correct 169 ms 19640 KB Output is correct
5 Correct 193 ms 23800 KB Output is correct
6 Correct 198 ms 24892 KB Output is correct
7 Correct 206 ms 25108 KB Output is correct
8 Correct 261 ms 23172 KB Output is correct
9 Correct 188 ms 21804 KB Output is correct
10 Correct 196 ms 20736 KB Output is correct
11 Correct 204 ms 21416 KB Output is correct
12 Correct 177 ms 19160 KB Output is correct
13 Correct 185 ms 19976 KB Output is correct
14 Correct 191 ms 23756 KB Output is correct
15 Correct 183 ms 20196 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 209 ms 19440 KB Output is correct
18 Correct 162 ms 19312 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 311 ms 37280 KB Output is correct
22 Correct 284 ms 37220 KB Output is correct
23 Correct 235 ms 31916 KB Output is correct
24 Correct 214 ms 32052 KB Output is correct
25 Correct 222 ms 32792 KB Output is correct
26 Correct 206 ms 31620 KB Output is correct
27 Correct 250 ms 37108 KB Output is correct
28 Correct 246 ms 35568 KB Output is correct
29 Correct 215 ms 32668 KB Output is correct
30 Correct 227 ms 35392 KB Output is correct
31 Correct 193 ms 32448 KB Output is correct
32 Correct 192 ms 31484 KB Output is correct
33 Correct 234 ms 35760 KB Output is correct
34 Correct 196 ms 32448 KB Output is correct
35 Correct 304 ms 36352 KB Output is correct
36 Correct 27 ms 15044 KB Output is correct
37 Correct 284 ms 35808 KB Output is correct
38 Correct 171 ms 31740 KB Output is correct
39 Correct 66 ms 19208 KB Output is correct
40 Correct 72 ms 18704 KB Output is correct
41 Correct 81 ms 10932 KB Output is correct
42 Correct 76 ms 10704 KB Output is correct
43 Correct 85 ms 10380 KB Output is correct
44 Correct 62 ms 10504 KB Output is correct
45 Correct 58 ms 10700 KB Output is correct
46 Correct 43 ms 10276 KB Output is correct
47 Correct 73 ms 10644 KB Output is correct
48 Correct 51 ms 10264 KB Output is correct
49 Correct 80 ms 10492 KB Output is correct
50 Correct 44 ms 10160 KB Output is correct
51 Correct 58 ms 10700 KB Output is correct
52 Correct 43 ms 10276 KB Output is correct
53 Correct 37 ms 9968 KB Output is correct
54 Correct 40 ms 10488 KB Output is correct
55 Correct 35 ms 10328 KB Output is correct
56 Correct 19 ms 7632 KB Output is correct
57 Correct 44 ms 9520 KB Output is correct
58 Correct 45 ms 9508 KB Output is correct
59 Correct 1 ms 256 KB Output is correct
60 Correct 1 ms 260 KB Output is correct
61 Correct 738 ms 45772 KB Output is correct
62 Correct 476 ms 45328 KB Output is correct
63 Correct 423 ms 43800 KB Output is correct
64 Correct 361 ms 43944 KB Output is correct
65 Correct 459 ms 45716 KB Output is correct
66 Correct 357 ms 43652 KB Output is correct
67 Correct 319 ms 43192 KB Output is correct
68 Correct 355 ms 41468 KB Output is correct
69 Correct 359 ms 44688 KB Output is correct
70 Correct 278 ms 39792 KB Output is correct
71 Correct 265 ms 42836 KB Output is correct
72 Correct 292 ms 40572 KB Output is correct
73 Correct 301 ms 41776 KB Output is correct
74 Correct 308 ms 43972 KB Output is correct
75 Correct 281 ms 41460 KB Output is correct
76 Correct 26 ms 15048 KB Output is correct
77 Correct 315 ms 36192 KB Output is correct
78 Correct 362 ms 35968 KB Output is correct
79 Correct 0 ms 212 KB Output is correct
80 Correct 0 ms 212 KB Output is correct