QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#706683#302. Find the PathTheZone100 ✓732ms132372kbC++237.7kb2024-11-03 12:57:532024-11-03 12:57:55

Judging History

你现在查看的是最新测评结果

  • [2024-11-03 12:57:55]
  • 评测
  • 测评结果:100
  • 用时:732ms
  • 内存:132372kb
  • [2024-11-03 12:57:53]
  • 提交

answer

/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
- insert(x),erase(x)
- find_by_order(k): return iterator to the k-th smallest element
- order_of_key(x): the number of elements that are strictly smaller
*/
#include<bits/stdc++.h>
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
uniform_real_distribution<> pp(0.0,1.0);
#define ll long long
#define ld long double
#define pii pair<int,int>
#define piii pair<int,pii>
#define mpp make_pair
#define fi first
#define se second
const int inf = 1e9;
const int mod=998244353;
const int maxn=4005;
const int B=650;
const int maxs=100005;
const int maxm=200005;
const int maxq=1000005;
const int maxl=25;
const int maxa=1000000;
const int root=3;
const int base=101;

int dx[]={0,0,1,-1},
    dy[]={1,-1,0,0};

int n,X,Y,U,V;

int sx,sy;
vector<int> cx,cy;
struct rec{
    int x,y,u,v;
}R[maxn];
int a[maxn][maxn],f[maxn][maxn],cnt;
vector<int> edge[maxs];
pii pos[maxs];

void solve(){
    cin >> X >> Y >> U >> V >> n;

    cnt=0;
    cx.clear();
    cy.clear();

    cx.push_back(X);
    cx.push_back(X+1);
    cx.push_back(U);
    cx.push_back(U+1);
    cy.push_back(Y);
    cy.push_back(Y+1);
    cy.push_back(V);
    cy.push_back(V+1);

    for(int i=1;i<=n;i++){
        int x,y,u,v;cin >> x >> y >> u >> v;
        if(x>u) swap(x,u);
        if(y>v) swap(y,v);
        cx.push_back(x);
        cx.push_back(x+1);
        cx.push_back(u);
        cx.push_back(u+1);
        cy.push_back(y);
        cy.push_back(y+1);
        cy.push_back(v);
        cy.push_back(v+1);
        R[i]={x,y,u,v};
    }
    sort(cx.begin(),cx.end());
    sort(cy.begin(),cy.end());
    cx.erase(unique(cx.begin(),cx.end()),cx.end());
    cy.erase(unique(cy.begin(),cy.end()),cy.end());
    sx=(int)cx.size()-1;sy=(int)cy.size()-1;

    X=lower_bound(cx.begin(),cx.end(),X)-cx.begin();
    Y=lower_bound(cy.begin(),cy.end(),Y)-cy.begin();
    U=lower_bound(cx.begin(),cx.end(),U)-cx.begin();
    V=lower_bound(cy.begin(),cy.end(),V)-cy.begin();
    //cout << X << ' ' << Y << ' ' << U << ' ' << V << '\n';

    for(int i=0;i<sx;i++) for(int j=0;j<sy;j++){
        a[i][j]=f[i][j]=0;
    }

    auto add = [&](int x,int y,int u,int v,int val){
        a[x][y]+=val;a[x][v+1]-=val;
        a[u+1][y]-=val;a[u+1][v+1]+=val;
    };
    for(int i=1;i<=n;i++){
        int x=R[i].x,y=R[i].y,u=R[i].u,v=R[i].v;
        x=lower_bound(cx.begin(),cx.end(),x)-cx.begin();
        y=lower_bound(cy.begin(),cy.end(),y)-cy.begin();
        u=lower_bound(cx.begin(),cx.end(),u)-cx.begin();
        v=lower_bound(cy.begin(),cy.end(),v)-cy.begin();
        R[i]={x,y,u,v};
        add(x,y,u,v,i);
        add(x+1,y+1,u-1,v-1,-i);
    }
    for(int i=0;i<sx;i++) for(int j=0;j<sy;j++){
        if(i) a[i][j]+=a[i-1][j];
        if(j) a[i][j]+=a[i][j-1];
        if(i && j) a[i][j]-=a[i-1][j-1];
    }

    if(!a[X][Y]) a[X][Y]=n+1;
    if(!a[U][V]) a[U][V]=n+2;

    for(int i=0;i<sx;i++){
        int p=-1;
        for(int j=0;j<sy;j++){
            if(a[i][j]){
                f[i][j]=++cnt;
                pos[cnt]={i,j};
                edge[cnt].clear();
                //cout << "pos " << cnt << ' ' << i << ' ' << j << '\n';
            }
            if(!a[i][j]) continue;
            if(p==-1){p=j;continue;}
            int id=a[i][j];
            if(id!=a[i][p] || i==R[id].x || i==R[id].u){
                edge[f[i][j]].push_back(f[i][p]);
                //cout << "edge " << f[i][j] << ' ' << f[i][p] << '\n';
            }
            p=j;
        }
        p=-1;
        for(int j=sy-1;j>=0;j--){
            if(!a[i][j]) continue;
            if(p==-1){p=j;continue;}
            int id=a[i][j];
            if(id!=a[i][p] || i==R[id].x || i==R[id].u){
                edge[f[i][j]].push_back(f[i][p]);
                //cout << "edge " << f[i][j] << ' ' << f[i][p] << '\n';
            }
            p=j;
        }
    }
    for(int j=0;j<sy;j++){
        int p=-1;
        for(int i=0;i<sx;i++){
            if(!a[i][j]) continue;
            if(p==-1){p=i;continue;}
            int id=a[i][j];
            if(id!=a[p][j] || j==R[id].y || j==R[id].v) edge[f[i][j]].push_back(f[p][j]);
            p=i;
        }
        p=-1;
        for(int i=sx-1;i>=0;i--){
            if(!a[i][j]) continue;
            if(p==-1){p=i;continue;}
            int id=a[i][j];
            if(id!=a[p][j] || j==R[id].y || j==R[id].v) edge[f[i][j]].push_back(f[p][j]);
            p=i;
        }
    }

    assert(cnt<=100000);
    for(int i=0;i<sx;i++) for(int j=0;j<sy;j++) a[i][j]=inf;

    a[X][Y]=0;
    X=f[X][Y];U=f[U][V];
    priority_queue<pair<ll,int>,vector<pair<ll,int>>,greater<pair<ll,int>>> pq;
    pq.push({0,X});
    while(!pq.empty()){
        auto [d,u]=pq.top();pq.pop();
        auto [x,y]=pos[u];
        //cout << d << ' ' << u << ' ' << x << ' ' << y << '\n';
        if(u==U){
            cout << d << '\n';
            return;
        }
        if(a[x][y]!=d) continue;
        for(int v:edge[u]){
            auto [xv,yv]=pos[v];
            int dv=d+abs(cx[xv]-cx[x])+abs(cy[yv]-cy[y]);
            //cout << "edge " << u << ' ' << v << '\n';
            if(a[xv][yv]>dv){
                pq.push({a[xv][yv]=dv,v});
            }
        }
    }
    cout << "No Path\n";

}


signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    int test=1;cin >> test;
    while(test--) solve();
}






















































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































詳細信息


Pretests


Final Tests

Test #1:

score: 5
Accepted
time: 0ms
memory: 9812kb

input:

4

47 36 44 29
5
15 29 24 42
37 18 46 25
7 15 20 20
26 17 28 19
38 0 40 4

36 42 19 21
5
23 19 26 21
5 7 9 13
32 9 39 12
30 29 37 34
0 13 2 15

23 0 32 2
5
31 22 40 32
27 5 36 7
37 17 41 19
19 32 26 34
10 3 13 12

26 11 1 22
5
31 32 36 34
12 29 21 36
10 6 14 14
36 5 37 11
1 10 5 16

output:

50
No Path
79
36

result:

ok 4 lines

Test #2:

score: 5
Accepted
time: 2ms
memory: 7828kb

input:

7

44 62 8 23
7
30 19 43 25
3 4 18 13
79 49 83 59
10 61 25 68
40 34 46 41
58 51 59 60
72 13 80 29

48 55 59 34
7
57 5 67 10
20 24 28 36
38 5 45 9
52 40 55 47
43 16 53 24
8 26 11 34
27 38 29 43

14 40 59 26
7
33 14 41 21
70 7 73 17
66 35 73 41
39 1 59 4
30 55 38 57
6 4 12 10
24 39 32 45

49 73 37 40
...

output:

75
92
133
No Path
93
80
16

result:

ok 7 lines

Test #3:

score: 5
Accepted
time: 2ms
memory: 9892kb

input:

6

77 16 30 97
10
46 41 55 50
36 11 38 18
75 41 82 58
5 75 15 85
37 64 48 87
99 32 106 40
47 4 70 14
68 71 77 78
0 105 14 114
7 29 32 41

32 70 0 30
10
46 48 48 58
17 55 23 66
61 98 68 111
42 80 44 83
74 43 92 65
3 98 9 105
79 84 100 88
29 95 41 102
26 23 35 27
25 41 40 42

1 73 77 110
10
98 108 108...

output:

128
No Path
125
192
129
189

result:

ok 6 lines

Test #4:

score: 5
Accepted
time: 2ms
memory: 11864kb

input:

5

