QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#140370#4563. Radio Towerspandapythoner#0 154ms73440kbC++176.0kb2023-08-15 20:05:592024-07-04 01:44:20

Judging History

This is the latest submission verdict.

  • [2024-07-04 01:44:20]
  • Judged
  • Verdict: 0
  • Time: 154ms
  • Memory: 73440kb
  • [2023-08-15 20:05:59]
  • Submitted

answer

#include <bits/stdc++.h>

#include "towers.h"

using namespace std;

#define ll long long
#define flt double
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()

const ll inf = 1e18;
mt19937 rnd(234);

struct SGT_mn {
    int n;
    vector<pair<ll, int>> t;

    void build(vector<ll> &a) {
        n = 1;
        while (n < (int)a.size()) {
            n *= 2;
        }
        t.assign(n + n, {0, 0});
        for (int i = 0; i < (int)a.size(); i += 1) {
            t[i + n] = make_pair(a[i], i);
        }
        for (int i = n - 1; i >= 1; i -= 1) {
            t[i] = min(t[i + i], t[i + i + 1]);
        }
    }

    pair<ll, int> get(int l, int r) {
        l += n;
        r += n + 1;
        pair<ll, int> rs = {inf, -1};
        while (l < r) {
            if (l & 1) {
                rs = min(rs, t[l]);
                l += 1;
            }
            if (r & 1) {
                r -= 1;
                rs = min(rs, t[r]);
            }
            l /= 2;
            r /= 2;
        }
        return rs;
    }
};

struct SGT_bebra {
    int n;
    vector<vector<ll>> t;

    void build(vector<vector<ll>> &a) {
        n = 1;
        while (n < (int)a.size()) {
            n *= 2;
        }
        t.assign(n + n, {0, 0});
        for (int i = 0; i < (int)a.size(); i += 1) {
            t[i + n] = a[i];
        }
        for (int i = n - 1; i >= 1; i -= 1) {
            t[i].resize((int)t[i + i].size() + (int)t[i + i + 1].size());
            merge(all(t[i + i]), all(t[i + i + 1]), t[i].begin());
        }
    }

    int get(int l, int r, ll x) {
        l += n;
        r += n + 1;
        int rs = 0;
        while (l < r) {
            if (l & 1) {
                rs += lower_bound(all(t[l]), x) - t[l].begin();
                l += 1;
            }
            if (r & 1) {
                r -= 1;
                rs += lower_bound(all(t[r]), x) - t[r].begin();
            }
            l /= 2;
            r /= 2;
        }
        return rs;
    }
};

int n;
vector<ll> a, na;
SGT_mn sgt_mn, sgt_mx;
vector<int> nxt_smlr, prvs_smlr;
vector<ll> bbr;
vector<ll> bbr_srtd;
SGT_bebra sgtl, sgtr;
vector<vector<ll>> dl, dr;
ll super_d;
vector<ll> psms;

void build_super_d(ll d) {
    super_d = d;
    dl.assign(n, vector<ll>());
    dr.assign(n, vector<ll>());
    psms.assign(n + 1, 0);
    for (int i = 0; i < n; i += 1) {
        ll lval = inf;
        ll rval = inf;
        if (prvs_smlr[i] != -1) {
            lval = -sgt_mx.get(prvs_smlr[i] + 1, i - 1).first;
        }
        if (nxt_smlr[i] != n) {
            rval = -sgt_mx.get(i + 1, nxt_smlr[i] - 1).first;
        }
        ll lx = lval - a[i];
        ll rx = rval - a[i];
        if (lx >= d && rx >= d) {
            psms[i] += 1;
        } else if (lx >= d) {
            dr[i].push_back(-nxt_smlr[i]);
        } else if (rx >= d) {
            dl[i].push_back(prvs_smlr[i]);
        }
    }
    for (int i = 1; i <= n; i += 1) {
        psms[i] += psms[i - 1];
    }
    sgtl.build(dl);
    sgtr.build(dr);
}

