QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#344727#8301. Hold the Linehos_lyricAC ✓1988ms443196kbC++145.7kb2024-03-05 01:11:152024-03-05 01:11:16

Judging History

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

  • [2024-03-05 01:11:16]
  • 评测
  • 测评结果:AC
  • 用时:1988ms
  • 内存:443196kb
  • [2024-03-05 01:11:15]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


struct Set {
  // max{ceil(log_64(n)), 1}
  int log64N, n;
  vector<unsigned long long> a[6];
  explicit Set(int n_ = 0) : n(n_) {
    assert(n >= 0);
    int m = n ? n : 1;
    for (int d = 0; ; ++d) {
      m = (m + 63) >> 6;
      a[d].assign(m, 0);
      if (m == 1) {
        log64N = d + 1;
        break;
      }
    }
  }
  bool empty() const {
    return !a[log64N - 1][0];
  }
  bool contains(int x) const {
    return (a[0][x >> 6] >> (x & 63)) & 1;
  }
  void insert(int x) {
    for (int d = 0; d < log64N; ++d) {
      const int q = x >> 6, r = x & 63;
      a[d][q] |= 1ULL << r;
      x = q;
    }
  }
  void erase(int x) {
    for (int d = 0; d < log64N; ++d) {
      const int q = x >> 6, r = x & 63;
      if ((a[d][q] &= ~(1ULL << r))) break;
      x = q;
    }
  }
  // max s.t. <= x (or -1)
  int prev(int x) const {
    if (x > n - 1) x = n - 1;
    for (int d = 0; d <= log64N; ++d) {
      if (x < 0) break;
      const int q = x >> 6, r = x & 63;
      const unsigned long long lower = a[d][q] << (63 - r);
      if (lower) {
        x -= __builtin_clzll(lower);
        for (int e = d; --e >= 0; ) x = x << 6 | (63 - __builtin_clzll(a[e][x]));
        return x;
      }
      x = q - 1;
    }
    return -1;
  }
  // min s.t. >= x (or n)
  int next(int x) const {
    if (x < 0) x = 0;
    for (int d = 0; d < log64N; ++d) {
      const int q = x >> 6, r = x & 63;
      if (static_cast<unsigned>(q) >= a[d].size()) break;
      const unsigned long long upper = a[d][q] >> r;
      if (upper) {
        x += __builtin_ctzll(upper);
        for (int e = d; --e >= 0; ) x = x << 6 | __builtin_ctzll(a[e][x]);
        return x;
      }
      x = q + 1;
    }
    return n;
  }
};

////////////////////////////////////////////////////////////////////////////////


/*
  problem
    query
      0 x h: add h to position x (which was empty)
      1 L R H: find distance from values in [L, R] to H
*/

constexpr int INF = 1001001001;

int N, Q;
vector<int> O, X, L, R, H;

