QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#704783#8301. Hold the LineLavender_FieldTL 4385ms44344kbC++204.4kb2024-11-02 21:01:122024-11-02 21:01:13

Judging History

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

  • [2024-11-02 21:01:13]
  • 评测
  • 测评结果:TL
  • 用时:4385ms
  • 内存:44344kb
  • [2024-11-02 21:01:12]
  • 提交

answer

// 考虑绝对值很难做,分别做大于和小于的部分。
// 对于小于,有贡献的形如 ti < tj 且 ai < hj. 这是一个经典(?)的双指针cdq

#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i = a; i <= b; ++i)
using namespace std;

inline int rd() {
    int r = 0; bool w = false; char ch = getchar();
    while( ch < '0' || ch > '9' ) w = !(ch^45), ch = getchar();
    while( ch >= '0' && ch <= '9' ) r = (r<<1) + (r<<3) + (ch^48), ch = getchar();
    return w ? -r : r;
}

#define MAXM 1000000
#define MAXN 500000
int n, m;
int x[MAXM+5], h[MAXM+5];
int L[MAXM+5], R[MAXM+5];
int ans[MAXM+5];

int tots, tote;
struct Soldier {
    int x, h;
    Soldier() {}
    Soldier(int xi, int hi): x(xi), h(hi) {}
}bucs[MAXM+5];
struct Enemy {
    int l, r, h, idx;
    Enemy() {}
    Enemy(int li, int ri, int hi, int i): l(li), r(ri), h(hi), idx(i) {}
}buce[MAXM+5];

int a[MAXN*4+5];
#define lson (p<<1)
#define rson ((p<<1)+1)
const int INF = 1e9;
void pushup( int p, int typ ) {
    if( a[lson] == -1 || a[rson] == -1 ) a[p] = ((a[lson]+1) | (a[rson]+1)) - 1; 
    else a[p] = typ ? max(a[lson], a[rson]) : min(a[lson], a[rson]);
}
void update( int p , int x , int k , int typ , int L = 1, int R = n ) {
    if( L == R ) {
        a[p] = k;
        return;
    }
    int mid = (L + R) >> 1;
    if( x <= mid ) update(lson, x, k, typ, L, mid);
    else update(rson, x, k, typ, mid+1, R);
    pushup(p, typ);
}
void refresh( int p , int x , int typ , int L = 1 , int R = n ) {
    if( L == R ) {
        a[p] = -1;
        return;
    }
    int mid = (L + R) >> 1;
    if( x <= mid ) refresh(lson, x, typ, L, mid);
    else refresh(rson, x, typ, mid+1, R);
    pushup(p, typ);
}
int query( int p , int l , int r , int typ , int L = 1 , int R = n ) {
    if( l == L && r == R  ) {
        return a[p];
    }
    int mid = (L + R) >> 1;
    if( r <= mid ) return query(lson, l, r, typ, L, mid);
    else if( l > mid ) return query(rson, l, r, typ, mid+1, R);
    else {
        int ql = query(lson, l, mid, typ, L, mid);
        int qr = query(rson, mid+1, r, typ, mid+1, R);
        if( ql == -1 || qr == -1 ) return ((ql + 1) | (qr + 1)) - 1;
        return typ ? max(ql, qr) : min(ql, qr);
    }
}

void cdq( int l , int r ) {
    if( l == r ) return;
    int mid = (l + r) >> 1;
    
    tots = tote = 0;
    FOR(i,l,mid) if( x[i] != 0 ) {
        bucs[++tots] = Soldier(x[i], h[i]);
    }
    FOR(i,mid+1,r) if( L[i] != 0 ) {
        buce[++tote] = Enemy(L[i], R[i], h[i], i);
    }

    sort(bucs+1, bucs+1+tots, [](const Soldier &a, const Soldier &b) {
        return a.h < b.h;
    });
    sort(buce+1, buce+1+tote, [](const Enemy &a, const Enemy&b) {
        return a.h < b.h;
    });
    int p = 0;
    FOR(i,1,tote) {
        while( p < tots && bucs[p+1].h <= buce[i].h ) {
            ++p;
            update(1, bucs[p].x, bucs[p].h, 1);
        }
        int cons = query(1, buce[i].l, buce[i].r, 1);
        if( cons != -1 ) {
            if( ans[buce[i].idx] == -1 ) ans[buce[i].idx] = buce[i].h - cons;
            else ans[buce[i].idx] = min(ans[buce[i].idx], buce[i].h - cons);
        }
    }
    FOR(i,1,p) refresh(1, bucs[i].x, 1);

    sort(bucs+1, bucs+1+tots, [](const Soldier &a, const Soldier &b) {
        return a.h > b.h;
    });
    sort(buce+1, buce+1+tote, [](const Enemy &a, const Enemy&b) {
        return a.h > b.h;
    });
    p = 0;
    FOR(i,1,tote) {
        while( p < tots && bucs[p+1].h > buce[i].h ) {
            ++p;
            update(1, bucs[p].x, bucs[p].h, 0);
        }
        int cons = query(1, buce[i].l, buce[i].r, 0);
        if( cons != -1 ) {
            if( ans[buce[i].idx] == -1 ) ans[buce[i].idx] = cons - buce[i].h;
            else ans[buce[i].idx] = min(ans[buce[i].idx], cons - buce[i].h);
        }
    }
    FOR(i,1,p) refresh(1, bucs[i].x, 0);

    cdq(l, mid);
    cdq(mid+1, r);
}
void solve() {
    n = rd(), m = rd();
    FOR(i,1,m) ans[i] = -1;
    FOR(i,1,m) L[i] = R[i] = x[i] = h[i] = 0;
    FOR(i,1,m) {
        int typ = rd();
        if( typ == 0 ) {
            x[i] = rd(), h[i] = rd();
        } else {
            L[i] = rd(), R[i] = rd(), h[i] = rd();
        }
    }
    cdq(1, m);
    FOR(i,1,m) if( L[i] ) printf("%d\n", ans[i]);
}

int main() {
    memset(a, -1, sizeof(a));

    int T = rd(); while( T-- ) solve();    
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 842ms
memory: 24824kb

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: 1513ms
memory: 24456kb

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: 2338ms
memory: 25840kb

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: 3148ms
memory: 39796kb

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: 4385ms
memory: 40236kb

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: 3367ms
memory: 44344kb

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: 3238ms
memory: 37944kb

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: -100
Time Limit Exceeded

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:


result: