Submission #496676

# Submission time Handle Problem Language Result Execution time Memory
496676 2021-12-21T19:36:02 Z PedroBigMan Ancient Books (IOI17_books) C++17
100 / 100
1814 ms 802476 KB
/*
Author of all code: Pedro BIGMAN Dias
Last edit: 15/02/2021
*/
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <deque>
#include <list>
#include <iomanip>
#include <stdlib.h>
#include <time.h>
#include <cstring>
#include "books.h"
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
#define REP(i,a,b) for(ll i=(ll) a; i<(ll) b; i++)
#define pb push_back
#define mp make_pair
#define pl pair<ll,ll>
#define ff first
#define ss second
#define whole(x) x.begin(),x.end()
#define DEBUG(i) cout<<"Pedro Is The Master "<<i<<endl
#define INF 500000000LL
#define EPS 0.00000001
#define pi 3.14159
#define VV(vvvv,NNNN,xxxx); REP(i,0,NNNN) {vvvv.pb(xxxx);}
ll mod=1000000007LL;

template<class A=ll> 
void Out(vector<A> a) {REP(i,0,a.size()) {cout<<a[i]<<" ";} cout<<endl;}

template<class A=ll>
void In(vector<A> &a, ll N) {A cur; REP(i,0,N) {cin>>cur; a.pb(cur);}} 

template<class T=ll>
class SparseTable2 //Range Minimum Queries
{
    public:
    ll N; 
    vector<T> a;
    vector<vector<T> > v;
    
    SparseTable2() {N=0LL;}
    SparseTable2(vector<T> b)
    {
        a=b; N=a.size();
        ll lo=(ll) floor((double) log2(N)) +1LL;
        vector<T> xx; 
        REP(i,0,lo) {xx.pb(-1);} REP(i,0,N) {v.pb(xx);}
        REP(step,0LL,lo)
        {
            REP(i,0,N-(1LL<<step)+1LL)
            {
                if(step==0) {v[i][0]=a[i];}
                else {v[i][step]=max(v[i][step-1],v[i+(1LL<<(step-1))][step-1]);}
            }
        }
    }
    
    T query(ll l, ll r)
    {
        ll step=(ll) floor((double) log2(r-l+1LL));
        return max(v[l][step],v[r-(1LL<<step)+1LL][step]);
    }
};

template<class T=ll>
class SparseTable1 //Range Minimum Queries
{
    public:
    ll N; 
    vector<T> a;
    vector<vector<T> > v;
    
    SparseTable1() {N=0LL;}
    SparseTable1(vector<T> b)
    {
        a=b; N=a.size();
        ll lo=(ll) floor((double) log2(N)) +1LL;
        vector<T> xx; 
        REP(i,0,lo) {xx.pb(INF);} REP(i,0,N) {v.pb(xx);}
        REP(step,0LL,lo)
        {
            REP(i,0,N-(1LL<<step)+1LL)
            {
                if(step==0) {v[i][0]=a[i];}
                else {v[i][step]=min(v[i][step-1],v[i+(1LL<<(step-1))][step-1]);}
            }
        }
    }
    
    T query(ll l, ll r)
    {
        ll step=(ll) floor((double) log2(r-l+1LL));
        return min(v[l][step],v[r-(1LL<<step)+1LL][step]);
    }
};

class ST
{
    public:
    ll N;
    
    class SV //seg value
    {
        public:
        ll a; 
        SV() {a=0LL;}
        SV(ll x) {a=x;}
        
        SV operator & (SV X) {SV ANS(a+X.a); return ANS;}
    };
      
    class LV //lazy value
    {
        public:
        ll a;
        LV() {a=0LL;}
        LV(ll x) {a=x;}
        
        LV operator & (LV X) {LV ANS(a+X.a); return ANS;}
    };
    
    SV upval(ll c) //how lazy values modify a seg value inside a node, c=current node
    {
        SV X(p[c].a+(range[c].ss-range[c].ff+1)*lazy[c].a);
        return X;
    }
    
    SV neuts; LV neutl;
    
    vector<SV> p;
    vector<LV> lazy;
    vector<pl> range;
    
