Submission #858568

# Submission time Handle Problem Language Result Execution time Memory
858568 2023-10-08T21:08:13 Z ofrankel Hamburg Steak (JOI20_hamburg) C++17
100 / 100
547 ms 21760 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) (x).begin(),(x).end()
void gtmn(int &x,int y){if(y<x)x=y;}
void gtmx(int &x,int y){if(y>x)x=y;}
vector<pair<int,int>>rec(int n,int k,vector<int>&l,vector<int>&d,vector<int>&r,vector<int>&u,vector<int>&was){
    int sx=n+1,sy=n+1,bx=0,by=0;for(int i=0;n>i;++i)if(!was[i]){sx=min(sx,r[i]);bx=max(bx,l[i]);sy=min(sy,u[i]);by=max(by,d[i]);}
    //small x,small y,big x,big y
    if(sx==n+1)sx=0;if(sy==n+1)sy=0;
    //for k=1,(sx,sy) should work
    if(k==1){for(int i=0;n>i;++i)if(!was[i])if(!(l[i]<=sx&&sx<=r[i]&&d[i]<=sy&&sy<=u[i]))return {};return {{sx,sy}};}
    
    //for other k values, one of the four options should work
    for(int x:{sx,bx})for(int y:{sy,by}){
        
        vector<int>was2=was;
        for(int i=0;n>i;++i)if(!was2[i])if(l[i]<=x&&x<=r[i]&&d[i]<=y&&y<=u[i])was2[i]=1;
        auto v=rec(n,k-1,l,d,r,u,was2);
        if(v.size()){v.push_back({x,y});return v;}
    }
    return {};
}
int main()
{
    int n,k;
    cin>>n>>k;
    vector<int>l(n),d(n),r(n),u(n);
    for(int i=0;n>i;++i)cin>>l[i]>>d[i]>>r[i]>>u[i];
    vector<int>indx=l;sort(all(indx));indx.resize(unique(all(indx))-indx.begin());
    vector<int>indy=d;sort(all(indy));indy.resize(unique(all(indy))-indy.begin());
    for(int i=0;n>i;++i){l[i]=lower_bound(all(indx),l[i])-indx.begin();r[i]=upper_bound(all(indx),r[i])-indx.begin()-1;
        d[i]=lower_bound(all(indy),d[i])-indy.begin();u[i]=upper_bound(all(indy),u[i])-indy.begin()-1;
    }
    vector<int>was(n,0);
    vector<pair<int,int>>toz=rec(n,k,l,d,r,u,was);
    if(toz.size()){for(auto&[i,j]:toz)cout<<indx[i]<<" "<<indy[j]<<endl;return 0;}
    
    int sx=n+1,sy=n+1,bx=0,by=0;for(int i=0;n>i;++i)if(!was[i]){sx=min(sx,r[i]);bx=max(bx,l[i]);sy=min(sy,u[i]);by=max(by,d[i]);}
    int h=indy.size(),w=indx.size();
    vector<int>type1(w,h),type2(w,h);
    int min_u=h;
    vector<int>to_down_st(h,0),to_down_en(h,h);
    vector<int>to_left_st(w,0),to_left_en(w,w),to_right_st(w,0),to_right_en(w,w);
    vector<int>type3(h,w),type4(h,0);
    int min1=sy+1,max1=by-1,min2=sx+1,max2=bx-1,min3=sy+1,max3=by-1,min4=sx+1,max4=bx-1;
    for(int i=0;n>i;++i){
        bool b1=l[i]<=sx,b2=d[i]<=sy,b3=r[i]>=bx,b4=u[i]>=by;
        if(b1+b2+b3+b4>=3)continue;
        if(b1+b2+b3+b4==1){
            if(b1){min1=max(min1,d[i]);max1=min(max1,u[i]);}
            else if(b3){min3=max(min3,d[i]);max3=min(max3,u[i]);}
            else if(b2){min2=max(min2,l[i]);max2=min(max2,r[i]);}
            else if(b4){min4=max(min4,l[i]);max4=min(max4,r[i]);}
        }
        else{
            if(b1&&b2)gtmn(type1[r[i]+1],u[i]);
            else if(b2&&b3)gtmn(type2[l[i]-1],u[i]);
            else if(b1&&b4)gtmn(type3[d[i]-1],r[i]);
            else if(b3&&b4)gtmx(type4[d[i]-1],l[i]);
            else if(b1&&b3){min_u=min(min_u,u[i]);gtmx(to_down_st[d[i]-1],d[i]);gtmn(to_down_en[d[i]-1],u[i]);}
            else if(b2&&b4){gtmn(to_left_en[l[i]-1],r[i]);gtmx(to_left_st[l[i]-1],l[i]);
                gtmn(to_right_en[r[i]+1],r[i]);gtmx(to_right_st[r[i]+1],l[i]);
            }
        }
    }
    for(int i=sx+1;bx>=i;++i){gtmn(type1[i],type1[i-1]);gtmn(to_right_en[i],to_right_en[i-1]);gtmx(to_right_st[i],to_right_st[i-1]);}
    for(int i=bx-1;i>=sx;--i){gtmn(type2[i],type2[i+1]);gtmn(to_left_en[i],to_left_en[i+1]);gtmx(to_left_st[i],to_left_st[i+1]);}
    for(int i=by-1;i>=sy;--i){gtmn(type3[i],type3[i+1]);gtmx(type4[i],type4[i+1]);
        gtmn(to_down_en[i],to_down_en[i+1]);gtmx(to_down_st[i],to_down_st[i+1]);}
    
    for(int num2=min2;max2>=num2;++num2){
        int cmax1=min(max1,type1[num2]);
        int cmax3=min(max3,type2[num2]);
        int cmin4=max({min4,to_right_st[num2],to_left_st[num2]});
        int cmax4=min({max4,to_right_en[num2],to_left_en[num2]});
        //opt1
        int num1=min(cmax1,min_u);
        int cmin3=max(min3,to_down_st[num1]);
        int num3=min(cmax3,to_down_en[num1]);
        if(num3>=cmin3&&num1>=min1){
            int ccmin4=max(cmin4,type4[num3]);
            int ccmax4=min(cmax4,type3[num1]);
            if(ccmax4>=ccmin4){toz={{sx,num1},{num2,sy},{bx,num3},{ccmin4,by}};}
        }
        //opt2
        num3=min(cmax3,min_u);
        int cmin1=max(min1,to_down_st[num3]);
        num1=min(cmax1,to_down_en[num3]);
        if(num3>=min3&&num1>=cmin1){
            int ccmin4=max(cmin4,type4[num3]);
            int ccmax4=min(cmax4,type3[num1]);
            if(ccmax4>=ccmin4){toz={{sx,num1},{num2,sy},{bx,num3},{ccmin4,by}};}
        }
    }
    if(toz.size()){for(auto&[i,j]:toz)cout<<indx[i]<<" "<<indy[j]<<endl;}
   // for(auto&[i,j]:toz)cout<<i<<" "<<j<<endl;
    //for(int i=0;n>i;++i){bool b=0;for(auto [j,q]:toz)if(l[i]<=j&&j<=r[i]&&d[i]<=q&&q<=u[i])b=1;if(!b)cout<<l[i]<<" "<<d[i]<<" "<<r[i]<<" "<<u[i]<<endl;}
    return 0;
}

