답안 #924805

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
924805 2024-02-09T17:40:28 Z HuyQuang_re_Zero 고대 책들 (IOI17_books) C++14
50 / 100
417 ms 150156 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 };
        }

    sort(seg_cycle+1,seg_cycle+n_cycle+1,[&](II a,II b){ return a.snd<b.snd; });


    //Build extend segment
    FT_mi.Init();
    vector <II> vec;
    for(i=1;i<=n_cycle;i++)
    {
        int l=seg_cycle[i].fst,r=seg_cycle[i].snd;
    //    cerr<<l<<" "<<r<<'\n';
        if(FT_ma.get(1,l)>=r) continue;
   //     if(l<=s && s<=r) cerr<<l<<" "<<r<<'\n';
        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);
        FT_mi.update(r,l);
        FT_ma.update(l,r);
        vec.push_back({ l,r });
    }
    for(II k:vec)
    {
        int l=k.fst,r=k.snd;
        if(FT_mi.get(l,r)>=l && FT_ma.get(l,r)<=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:174:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  174 |         for(pos=0;pos<a[j].size();pos++)
      |                   ~~~^~~~~~~~~~~~
books.cpp:181:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  181 |         for(k=pos;k<a[j].size();k++)
      |                   ~^~~~~~~~~~~~
books.cpp:184:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  184 |             if(k<a[j].size()-1 && seg_node[a[j][k+1]].fst-seg_node[a[j][k]].snd>1) break;
      |                ~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 39516 KB Output is correct
2 Correct 7 ms 39516 KB Output is correct
3 Correct 8 ms 39516 KB Output is correct
4 Correct 8 ms 39516 KB Output is correct
5 Correct 8 ms 39516 KB Output is correct
6 Correct 8 ms 39516 KB Output is correct
7 Correct 7 ms 39392 KB Output is correct
8 Correct 7 ms 39516 KB Output is correct
9 Correct 7 ms 39440 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 7 ms 39512 KB Output is correct
14 Correct 8 ms 39516 KB Output is correct
15 Correct 8 ms 39512 KB Output is correct
16 Correct 9 ms 39516 KB Output is correct
17 Correct 7 ms 39516 KB Output is correct
18 Correct 8 ms 39516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 39516 KB Output is correct
2 Correct 7 ms 39516 KB Output is correct
3 Correct 8 ms 39516 KB Output is correct
4 Correct 8 ms 39516 KB Output is correct
5 Correct 8 ms 39516 KB Output is correct
6 Correct 8 ms 39516 KB Output is correct
7 Correct 7 ms 39392 KB Output is correct
8 Correct 7 ms 39516 KB Output is correct
9 Correct 7 ms 39440 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 7 ms 39512 KB Output is correct
14 Correct 8 ms 39516 KB Output is correct
15 Correct 8 ms 39512 KB Output is correct
16 Correct 9 ms 39516 KB Output is correct
17 Correct 7 ms 39516 KB Output is correct
18 Correct 8 ms 39516 KB Output is correct
19 Correct 8 ms 39512 KB Output is correct
20 Correct 8 ms 39512 KB Output is correct
21 Correct 7 ms 39516 KB Output is correct
22 Correct 8 ms 39516 KB Output is correct
23 Correct 8 ms 39516 KB Output is correct
24 Correct 8 ms 39516 KB Output is correct
25 Correct 8 ms 39424 KB Output is correct
26 Correct 8 ms 39516 KB Output is correct
27 Correct 8 ms 39512 KB Output is correct
28 Correct 7 ms 39516 KB Output is correct
29 Correct 8 ms 39668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 39516 KB Output is correct
2 Correct 7 ms 39516 KB Output is correct
3 Correct 8 ms 39516 KB Output is correct
4 Correct 8 ms 39516 KB Output is correct
5 Correct 8 ms 39516 KB Output is correct
6 Correct 8 ms 39516 KB Output is correct
7 Correct 7 ms 39392 KB Output is correct
8 Correct 7 ms 39516 KB Output is correct
9 Correct 7 ms 39440 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 7 ms 39512 KB Output is correct
14 Correct 8 ms 39516 KB Output is correct
15 Correct 8 ms 39512 KB Output is correct
16 Correct 9 ms 39516 KB Output is correct
17 Correct 7 ms 39516 KB Output is correct
18 Correct 8 ms 39516 KB Output is correct
19 Correct 8 ms 39512 KB Output is correct
20 Correct 8 ms 39512 KB Output is correct
21 Correct 7 ms 39516 KB Output is correct
22 Correct 8 ms 39516 KB Output is correct
23 Correct 8 ms 39516 KB Output is correct
24 Correct 8 ms 39516 KB Output is correct
25 Correct 8 ms 39424 KB Output is correct
26 Correct 8 ms 39516 KB Output is correct
27 Correct 8 ms 39512 KB Output is correct
28 Correct 7 ms 39516 KB Output is correct
29 Correct 8 ms 39668 KB Output is correct
30 Correct 102 ms 53052 KB Output is correct
31 Correct 108 ms 53072 KB Output is correct
32 Correct 405 ms 150156 KB Output is correct
33 Correct 322 ms 131332 KB Output is correct
34 Correct 320 ms 131020 KB Output is correct
35 Correct 284 ms 115612 KB Output is correct
36 Correct 191 ms 87204 KB Output is correct
37 Correct 107 ms 63644 KB Output is correct
38 Correct 96 ms 59952 KB Output is correct
39 Correct 98 ms 59704 KB Output is correct
40 Correct 91 ms 55380 KB Output is correct
41 Correct 98 ms 53304 KB Output is correct
42 Correct 96 ms 53812 KB Output is correct
43 Correct 417 ms 132032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 39512 KB Output is correct
2 Correct 8 ms 39516 KB Output is correct
3 Correct 8 ms 39516 KB Output is correct
4 Correct 8 ms 39608 KB Output is correct
5 Correct 8 ms 39516 KB Output is correct
6 Incorrect 8 ms 39772 KB 3rd lines differ - on the 1st token, expected: '22140', found: '22156'
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 39516 KB Output is correct
2 Correct 7 ms 39516 KB Output is correct
3 Correct 8 ms 39516 KB Output is correct
4 Correct 8 ms 39516 KB Output is correct
5 Correct 8 ms 39516 KB Output is correct
6 Correct 8 ms 39516 KB Output is correct
7 Correct 7 ms 39392 KB Output is correct
8 Correct 7 ms 39516 KB Output is correct
9 Correct 7 ms 39440 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 7 ms 39512 KB Output is correct
14 Correct 8 ms 39516 KB Output is correct
15 Correct 8 ms 39512 KB Output is correct
16 Correct 9 ms 39516 KB Output is correct
17 Correct 7 ms 39516 KB Output is correct
18 Correct 8 ms 39516 KB Output is correct
19 Correct 8 ms 39512 KB Output is correct
20 Correct 8 ms 39512 KB Output is correct
21 Correct 7 ms 39516 KB Output is correct
22 Correct 8 ms 39516 KB Output is correct
23 Correct 8 ms 39516 KB Output is correct
24 Correct 8 ms 39516 KB Output is correct
25 Correct 8 ms 39424 KB Output is correct
26 Correct 8 ms 39516 KB Output is correct
27 Correct 8 ms 39512 KB Output is correct
28 Correct 7 ms 39516 KB Output is correct
29 Correct 8 ms 39668 KB Output is correct
30 Correct 102 ms 53052 KB Output is correct
31 Correct 108 ms 53072 KB Output is correct
32 Correct 405 ms 150156 KB Output is correct
33 Correct 322 ms 131332 KB Output is correct
34 Correct 320 ms 131020 KB Output is correct
35 Correct 284 ms 115612 KB Output is correct
36 Correct 191 ms 87204 KB Output is correct
37 Correct 107 ms 63644 KB Output is correct
38 Correct 96 ms 59952 KB Output is correct
39 Correct 98 ms 59704 KB Output is correct
40 Correct 91 ms 55380 KB Output is correct
41 Correct 98 ms 53304 KB Output is correct
42 Correct 96 ms 53812 KB Output is correct
43 Correct 417 ms 132032 KB Output is correct
44 Correct 8 ms 39512 KB Output is correct
45 Correct 8 ms 39516 KB Output is correct
46 Correct 8 ms 39516 KB Output is correct
47 Correct 8 ms 39608 KB Output is correct
48 Correct 8 ms 39516 KB Output is correct
49 Incorrect 8 ms 39772 KB 3rd lines differ - on the 1st token, expected: '22140', found: '22156'
50 Halted 0 ms 0 KB -