    ST() {N=0LL;}
    ST(vector<ll> arr)
    {
        N = (ll) 1<<(ll) ceil(log2(arr.size()));
        REP(i,0,2*N) {range.pb(mp(0LL,0LL));}
        REP(i,0,N) {p.pb(neuts);}
        REP(i,0,arr.size()) {SV X(arr[i]); p.pb(X); range[i+N]=mp(i,i);}
        REP(i,arr.size(),N) {p.pb(neuts); range[i+N]=mp(i,i);}
        ll cur = N-1;
        while(cur>0)
        {
            p[cur]=p[2*cur]&p[2*cur+1];
            range[cur]=mp(range[2*cur].ff,range[2*cur+1].ss);
            cur--;
        }
        REP(i,0,2*N) {lazy.pb(neutl);}
    }
    
    void prop(ll c) //how lazy values propagate
    {
        lazy[2*c]=lazy[c]&lazy[2*c]; lazy[2*c+1]=lazy[c]&lazy[2*c+1];
        lazy[c]=neutl;
    }
    
    SV query(ll a,ll b, ll c=1LL) //range [a,b], current node. initially: query(a,b)
    {
        ll x=range[c].ff; ll y=range[c].ss;
        if(y<a || x>b) {return neuts;}
        if(x>=a && y<=b) {return upval(c);}
        prop(c);
		p[c]=upval(2*c)&upval(2*c+1);
        SV ans = query(a,b,2*c)&query(a,b,2*c+1);
        return ans;
    }
    
    void update(LV s, ll a, ll b, ll c=1LL) //update LV, range [a,b], current node, current range. initially: update(s,a,b)
    {
        ll x=range[c].ff; ll y=range[c].ss;
        if(y<a || x>b) {return ;}
        if(x>=a && y<=b) 
        {
            lazy[c]=s&lazy[c]; 
            return;
        }
		prop(c);
        update(s,a,b,2*c); update(s,a,b,2*c+1);
        p[c]=upval(2*c)&upval(2*c+1);
    }
};

vector<vector<ll> > CycleDecomp(vector<int> p) //cycle decomposition of permutation
{
	ll N = p.size(); vector<vector<ll> > ans; vector<ll> cur;
	vector<bool> visited; REP(i,0,N) {visited.pb(false);} 
	ll node;
	REP(i,0,N)
	{
		if(visited[i]) {continue;}
		node=i; cur.pb(node); node=p[node];
		while(node!=i)
		{
			cur.pb(node); node=p[node];
		}
		REP(i,0,cur.size()) {visited[cur[i]]=true;}
		ans.pb(cur);
		cur.clear();
	}
	return ans;
}

class WDiGraph
{
    public:
    ll N;
    vector<vector<pl> > adj; 
    vector<bool> visited;
    vector<bool> pr;
	
    WDiGraph(vector<vector<pl> > ad)
    {
        adj=ad; N=adj.size(); REP(i,0,N) {visited.pb(false); pr.pb(false);}
    }
    
    vector<ll> Djikstra(ll s)
    {
        vector<ll> d; REP(i,0,N) {d.pb(INF);}
        d[s]=0;
        priority_queue<pl> q;
        q.push(mp(0,s));
        ll cur;
        while(!q.empty())
        {
            cur=q.top().ss; q.pop();
            if(pr[cur]) {continue;}
            pr[cur]=true; 
            REP(i,0,adj[cur].size())
            {
                if(d[adj[cur][i].ff]>d[cur]+adj[cur][i].ss)
                {
                    d[adj[cur][i].ff]=d[cur]+adj[cur][i].ss;
                    q.push(mp(-d[adj[cur][i].ff],adj[cur][i].ff));
                }
            }
        }
        return d;
    }
};