ll get_super_d(int l, int r) {
    int rs = psms[r + 1] - psms[l];
    rs += sgtl.get(l, r, l);
    rs += sgtr.get(l, r, -r);
    int i = sgt_mn.get(l, r).second;
    ll lval = inf;
    ll rval = inf;
    if (prvs_smlr[i] != -1) {
        lval = -sgt_mx.get(prvs_smlr[i] + 1, i - 1).first;
    }
    if (nxt_smlr[i] != n) {
        rval = -sgt_mx.get(i + 1, nxt_smlr[i] - 1).first;
    }
    ll lx = lval - a[i];
    ll rx = rval - a[i];
    if (lx < super_d && rx < super_d) {
        rs += 1;
    }
    return rs;
}

void init(int N, vector<int> H) {
    n = N;
    a.resize(n);
    na.resize(n);
    for (int i = 0; i < n; i += 1) {
        a[i] = H[i];
        na[i] = -H[i];
    }
    sgt_mn.build(a);
    sgt_mx.build(na);
    nxt_smlr.resize(n);
    prvs_smlr.resize(n);
    vector<pair<ll, int>> stck = {make_pair(-inf, -1)};
    for (int i = 0; i < n; i += 1) {
        while (stck.back().first > a[i]) {
            stck.pop_back();
        }
        prvs_smlr[i] = stck.back().second;
        stck.push_back(make_pair(a[i], i));
    }
    stck = {make_pair(-inf, n)};
    for (int i = n - 1; i >= 0; i -= 1) {
        while (stck.back().first >= a[i]) {
            stck.pop_back();
        }
        nxt_smlr[i] = stck.back().second;
        stck.push_back(make_pair(a[i], i));
    }
    bbr.resize(n);
    for (int i = 0; i < n; i += 1) {
        ll lval = inf;
        ll rval = inf;
        if (prvs_smlr[i] != -1) {
            lval = -sgt_mx.get(prvs_smlr[i] + 1, i - 1).first;
        }
        if (nxt_smlr[i] != n) {
            rval = -sgt_mx.get(i + 1, nxt_smlr[i] - 1).first;
        }
        bbr[i] = min(lval, rval) - a[i];
    }
    bbr_srtd = bbr;
    sort(all(bbr_srtd));
    super_d = -1;
}

int solve_rec(int l, int r, ll lval, ll rval, ll lmx, ll rmx, ll d) {
    if (l > r) {
        return 0;
    }
    int mni = sgt_mn.get(l, r).second;
    ll mn = a[mni];
    ll mxl = -sgt_mx.get(l, mni - 1).first;
    ll mxr = -sgt_mx.get(mni + 1, r).first;
    mxl = max(mxl, lmx);
    mxr = max(mxr, rmx);
    if (max(mn, lval) + d > mxl && max(mn, rval) + d > mxr) {
        return 0;
    }
    if (max(mn, lval) + d <= mxl && max(mn, rval) + d <= mxr) {
        return 1 + solve_rec(l, mni - 1, lval, mn, lmx, -inf, d) + solve_rec(mni + 1, r, mn, rval, -inf, rmx, d);
    }
    if (max(mn, lval) + d <= mxl) {
        return solve_rec(l, mni - 1, lval, rval, lmx, mxr, d);
    }
    return solve_rec(mni + 1, r, lval, rval, mxl, rmx, d);
}

