Submission #1037090

# Submission time Handle Problem Language Result Execution time Memory
1037090 2024-07-28T03:52:37 Z huutuan Ancient Books (IOI17_books) C++14
50 / 100
137 ms 183308 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].first;
   }
   if (rr[r].first || rr[r].second){
      r2=rr[r].second;
   }
   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;
      int maxl=-1, minr=-1;
      while (!vis[u]){
         if (u<s){
            if (maxl==-1 || maxl<u) maxl=u;
         }
         if (u>s){
            if (minr==-1 || minr>u) minr=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]+1]=rr[stmax[0][i]-1]={maxl, minr};
      }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;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 14684 KB Output is correct
2 Correct 1 ms 14684 KB Output is correct
3 Correct 1 ms 14684 KB Output is correct
4 Correct 1 ms 10588 KB Output is correct
5 Correct 1 ms 14684 KB Output is correct
6 Correct 1 ms 14684 KB Output is correct
7 Correct 1 ms 14680 KB Output is correct
8 Correct 1 ms 14684 KB Output is correct
9 Correct 1 ms 14684 KB Output is correct
10 Correct 2 ms 14684 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 6488 KB Output is correct
15 Correct 2 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 1 ms 14684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 14684 KB Output is correct
2 Correct 1 ms 14684 KB Output is correct
3 Correct 1 ms 14684 KB Output is correct
4 Correct 1 ms 10588 KB Output is correct
5 Correct 1 ms 14684 KB Output is correct
6 Correct 1 ms 14684 KB Output is correct
7 Correct 1 ms 14680 KB Output is correct
8 Correct 1 ms 14684 KB Output is correct
9 Correct 1 ms 14684 KB Output is correct
10 Correct 2 ms 14684 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 6488 KB Output is correct
15 Correct 2 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 1 ms 14684 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 43460 KB Output is correct
22 Correct 4 ms 43356 KB Output is correct
23 Correct 4 ms 43464 KB Output is correct
24 Correct 4 ms 43352 KB Output is correct
25 Correct 4 ms 43520 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
# Verdict Execution time Memory Grader output
1 Correct 1 ms 14684 KB Output is correct
2 Correct 1 ms 14684 KB Output is correct
3 Correct 1 ms 14684 KB Output is correct
4 Correct 1 ms 10588 KB Output is correct
5 Correct 1 ms 14684 KB Output is correct
6 Correct 1 ms 14684 KB Output is correct
7 Correct 1 ms 14680 KB Output is correct
8 Correct 1 ms 14684 KB Output is correct
9 Correct 1 ms 14684 KB Output is correct
10 Correct 2 ms 14684 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 6488 KB Output is correct
15 Correct 2 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 1 ms 14684 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 43460 KB Output is correct
22 Correct 4 ms 43356 KB Output is correct
23 Correct 4 ms 43464 KB Output is correct
24 Correct 4 ms 43352 KB Output is correct
25 Correct 4 ms 43520 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 134 ms 183088 KB Output is correct
31 Correct 133 ms 183308 KB Output is correct
32 Correct 124 ms 181240 KB Output is correct
33 Correct 131 ms 181328 KB Output is correct
34 Correct 127 ms 181328 KB Output is correct
35 Correct 127 ms 181240 KB Output is correct
36 Correct 127 ms 181588 KB Output is correct
37 Correct 116 ms 181336 KB Output is correct
38 Correct 116 ms 181328 KB Output is correct
39 Correct 116 ms 181332 KB Output is correct
40 Correct 117 ms 181628 KB Output is correct
41 Correct 137 ms 182116 KB Output is correct
42 Correct 126 ms 182156 KB Output is correct
43 Correct 131 ms 181260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 47448 KB Output is correct
2 Correct 4 ms 47452 KB Output is correct
3 Correct 4 ms 47564 KB Output is correct
4 Correct 4 ms 43356 KB Output is correct
5 Correct 4 ms 43356 KB Output is correct
6 Incorrect 4 ms 47452 KB 3rd lines differ - on the 1st token, expected: '22140', found: '22206'
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 14684 KB Output is correct
2 Correct 1 ms 14684 KB Output is correct
3 Correct 1 ms 14684 KB Output is correct
4 Correct 1 ms 10588 KB Output is correct
5 Correct 1 ms 14684 KB Output is correct
6 Correct 1 ms 14684 KB Output is correct
7 Correct 1 ms 14680 KB Output is correct
8 Correct 1 ms 14684 KB Output is correct
9 Correct 1 ms 14684 KB Output is correct
10 Correct 2 ms 14684 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 6488 KB Output is correct
15 Correct 2 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 1 ms 14684 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 43460 KB Output is correct
22 Correct 4 ms 43356 KB Output is correct
23 Correct 4 ms 43464 KB Output is correct
24 Correct 4 ms 43352 KB Output is correct
25 Correct 4 ms 43520 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 134 ms 183088 KB Output is correct
31 Correct 133 ms 183308 KB Output is correct
32 Correct 124 ms 181240 KB Output is correct
33 Correct 131 ms 181328 KB Output is correct
34 Correct 127 ms 181328 KB Output is correct
35 Correct 127 ms 181240 KB Output is correct
36 Correct 127 ms 181588 KB Output is correct
37 Correct 116 ms 181336 KB Output is correct
38 Correct 116 ms 181328 KB Output is correct
39 Correct 116 ms 181332 KB Output is correct
40 Correct 117 ms 181628 KB Output is correct
41 Correct 137 ms 182116 KB Output is correct
42 Correct 126 ms 182156 KB Output is correct
43 Correct 131 ms 181260 KB Output is correct
44 Correct 5 ms 47448 KB Output is correct
45 Correct 4 ms 47452 KB Output is correct
46 Correct 4 ms 47564 KB Output is correct
47 Correct 4 ms 43356 KB Output is correct
48 Correct 4 ms 43356 KB Output is correct
49 Incorrect 4 ms 47452 KB 3rd lines differ - on the 1st token, expected: '22140', found: '22206'
50 Halted 0 ms 0 KB -