CSES - Putka Open 2015 – 4/6 - Results
Submission details
Task:Labyrintti
Sender:
Submission time:2015-10-09 17:44:45 +0300
Language:C++
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED12
#2ACCEPTED29
#3ACCEPTED59
Test results
testverdicttimegroup
#1ACCEPTED0.05 s1details
#2ACCEPTED0.06 s1details
#3ACCEPTED0.05 s1details
#4ACCEPTED0.05 s1details
#5ACCEPTED0.05 s1details
#6ACCEPTED0.05 s2details
#7ACCEPTED0.06 s2details
#8ACCEPTED0.06 s2details
#9ACCEPTED0.05 s2details
#10ACCEPTED0.06 s2details
#11ACCEPTED0.07 s3details
#12ACCEPTED0.05 s3details
#13ACCEPTED0.06 s3details
#14ACCEPTED0.06 s3details
#15ACCEPTED0.05 s3details

Code

#include <stdint.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <vector>

typedef std::pair<int64_t,int64_t>point;

inline int64_t& X(point& p){return p.first;}
inline int64_t& Y(point& p){return p.second;}

bool within(point p,std::pair<point,point>bounds){
  auto r1=bounds.first,r2=bounds.second;
  int64_t min_x=std::min(X(r1),X(r2));
  int64_t min_y=std::min(Y(r1),Y(r2));
  int64_t max_x=std::max(X(r1),X(r2));
  int64_t max_y=std::max(Y(r1),Y(r2));
  return X(p)>min_x&&X(p)<max_x&&Y(p)>min_y&&Y(p)<max_y;
}

point spiral(int i){
  if(i<=0)return{0,0};
  static std::vector<point>spiral{{0,0}};
  static std::vector<int>dx{1,0,-1,0},dy{0,-1,0,1};
  for(int j=spiral.size()-1;j<i;++j){
    int64_t d=2ULL<<j;
    int64_t x=spiral[j].first;
    int64_t y=spiral[j].second;
    spiral.emplace_back(x+d*dx[j%4],y+d*dy[j%4]);
  }
  return spiral[i];
}

std::pair<point,point>box(int i){
  point p[3]{spiral(i),spiral(i-1),spiral(i-4)};
  int64_t xs[3]{X(p[0]),X(p[1]),X(p[2])};
  int64_t ys[3]{Y(p[0]),Y(p[1]),Y(p[2])};
  auto MIN=[](int64_t a,int64_t b,int64_t c){return std::min(std::min(a,b),c);};
  auto MAX=[](int64_t a,int64_t b,int64_t c){return std::max(std::max(a,b),c);};
  return {
    {MIN(xs[0],xs[1],xs[2]),MIN(ys[0],ys[1],ys[2])},
    {MAX(xs[0],xs[1],xs[2]),MAX(ys[0],ys[1],ys[2])}
  };
}

point corner(int i){
  static std::vector<int>dx{-1,1,1,-1},dy{1,1,-1,-1};
  int m=(i%4+4)%4;
  auto p=spiral(i-4);
  return{X(p)+dx[m],Y(p)+dy[m]};
}

int64_t manhattan(point a,point b){
  return ::llabs(X(a)-X(b))+::llabs(Y(a)-Y(b));
}

int main(){
  point A,B;std::cin>>Y(A)>>X(A)>>Y(B)>>X(B);
  int64_t distance=0;
  bool foundA=false;
  for(int i=3;;++i){
    auto b=box(i);
    if(within(A,b)&&within(B,b))return std::cout<<manhattan(A,B)<<'\n',0;
    if(foundA){
      if(within(B,b))return std::cout<<distance+manhattan(B,corner(i-1))<<'\n',0;
      distance+=manhattan(corner(i-1),corner(i));
    }
    else{
      if(within(B,b))std::swap(A,B);
      if(within(A,b)){
	foundA=true;
	distance=manhattan(A,corner(i));
      }
    }
  }
}

Test details

Test 1

Group: 1

Verdict: ACCEPTED

input
6 -1 8 -2

correct output
3

user output
3

Test 2

Group: 1

Verdict: ACCEPTED

input
-8 2 8 -9

correct output
27

user output
27

Test 3

Group: 1

Verdict: ACCEPTED

input
3 2 -5 -1

correct output
13

user output
13

Test 4

Group: 1

Verdict: ACCEPTED

input
4 4 2 7

correct output
5

user output
5

Test 5

Group: 1

Verdict: ACCEPTED

input
-5 4 -6 -3

correct output
8

user output
8

Test 6

Group: 2

Verdict: ACCEPTED

input
-156 226 -9 7

correct output
438

user output
438

Test 7

Group: 2

Verdict: ACCEPTED

input
403 265 10 0

correct output
1100

user output
1100

Test 8

Group: 2

Verdict: ACCEPTED

input
146 462 9 -3

correct output
1160

user output
1160

Test 9

Group: 2

Verdict: ACCEPTED

input
223 82 -1 5

correct output
725

user output
725

Test 10

Group: 2

Verdict: ACCEPTED

input
1 390 -5 2

correct output
436

user output
436

Test 11

Group: 3

Verdict: ACCEPTED

input
-540305713988379 -580514137141...

correct output
1233409841814111

user output
1233409841814111

Test 12

Group: 3

Verdict: ACCEPTED

input
-156703691254895 -816797859965...

correct output
1086091541904259

user output
1086091541904259

Test 13

Group: 3

Verdict: ACCEPTED

input
-438806811971369 -748477242640...

correct output
1299874045296142

user output
1299874045296142

Test 14

Group: 3

Verdict: ACCEPTED

input
939351840049839 -9541207461566...

correct output
2118652567575152

user output
2118652567575152

Test 15

Group: 3

Verdict: ACCEPTED

input
-840127817790022 1113515308437...

correct output
1007774343975947

user output
1007774343975947