Submission #891093

# Submission time Handle Problem Language Result Execution time Memory
891093 2023-12-22T07:31:43 Z vjudge1 Bitaro's travel (JOI23_travel) C++17
100 / 100
845 ms 548316 KB
#include <bits/stdc++.h>
using namespace std;
#define ios ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fr first
#define sc second
#define endl "\n"
#define ll long long
#define int ll
int E,B;
void fopn(string name){
    freopen((name+".in").c_str(),"r",stdin);
    freopen((name+".out").c_str(),"w",stdout);
}
const ll N=5e5+5,INF=1e18,mod=1e9+7;
vector<vector<int>> pr(N,vector<int>(65,0)),
					nx(N,vector<int>(65,0));

void solve(){
	int n;cin>>n;
	vector<int> x(n+2);
	for(int i=1;i<=n;i++) cin>>x[i];
	x[0]=-INF;
	x[n+1]=INF;
	for(int i=0;i<=55;i++) pr[1][i]=1;
	for(int i=2;i<=n;i++){
		pr[i]=pr[i-1];
		for(int j=0;j<=55;j++){
			if((1<<j)<=x[i]-x[i-1])
				pr[i][j]=i;
		}
	}
	for(int i=0;i<=55;i++) nx[n][i]=n;
	for(int i=n-1;i>=1;i--){
		nx[i]=nx[i+1];
		for(int j=0;j<=55;j++){
			if((1<<j)<=x[i+1]-x[i])
				nx[i][j]=i;
		}
	}
	int q,l,r,ans;cin>>q;
	while(q--){
		int a;cin>>a;
		int pos=upper_bound(all(x),a)-x.begin();
		if(a-x[pos-1]<=x[pos]-a) pos--;
		ans=abs(a-x[pos]);
		l=r=pos;
		bool s=(x[pos]-x[pos-1]<=x[pos+1]-x[pos]);
		int k=0;
		while(l!=1 || r!=n){
			while((1<<(k+1))<=(x[r]-x[l])) k++;
			if(s){
				ans+=x[l]-x[pr[l][k]];
				l=pr[l][k];
				if(x[l]-x[l-1]<=x[r+1]-x[l] && l>1){
					ans+=x[l]-x[l-1];l--;
				}
				else if(r<n){
					ans+=x[r]-x[l];
					s=0;
				}
			}
			else{
				ans+=x[nx[r][k]]-x[r];
				r=nx[r][k];
				if(x[r+1]-x[r]<x[r]-x[l-1] && r<n){
					ans+=x[r+1]-x[r];r++;
				}
				else if(l>1){
					ans+=x[r]-x[l];
					s=1;
				}
			}
		}
		cout<<ans<<endl;
	}
	
}
main(){
	B=clock();
    ios;
    int T=1;
	//cin>>T;
    for(int i=1;i<=T;i++){
        //cout<<"Case #"<<i<<": ";
        solve();
    }
    E=clock();
    cerr<<"\n\n\n\nTIME: "<<((float)E-B)/CLOCKS_PER_SEC<<" SECONDS";
}

Compilation message

