Submission #924792

# Submission time Handle Problem Language Result Execution time Memory
924792 2024-02-09T16:51:14 Z HuyQuang_re_Zero Ancient Books (IOI17_books) C++14
50 / 100
403 ms 142088 KB
#include <bits/stdc++.h>
#define ll long long
#define db long double
#define N 1000005
#define II pair <ll,ll>
#define III pair <ll,II>
#define IV pair <vector <int>,vector <int> >
#define IDB pair <db,int>
#define TII pair <treap*,treap*>
#define fst first
#define snd second
#define BIT(x,i) ((x>>i)&1)
#define pi acos(-1)
#define to_radian(x) (x*pi/180.0)
#define to_degree(x) (x*180.0/pi)
#define Log(x) (31-__builtin_clz((int)x))
#define LogLL(x) (63-__builtin_clzll((ll)x)
using namespace std;
#include "books.h"
struct Minimum_Fenwick_Tree
{
    int bit[N],d[N];
    void Init()
    {
        memset(bit,63,sizeof(bit));
        memset(d,63,sizeof(d));
    }
    void update(int i,int k)
    {
        d[i]=min(d[i],k);
        while(i<N) bit[i]=min(bit[i],k),i+=(i & -i);
    }
    int get(int l,int r)
    {
        int res=1e9,i=r;
        while(i>=l)
            if(i-(i & -i)>=l) res=min(res,bit[i]),i-=(i & -i);
            else res=min(res,d[i]),i--;
        return res;
    }
} FT_mi;

struct Maximum_Fenwick_Tree
{
    int bit[N],d[N];
    void update(int i,int k)
    {
        d[i]=max(d[i],k);
        while(i<N) bit[i]=max(bit[i],k),i+=(i & -i);
    }
    int get(int l,int r)
    {
        int res=0,i=r;
        while(i>=l)
            if(i-(i & -i)>=l) res=max(res,bit[i]),i-=(i & -i);
            else res=max(res,d[i]),i--;
        return res;
    }
} FT_ma,FT_done;

int n,i,j,k,pos,p[N],n_cycle,n_node,n_point,in[N],par[N];
vector <int> a[N];
II seg_node[N],seg_cycle[N],point[2*N];
ll minimum_walk(vector <int> _p,int s)
{
    s++;
    for(int k:_p) p[++n]=k,p[n]++;

    //Build cycle
    for(i=1;i<=n;i++)
        if(in[i]==0)
        {
            n_cycle++;
            int l=1e9,r=0;
            for(j=i;in[j]==0;j=p[j])
            {
                l=min(l,j);
                r=max(r,j);
                in[j]=n_cycle;
            }
            seg_cycle[n_cycle]={ l,r };
        }
    FT_mi.Init();
    for(i=1;i<=n;i++)
    {
        FT_mi.update(i,seg_cycle[in[i]].fst);
        FT_ma.update(i,seg_cycle[in[i]].snd);
    }
    sort(seg_cycle+1,seg_cycle+n_cycle+1,[&](II a,II b){ return a.snd<b.snd; });


    //Build extend segment
    for(i=1;i<=n_cycle;i++)
    {
        int l=seg_cycle[i].fst,r=seg_cycle[i].snd;
        if(FT_done.get(1,l)>=r) continue;
        while(1)
        {
            int k;
            if((k=FT_mi.get(l,r))<l) l=k;
            else if((k=FT_ma.get(l,r))>r) r=k;
            else break;
        }
        FT_done.update(l,r);
        point[++n_point]={ l,0 };
        point[++n_point]={ r,1 };
    }
    sort(point+1,point+n_point+1);


    //Build Tree of extend segment
    stack <II> st;
    for(i=1;i<=n_point;i++)
        if(point[i].snd==0) st.push(point[i]);
        else
        {
            n_node++;
            while(st.top().snd==1)
            {
                int u=st.top().fst;
                a[n_node].push_back(u);
                par[u]=n_node;
                st.pop();
            }
            seg_node[n_node]={ st.top().fst,point[i].fst };
            reverse(a[n_node].begin(),a[n_node].end());
            st.pop();
            st.push({ n_node,1 });
        }


    //Init move
    int Begin=0,End=0;
    ll res=0;
    for(i=1;i<=n;i++)
    {
        res+=abs(i-p[i]);
        if(i!=p[i])
        {
            if(Begin==0) Begin=i;
            End=i;
        }
    }
    if(Begin==0) return 0;
    Begin=min(Begin,s);
    End=max(End,s);
    for(i=1;i<=n_node;i++)
    {
        if(seg_node[i].fst>=Begin && seg_node[i].snd<=End && par[i]==0)
        res+=2;
    }
    res-=2;
    int mn_len=1e9;
    for(i=1;i<=n_node;i++)
        if(seg_node[i].fst<=s && s<=seg_node[i].snd && mn_len>seg_node[i].snd-seg_node[i].fst)
        {
            mn_len=seg_node[i].snd-seg_node[i].fst;
            pos=i;
        }
    i=pos;
    while(par[i]>0)
    {
        j=par[i];
        ll cl=0,cr=0;
        for(pos=0;pos<a[j].size();pos++)
            if(a[j][pos]==i) break;
        for(k=pos;k>=0;k--)
        {
            cl++;
            if(k>0 && seg_node[a[j][k]].fst-seg_node[a[j][k-1]].snd>1) break;
        }
        for(k=pos;k<a[j].size();k++)
        {
            cr++;
            if(k<a[j].size()-1 && seg_node[a[j][k+1]].fst-seg_node[a[j][k]].snd>1) break;
        }
        res+=2*min(cl,cr);
        i=j;
    }
    return res;
}
/*
int main()
{
    freopen("books.inp","r",stdin);
    freopen("books.out","w",stdout);
    vector <int> p;
    int n,s,k;
    cin>>n>>s;
    for(int i=1;i<=n;i++) cin>>k,p.push_back(k);
    cout<<minimum_walk(p,s);
}
*/

