Submission #283841

# Submission time Handle Problem Language Result Execution time Memory
283841 2020-08-26T07:54:13 Z hank55663 Ancient Books (IOI17_books) C++14
100 / 100
1983 ms 488648 KB
#include "books.h"
#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define x first
#define y second
int f[1000005];
int l[1000005];
int r[1000005];
int Find(int x){
	if(f[x]==x)return x;
	return f[x]=Find(f[x]);
}
int pre[1000005];
vector<pii> edge[4000005];
int dis[4000005];
int ok[1000005];
int vis[4000005];
int mindex;
struct node{
	node *l,*r;
	//list<int> v;
	int num;
	int a,b;
	node(int _a,int _b):a(_a),b(_b),l(NULL),r(NULL){
		num=mindex++;
	}
}*root;
void build(node *n){
	if(n->a==n->b){
		edge[n->num].pb(mp(n->a,0));
		return;
	}
	int mid=(n->a+n->b)/2;
	n->l=new node(n->a,mid);
	n->r=new node(mid+1,n->b);
	edge[n->num].push_back(mp(n->l->num,0));
	edge[n->num].push_back(mp(n->r->num,0));
	build(n->l);
	build(n->r);
}
void go(node *n,int l,int r,int x){
	if(n->a>=l&&n->b<=r){
		edge[x].push_back(mp(n->num,0));
		return ;
	}
	if(n->b<l||n->a>r)return;
	go(n->l,l,r,x);
	go(n->r,l,r,x);
}
long long minimum_walk(std::vector<int> p, int s) {
//	vector<pii> v;
	mindex=p.size();
	long long ans=0;
	int Min=1e9;
	int over=0;
	for(int i = 0;i<p.size();i++){
		if(p[i]!=i){
//			v.pb(mp(p[i],i));
			ans+=abs(p[i]-i);
		//	Min=min(Min,abs(i-s));
			if((i<s&&p[i]>s)||(i>s&&p[i]<s))over=1;
		}
		l[i]=1e9;
		r[i]=0;
		f[i]=i;
	}
	for(int i = 0;i<p.size();i++){
		f[Find(p[i])]=Find(i);
	}
	for(int i = 0;i<p.size();i++){
		l[Find(i)]=min(l[Find(i)],i);
		r[Find(i)]=max(r[Find(i)],i);
	}
	vector<pii> v;
	root = new node(0,p.size()-1);
	build(root);
	for(int i = 0;i<p.size();i++){
		if(f[i]==i&&l[i]!=r[i]){
			v.pb(mp(l[i],r[i]));
		}
	}
	if(v.empty())return 0;
	sort(v.begin(),v.end());
	int Max=v[0].y;
	ok[v[0].x]=1;
	for(auto it:v){
		if(Max<it.x){
			ok[Max]=1;
			ok[it.x]=1;
		}
		ans+=max(0,(it.x-Max)*2);
		Max=max(Max,it.y);
	}
	ok[Max]=1;
	if(!over){
	//	printf("?\n");
		if(s>Max||s<v[0].x){
			
			Min=max(s-Max,v[0].x-s);
		}
		else{
			//printf("%d ?\n",ans);
			Min=0;
		}
	}
	else{
		for(int i = 0;i<p.size();i++){
			if(p[i]<i){
				go(root,p[i],i,i);
			}
			if(p[i]>i){
				go(root,i,p[i],i);
			}
			if(i!=0){
				edge[i].pb(mp(i-1,1));
			}
			if(i!=p.size()){
				edge[i].pb(mp(i+1,1));
			}
		}
		vector<int> q,q2;
		q.pb(s);
		fill(dis,dis+mindex,1e9);
		dis[s]=0;
		fill(vis,vis+mindex,0);
		while(!q.empty()){
			for(int i = 0;i<q.size();i++){
				int x=q[i];
				if(vis[x])continue;
				vis[x]=1;
			//	printf("%d %d\n",x,dis[x]);
				for(auto it:edge[x]){
					if(it.y==0){
						if(dis[it.x]>dis[x]){
							dis[it.x]=dis[x];
			//				printf("go %d %d\n",it.x,dis[it.x]);
							q.pb(it.x);
						}
					}
					else{
						if(dis[it.x]>dis[x]+1){
							dis[it.x]=dis[x]+1;
			//				printf("go2 %d %d\n",it.x,dis[it.x]);
							q2.pb(it.x);
						}
					}
				}
			}
			swap(q,q2);
			q2.clear();
		}
		/*
		while(!q.empty()){
			int x=q.front();
			q.pop_front();
			if(vis[x])continue;
			vis[x]=1;
			for(auto it:edge[x]){
				
				}
			}
		}*/
		for(int i = 0;i<p.size();i++){
			if(ok[i]){
				Min=min(Min,dis[i]);
			}
		}
	}
//	printf("%d\n",Min);
	return ans+Min*2;
}
/*
8
7 3 4 1 2 5 6 0
*/

