QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#282491#6420. Certain Scientific RailgunjrjyyAC ✓47ms128416kbC++203.6kb2023-12-12 10:41:492023-12-12 10:41:50

Judging History

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

  • [2023-12-12 10:41:50]
  • 评测
  • 测评结果:AC
  • 用时:47ms
  • 内存:128416kb
  • [2023-12-12 10:41:49]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;

constexpr i64 inf = 1e18;

template <typename T, typename Cmp = std::less<T>>
struct RMQ {
    const Cmp cmp{};
    int n;
    std::vector<std::vector<T>> a;
    RMQ() = default;
    RMQ(const std::vector<T> &v) {
        init(v);
    }
    RMQ(const std::vector<T> &v, const Cmp &cmp_) : cmp{cmp_} {
        init(v);
    }
    void init(const std::vector<T> &ini) {
        n = int(ini.size());
        int lg = std::__lg(n);
        a.assign(lg + 1, std::vector<T>(n));
        a[0] = ini;
        for (int i = 0; i < lg; ++i) {
            for (int j = 0; j <= n - (2 << i); ++j) {
                a[i + 1][j] = std::min(a[i][j], a[i][j + (1 << i)], cmp);
            }
        }
    }
    T operator()(int l, int r) const {
        int k = std::__lg(r - l);
        return std::min(a[k][l], a[k][r - (1 << k)], cmp);
    }
};

void solve() {
    int n;
    std::cin >> n;

    std::vector<i64> x(n), y(n);
    for (int i = 0; i < n; ++i) {
        std::cin >> x[i] >> y[i];
    }
    x.push_back(0);
    y.push_back(0);
    ++n;

    auto get = [&](const std::vector<int> &id) {
        std::vector<i64> v;
        for (auto i : id) {
            v.push_back(std::abs(x[i]));
        }
        std::sort(v.begin(), v.end());
        v.erase(std::unique(v.begin(), v.end()), v.end());

        std::vector<i64> l(v.size() + 1), r(v.size() + 1);
        for (auto i : id) {
            int nx = std::lower_bound(v.begin(), v.end(), std::abs(x[i])) - v.begin();
            l[nx] = std::min(l[nx], y[i]);
            r[nx] = std::max(r[nx], y[i]);
        }
        for (int i = int(v.size()) - 1; i >= 0; --i) {
            l[i] = std::min(l[i], l[i + 1]);
            r[i] = std::max(r[i], r[i + 1]);
        }
        return std::make_tuple(v, l, r);
    };
    std::vector<int> pos, neg{n - 1};
    for (int i = 0; i < n; ++i) {
        if (x[i] >= 0) {
            pos.push_back(i);
        } else {
            neg.push_back(i);
        }
    }
    auto [pv, pl, pr] = get(pos);
    auto [nv, nl, nr] = get(neg);

    auto calc = [&](int s, i64 v, i64 l, i64 r) {
        return (s / 4 % 2 + 1) * v + s / 2 % 2 * (1 + s / 8) * r - s % 2 * (2 - s / 8) * l;
    };
    std::vector<i64> f(nv.size());
    std::array<RMQ<i64>, 16> rmq;
    for (int s = 0; s < 16; ++s) {
        for (int i = 0; i < int(nv.size()); ++i) {
            f[i] = calc(s, nv[i], nl[i + 1], nr[i + 1]);
        }
        rmq[s].init(f);
    }

    i64 ans = inf;
    for (int i = 0, jl = 0, jr = 0, kl = 0, kr = 0; i < int(pv.size()); ++i) {
        while (jl < int(nv.size()) && pl[i + 1] > nl[jl + 1]) {
            ++jl;
        }
        while (kl < int(nv.size()) && pl[i + 1] >= nl[kl + 1]) {
            ++kl;
        }
        while (jr < int(nv.size()) && pr[i + 1] < nr[jr + 1]) {
            ++jr;
        }
        while (kr < int(nv.size()) && pr[i + 1] <= nr[kr + 1]) {
            ++kr;
        }
        for (int s = 0; s < 16; ++s) {
            int ql = std::max(s % 2 ? 0 : jl, s / 2 % 2 ? 0 : jr);
            int qr = std::min(s % 2 ? kl : int(nv.size()), s / 2 % 2 ? kr : int(nv.size()));
            if (ql < qr) {
                i64 res = rmq[s](ql, qr) + calc(s ^ 7, pv[i], pl[i + 1], pr[i + 1]);
                ans = std::min(ans, res);
            }
        }
    }

    std::cout << ans << "\n";
}

int main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);

    int t;
    std::cin >> t;

    while (t--) {
        solve();
    }

    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3448kb

input:

3
2
0 1
1 0
4
1 1
-3 -3
4 -4
-2 2
4
1 100
3 100
-100 1
3 -100

output:

0
8
4

result:

ok 3 number(s): "0 8 4"

Test #2:

score: 0
Accepted
time: 1ms
memory: 3452kb

input:

