Submission #924781

# Submission time Handle Problem Language Result Execution time Memory
924781 2024-02-09T16:29:23 Z HuyQuang_re_Zero Ancient Books (IOI17_books) C++14
50 / 100
405 ms 145820 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;
    for(i=1;i<=n_node;i++)
        if(seg_node[i].fst<=s && s<=seg_node[i].snd && a[i].size()==0)
            break;


    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+1;k<a[j].size();j++)
        {
            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:162:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  162 |         for(pos=0;pos<a[j].size();pos++)
      |                   ~~~^~~~~~~~~~~~
books.cpp:169:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  169 |         for(k=pos+1;k<a[j].size();j++)
      |                     ~^~~~~~~~~~~~
books.cpp:172:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  172 |             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 8 ms 39516 KB Output is correct
3 Correct 7 ms 39516 KB Output is correct
4 Correct 7 ms 39412 KB Output is correct
5 Correct 7 ms 39516 KB Output is correct
6 Correct 8 ms 39664 KB Output is correct
7 Correct 8 ms 39436 KB Output is correct
8 Correct 7 ms 39516 KB Output is correct
9 Correct 8 ms 39516 KB Output is correct
10 Correct 8 ms 39516 KB Output is correct
11 Correct 8 ms 39516 KB Output is correct
12 Correct 8 ms 39516 KB Output is correct
13 Correct 9 ms 39512 KB Output is correct
14 Correct 9 ms 39652 KB Output is correct
15 Correct 9 ms 39516 KB Output is correct
16 Correct 8 ms 39516 KB Output is correct
17 Correct 8 ms 39516 KB Output is correct
18 Correct 8 ms 39512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 39516 KB Output is correct
2 Correct 8 ms 39516 KB Output is correct
3 Correct 7 ms 39516 KB Output is correct
4 Correct 7 ms 39412 KB Output is correct
5 Correct 7 ms 39516 KB Output is correct
6 Correct 8 ms 39664 KB Output is correct
7 Correct 8 ms 39436 KB Output is correct
8 Correct 7 ms 39516 KB Output is correct
9 Correct 8 ms 39516 KB Output is correct
10 Correct 8 ms 39516 KB Output is correct
11 Correct 8 ms 39516 KB Output is correct
12 Correct 8 ms 39516 KB Output is correct
13 Correct 9 ms 39512 KB Output is correct
14 Correct 9 ms 39652 KB Output is correct
15 Correct 9 ms 39516 KB Output is correct
16 Correct 8 ms 39516 KB Output is correct
17 Correct 8 ms 39516 KB Output is correct
18 Correct 8 ms 39512 KB Output is correct
19 Correct 7 ms 39516 KB Output is correct
20 Correct 8 ms 39660 KB Output is correct
21 Correct 8 ms 39700 KB Output is correct
22 Correct 9 ms 39708 KB Output is correct
23 Correct 8 ms 39516 KB Output is correct
24 Correct 9 ms 39516 KB Output is correct
25 Correct 8 ms 39640 KB Output is correct
26 Correct 8 ms 39660 KB Output is correct
27 Correct 8 ms 39516 KB Output is correct
28 Correct 8 ms 39516 KB Output is correct
29 Correct 8 ms 39516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 39516 KB Output is correct
2 Correct 8 ms 39516 KB Output is correct
3 Correct 7 ms 39516 KB Output is correct
4 Correct 7 ms 39412 KB Output is correct
5 Correct 7 ms 39516 KB Output is correct
6 Correct 8 ms 39664 KB Output is correct
7 Correct 8 ms 39436 KB Output is correct
8 Correct 7 ms 39516 KB Output is correct
9 Correct 8 ms 39516 KB Output is correct
10 Correct 8 ms 39516 KB Output is correct
11 Correct 8 ms 39516 KB Output is correct
12 Correct 8 ms 39516 KB Output is correct
13 Correct 9 ms 39512 KB Output is correct
14 Correct 9 ms 39652 KB Output is correct
15 Correct 9 ms 39516 KB Output is correct
16 Correct 8 ms 39516 KB Output is correct
17 Correct 8 ms 39516 KB Output is correct
18 Correct 8 ms 39512 KB Output is correct
19 Correct 7 ms 39516 KB Output is correct
20 Correct 8 ms 39660 KB Output is correct
21 Correct 8 ms 39700 KB Output is correct
22 Correct 9 ms 39708 KB Output is correct
23 Correct 8 ms 39516 KB Output is correct
24 Correct 9 ms 39516 KB Output is correct
25 Correct 8 ms 39640 KB Output is correct
26 Correct 8 ms 39660 KB Output is correct
27 Correct 8 ms 39516 KB Output is correct
28 Correct 8 ms 39516 KB Output is correct
29 Correct 8 ms 39516 KB Output is correct
30 Correct 133 ms 64016 KB Output is correct
31 Correct 132 ms 64084 KB Output is correct
32 Correct 405 ms 145820 KB Output is correct
33 Correct 327 ms 131004 KB Output is correct
34 Correct 332 ms 131184 KB Output is correct
35 Correct 313 ms 115824 KB Output is correct
36 Correct 200 ms 91732 KB Output is correct
37 Correct 129 ms 74280 KB Output is correct
38 Correct 121 ms 70480 KB Output is correct
39 Correct 120 ms 69968 KB Output is correct
40 Correct 124 ms 65616 KB Output is correct
41 Correct 129 ms 64084 KB Output is correct
42 Correct 127 ms 64476 KB Output is correct
43 Correct 394 ms 121932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 8 ms 39516 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 11 ms 39516 KB Output is correct
2 Correct 8 ms 39516 KB Output is correct
3 Correct 7 ms 39516 KB Output is correct
4 Correct 7 ms 39412 KB Output is correct
5 Correct 7 ms 39516 KB Output is correct
6 Correct 8 ms 39664 KB Output is correct
7 Correct 8 ms 39436 KB Output is correct
8 Correct 7 ms 39516 KB Output is correct
9 Correct 8 ms 39516 KB Output is correct
10 Correct 8 ms 39516 KB Output is correct
11 Correct 8 ms 39516 KB Output is correct
12 Correct 8 ms 39516 KB Output is correct
13 Correct 9 ms 39512 KB Output is correct
14 Correct 9 ms 39652 KB Output is correct
15 Correct 9 ms 39516 KB Output is correct
16 Correct 8 ms 39516 KB Output is correct
17 Correct 8 ms 39516 KB Output is correct
18 Correct 8 ms 39512 KB Output is correct
19 Correct 7 ms 39516 KB Output is correct
20 Correct 8 ms 39660 KB Output is correct
21 Correct 8 ms 39700 KB Output is correct
22 Correct 9 ms 39708 KB Output is correct
23 Correct 8 ms 39516 KB Output is correct
24 Correct 9 ms 39516 KB Output is correct
25 Correct 8 ms 39640 KB Output is correct
26 Correct 8 ms 39660 KB Output is correct
27 Correct 8 ms 39516 KB Output is correct
28 Correct 8 ms 39516 KB Output is correct
29 Correct 8 ms 39516 KB Output is correct
30 Correct 133 ms 64016 KB Output is correct
31 Correct 132 ms 64084 KB Output is correct
32 Correct 405 ms 145820 KB Output is correct
33 Correct 327 ms 131004 KB Output is correct
34 Correct 332 ms 131184 KB Output is correct
35 Correct 313 ms 115824 KB Output is correct
36 Correct 200 ms 91732 KB Output is correct
37 Correct 129 ms 74280 KB Output is correct
38 Correct 121 ms 70480 KB Output is correct
39 Correct 120 ms 69968 KB Output is correct
40 Correct 124 ms 65616 KB Output is correct
41 Correct 129 ms 64084 KB Output is correct
42 Correct 127 ms 64476 KB Output is correct
43 Correct 394 ms 121932 KB Output is correct
44 Incorrect 8 ms 39516 KB 3rd lines differ - on the 1st token, expected: '3304', found: '2744'
45 Halted 0 ms 0 KB -