int main() {
  for (int numCases; ~scanf("%d", &numCases); ) { for (int caseId = 1; caseId <= numCases; ++caseId) {
    scanf("%d%d", &N, &Q);
    O.assign(Q, -1);
    X.assign(Q, -1);
    L.assign(Q, -1);
    R.assign(Q, -1);
    H.assign(Q, -1);
    for (int q = 0; q < Q; ++q) {
      scanf("%d", &O[q]);
      if (O[q] == 0) {
        scanf("%d%d", &X[q], &H[q]);
        --X[q];
      } else if (O[q] == 1) {
        scanf("%d%d%d", &L[q], &R[q], &H[q]);
        --L[q];
      } else {
        assert(false);
      }
    }
    
    vector<pair<int, int>> hqs(Q);
    for (int q = 0; q < Q; ++q) hqs[q] = make_pair(H[q], q);
    sort(hqs.begin(), hqs.end());
    
    int segN;
    for (segN = 1; segN < N; segN <<= 1) {}
    vector<vector<int>> hss(segN << 1);
    
    vector<vector<int>> jss(Q);
    for (const auto &hq : hqs) {
      const int q = hq.second;
      if (O[q] == 0) {
        for (int a = segN + X[q]; a; a >>= 1) {
          jss[q].push_back((int)hss[a].size());
          hss[a].push_back(H[q]);
        }
      } else if (O[q] == 1) {
        for (int a = segN + L[q], b = segN + R[q]; a < b; a >>= 1, b >>= 1) {
          if (a & 1) jss[q].push_back((int)hss[a++].size());
          if (b & 1) jss[q].push_back((int)hss[--b].size());
        }
      } else {
        assert(false);
      }
    }
    
    vector<Set> sets(segN << 1);
    for (int a = 1; a < segN << 1; ++a) {
      sets[a] = Set((int)hss[a].size());
    }
    
    for (int q = 0; q < Q; ++q) {
      if (O[q] == 0) {
        int pos = 0;
        for (int a = segN + X[q]; a; a >>= 1) {
          const int j = jss[q][pos++];
          sets[a].insert(j);
        }
      } else if (O[q] == 1) {
        int ans = INF;
        int pos = 0;
        auto check = [&](int a) -> void {
          const int j = jss[q][pos++];
          const int l = sets[a].prev(j - 1);
          if (l >= 0) {
            chmin(ans, H[q] - hss[a][l]);
          }
          const int r = sets[a].next(j);
          if (r < sets[a].n) {
            chmin(ans, hss[a][r] - H[q]);
          }
        };
        for (int a = segN + L[q], b = segN + R[q]; a < b; a >>= 1, b >>= 1) {
          if (a & 1) check(a++);
          if (b & 1) check(--b);
        }
        printf("%d\n", (ans < INF) ? ans : -1);
      } else {
        assert(false);
      }
    }
  }
#ifndef LOCAL
  break;
#endif
  }
  return 0;
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3748kb

input:

1
3 5
1 1 3 2
0 1 1
0 3 3
1 1 2 2
1 2 3 2

output:

-1
1
1

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 504ms
memory: 3920kb

input:

3000
100 200
0 59 64091111
1 10 94 45205032
0 41 67249140
1 15 93 79570187
0 51 83215051
1 3 22 20062363
0 21 5188814
1 43 94 79642299
0 73 39313603
1 43 67 17001771
0 65 10784990
1 51 69 73368509
0 42 57377517
1 36 49 17483147
0 40 67280095
1 3 41 25139505
0 56 22833553
1 26 65 15640242
0 22 189761...

output:

18886079
12321047
-1
3572752
47089340
9277398
39894370
19950691
4855252
2221206
1596905
-1
3120922
34260194
3353597
-1
2499997
-1
15114024
1193064
49448136
734969
3981124
4159424
5836824
61155540
5163746
-1
283130
3982548
-1
-1
-1
9647216
2356693
8711627
379947
70230536
2637615
7856589
153976
148089...

result:

ok 700000 lines

Test #3:

score: 0
Accepted
time: 559ms
memory: 5064kb

input:

300
1000 2000
0 421 19938
1 153 254 35195
0 567 74665
1 88 371 61709
0 689 57559
1 39 744 67718
0 770 25816
1 576 955 75098
0 215 17619
1 133 133 29547
0 207 25038
1 929 965 45024
0 820 40726
1 245 505 82535
0 52 99669
1 631 819 77027
0 436 69966
1 102 243 65413
0 878 73531
1 331 759 23736
0 698 594...

output:

-1
-1
6947
17539
-1
-1
62597
19468
40375
3798
45299
-1
-1
11815
-1
-1
-1
6706
-1
-1
-1
9628
1883
-1
1822
-1
972
978
818
-1
3362
1758
53092
-1
712
-1
16697
-1
-1
1592
11462
-1
24068
12896
335
964
2836
29744
501
-1
-1
2298
1859
-1
6311
10290
2543
1589
838
920
7210
719
631
2781
-1
59401
2809
77412
1149...

result:

ok 700000 lines

Test #4:

score: 0
Accepted
time: 718ms
memory: 19460kb

input:

30
10000 20000
0 6444 22597278
1 940 8167 40648977
0 630 71321002
1 4054 4055 30762754
0 303 59383865
1 3410 3454 1633609
0 3376 42435219
1 6856 7397 92892411
0 1534 14886520
1 474 1932 21770410
0 9387 10819286
1 1640 1726 34316178
0 7331 75627104
1 8763 8764 83586695
0 3923 78696653
1 5016 5016 923...

output:

18051699
-1
-1
-1
6883890
-1
-1
-1
44912717
2247991
-1
5148557
-1
4713193
6643123
2730913
-1
6589982
-1
-1
-1
-1
-1
3691582
-1
1774051
-1
41333276
-1
1422761
-1
-1
-1
-1
895071
-1
692358
-1
2207326
21917947
3850486
-1
53145894
-1
2896385
45348895
3875216
-1
2503573
514164
-1
-1
-1
10502418
-1
458238...

result:

ok 700000 lines

Test #5:

score: 0
Accepted
time: 1545ms
memory: 196324kb

input:

3
100000 200000
0 77354 7
1 24769 44491 1
0 75190 6
1 3816 98172 1
0 45000 4
1 54504 97112 6
0 27855 3
1 53289 54534 9
0 87688 1
1 13220 13577 1
0 31245 7
1 4547 4547 3
0 43311 1
1 429 429 6
0 30798 2
1 28708 84952 4
0 99958 4
1 22719 22734 6
0 46564 3
1 1612 1664 7
0 95692 9
1 77806 93572 9
0 91654...

output:

-1
5
0
-1
-1
-1
-1
0
-1
-1
8
1
-1
-1
1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
1
2
-1
-1
-1
-1
-1
3
2
-1
-1
-1
-1
-1
-1
-1
1
-1
1
0
-1
2
0
2
2
-1
-1
-1
-1
-1
-1
-1
-1
-1
6
-1
2
0
-1
5
-1
2
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
1
0
1
-1
-1
0
0
-1
4
8
-1
-1
0
-1
0
-1
-1
-1
-1
-1
-1
-1
1
0
0
-1
-1
-1
0
-1
-1
-1...

result:

ok 500000 lines

Test #6:

score: 0
Accepted
time: 1589ms
memory: 424520kb

input:

1
500000 1000000
0 500000 10611289
1 449739 449917 13213816
0 1 94492701
1 21362 21393 55157967
0 499999 22844866
1 188062 188899 88627032
0 2 16392895
1 436969 437010 47518757
0 499998 74741014
1 339202 339203 89522398
0 3 97448236
1 351554 351622 37177728
0 499997 93234547
1 104463 104504 40778310...

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
...

result:

ok 500000 lines

Test #7:

score: 0
Accepted
time: 1750ms
memory: 392860kb

input:

1
300000 1000000
0 223097 849465603
0 294159 3631397
0 294768 245464219
0 293249 739562156
0 252356 115647988
0 275477 843753533
0 266798 803630592
0 284177 397919995
0 289516 645723272
0 296288 427094934
0 243438 379912048
0 279165 972282807
0 286884 23799613
0 298461 104253087
0 293456 5989076
0 2...

output:

123930
9052
237769906
111093202
641162355
974751
167369
412132
6213
3357797
62696
48557841
381592
25920
76265212
828254902
28954299
657581
1179
72973
449625448
155127907
1259378
133221
1454116
26533961
9818
12000
274889
16099129
65141
3533
1251
804086
88450
3116044
21135
42077970
231804124
19983
781...

result:

ok 700000 lines

Test #8:

score: 0
Accepted
time: 1537ms
memory: 195988kb

input:

3
200000 400000
0 167521 997676990
1 39082 39083 660900701
0 179611 546232924
1 169891 173500 44048275
0 97973 153059166
1 23851 24157 550054598
0 131351 995855540
1 35458 46540 290490052
0 118223 790116640
1 89393 91594 148217213
0 185914 416837134
1 154035 154038 921019533
0 183867 458165143
1 156...

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
84109557
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
68968838
274903246
-1
-1
-1
-1
-1
-1
-1
-1
-1
510756596
84212348
-1
169931837
12076147
42998159
-1
-1
-1
-1
-1
465145987
312779455
112093517
-1
-1
-1
-1
-1
-1
-1
-1
191544164
-1
9212105
-1
...

result:

ok 500000 lines

Test #9:

score: 0
Accepted
time: 1762ms
memory: 429728kb

input:

1
500000 1000000
0 452745 97601548
1 221172 221172 27624800
0 471744 251875370
1 286084 286138 271330036
0 473762 326478151
1 173872 276310 495131364
0 472234 225813607
1 252080 253017 396467261
0 474004 114390317
1 307517 333629 2049935
0 499348 30053748
1 228607 228608 236356716
0 397011 66760379
...

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
14064902
-1
-1
-1
-1
274614870
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
107395851
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
4137554...

result:

ok 500000 lines

Test #10:

score: 0
Accepted
time: 1988ms
memory: 443196kb

input:

1
500000 1000000
0 429334 632508814
1 421842 422923 808234341
0 464594 8122310
1 239565 271940 280350038
0 409917 847866461
1 183243 185913 104822005
0 488647 865246987
1 291013 296855 425103662
0 469079 619537849
1 46886 65611 622958516
0 494026 883711288
1 389556 412300 343303360
0 491157 84162517...

output:

-1
-1
-1
-1
-1
504563101
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
191320859
-1
-1
-1
-1
3437271
-1
-1
-1
-1
2707541
-1
85356458
-1
-1
-1
-1
-1
-1
-1
799473661
-1
-1
-1
-1
3315815
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
8957115
-1
523456356
-1
183124796
-1
-1
294092691
9446200
-1
-1
-1
21124792
-1...

result:

ok 500000 lines

Test #11:

score: 0
Accepted
time: 1883ms
memory: 441864kb

input:

1
500000 1000000
0 489091 181783832
1 251943 252190 480663043
0 492774 876059952
1 21633 75861 209182504
0 486428 612395749
1 199455 201152 883274151
0 483434 2296960
1 106239 110966 76714593
0 474455 930568438
1 8157 450262 50302189
0 462360 489003114
1 81727 84649 969513930
0 444701 39476541
1 129...

output:

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
39747464
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
19230726
-1
-1
-1
-1
-1
-1
546542593
-1
-1
-1
-1
-1
115751600
-1
-1
-1
24561499
-1
-1
109219277
-1
52084000
-1
-1
-1
-1
-1
-1
-1
-1
-1
555618240
2064221
-1
-1
66332210
-1
-1
-1
-1
-1
-1
7428059
-1
1348...

result:

ok 500000 lines

Test #12:

score: 0
Accepted
time: 1951ms
memory: 441932kb

input:

1
500000 1000000
0 479203 423471477
0 494496 16245438
0 495621 757324731
0 467828 514739190
0 463164 179202697
0 479731 839328973
0 494898 682351442
0 496804 492724197
0 496324 435291241
0 496288 339691980
0 486138 150304615
0 445938 63759999
0 443262 864152268
0 424348 11664940
0 484937 815261417
0...

output:

2831
1306
3076
1324
7447
131
5570697
741074
12243057
13738
7846
4209769
9622
655131
262549
7654693
1543823
347506
10650
383
720
102774
39137
4272
178743
71588
2749197
2987580
473363
15764
5442
2138333
19792
24048
18153
1448
17608
485538
3124414
1849
260155
920
1808
1431
4918441
1537208
240950
16234
...

result:

ok 500000 lines

Test #13:

score: 0
Accepted
time: 1967ms
memory: 441728kb

input:

1
500000 1000000
0 384626 519571036
0 472269 593180160
0 499334 264676181
0 493709 827154732
0 497369 232605427
0 452462 36396076
0 424884 633123012
0 446805 966573832
0 485651 297556549
0 440717 188471633
0 430252 258934426
0 489465 521388908
0 462743 60979842
0 450893 1391149
0 483513 56993419
0 4...

output:

3004
77485
399094
34947
7341
2263800
10063369
709398
1161
1705
42511
1567846
560
7341430
161
207
349066
19918
1479019
13627
999
9617
9335292
1660
30786
1505523
525791
1722159
4967
738
999
102315
1920
659128
6403
145687
1518
10146
634145
314778
225226
2126
6807842
1118542
370342
1438543
21908
130281
...

result:

ok 500000 lines

Test #14:

score: 0
Accepted
time: 1921ms
memory: 441888kb

input:

1
500000 1000000
0 445354 2834
0 479936 3410
0 476272 4228
0 498648 3446
0 407019 5766
0 482733 9446
0 475118 284
0 467669 8106
0 486823 2350
0 479716 7754
0 464498 5978
0 478699 5533
0 471120 9980
0 464880 6947
0 477428 2188
0 451948 6320
0 495728 1961
0 486997 9513
0 421328 6997
0 480688 3940
0 49...

output:

0
13
0
0
0
1
0
107
0
94
0
0
3
22
0
0
0
0
0
0
0
0
148
0
13
49
96
0
1
0
1
0
2
0
0
1
0
7
137
0
0
0
6
3
0
13
1029
0
0
2
0
2
25
0
1
272
0
0
29
0
0
4
82
7
422
0
14
0
1
33
7
0
0
1
0
0
11
5
1765
34
0
12
13
3
1
4
1
0
1
0
2
0
116
0
0
10
59
0
6
1
0
0
23
8
1
8
0
128
0
3
0
1
0
5
0
1
0
0
28
0
3
0
5
0
0
0
0
0
1
1
...

result:

ok 500000 lines

Extra Test:

score: 0
Extra Test Passed