int max_towers(int L, int R, int D) {
    if (super_d == -1) {
        build_super_d(D);
    }
    if (D == super_d) {
        int rs = get_super_d(L, R);
        return rs;
    }
    if (L == 0 && R == n - 1) {
        int rs = n - (lower_bound(all(bbr_srtd), D) - bbr_srtd.begin());
        return rs;
    }
    int rs = solve_rec(L, R, -inf, -inf, inf, inf, D);
    return rs;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Time Limit Exceeded

Test #1:

score: 0
Time Limit Exceeded

input:

59640
49885 57346 58504 87383 113182 129676 204090 205404 259925 276583 300332 324014 333675 359377 364049 408489 414852 428310 438038 440113 458193 554789 643468 666525 683112 690791 707313 720088 741028 748785 789826 796576 800966 832867 851750 861044 862283 900756 926925 939560 955678 965636 9740...

output:

1
1
1
1
1
1
1
1
1
1
1
2
1
1
2
1
1
1
1
1
1
1
1
1
1
1
2
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2
2
1
2
1
1
2
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2
1
1
1
1
2
2
1
1
1
1
1
1
1
1
2
1
2
1
1
1
1
1
1
1
1
1
2
1
1
1
2
1
1
1
1
1
1
2
1
1
1
1
2
1
2
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
...

result:


Subtask #2:

score: 0
Wrong Answer

Test #8:

score: 0
Wrong Answer
time: 1ms
memory: 3956kb

input:

425
753881706 405729786 890889563 29736246 598281970 208352067 357783003 663497023 178397034 4832890 562140755 510307001 354540290 538848551 436879256 86659033 42928516 24145404 749159097 118423198 506851985 204895765 719719998 726244368 991372008 681703480 799303017 657138050 88278945 417801236 260...

output:

14

result:

wrong answer 3rd lines differ - expected: '13', found: '14'

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Wrong Answer

Test #65:

score: 0
Wrong Answer
time: 148ms
memory: 70124kb

input:

99308
491693640 24020487 317364185 726230755 737284540 951143270 709116045 641023337 360629062 964879440 47884022 532494780 803629825 635450854 688041998 573937055 113198481 191311841 929603572 636688 598629732 895342035 396521271 619919754 716589045 657789547 373121546 866402108 609316614 60046511 ...

output:

11904
4770
14278
13956
572
9308
4388
9
22097
16784
26037
2814
8487
16603
2028
6157
17235
29707
19863
12084
20816
18090
4195
11611
4623
6658
7660
624
9840
13011
14475
11798
14794
6365
7307
5980
13614
14208
15922
17325
6020
10291
1819
29077
3117
6638
5810
28747
14944
9541
5497
1015
5001
20938
306
1993...

result:

wrong answer 3rd lines differ - expected: '11903', found: '11904'

Subtask #5:

score: 0
Wrong Answer

Test #86:

score: 17
Accepted
time: 27ms
memory: 19536kb

input:

23881
605288257 422163932 155875880 339929874 76049859 196344969 958160745 767945284 469191956 997116006 387749577 15911341 920437918 367576975 800789357 351557615 283723284 369507452 841548133 643412903 309731505 256549694 370065644 699518122 559017597 347646657 469353381 575240521 501893001 454519...

output:

7197
7063
150
5030
5227
7333
1778
6675
2012
5921
4878
7477
4598
2769
5360
6465
7660
7234
7794
2760
6321
7056
7302
4749
464
2029
5650
1973
6227
4900
4966
4990
3142
785
5818
3005
7705
6967
1940
5880
7201
4752
1278
6554
2951
894
6601
7019
7236
6076
5182
6579
2343
2070
4337
5744
4437
2262
6686
1739
7756...

result:

ok 31567 lines

Test #87:

score: 0
Wrong Answer
time: 98ms
memory: 68296kb

input:

100000
187962236 252322706 659740540 756184857 615615021 870954164 997894181 924184575 178246679 206117936 948374169 611376809 940125697 583402158 621243496 179806768 420420048 261580744 495350370 179501503 624736464 865025098 132756697 396891347 254854635 384499314 232013282 699329403 718265326 972...

output:

21500
24099
33073
16936
24145
8440
674
23350
29618
2899
7659
19499
19027
10215
4083
14518
30601
23009
17970
7096
15472
32634
26673
29553
6232
11457
690
8753
7786
4078
28404
25386
28535
1752
5912
16456
18098
25382
30618
28242
30215
30920
19228
20981
27023
18696
16047
19091
7953
9832
13542
4224
26991
...

result:

wrong answer 3rd lines differ - expected: '21501', found: '21500'

Subtask #6:

score: 0
Wrong Answer

Test #97:

score: 0
Wrong Answer
time: 154ms
memory: 73440kb

input:

88768
238644804 620122421 789364401 899010695 885388612 437964772 845379533 657562749 773925456 625793781 916240972 291506550 379611161 905077982 629848170 530056471 520438258 806293637 326792996 785404568 74285074 565304193 846963319 63529729 804909008 212070492 69936548 656129389 408708069 3070450...

output:

7292
18701
4921
19045
23487
1992
20888
9155
21247
15709
115
11735
8529
19157
9407
9139
5401
20113
1699
3992
22639
5925
17884
12913
5727
12378
21617
15763
646
5418
16981
7639
6140
1777
6289
619
766
3080
8805
11541
7217
2650
15836
2237
2024
9704
23983
4664
8898
5006
2391
20171
8342
14535
16454
6624
18...

result:

wrong answer 3rd lines differ - expected: '7293', found: '7292'

Subtask #7:

score: 0
Skipped

Dependency #1:

0%