48 44 65 60
10
7 35 20 40
8 65 21 71
4 87 5 92
29 26 33 36
92 17 95 28
50 65 53 76
47 17 69 29
34 4 53 6
86 71 101 87
8 53 18 55

82 56 114 35
10
36 24 60 32
10 80 19 87
15 57 17 59
75 22 86 26
9 29 19 38
82 75 109 79
44 60 56 73
3 101 17 104
38 0 60 12
62 88 71 103

117 78 66 5
10
74 94 86 98
6 ...

output:

63
197
124
70
160

result:

ok 5 lines

Test #5:

score: 5
Accepted
time: 2ms
memory: 9832kb

input:

7

149 96 5 66
15
109 123 120 125
79 102 90 109
70 20 84 24
11 46 16 51
111 37 134 43
52 110 54 122
42 81 56 85
1 107 3 117
118 97 136 103
105 51 113 69
80 38 95 49
96 123 100 130
50 56 59 61
16 18 26 33
134 85 150 90

88 22 104 132
15
111 13 130 17
31 33 51 49
36 81 55 92
125 64 129 86
48 8 69 26
3...

output:

220
198
136
52
114
104
118

result:

ok 7 lines

Test #6:

score: 5
Accepted
time: 2ms
memory: 9836kb

input:

5

73 127 198 154
25
64 39 74 50
111 57 126 82
155 3 173 20
170 123 176 127
17 104 36 113
54 89 63 104
174 50 189 61
85 90 87 109
87 9 95 24
18 126 23 127
153 137 167 141
14 62 27 82
190 20 193 21
99 91 105 101
112 1 128 6
13 11 28 27
156 84 169 90
170 35 179 40
126 142 134 152
70 15 72 34
110 20 11...

output:

No Path
234
183
81
106

result:

ok 5 lines

Test #7:

score: 5
Accepted
time: 3ms
memory: 12080kb

input:

7

240 260 223 151
40
183 109 192 119
100 17 107 27
173 144 181 168
52 238 56 253
11 11 15 29
27 3 40 7
209 123 221 133
132 79 143 89
65 184 76 198
216 162 233 193
200 28 213 41
10 208 17 209
231 18 236 32
181 31 190 49
234 137 243 138
172 192 181 206
101 63 107 68
52 110 63 125
65 3 66 7
14 65 21 7...

output:

290
124
No Path
226
273
132
187

result:

ok 7 lines

Test #8:

score: 5
Accepted
time: 0ms
memory: 14164kb

input:

7

59 320 269 338
55
190 227 205 241
111 350 120 355
194 137 214 154
339 64 354 85
288 279 298 295
213 280 229 286
172 291 190 310
338 120 339 127
13 338 19 354
219 38 223 63
34 214 44 216
322 161 334 172
47 226 49 239
13 81 31 102
319 305 327 308
268 65 288 84
147 171 164 177
150 148 164 160
314 21...

output:

No Path
No Path
189
163
278
351
339

result:

ok 7 lines

Test #9:

score: 5
Accepted
time: 0ms
memory: 13980kb

input:

3

248 80 0 469
80
225 436 230 449
486 179 497 185
109 63 117 69
167 217 185 219
1 262 2 280
368 247 374 262
488 241 497 246
450 147 451 166
370 78 387 100
383 151 384 153
459 62 465 71
310 257 329 260
43 410 66 418
30 324 42 337
93 0 103 20
487 313 506 318
44 457 72 466
377 214 388 229
131 118 145 ...

output:

No Path
853
255

result:

ok 3 lines

Test #10:

score: 5
Accepted
time: 7ms
memory: 16124kb

input:

7

113 28 68 49
100
298 564 309 579
52 220 57 233
58 125 63 138
461 62 464 69
36 122 47 124
384 554 401 567
165 412 183 415
171 145 188 156
319 140 338 145
546 408 550 419
540 513 543 514
536 113 539 122
386 421 395 439
546 333 561 354
120 464 138 468
99 342 108 353
53 16 59 23
96 568 98 582
201 352...

output:

858
622
587
403
612
724
403

result:

ok 7 lines

Test #11:

score: 5
Accepted
time: 0ms
memory: 18120kb

input:

3

220 191 32 301
100
264 123 267 130
152 291 160 293
448 206 454 218
162 268 175 274
219 125 230 136
191 521 194 532
382 241 383 252
529 69 549 81
197 69 198 75
174 320 179 323
19 64 32 67
144 420 151 436
21 160 35 184
127 250 129 255
270 454 281 457
20 10 21 35
235 237 240 252
501 498 521 505
257 ...

output:

334
425
361

result:

ok 3 lines

Test #12:

score: 5
Accepted
time: 3ms
memory: 20384kb

input:

7