ll minimum_walk(vector<int> p, int s) 
{
	ll N = p.size();
	vector<vector<ll> > C = CycleDecomp(p);
	vector<pl> range;
	REP(i,0,C.size()) {range.pb({*min_element(whole(C[i])),*max_element(whole(C[i]))});}
	vector<ll> xx; VV(xx,N-1,0); ST S(xx);
	REP(i,0,range.size()) {if(range[i].ff==range[i].ss) {continue;} S.update(1,range[i].ff,range[i].ss-1);}
	vector<bool> in; VV(in,N-1,false);
	REP(i,0,N-1) {if(S.query(i,i).a>0) {in[i]=true;}}
	ll l=0,r=N-2; 
	while(l<s && !in[l]) {l++;}
	while(r>=s && !in[r]) {r--;}
	ll ans=0LL;
	REP(i,l,r+1) {if(!in[i]) {ans+=2LL;}}
	REP(i,0,N) {ans+=((ll) (abs(p[i]-i)));}
	ll T=s; while(T<N-1 && in[T]) {T++;} 
	ll SS;
	REP(i,0,C.size()) {sort(whole(C[i]));}
	vector<vector<pl> > adj; VV(adj,C.size(),{});
	vector<ll> rep; VV(rep,N,-1); REP(i,0,C.size()) {REP(j,0,C[i].size()) {rep[C[i][j]]=i;}}
	SS=rep[s]; T=rep[T];
	vector<ll>::iterator it;
	REP(i,0,N-1) {adj[rep[i]].pb({rep[i+1],1LL}); adj[rep[i+1]].pb({rep[i],1LL});} 
	vector<ll> lend,rend; REP(i,0,N) {lend.pb(range[rep[i]].ff); rend.pb(range[rep[i]].ss);}
	SparseTable1 L(lend); SparseTable2 R(rend);
	REP(i,0,C.size())
	{
		ll LL = L.query(range[i].ff,range[i].ss); ll RR = R.query(range[i].ff,range[i].ss);
		if(LL<range[i].ff) {adj[i].pb({rep[LL],0LL});}
		if(RR>range[i].ss) {adj[i].pb({rep[RR],0LL});}
	}
	WDiGraph G(adj); vector<ll> d = G.Djikstra(SS); ans+=2LL*d[T];
	return ans;
}

Compilation message

books.cpp:5: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    5 | #pragma GCC optimization ("O3")
      | 