travel.cpp:81:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   81 | main(){
      | ^~~~
travel.cpp: In function 'void fopn(std::string)':
travel.cpp:14:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   14 |     freopen((name+".in").c_str(),"r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
travel.cpp:15:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   15 |     freopen((name+".out").c_str(),"w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 569 ms 540540 KB Output is correct
2 Correct 529 ms 540756 KB Output is correct
3 Correct 531 ms 540444 KB Output is correct
4 Correct 515 ms 540496 KB Output is correct
5 Correct 524 ms 540416 KB Output is correct
6 Correct 527 ms 540580 KB Output is correct
7 Correct 529 ms 540508 KB Output is correct
8 Correct 527 ms 540416 KB Output is correct
9 Correct 519 ms 540392 KB Output is correct
10 Correct 513 ms 540432 KB Output is correct
11 Correct 522 ms 540500 KB Output is correct
12 Correct 530 ms 540576 KB Output is correct
13 Correct 526 ms 540572 KB Output is correct
14 Correct 540 ms 540496 KB Output is correct
15 Correct 531 ms 540360 KB Output is correct
16 Correct 517 ms 540548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 569 ms 540540 KB Output is correct
2 Correct 529 ms 540756 KB Output is correct
3 Correct 531 ms 540444 KB Output is correct
4 Correct 515 ms 540496 KB Output is correct
5 Correct 524 ms 540416 KB Output is correct
6 Correct 527 ms 540580 KB Output is correct
7 Correct 529 ms 540508 KB Output is correct
8 Correct 527 ms 540416 KB Output is correct
9 Correct 519 ms 540392 KB Output is correct
10 Correct 513 ms 540432 KB Output is correct
11 Correct 522 ms 540500 KB Output is correct
12 Correct 530 ms 540576 KB Output is correct
13 Correct 526 ms 540572 KB Output is correct
14 Correct 540 ms 540496 KB Output is correct
15 Correct 531 ms 540360 KB Output is correct
16 Correct 517 ms 540548 KB Output is correct
17 Correct 569 ms 542016 KB Output is correct
18 Correct 564 ms 542076 KB Output is correct
19 Correct 558 ms 542036 KB Output is correct
20 Correct 572 ms 542036 KB Output is correct
21 Correct 565 ms 542292 KB Output is correct
22 Correct 558 ms 542076 KB Output is correct
23 Correct 583 ms 542168 KB Output is correct
24 Correct 567 ms 542260 KB Output is correct
25 Correct 597 ms 542024 KB Output is correct
26 Correct 577 ms 542116 KB Output is correct
27 Correct 576 ms 542116 KB Output is correct
28 Correct 575 ms 542056 KB Output is correct
29 Correct 565 ms 542036 KB Output is correct
30 Correct 576 ms 542248 KB Output is correct
31 Correct 567 ms 542308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 528 ms 540516 KB Output is correct
2 Correct 518 ms 540496 KB Output is correct
3 Correct 534 ms 540576 KB Output is correct
4 Correct 544 ms 540452 KB Output is correct
5 Correct 520 ms 540540 KB Output is correct
6 Correct 526 ms 540480 KB Output is correct
7 Correct 710 ms 543428 KB Output is correct
8 Correct 710 ms 543332 KB Output is correct
9 Correct 705 ms 543424 KB Output is correct
10 Correct 706 ms 543316 KB Output is correct
11 Correct 704 ms 543348 KB Output is correct
12 Correct 761 ms 543572 KB Output is correct
13 Correct 561 ms 542352 KB Output is correct
14 Correct 541 ms 541072 KB Output is correct
15 Correct 677 ms 543916 KB Output is correct
16 Correct 716 ms 544084 KB Output is correct
17 Correct 680 ms 543964 KB Output is correct
18 Correct 559 ms 542292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 569 ms 540540 KB Output is correct
2 Correct 529 ms 540756 KB Output is correct
3 Correct 531 ms 540444 KB Output is correct
4 Correct 515 ms 540496 KB Output is correct
5 Correct 524 ms 540416 KB Output is correct
6 Correct 527 ms 540580 KB Output is correct
7 Correct 529 ms 540508 KB Output is correct
8 Correct 527 ms 540416 KB Output is correct
9 Correct 519 ms 540392 KB Output is correct
10 Correct 513 ms 540432 KB Output is correct
11 Correct 522 ms 540500 KB Output is correct
12 Correct 530 ms 540576 KB Output is correct
13 Correct 526 ms 540572 KB Output is correct
14 Correct 540 ms 540496 KB Output is correct
15 Correct 531 ms 540360 KB Output is correct
16 Correct 517 ms 540548 KB Output is correct
17 Correct 569 ms 542016 KB Output is correct
18 Correct 564 ms 542076 KB Output is correct
19 Correct 558 ms 542036 KB Output is correct
20 Correct 572 ms 542036 KB Output is correct
21 Correct 565 ms 542292 KB Output is correct
22 Correct 558 ms 542076 KB Output is correct
23 Correct 583 ms 542168 KB Output is correct
24 Correct 567 ms 542260 KB Output is correct
25 Correct 597 ms 542024 KB Output is correct
26 Correct 577 ms 542116 KB Output is correct
27 Correct 576 ms 542116 KB Output is correct
28 Correct 575 ms 542056 KB Output is correct
29 Correct 565 ms 542036 KB Output is correct
30 Correct 576 ms 542248 KB Output is correct
31 Correct 567 ms 542308 KB Output is correct
32 Correct 528 ms 540516 KB Output is correct
33 Correct 518 ms 540496 KB Output is correct
34 Correct 534 ms 540576 KB Output is correct
35 Correct 544 ms 540452 KB Output is correct
36 Correct 520 ms 540540 KB Output is correct
37 Correct 526 ms 540480 KB Output is correct
38 Correct 710 ms 543428 KB Output is correct
39 Correct 710 ms 543332 KB Output is correct
40 Correct 705 ms 543424 KB Output is correct
41 Correct 706 ms 543316 KB Output is correct
42 Correct 704 ms 543348 KB Output is correct
43 Correct 761 ms 543572 KB Output is correct
44 Correct 561 ms 542352 KB Output is correct
45 Correct 541 ms 541072 KB Output is correct
46 Correct 677 ms 543916 KB Output is correct
47 Correct 716 ms 544084 KB Output is correct
48 Correct 680 ms 543964 KB Output is correct
49 Correct 559 ms 542292 KB Output is correct
50 Correct 807 ms 544228 KB Output is correct
51 Correct 766 ms 544148 KB Output is correct
52 Correct 776 ms 544300 KB Output is correct
53 Correct 811 ms 548316 KB Output is correct
54 Correct 845 ms 548064 KB Output is correct
55 Correct 843 ms 548020 KB Output is correct
56 Correct 633 ms 548008 KB Output is correct
57 Correct 618 ms 547920 KB Output is correct
58 Correct 629 ms 547956 KB Output is correct