답안 #1037086

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1037086 2024-07-28T03:35:06 Z huutuan 고대 책들 (IOI17_books) C++14
50 / 100
143 ms 189692 KB
#include "books.h"

#include <bits/stdc++.h>

using namespace std;

const int N=1e6, LG=20;
int tl, tr;
int vis[N], n, stmin[LG][N], stmax[LG][N];
pair<int, int> ll[N], rr[N];
int check[N];
long long ans=0;

int getmin(int l, int r){
   int lg=__lg(r-l+1);
   return min(stmin[lg][l], stmin[lg][r-(1<<lg)+1]);
}

int getmax(int l, int r){
   int lg=__lg(r-l+1);
   return max(stmax[lg][l], stmax[lg][r-(1<<lg)+1]);
}

void extend(int &l, int &r){
   while (1){
      int nl=getmin(l, r), nr=getmax(l, r);
      if (nl==l && nr==r) break;
      l=nl; r=nr;
   }
}

void dp(int l, int r){
   extend(l, r);
   if (l==tl && r==tr) return;
   int l1=tl, r1=r, l2=l, r2=tr;
   if (ll[l].first || ll[l].second){
      l1=ll[l].second;
   }
   if (rr[r].first || rr[r].second){
      r2=rr[r].first;
   }
   int c1=check[l]-check[l1];
   int c2=check[r2]-check[r];
   if (l1==l) c1=1e9;
   if (r2==r) c2=1e9;
   ans+=min(c1, c2)*2;
   if (c1<=c2) return dp(l1, r1);
   return dp(l2, r2);
}