Compilation message

books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:165:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  165 |         for(pos=0;pos<a[j].size();pos++)
      |                   ~~~^~~~~~~~~~~~
books.cpp:172:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  172 |         for(k=pos;k<a[j].size();k++)
      |                   ~^~~~~~~~~~~~
books.cpp:175:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  175 |             if(k<a[j].size()-1 && seg_node[a[j][k+1]].fst-seg_node[a[j][k]].snd>1) break;
      |                ~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 11 ms 39516 KB Output is correct
2 Correct 7 ms 39644 KB Output is correct
3 Correct 8 ms 39636 KB Output is correct
4 Correct 7 ms 39516 KB Output is correct
5 Correct 8 ms 39516 KB Output is correct
6 Correct 7 ms 39516 KB Output is correct
7 Correct 7 ms 39516 KB Output is correct
8 Correct 8 ms 39516 KB Output is correct
9 Correct 7 ms 39512 KB Output is correct
10 Correct 7 ms 39512 KB Output is correct
11 Correct 7 ms 39516 KB Output is correct
12 Correct 7 ms 39516 KB Output is correct
13 Correct 7 ms 39516 KB Output is correct
14 Correct 7 ms 39516 KB Output is correct
15 Correct 8 ms 39632 KB Output is correct
16 Correct 7 ms 39516 KB Output is correct
17 Correct 8 ms 39648 KB Output is correct
18 Correct 7 ms 39512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 39516 KB Output is correct
2 Correct 7 ms 39644 KB Output is correct
3 Correct 8 ms 39636 KB Output is correct
4 Correct 7 ms 39516 KB Output is correct
5 Correct 8 ms 39516 KB Output is correct
6 Correct 7 ms 39516 KB Output is correct
7 Correct 7 ms 39516 KB Output is correct
8 Correct 8 ms 39516 KB Output is correct
9 Correct 7 ms 39512 KB Output is correct
10 Correct 7 ms 39512 KB Output is correct
11 Correct 7 ms 39516 KB Output is correct
12 Correct 7 ms 39516 KB Output is correct
13 Correct 7 ms 39516 KB Output is correct
14 Correct 7 ms 39516 KB Output is correct
15 Correct 8 ms 39632 KB Output is correct
16 Correct 7 ms 39516 KB Output is correct
17 Correct 8 ms 39648 KB Output is correct
18 Correct 7 ms 39512 KB Output is correct
19 Correct 8 ms 39512 KB Output is correct
20 Correct 7 ms 39516 KB Output is correct
21 Correct 7 ms 39692 KB Output is correct
22 Correct 8 ms 39516 KB Output is correct
23 Correct 7 ms 39516 KB Output is correct
24 Correct 8 ms 39516 KB Output is correct
25 Correct 7 ms 39680 KB Output is correct
26 Correct 7 ms 39516 KB Output is correct
27 Correct 8 ms 39624 KB Output is correct
28 Correct 8 ms 39516 KB Output is correct
29 Correct 7 ms 39516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 39516 KB Output is correct
2 Correct 7 ms 39644 KB Output is correct
3 Correct 8 ms 39636 KB Output is correct
4 Correct 7 ms 39516 KB Output is correct
5 Correct 8 ms 39516 KB Output is correct
6 Correct 7 ms 39516 KB Output is correct
7 Correct 7 ms 39516 KB Output is correct
8 Correct 8 ms 39516 KB Output is correct
9 Correct 7 ms 39512 KB Output is correct
10 Correct 7 ms 39512 KB Output is correct
11 Correct 7 ms 39516 KB Output is correct
12 Correct 7 ms 39516 KB Output is correct
13 Correct 7 ms 39516 KB Output is correct
14 Correct 7 ms 39516 KB Output is correct
15 Correct 8 ms 39632 KB Output is correct
16 Correct 7 ms 39516 KB Output is correct
17 Correct 8 ms 39648 KB Output is correct
18 Correct 7 ms 39512 KB Output is correct
19 Correct 8 ms 39512 KB Output is correct
20 Correct 7 ms 39516 KB Output is correct
21 Correct 7 ms 39692 KB Output is correct
22 Correct 8 ms 39516 KB Output is correct
23 Correct 7 ms 39516 KB Output is correct
24 Correct 8 ms 39516 KB Output is correct
25 Correct 7 ms 39680 KB Output is correct
26 Correct 7 ms 39516 KB Output is correct
27 Correct 8 ms 39624 KB Output is correct
28 Correct 8 ms 39516 KB Output is correct
29 Correct 7 ms 39516 KB Output is correct
30 Correct 131 ms 60716 KB Output is correct
31 Correct 170 ms 60500 KB Output is correct
32 Correct 399 ms 142088 KB Output is correct
33 Correct 346 ms 127552 KB Output is correct
34 Correct 323 ms 127420 KB Output is correct
35 Correct 301 ms 112316 KB Output is correct
36 Correct 205 ms 88144 KB Output is correct
37 Correct 134 ms 70736 KB Output is correct
38 Correct 129 ms 66864 KB Output is correct
39 Correct 122 ms 66388 KB Output is correct
40 Correct 122 ms 61992 KB Output is correct
41 Correct 130 ms 60708 KB Output is correct
42 Correct 126 ms 61012 KB Output is correct
43 Correct 403 ms 121940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 39516 KB Output is correct
2 Correct 9 ms 39696 KB Output is correct
3 Incorrect 8 ms 39516 KB 3rd lines differ - on the 1st token, expected: '5920', found: '5614'
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 11 ms 39516 KB Output is correct
2 Correct 7 ms 39644 KB Output is correct
3 Correct 8 ms 39636 KB Output is correct
4 Correct 7 ms 39516 KB Output is correct
5 Correct 8 ms 39516 KB Output is correct
6 Correct 7 ms 39516 KB Output is correct
7 Correct 7 ms 39516 KB Output is correct
8 Correct 8 ms 39516 KB Output is correct
9 Correct 7 ms 39512 KB Output is correct
10 Correct 7 ms 39512 KB Output is correct
11 Correct 7 ms 39516 KB Output is correct
12 Correct 7 ms 39516 KB Output is correct
13 Correct 7 ms 39516 KB Output is correct
14 Correct 7 ms 39516 KB Output is correct
15 Correct 8 ms 39632 KB Output is correct
16 Correct 7 ms 39516 KB Output is correct
17 Correct 8 ms 39648 KB Output is correct
18 Correct 7 ms 39512 KB Output is correct
19 Correct 8 ms 39512 KB Output is correct
20 Correct 7 ms 39516 KB Output is correct
21 Correct 7 ms 39692 KB Output is correct
22 Correct 8 ms 39516 KB Output is correct
23 Correct 7 ms 39516 KB Output is correct
24 Correct 8 ms 39516 KB Output is correct
25 Correct 7 ms 39680 KB Output is correct
26 Correct 7 ms 39516 KB Output is correct
27 Correct 8 ms 39624 KB Output is correct
28 Correct 8 ms 39516 KB Output is correct
29 Correct 7 ms 39516 KB Output is correct
30 Correct 131 ms 60716 KB Output is correct
31 Correct 170 ms 60500 KB Output is correct
32 Correct 399 ms 142088 KB Output is correct
33 Correct 346 ms 127552 KB Output is correct
34 Correct 323 ms 127420 KB Output is correct
35 Correct 301 ms 112316 KB Output is correct
36 Correct 205 ms 88144 KB Output is correct
37 Correct 134 ms 70736 KB Output is correct
38 Correct 129 ms 66864 KB Output is correct
39 Correct 122 ms 66388 KB Output is correct
40 Correct 122 ms 61992 KB Output is correct
41 Correct 130 ms 60708 KB Output is correct
42 Correct 126 ms 61012 KB Output is correct
43 Correct 403 ms 121940 KB Output is correct
44 Correct 8 ms 39516 KB Output is correct
45 Correct 9 ms 39696 KB Output is correct
46 Incorrect 8 ms 39516 KB 3rd lines differ - on the 1st token, expected: '5920', found: '5614'
47 Halted 0 ms 0 KB -