192 243 410 479
100
512 532 524 537
267 225 273 228
184 151 186 165
43 275 50 280
383 108 391 114
26 137 38 140
70 70 78 95
148 452 154 466
380 301 389 311
385 257 398 261
29 159 40 163
257 303 272 318
285 43 289 59
311 569 314 578
276 370 289 384
145 274 150 280
311 403 313 413
152 176 169 202
6...

output:

704
373
722
577
No Path
387
563

result:

ok 7 lines

Test #13:

score: 5
Accepted
time: 9ms
memory: 22604kb

input:

7

30716 132781 1857 233375
111
493748 402938 514241 412084
579900 18029 580467 31831
469392 99035 470980 114848
214206 581649 222349 594497
238814 399332 257195 407773
107848 101271 119245 115769
266668 272949 272296 280858
147475 287326 161255 297415
226559 53188 232324 65217
152458 345111 157051 ...

output:

650891
343157
636310
572657
246156
732168
401322

result:

ok 7 lines

Test #14:

score: 5
Accepted
time: 36ms
memory: 32848kb

input:

7

797769 776056 464965 316876
200
530067 661903 540423 677854
248107 465156 256864 465613
78746 596314 94822 606569
593980 634945 603952 645399
407706 147178 417802 148474
444868 398425 446937 414247
792106 258355 793081 265950
15691 306373 28071 314872
338760 265451 349609 266498
90679 634926 9216...

output:

795348
960332
No Path
400748
970395
502375
1350448

result:

ok 7 lines

Test #15:

score: 5
Accepted
time: 58ms
memory: 47536kb

input:

6

233530 101006 1056613 370809
300
664858 1047306 671419 1049258
920567 568305 934855 575596
1386601 260305 1398400 272722
489687 346811 495180 351832
1169269 1037772 1178358 1047022
1301705 316274 1311427 316647
1157149 1285696 1161619 1287262
431414 480788 432174 492647
165409 1065038 178670 1072...

output:

1922626
1150202
No Path
1978897
1208380
1289792

result:

ok 6 lines

Test #16:

score: 5
Accepted
time: 125ms
memory: 66340kb

input:

6

728140 1536447 2103706 1397474
444
1602427 58765 1604252 68971
1475435 1085524 1491668 1104539
132775 788753 146103 798673
1199024 840972 1214918 853109
888792 1820279 890348 1834927
1803419 344569 1818838 362655
403403 833631 407675 838517
1461930 258203 1464917 273103
1082120 278356 1083005 292...

output:

3036307
637213
1516491
2152173
No Path
1627705

result:

ok 6 lines

Test #17:

score: 5
Accepted
time: 333ms
memory: 93124kb

input:

7

1077790 2183871 3089356 2916641
666
1121544 1354697 1138760 1357827
1175093 503348 1188277 509541
1065892 2198645 1076208 2201823
2062773 2282678 2074079 2283768
1976039 174478 1979012 178178
49247 1878976 50249 1882090
431234 977321 442543 987328
2769738 848853 2772136 856905
285662 850116 28653...

output:

4662996
2200435
4815782
1733875
1977754
No Path
4467785

result:

ok 7 lines

Test #18:

score: 5
Accepted
time: 345ms
memory: 108256kb

input:

5

1259784 3335016 3151280 3350075
789
547992 1124401 560479 1131859
923471 1386892 932373 1391749
1810441 1612764 1816920 1619360
467107 548823 479792 554516
1867913 778580 1876828 792669
2721257 2436181 2726743 2448684
1368889 1939745 1376020 1951708
2622447 2285734 2639290 2292684
2365513 1840011...

output:

2864213
2715546
4867307
2857653
3111673

result:

ok 5 lines

Test #19:

score: 5
Accepted
time: 732ms
memory: 131892kb

input:

6

3185804 829871 1608955 3605090
999
3730847 3784806 3735258 3800021
2087276 4356546 2092201 4357957
4537037 50688 4553120 57933
1782841 1583787 1784704 1590383
2783960 1146796 2789337 1147637
2771241 3743469 2782829 3753271
1003103 3635705 1017706 3642111
200851 3229437 221333 3243869
2279278 1857...

output:

5114662
2805437
5242874
3280419
7102667
6574589

result:

ok 6 lines

Test #20:

score: 5
Accepted
time: 732ms
memory: 132372kb

input:

6

345725 4518985 2338295 4611521
1000
3449670 2680878 3455019 2696301
1438554 1792976 1444138 1805577
4096971 518517 4110653 523372
1593790 1811464 1605002 1824952
1137120 3151830 1139929 3169095
933765 2894529 934740 2901796
31217 2512522 43232 2517264
1638568 2302114 1647055 2313608
175481 172109...

output:

6945670
4591509
No Path
5219156
5244786
6010298

result:

ok 6 lines