long long minimum_walk(vector<int> p, int s) {
   n=p.size();
   for (int i=0; i<n; ++i) if (!vis[i]){
      vector<int> cc;
      int u=i;
      stmin[0][i]=i; stmax[0][i]=i;
      while (!vis[u]){
         vis[u]=1;
         cc.push_back(u);
         stmin[0][i]=min(stmin[0][i], u);
         stmax[0][i]=max(stmax[0][i], u);
         ans+=abs(u-p[u]);
         u=p[u];
      }
      if (stmin[0][i]<s && s<stmax[0][i]) ll[stmin[0][i]]=rr[stmax[0][i]]={stmin[0][i], stmax[0][i]};
      else{
         int l=stmin[0][i], r=stmax[0][i];
         ++check[l]; --check[r];
      }
      for (int j:cc) stmin[0][j]=stmin[0][i], stmax[0][j]=stmax[0][i];
   }
   partial_sum(check, check+n, check);
   for (int i=n-1; i>0; --i) check[i]=!check[i-1];
   check[0]=0;
   partial_sum(check, check+n, check);
   for (int i=1; i<=s; ++i){
      if (ll[i].first==0 && ll[i].second==0) ll[i]=ll[i-1];
   }
   for (int i=n-2; i>=s; --i){
      if (rr[i].first==0 && rr[i].second==0) rr[i]=rr[i+1];
   }
   for (int i=1; i<LG; ++i) for (int j=0; j+(1<<i)-1<n; ++j){
      stmin[i][j]=min(stmin[i-1][j], stmin[i-1][j+(1<<(i-1))]);
      stmax[i][j]=max(stmax[i-1][j], stmax[i-1][j+(1<<(i-1))]);
   }
   tl=0, tr=n-1;
   while (tl<s && stmin[0][tl]==tl && stmax[0][tl]==tl) ++tl;
   while (tr>s && stmin[0][tr]==tr && stmax[0][tr]==tr) --tr;
   dp(s, s);
   return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14684 KB Output is correct
2 Correct 1 ms 14684 KB Output is correct
3 Correct 2 ms 14684 KB Output is correct
4 Correct 1 ms 10704 KB Output is correct
5 Correct 1 ms 14680 KB Output is correct
6 Correct 1 ms 14684 KB Output is correct
7 Correct 1 ms 14936 KB Output is correct
8 Correct 1 ms 14684 KB Output is correct
9 Correct 1 ms 14684 KB Output is correct
10 Correct 1 ms 14776 KB Output is correct
11 Correct 1 ms 10588 KB Output is correct
12 Correct 1 ms 10588 KB Output is correct
13 Correct 1 ms 14684 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 14684 KB Output is correct
16 Correct 1 ms 14684 KB Output is correct
17 Correct 1 ms 14684 KB Output is correct
18 Correct 2 ms 14680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14684 KB Output is correct
2 Correct 1 ms 14684 KB Output is correct
3 Correct 2 ms 14684 KB Output is correct
4 Correct 1 ms 10704 KB Output is correct
5 Correct 1 ms 14680 KB Output is correct
6 Correct 1 ms 14684 KB Output is correct
7 Correct 1 ms 14936 KB Output is correct
8 Correct 1 ms 14684 KB Output is correct
9 Correct 1 ms 14684 KB Output is correct
10 Correct 1 ms 14776 KB Output is correct
11 Correct 1 ms 10588 KB Output is correct
12 Correct 1 ms 10588 KB Output is correct
13 Correct 1 ms 14684 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 14684 KB Output is correct
16 Correct 1 ms 14684 KB Output is correct
17 Correct 1 ms 14684 KB Output is correct
18 Correct 2 ms 14680 KB Output is correct
19 Correct 4 ms 43356 KB Output is correct
20 Correct 4 ms 43356 KB Output is correct
21 Correct 4 ms 43352 KB Output is correct
22 Correct 4 ms 43356 KB Output is correct
23 Correct 4 ms 43356 KB Output is correct
24 Correct 4 ms 43356 KB Output is correct
25 Correct 4 ms 43356 KB Output is correct
26 Correct 4 ms 43356 KB Output is correct
27 Correct 4 ms 43356 KB Output is correct
28 Correct 4 ms 43356 KB Output is correct
29 Correct 4 ms 43356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14684 KB Output is correct
2 Correct 1 ms 14684 KB Output is correct
3 Correct 2 ms 14684 KB Output is correct
4 Correct 1 ms 10704 KB Output is correct
5 Correct 1 ms 14680 KB Output is correct
6 Correct 1 ms 14684 KB Output is correct
7 Correct 1 ms 14936 KB Output is correct
8 Correct 1 ms 14684 KB Output is correct
9 Correct 1 ms 14684 KB Output is correct
10 Correct 1 ms 14776 KB Output is correct
11 Correct 1 ms 10588 KB Output is correct
12 Correct 1 ms 10588 KB Output is correct
13 Correct 1 ms 14684 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 14684 KB Output is correct
16 Correct 1 ms 14684 KB Output is correct
17 Correct 1 ms 14684 KB Output is correct
18 Correct 2 ms 14680 KB Output is correct
19 Correct 4 ms 43356 KB Output is correct
20 Correct 4 ms 43356 KB Output is correct
21 Correct 4 ms 43352 KB Output is correct
22 Correct 4 ms 43356 KB Output is correct
23 Correct 4 ms 43356 KB Output is correct
24 Correct 4 ms 43356 KB Output is correct
25 Correct 4 ms 43356 KB Output is correct
26 Correct 4 ms 43356 KB Output is correct
27 Correct 4 ms 43356 KB Output is correct
28 Correct 4 ms 43356 KB Output is correct
29 Correct 4 ms 43356 KB Output is correct
30 Correct 137 ms 189572 KB Output is correct
31 Correct 143 ms 189692 KB Output is correct
32 Correct 125 ms 187736 KB Output is correct
33 Correct 122 ms 187732 KB Output is correct
34 Correct 132 ms 187764 KB Output is correct
35 Correct 136 ms 187752 KB Output is correct
36 Correct 127 ms 187728 KB Output is correct
37 Correct 116 ms 187772 KB Output is correct
38 Correct 113 ms 187672 KB Output is correct
39 Correct 121 ms 187728 KB Output is correct
40 Correct 119 ms 187856 KB Output is correct
41 Correct 136 ms 188676 KB Output is correct
42 Correct 131 ms 188724 KB Output is correct
43 Correct 131 ms 187668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 39 ms 96092 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14684 KB Output is correct
2 Correct 1 ms 14684 KB Output is correct
3 Correct 2 ms 14684 KB Output is correct
4 Correct 1 ms 10704 KB Output is correct
5 Correct 1 ms 14680 KB Output is correct
6 Correct 1 ms 14684 KB Output is correct
7 Correct 1 ms 14936 KB Output is correct
8 Correct 1 ms 14684 KB Output is correct
9 Correct 1 ms 14684 KB Output is correct
10 Correct 1 ms 14776 KB Output is correct
11 Correct 1 ms 10588 KB Output is correct
12 Correct 1 ms 10588 KB Output is correct
13 Correct 1 ms 14684 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 14684 KB Output is correct
16 Correct 1 ms 14684 KB Output is correct
17 Correct 1 ms 14684 KB Output is correct
18 Correct 2 ms 14680 KB Output is correct
19 Correct 4 ms 43356 KB Output is correct
20 Correct 4 ms 43356 KB Output is correct
21 Correct 4 ms 43352 KB Output is correct
22 Correct 4 ms 43356 KB Output is correct
23 Correct 4 ms 43356 KB Output is correct
24 Correct 4 ms 43356 KB Output is correct
25 Correct 4 ms 43356 KB Output is correct
26 Correct 4 ms 43356 KB Output is correct
27 Correct 4 ms 43356 KB Output is correct
28 Correct 4 ms 43356 KB Output is correct
29 Correct 4 ms 43356 KB Output is correct
30 Correct 137 ms 189572 KB Output is correct
31 Correct 143 ms 189692 KB Output is correct
32 Correct 125 ms 187736 KB Output is correct
33 Correct 122 ms 187732 KB Output is correct
34 Correct 132 ms 187764 KB Output is correct
35 Correct 136 ms 187752 KB Output is correct
36 Correct 127 ms 187728 KB Output is correct
37 Correct 116 ms 187772 KB Output is correct
38 Correct 113 ms 187672 KB Output is correct
39 Correct 121 ms 187728 KB Output is correct
40 Correct 119 ms 187856 KB Output is correct
41 Correct 136 ms 188676 KB Output is correct
42 Correct 131 ms 188724 KB Output is correct
43 Correct 131 ms 187668 KB Output is correct
44 Runtime error 39 ms 96092 KB Execution killed with signal 11
45 Halted 0 ms 0 KB -