Compilation message

hamburg.cpp: In function 'std::vector<std::pair<int, int> > rec(int, int, std::vector<int>&, std::vector<int>&, std::vector<int>&, std::vector<int>&, std::vector<int>&)':
hamburg.cpp:11:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   11 |     if(sx==n+1)sx=0;if(sy==n+1)sy=0;
      |     ^~
hamburg.cpp:11:21: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   11 |     if(sx==n+1)sx=0;if(sy==n+1)sy=0;
      |                     ^~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 348 KB Output is correct
2 Correct 3 ms 348 KB Output is correct
3 Correct 3 ms 348 KB Output is correct
4 Correct 3 ms 464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 500 KB Output is correct
2 Correct 3 ms 460 KB Output is correct
3 Correct 3 ms 600 KB Output is correct
4 Correct 3 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 344 KB Output is correct
2 Correct 3 ms 348 KB Output is correct
3 Correct 3 ms 348 KB Output is correct
4 Correct 3 ms 344 KB Output is correct
5 Correct 3 ms 348 KB Output is correct
6 Correct 3 ms 348 KB Output is correct
7 Correct 3 ms 348 KB Output is correct
8 Correct 3 ms 348 KB Output is correct
9 Correct 3 ms 348 KB Output is correct
10 Correct 3 ms 344 KB Output is correct
11 Correct 4 ms 348 KB Output is correct
12 Correct 4 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 348 KB Output is correct
2 Correct 3 ms 472 KB Output is correct
3 Correct 4 ms 348 KB Output is correct
4 Correct 3 ms 348 KB Output is correct
5 Correct 3 ms 348 KB Output is correct
6 Correct 3 ms 348 KB Output is correct
7 Correct 3 ms 348 KB Output is correct
8 Correct 3 ms 348 KB Output is correct
9 Correct 3 ms 348 KB Output is correct
10 Correct 3 ms 344 KB Output is correct
11 Correct 3 ms 348 KB Output is correct
12 Correct 3 ms 348 KB Output is correct
13 Correct 4 ms 460 KB Output is correct
14 Correct 4 ms 348 KB Output is correct
15 Correct 4 ms 348 KB Output is correct
16 Correct 4 ms 704 KB Output is correct
17 Correct 5 ms 604 KB Output is correct
18 Correct 4 ms 348 KB Output is correct
19 Correct 4 ms 540 KB Output is correct
20 Correct 5 ms 600 KB Output is correct
21 Correct 4 ms 348 KB Output is correct
22 Correct 4 ms 540 KB Output is correct
23 Correct 5 ms 604 KB Output is correct
24 Correct 4 ms 348 KB Output is correct
25 Correct 3 ms 540 KB Output is correct
26 Correct 4 ms 348 KB Output is correct
27 Correct 4 ms 544 KB Output is correct
28 Correct 4 ms 348 KB Output is correct
29 Correct 4 ms 448 KB Output is correct
30 Correct 3 ms 348 KB Output is correct
31 Correct 4 ms 604 KB Output is correct
32 Correct 4 ms 604 KB Output is correct
33 Correct 5 ms 604 KB Output is correct
34 Correct 5 ms 544 KB Output is correct
35 Correct 5 ms 604 KB Output is correct
36 Correct 5 ms 604 KB Output is correct
37 Correct 5 ms 604 KB Output is correct
38 Correct 4 ms 604 KB Output is correct
39 Correct 5 ms 604 KB Output is correct
40 Correct 5 ms 604 KB Output is correct
41 Correct 5 ms 548 KB Output is correct
42 Correct 5 ms 860 KB Output is correct
43 Correct 5 ms 600 KB Output is correct
44 Correct 4 ms 536 KB Output is correct
45 Correct 4 ms 348 KB Output is correct
46 Correct 5 ms 604 KB Output is correct
47 Correct 5 ms 604 KB Output is correct
48 Correct 5 ms 536 KB Output is correct
49 Correct 5 ms 600 KB Output is correct
50 Correct 5 ms 604 KB Output is correct
51 Correct 5 ms 604 KB Output is correct
52 Correct 4 ms 604 KB Output is correct
53 Correct 5 ms 608 KB Output is correct
54 Correct 4 ms 604 KB Output is correct
55 Correct 5 ms 604 KB Output is correct
56 Correct 5 ms 448 KB Output is correct
57 Correct 5 ms 552 KB Output is correct
58 Correct 5 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 348 KB Output is correct
2 Correct 3 ms 348 KB Output is correct
3 Correct 3 ms 348 KB Output is correct
4 Correct 3 ms 464 KB Output is correct
5 Correct 294 ms 5888 KB Output is correct
6 Correct 304 ms 5900 KB Output is correct
7 Correct 297 ms 5712 KB Output is correct
8 Correct 312 ms 5904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 500 KB Output is correct
2 Correct 3 ms 460 KB Output is correct
3 Correct 3 ms 600 KB Output is correct
4 Correct 3 ms 348 KB Output is correct
5 Correct 312 ms 6484 KB Output is correct
6 Correct 308 ms 6672 KB Output is correct
7 Correct 311 ms 6484 KB Output is correct
8 Correct 314 ms 6668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 344 KB Output is correct
2 Correct 3 ms 348 KB Output is correct
3 Correct 3 ms 348 KB Output is correct
4 Correct 3 ms 344 KB Output is correct
5 Correct 3 ms 348 KB Output is correct
6 Correct 3 ms 348 KB Output is correct
7 Correct 3 ms 348 KB Output is correct
8 Correct 3 ms 348 KB Output is correct
9 Correct 3 ms 348 KB Output is correct
10 Correct 3 ms 344 KB Output is correct
11 Correct 4 ms 348 KB Output is correct
12 Correct 4 ms 600 KB Output is correct
13 Correct 314 ms 7440 KB Output is correct
14 Correct 310 ms 7404 KB Output is correct
15 Correct 321 ms 7404 KB Output is correct
16 Correct 327 ms 7252 KB Output is correct
17 Correct 341 ms 7432 KB Output is correct
18 Correct 308 ms 7248 KB Output is correct
19 Correct 316 ms 7252 KB Output is correct
20 Correct 366 ms 7440 KB Output is correct
21 Correct 350 ms 7472 KB Output is correct
22 Correct 335 ms 7488 KB Output is correct
23 Correct 326 ms 7476 KB Output is correct
24 Correct 335 ms 7436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 348 KB Output is correct
2 Correct 3 ms 472 KB Output is correct
3 Correct 4 ms 348 KB Output is correct
4 Correct 3 ms 348 KB Output is correct
5 Correct 3 ms 348 KB Output is correct
6 Correct 3 ms 348 KB Output is correct
7 Correct 3 ms 348 KB Output is correct
8 Correct 3 ms 348 KB Output is correct
9 Correct 3 ms 348 KB Output is correct
10 Correct 3 ms 344 KB Output is correct
11 Correct 3 ms 348 KB Output is correct
12 Correct 3 ms 348 KB Output is correct
13 Correct 4 ms 460 KB Output is correct
14 Correct 4 ms 348 KB Output is correct
15 Correct 4 ms 348 KB Output is correct
16 Correct 4 ms 704 KB Output is correct
17 Correct 5 ms 604 KB Output is correct
18 Correct 4 ms 348 KB Output is correct
19 Correct 4 ms 540 KB Output is correct
20 Correct 5 ms 600 KB Output is correct
21 Correct 4 ms 348 KB Output is correct
22 Correct 4 ms 540 KB Output is correct
23 Correct 5 ms 604 KB Output is correct
24 Correct 4 ms 348 KB Output is correct
25 Correct 3 ms 540 KB Output is correct
26 Correct 4 ms 348 KB Output is correct
27 Correct 4 ms 544 KB Output is correct
28 Correct 4 ms 348 KB Output is correct
29 Correct 4 ms 448 KB Output is correct
30 Correct 3 ms 348 KB Output is correct
31 Correct 4 ms 604 KB Output is correct
32 Correct 4 ms 604 KB Output is correct
33 Correct 5 ms 604 KB Output is correct
34 Correct 5 ms 544 KB Output is correct
35 Correct 5 ms 604 KB Output is correct
36 Correct 5 ms 604 KB Output is correct
37 Correct 5 ms 604 KB Output is correct
38 Correct 4 ms 604 KB Output is correct
39 Correct 5 ms 604 KB Output is correct
40 Correct 5 ms 604 KB Output is correct
41 Correct 5 ms 548 KB Output is correct
42 Correct 5 ms 860 KB Output is correct
43 Correct 5 ms 600 KB Output is correct
44 Correct 4 ms 536 KB Output is correct
45 Correct 4 ms 348 KB Output is correct
46 Correct 5 ms 604 KB Output is correct
47 Correct 5 ms 604 KB Output is correct
48 Correct 5 ms 536 KB Output is correct
49 Correct 5 ms 600 KB Output is correct
50 Correct 5 ms 604 KB Output is correct
51 Correct 5 ms 604 KB Output is correct
52 Correct 4 ms 604 KB Output is correct
53 Correct 5 ms 608 KB Output is correct
54 Correct 4 ms 604 KB Output is correct
55 Correct 5 ms 604 KB Output is correct
56 Correct 5 ms 448 KB Output is correct
57 Correct 5 ms 552 KB Output is correct
58 Correct 5 ms 604 KB Output is correct
59 Correct 342 ms 16132 KB Output is correct
60 Correct 306 ms 15952 KB Output is correct
61 Correct 330 ms 15968 KB Output is correct
62 Correct 325 ms 15844 KB Output is correct
63 Correct 311 ms 15928 KB Output is correct
64 Correct 306 ms 15956 KB Output is correct
65 Correct 318 ms 15956 KB Output is correct
66 Correct 342 ms 15792 KB Output is correct
67 Correct 369 ms 16008 KB Output is correct
68 Correct 340 ms 15980 KB Output is correct
69 Correct 337 ms 15952 KB Output is correct
70 Correct 330 ms 15948 KB Output is correct
71 Correct 490 ms 16168 KB Output is correct
72 Correct 510 ms 21472 KB Output is correct
73 Correct 467 ms 16020 KB Output is correct
74 Correct 413 ms 16232 KB Output is correct
75 Correct 475 ms 21492 KB Output is correct
76 Correct 410 ms 16020 KB Output is correct
77 Correct 476 ms 16144 KB Output is correct
78 Correct 523 ms 21760 KB Output is correct
79 Correct 475 ms 16020 KB Output is correct
80 Correct 437 ms 16024 KB Output is correct
81 Correct 509 ms 21484 KB Output is correct
82 Correct 433 ms 16020 KB Output is correct
83 Correct 362 ms 16020 KB Output is correct
84 Correct 377 ms 16048 KB Output is correct
85 Correct 401 ms 15944 KB Output is correct
86 Correct 379 ms 16000 KB Output is correct
87 Correct 385 ms 16252 KB Output is correct
88 Correct 389 ms 15956 KB Output is correct
89 Correct 493 ms 21720 KB Output is correct
90 Correct 496 ms 21484 KB Output is correct
91 Correct 491 ms 21496 KB Output is correct
92 Correct 475 ms 20684 KB Output is correct
93 Correct 490 ms 21348 KB Output is correct
94 Correct 512 ms 21476 KB Output is correct
95 Correct 484 ms 21540 KB Output is correct
96 Correct 487 ms 21372 KB Output is correct
97 Correct 497 ms 21608 KB Output is correct
98 Correct 519 ms 21464 KB Output is correct
99 Correct 464 ms 21488 KB Output is correct
100 Correct 500 ms 21452 KB Output is correct
101 Correct 494 ms 21364 KB Output is correct
102 Correct 472 ms 21500 KB Output is correct
103 Correct 496 ms 21356 KB Output is correct
104 Correct 502 ms 21368 KB Output is correct
105 Correct 504 ms 21480 KB Output is correct
106 Correct 505 ms 21716 KB Output is correct
107 Correct 506 ms 21468 KB Output is correct
108 Correct 498 ms 21336 KB Output is correct
109 Correct 484 ms 21448 KB Output is correct
110 Correct 532 ms 21476 KB Output is correct
111 Correct 547 ms 21280 KB Output is correct
112 Correct 505 ms 21488 KB Output is correct
113 Correct 541 ms 21236 KB Output is correct
114 Correct 493 ms 21088 KB Output is correct
115 Correct 490 ms 21160 KB Output is correct
116 Correct 483 ms 21272 KB Output is correct
117 Correct 469 ms 16076 KB Output is correct
118 Correct 472 ms 16100 KB Output is correct
119 Correct 476 ms 16380 KB Output is correct
120 Correct 469 ms 16088 KB Output is correct
121 Correct 474 ms 16104 KB Output is correct
122 Correct 471 ms 16504 KB Output is correct
123 Correct 481 ms 16100 KB Output is correct
124 Correct 469 ms 16080 KB Output is correct
125 Correct 481 ms 16232 KB Output is correct
126 Correct 486 ms 16320 KB Output is correct