Compilation message

books.cpp: In constructor 'node::node(int, int)':
books.cpp:26:8: warning: 'node::b' will be initialized after [-Wreorder]
   26 |  int a,b;
      |        ^
books.cpp:23:8: warning:   'node* node::l' [-Wreorder]
   23 |  node *l,*r;
      |        ^
books.cpp:27:2: warning:   when initialized here [-Wreorder]
   27 |  node(int _a,int _b):a(_a),b(_b),l(NULL),r(NULL){
      |  ^~~~
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:59:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |  for(int i = 0;i<p.size();i++){
      |                ~^~~~~~~~~
books.cpp:70:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |  for(int i = 0;i<p.size();i++){
      |                ~^~~~~~~~~
books.cpp:73:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |  for(int i = 0;i<p.size();i++){
      |                ~^~~~~~~~~
books.cpp:80:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   80 |  for(int i = 0;i<p.size();i++){
      |                ~^~~~~~~~~
books.cpp:110:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  110 |   for(int i = 0;i<p.size();i++){
      |                 ~^~~~~~~~~
books.cpp:120:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  120 |    if(i!=p.size()){
      |       ~^~~~~~~~~~
books.cpp:130:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  130 |    for(int i = 0;i<q.size();i++){
      |                  ~^~~~~~~~~
books.cpp:166:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  166 |   for(int i = 0;i<p.size();i++){
      |                 ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 64 ms 94328 KB Output is correct
2 Correct 62 ms 94328 KB Output is correct
3 Correct 67 ms 94328 KB Output is correct
4 Correct 63 ms 94312 KB Output is correct
5 Correct 62 ms 94328 KB Output is correct
6 Correct 65 ms 94328 KB Output is correct
7 Correct 63 ms 94328 KB Output is correct
8 Correct 63 ms 94328 KB Output is correct
9 Correct 62 ms 94328 KB Output is correct
10 Correct 70 ms 94328 KB Output is correct
11 Correct 74 ms 94328 KB Output is correct
12 Correct 64 ms 94328 KB Output is correct
13 Correct 62 ms 94328 KB Output is correct
14 Correct 63 ms 94328 KB Output is correct
15 Correct 64 ms 94328 KB Output is correct
16 Correct 62 ms 94328 KB Output is correct
17 Correct 63 ms 94328 KB Output is correct
18 Correct 61 ms 94328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 64 ms 94328 KB Output is correct
2 Correct 62 ms 94328 KB Output is correct
3 Correct 67 ms 94328 KB Output is correct
4 Correct 63 ms 94312 KB Output is correct
5 Correct 62 ms 94328 KB Output is correct
6 Correct 65 ms 94328 KB Output is correct
7 Correct 63 ms 94328 KB Output is correct
8 Correct 63 ms 94328 KB Output is correct
9 Correct 62 ms 94328 KB Output is correct
10 Correct 70 ms 94328 KB Output is correct
11 Correct 74 ms 94328 KB Output is correct
12 Correct 64 ms 94328 KB Output is correct
13 Correct 62 ms 94328 KB Output is correct
14 Correct 63 ms 94328 KB Output is correct
15 Correct 64 ms 94328 KB Output is correct
16 Correct 62 ms 94328 KB Output is correct
17 Correct 63 ms 94328 KB Output is correct
18 Correct 61 ms 94328 KB Output is correct
19 Correct 62 ms 94456 KB Output is correct
20 Correct 61 ms 94456 KB Output is correct
21 Correct 71 ms 94456 KB Output is correct
22 Correct 65 ms 94584 KB Output is correct
23 Correct 63 ms 94456 KB Output is correct
24 Correct 63 ms 94456 KB Output is correct
25 Correct 65 ms 94456 KB Output is correct
26 Correct 64 ms 94480 KB Output is correct
27 Correct 69 ms 94456 KB Output is correct
28 Correct 64 ms 94432 KB Output is correct
29 Correct 64 ms 94456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 64 ms 94328 KB Output is correct
2 Correct 62 ms 94328 KB Output is correct
3 Correct 67 ms 94328 KB Output is correct
4 Correct 63 ms 94312 KB Output is correct
5 Correct 62 ms 94328 KB Output is correct
6 Correct 65 ms 94328 KB Output is correct
7 Correct 63 ms 94328 KB Output is correct
8 Correct 63 ms 94328 KB Output is correct
9 Correct 62 ms 94328 KB Output is correct
10 Correct 70 ms 94328 KB Output is correct
11 Correct 74 ms 94328 KB Output is correct
12 Correct 64 ms 94328 KB Output is correct
13 Correct 62 ms 94328 KB Output is correct
14 Correct 63 ms 94328 KB Output is correct
15 Correct 64 ms 94328 KB Output is correct
16 Correct 62 ms 94328 KB Output is correct
17 Correct 63 ms 94328 KB Output is correct
18 Correct 61 ms 94328 KB Output is correct
19 Correct 62 ms 94456 KB Output is correct
20 Correct 61 ms 94456 KB Output is correct
21 Correct 71 ms 94456 KB Output is correct
22 Correct 65 ms 94584 KB Output is correct
23 Correct 63 ms 94456 KB Output is correct
24 Correct 63 ms 94456 KB Output is correct
25 Correct 65 ms 94456 KB Output is correct
26 Correct 64 ms 94480 KB Output is correct
27 Correct 69 ms 94456 KB Output is correct
28 Correct 64 ms 94432 KB Output is correct
29 Correct 64 ms 94456 KB Output is correct
30 Correct 635 ms 270396 KB Output is correct
31 Correct 645 ms 270632 KB Output is correct
32 Correct 524 ms 270456 KB Output is correct
33 Correct 570 ms 275948 KB Output is correct
34 Correct 558 ms 276076 KB Output is correct
35 Correct 597 ms 276092 KB Output is correct
36 Correct 576 ms 275744 KB Output is correct
37 Correct 560 ms 274680 KB Output is correct
38 Correct 567 ms 273144 KB Output is correct
39 Correct 555 ms 272692 KB Output is correct
40 Correct 570 ms 270712 KB Output is correct
41 Correct 614 ms 270404 KB Output is correct
42 Correct 609 ms 270520 KB Output is correct
43 Correct 556 ms 272604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 64 ms 94584 KB Output is correct
2 Correct 64 ms 94712 KB Output is correct
3 Correct 66 ms 94584 KB Output is correct
4 Correct 62 ms 94456 KB Output is correct
5 Correct 62 ms 94456 KB Output is correct
6 Correct 66 ms 94588 KB Output is correct
7 Correct 66 ms 94732 KB Output is correct
8 Correct 65 ms 94584 KB Output is correct
9 Correct 63 ms 94516 KB Output is correct
10 Correct 64 ms 94456 KB Output is correct
11 Correct 66 ms 94608 KB Output is correct
12 Correct 68 ms 94712 KB Output is correct
13 Correct 63 ms 94584 KB Output is correct
14 Correct 69 ms 94584 KB Output is correct
15 Correct 68 ms 94712 KB Output is correct
16 Correct 64 ms 94584 KB Output is correct
17 Correct 63 ms 94584 KB Output is correct
18 Correct 70 ms 94584 KB Output is correct
19 Correct 71 ms 94584 KB Output is correct
20 Correct 63 ms 94584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 64 ms 94328 KB Output is correct
2 Correct 62 ms 94328 KB Output is correct
3 Correct 67 ms 94328 KB Output is correct
4 Correct 63 ms 94312 KB Output is correct
5 Correct 62 ms 94328 KB Output is correct
6 Correct 65 ms 94328 KB Output is correct
7 Correct 63 ms 94328 KB Output is correct
8 Correct 63 ms 94328 KB Output is correct
9 Correct 62 ms 94328 KB Output is correct
10 Correct 70 ms 94328 KB Output is correct
11 Correct 74 ms 94328 KB Output is correct
12 Correct 64 ms 94328 KB Output is correct
13 Correct 62 ms 94328 KB Output is correct
14 Correct 63 ms 94328 KB Output is correct
15 Correct 64 ms 94328 KB Output is correct
16 Correct 62 ms 94328 KB Output is correct
17 Correct 63 ms 94328 KB Output is correct
18 Correct 61 ms 94328 KB Output is correct
19 Correct 62 ms 94456 KB Output is correct
20 Correct 61 ms 94456 KB Output is correct
21 Correct 71 ms 94456 KB Output is correct
22 Correct 65 ms 94584 KB Output is correct
23 Correct 63 ms 94456 KB Output is correct
24 Correct 63 ms 94456 KB Output is correct
25 Correct 65 ms 94456 KB Output is correct
26 Correct 64 ms 94480 KB Output is correct
27 Correct 69 ms 94456 KB Output is correct
28 Correct 64 ms 94432 KB Output is correct
29 Correct 64 ms 94456 KB Output is correct
30 Correct 635 ms 270396 KB Output is correct
31 Correct 645 ms 270632 KB Output is correct
32 Correct 524 ms 270456 KB Output is correct
33 Correct 570 ms 275948 KB Output is correct
34 Correct 558 ms 276076 KB Output is correct
35 Correct 597 ms 276092 KB Output is correct
36 Correct 576 ms 275744 KB Output is correct
37 Correct 560 ms 274680 KB Output is correct
38 Correct 567 ms 273144 KB Output is correct
39 Correct 555 ms 272692 KB Output is correct
40 Correct 570 ms 270712 KB Output is correct
41 Correct 614 ms 270404 KB Output is correct
42 Correct 609 ms 270520 KB Output is correct
43 Correct 556 ms 272604 KB Output is correct
44 Correct 64 ms 94584 KB Output is correct
45 Correct 64 ms 94712 KB Output is correct
46 Correct 66 ms 94584 KB Output is correct
47 Correct 62 ms 94456 KB Output is correct
48 Correct 62 ms 94456 KB Output is correct
49 Correct 66 ms 94588 KB Output is correct
50 Correct 66 ms 94732 KB Output is correct
51 Correct 65 ms 94584 KB Output is correct
52 Correct 63 ms 94516 KB Output is correct
53 Correct 64 ms 94456 KB Output is correct
54 Correct 66 ms 94608 KB Output is correct
55 Correct 68 ms 94712 KB Output is correct
56 Correct 63 ms 94584 KB Output is correct
57 Correct 69 ms 94584 KB Output is correct
58 Correct 68 ms 94712 KB Output is correct
59 Correct 64 ms 94584 KB Output is correct
60 Correct 63 ms 94584 KB Output is correct
61 Correct 70 ms 94584 KB Output is correct
62 Correct 71 ms 94584 KB Output is correct
63 Correct 63 ms 94584 KB Output is correct
64 Correct 1090 ms 356920 KB Output is correct
65 Correct 1012 ms 353808 KB Output is correct
66 Correct 1592 ms 408192 KB Output is correct
67 Correct 1673 ms 426228 KB Output is correct
68 Correct 191 ms 124124 KB Output is correct
69 Correct 191 ms 123872 KB Output is correct
70 Correct 182 ms 123884 KB Output is correct
71 Correct 171 ms 120968 KB Output is correct
72 Correct 149 ms 119012 KB Output is correct
73 Correct 247 ms 132960 KB Output is correct
74 Correct 560 ms 276972 KB Output is correct
75 Correct 623 ms 277100 KB Output is correct
76 Correct 1244 ms 369276 KB Output is correct
77 Correct 1196 ms 371260 KB Output is correct
78 Correct 1249 ms 361776 KB Output is correct
79 Correct 1234 ms 362344 KB Output is correct
80 Correct 539 ms 271552 KB Output is correct
81 Correct 1666 ms 488648 KB Output is correct
82 Correct 1664 ms 488272 KB Output is correct
83 Correct 1250 ms 360044 KB Output is correct
84 Correct 1321 ms 360792 KB Output is correct
85 Correct 1363 ms 366068 KB Output is correct
86 Correct 1426 ms 378744 KB Output is correct
87 Correct 1262 ms 396844 KB Output is correct
88 Correct 1413 ms 375268 KB Output is correct
89 Correct 1261 ms 361104 KB Output is correct
90 Correct 1552 ms 400700 KB Output is correct
91 Correct 1612 ms 417676 KB Output is correct
92 Correct 1983 ms 444476 KB Output is correct