120
4
11 11
-22 -22
33 -33
-44 44
4
-11 11
22 -22
-33 -33
44 44
4
-11 -11
22 22
-33 33
44 -44
4
11 -11
-22 22
33 33
-44 -44
4
-11 11
22 -22
33 33
-44 -44
4
11 11
-22 -22
-33 33
44 -44
4
11 -11
-22 22
-33 -33
44 44
4
-11 -11
22 22
33 -33
-44 44
4
1 1
-2 -2
3 -3
-4 4
4
-1 1
2 -2
-3 -3
4 4
4
-1 -1
2 2
...

output:

99
99
99
99
99
99
99
99
9
9
9
9
9
9
9
9
5
5
5
5
5
5
5
5
4
4
4
4
4
4
4
4
0
0
0
0
0
0
0
0
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
12
12
12
12
12
12
12
12
0
0
0
0
0
0
0
0
11
11
11
11
11
11
11
11
111
111
111
111
111
111
111
111
300
300
300
300
300
300
300
300
5
5
5
5
5
5
5
5
2
2
2
2
2
2
2
2
48
48
48
48
48
48
48...

result:

ok 120 numbers

Test #3:

score: 0
Accepted
time: 36ms
memory: 10384kb

input:

1
100000
259716243 240441467
199457754 301066970
412772262 87809313
139833960 359731944
453667163 46926477
204936243 294432582
361538994 138967777
315714786 184515556
274799986 224851893
315004381 184713646
104778725 394583171
100521423 399036944
392072619 107037839
19992953 480118328
166443446 3325...

output:

500573654

result:

ok 1 number(s): "500573654"

Test #4:

score: 0
Accepted
time: 28ms
memory: 108780kb

input:

1
100000
231900334 268958119
8094073 491651466
181235767 318579810
152422229 348402615
453103138 47722205
169802544 329762126
66615606 433110853
63974760 435613334
412296501 86899836
380626267 120285438
234254409 265425647
343489254 155604658
456112096 43577922
389402607 110812358
361862073 13733807...

output:

500924008

result:

ok 1 number(s): "500924008"

Test #5:

score: 0
Accepted
time: 37ms
memory: 108880kb

input:

1
100000
243687982 257271514
406262214 92746823
115474686 384860427
179141355 321775276
111057575 388052482
85754748 413966937
89015940 411624061
97395736 402451385
394966118 105231558
118633450 381544518
121485690 377952946
93095255 406155875
285623743 214726445
230422095 268632132
137612706 361757...

output:

1001565829

result:

ok 1 number(s): "1001565829"

Test #6:

score: 0
Accepted
time: 43ms
memory: 108904kb

input:

1
100000
450661745 49199859
46152367 453651505
490854754 8343231
273253564 227017249
177014519 322480838
295759745 203932936
261849726 238301630
64867210 434153377
105275689 393750931
321581727 178232552
494756932 6104054
31280896 468013695
46903507 453060294
162085346 337538866
239622896 259681652
...

output:

1001452257

result:

ok 1 number(s): "1001452257"

Test #7:

score: 0
Accepted
time: 47ms
memory: 109140kb

input:

1
100000
150929109 349489222
6420724 493823770
464662613 35982721
402411960 97813587
40230561 460488339
470554995 30114056
344276320 154962447
219702967 280132033
104632512 395604723
479497355 19601658
117766907 383020273
36133778 463836986
175546685 324588426
43749701 456053065
130176612 369176811
...

output:

1501257683

result:

ok 1 number(s): "1501257683"

Test #8:

score: 0
Accepted
time: 29ms
memory: 21552kb

input:

8
10000
108715119 390304225
45853335 454319274
399590321 101245208
273660814 225432152
97670975 403201669
476219330 23861282
58911490 440808774
128367407 370649954
306462809 192987122
396996806 103458329
129292472 370369176
135445802 364903545
9556524 491441067
272140862 228185209
394219336 10628059...

output:

499504970
499504970
499504970
499504970
499504970
499504970
499504970
499504970

result:

ok 8 numbers

Test #9:

score: 0
Accepted
time: 15ms
memory: 12124kb

input:

8
10000
88475214 411685526
295971508 203464188
475025201 25141393
312127126 188194692
19542945 480366130
488061278 12628967
186934840 313365863
481957571 18601403
358812248 140919718
22594424 477659644
217700756 281415099
165264320 334364787
117366326 382623925
104117966 396054687
249226386 25018520...

output:

1000322231
1000322231
1000322231
1000322231
1000322231
1000322231
1000322231
1000322231

result:

ok 8 numbers

Test #10:

score: 0
Accepted
time: 23ms
memory: 12120kb

input:

8
10000
269878538 230854020
456626682 43333438
434144544 66494813
232368812 267733469
68096662 431017351
17852539 482545915
87803855 412171111
265856716 234461260
115315829 384621424
31746172 467793722
373607866 126486181
23907446 476867982
333063328 166736814
343578833 156172707
244551435 255629961...

output:

1000479661
1000479661
1000479661
1000479661
1000479661
1000479661
1000479661
1000479661

result:

ok 8 numbers