books.cpp:6: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    6 | #pragma GCC optimization ("unroll-loops")
      |
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 1 ms 716 KB Output is correct
20 Correct 1 ms 716 KB Output is correct
21 Correct 1 ms 844 KB Output is correct
22 Correct 1 ms 844 KB Output is correct
23 Correct 1 ms 844 KB Output is correct
24 Correct 2 ms 804 KB Output is correct
25 Correct 1 ms 844 KB Output is correct
26 Correct 1 ms 716 KB Output is correct
27 Correct 1 ms 716 KB Output is correct
28 Correct 1 ms 716 KB Output is correct
29 Correct 1 ms 844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 1 ms 716 KB Output is correct
20 Correct 1 ms 716 KB Output is correct
21 Correct 1 ms 844 KB Output is correct
22 Correct 1 ms 844 KB Output is correct
23 Correct 1 ms 844 KB Output is correct
24 Correct 2 ms 804 KB Output is correct
25 Correct 1 ms 844 KB Output is correct
26 Correct 1 ms 716 KB Output is correct
27 Correct 1 ms 716 KB Output is correct
28 Correct 1 ms 716 KB Output is correct
29 Correct 1 ms 844 KB Output is correct
30 Correct 1240 ms 617292 KB Output is correct
31 Correct 1238 ms 616936 KB Output is correct
32 Correct 1477 ms 795508 KB Output is correct
33 Correct 1458 ms 741744 KB Output is correct
34 Correct 1452 ms 741832 KB Output is correct
35 Correct 1464 ms 719776 KB Output is correct
36 Correct 1365 ms 676344 KB Output is correct
37 Correct 1237 ms 635668 KB Output is correct
38 Correct 1195 ms 625748 KB Output is correct
39 Correct 1208 ms 624848 KB Output is correct
40 Correct 1190 ms 622080 KB Output is correct
41 Correct 1210 ms 620068 KB Output is correct
42 Correct 1226 ms 623272 KB Output is correct
43 Correct 1703 ms 747248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 844 KB Output is correct
2 Correct 1 ms 844 KB Output is correct
3 Correct 1 ms 844 KB Output is correct
4 Correct 1 ms 844 KB Output is correct
5 Correct 1 ms 864 KB Output is correct
6 Correct 1 ms 844 KB Output is correct
7 Correct 1 ms 844 KB Output is correct
8 Correct 1 ms 844 KB Output is correct
9 Correct 2 ms 844 KB Output is correct
10 Correct 1 ms 844 KB Output is correct
11 Correct 2 ms 716 KB Output is correct
12 Correct 2 ms 716 KB Output is correct
13 Correct 2 ms 716 KB Output is correct
14 Correct 1 ms 716 KB Output is correct
15 Correct 1 ms 716 KB Output is correct
16 Correct 1 ms 716 KB Output is correct
17 Correct 2 ms 844 KB Output is correct
18 Correct 1 ms 844 KB Output is correct
19 Correct 1 ms 716 KB Output is correct
20 Correct 1 ms 716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 1 ms 716 KB Output is correct
20 Correct 1 ms 716 KB Output is correct
21 Correct 1 ms 844 KB Output is correct
22 Correct 1 ms 844 KB Output is correct
23 Correct 1 ms 844 KB Output is correct
24 Correct 2 ms 804 KB Output is correct
25 Correct 1 ms 844 KB Output is correct
26 Correct 1 ms 716 KB Output is correct
27 Correct 1 ms 716 KB Output is correct
28 Correct 1 ms 716 KB Output is correct
29 Correct 1 ms 844 KB Output is correct
30 Correct 1240 ms 617292 KB Output is correct
31 Correct 1238 ms 616936 KB Output is correct
32 Correct 1477 ms 795508 KB Output is correct
33 Correct 1458 ms 741744 KB Output is correct
34 Correct 1452 ms 741832 KB Output is correct
35 Correct 1464 ms 719776 KB Output is correct
36 Correct 1365 ms 676344 KB Output is correct
37 Correct 1237 ms 635668 KB Output is correct
38 Correct 1195 ms 625748 KB Output is correct
39 Correct 1208 ms 624848 KB Output is correct
40 Correct 1190 ms 622080 KB Output is correct
41 Correct 1210 ms 620068 KB Output is correct
42 Correct 1226 ms 623272 KB Output is correct
43 Correct 1703 ms 747248 KB Output is correct
44 Correct 1 ms 844 KB Output is correct
45 Correct 1 ms 844 KB Output is correct
46 Correct 1 ms 844 KB Output is correct
47 Correct 1 ms 844 KB Output is correct
48 Correct 1 ms 864 KB Output is correct
49 Correct 1 ms 844 KB Output is correct
50 Correct 1 ms 844 KB Output is correct
51 Correct 1 ms 844 KB Output is correct
52 Correct 2 ms 844 KB Output is correct
53 Correct 1 ms 844 KB Output is correct
54 Correct 2 ms 716 KB Output is correct
55 Correct 2 ms 716 KB Output is correct
56 Correct 2 ms 716 KB Output is correct
57 Correct 1 ms 716 KB Output is correct
58 Correct 1 ms 716 KB Output is correct
59 Correct 1 ms 716 KB Output is correct
60 Correct 2 ms 844 KB Output is correct
61 Correct 1 ms 844 KB Output is correct
62 Correct 1 ms 716 KB Output is correct
63 Correct 1 ms 716 KB Output is correct
64 Correct 1566 ms 751980 KB Output is correct
65 Correct 1571 ms 769708 KB Output is correct
66 Correct 1218 ms 640684 KB Output is correct
67 Correct 1193 ms 636224 KB Output is correct
68 Correct 124 ms 64488 KB Output is correct
69 Correct 120 ms 61632 KB Output is correct
70 Correct 124 ms 65300 KB Output is correct
71 Correct 139 ms 70612 KB Output is correct
72 Correct 146 ms 73552 KB Output is correct
73 Correct 107 ms 58516 KB Output is correct
74 Correct 1481 ms 748716 KB Output is correct
75 Correct 1479 ms 748768 KB Output is correct
76 Correct 1704 ms 752184 KB Output is correct
77 Correct 1814 ms 753368 KB Output is correct
78 Correct 1639 ms 733060 KB Output is correct
79 Correct 1623 ms 733288 KB Output is correct
80 Correct 1388 ms 802476 KB Output is correct
81 Correct 1503 ms 708020 KB Output is correct
82 Correct 1454 ms 708068 KB Output is correct
83 Correct 1478 ms 700904 KB Output is correct
84 Correct 1442 ms 692208 KB Output is correct
85 Correct 1310 ms 665848 KB Output is correct
86 Correct 1254 ms 647708 KB Output is correct
87 Correct 1522 ms 773412 KB Output is correct
88 Correct 1540 ms 746832 KB Output is correct
89 Correct 1474 ms 710472 KB Output is correct
90 Correct 1204 ms 638832 KB Output is correct
91 Correct 1188 ms 632704 KB Output is correct
92 Correct 1195 ms 629176 KB Output is correct