Test #11:

score: 0
Accepted
time: 36ms
memory: 16884kb

input:

8
10000
159480657 340897530
349562243 150040650
173755554 326392525
282856769 217394893
392793822 107352227
491621792 7387423
440310768 58951882
187152471 313516759
227502759 272030922
48462295 450696921
183559088 317228169
365794915 133411085
345365867 154485210
493284111 6477090
397676402 10174717...

output:

1000582139
1000582139
1000582139
1000582139
1000582139
1000582139
1000582139
1000582139

result:

ok 8 numbers

Test #12:

score: 0
Accepted
time: 32ms
memory: 11972kb

input:

8
10000
445062805 54324634
73636583 427209121
94242291 406610072
426842404 73694287
124540787 376414432
56028719 443696776
390646024 108939638
322649687 177349434
398037737 102667192
130048845 369861315
51589239 448100026
11585337 488662930
291289277 208605416
314642705 186222797
405227467 95703222
...

output:

1497536843
1497536843
1497536843
1497536843
1497536843
1497536843
1497536843
1497536843

result:

ok 8 numbers

Test #13:

score: 0
Accepted
time: 19ms
memory: 3584kb

input:

5000
16
29 -33
-5 -42
3 -2
-21 -32
11 43
-12 28
50 10
-31 47
49 -11
43 -45
-30 33
-34 -40
18 49
-50 8
31 8
-6 -31
14
34 6
31 -7
48 16
-18 23
44 -35
7 -49
-12 -48
15 3
7 -15
9 18
-21 -8
24 32
-3 44
5 -12
17
16 20
4 46
18 -38
-19 -43
-36 9
-28 -38
38 9
35 40
27 -31
37 -24
-28 -3
3 -11
16 41
4 -43
-48 ...

output:

126
90
126
54
86
118
125
8
116
123
94
63
94
46
100
103
82
88
91
108
116
111
38
88
109
121
11
16
133
40
52
139
126
44
133
24
70
101
122
105
86
52
32
122
44
30
87
35
95
99
78
109
133
64
82
80
111
22
123
108
92
144
104
62
60
102
67
49
22
98
58
119
109
138
75
71
133
90
88
91
59
110
119
136
9
94
85
105
6...

result:

ok 5000 numbers

Test #14:

score: 0
Accepted
time: 44ms
memory: 109156kb

input:

1
100000
141603120 358395957
91949299 408051533
280127622 219872540
269839621 230159940
398246595 101753077
348822087 151177633
348419099 151581884
296902611 203098137
338097907 161901334
372622396 127377407
394835486 105164842
447472828 52526264
400180018 99819426
413592564 86408119
360494425 13950...

output:

1349927669

result:

ok 1 number(s): "1349927669"

Test #15:

score: 0
Accepted
time: 42ms
memory: 128416kb

input:

1
90000
443555563 56444294
83262571 416738313
281645457 218355326
445801717 54198908
83627758 416372853
211139494 288860857
361318442 138681703
212318967 287681202
149020856 350978578
123082307 376916773
191763673 308236472
263766243 236233924
339734272 160266716
376958675 123040885
164258118 335742...

output:

899981686

result:

ok 1 number(s): "899981686"

Test #16:

score: 0
Accepted
time: 45ms
memory: 108796kb

input:

1
100000
391869133 108131089
153296049 346704832
435824119 64176139
402512179 97488625
108905299 391095683
287197881 212802334
303152943 196846093
337713912 162286279
249180885 250819612
178060316 321940429
393979501 106020779
66320276 433680592
116087834 383911295
65460918 434538801
304863521 19513...

output:

449995391

result:

ok 1 number(s): "449995391"

Test #17:

score: 0
Accepted
time: 41ms
memory: 108888kb

input:

1
100000
443245093 56754965
154391186 345608928
423601982 76397561
391610115 108390868
118474673 381525118
240893979 259106413
221239315 278759789
419622607 80377653
254528678 245471355
296991874 203008329
423257726 76742206
52387171 447612751
394711133 105288839
311036506 188963231
299331944 200667...

output:

449997854

result:

ok 1 number(s): "449997854"

Test #18:

score: 0
Accepted
time: 36ms
memory: 10312kb

input:

1
100000
384625660 115374409
225960109 274039535
178741365 321258128
170591713 329407412
308060861 191938282
220134970 279865276
213943533 286056786
103518048 396481052
207461712 292537600
252876866 247123499
351804867 148195958
64016058 435984211
52872452 447126785
86817782 413183092
220795830 2792...

output:

449994089

result:

ok 1 number(s): "449994089"

Test #19:

score: 0
Accepted
time: 36ms
memory: 10296kb

input:

1
100000
77035514 422964491
178817513 321182493
158098643 341901347
288748161 211251852
376028292 123971728
195670056 304329924
148630742 351369259
101439376 398560643
50630609 449369410
78580086 421419917
446984671 53015338
426980710 73019271
272161740 227838245
322523473 177476546
445486159 545138...

output:

449991329

result:

ok 1 number(s): "449991329"