QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#395172#1375. TripTikmegz112233TL 5975ms209644kbC++235.4kb2024-04-21 09:50:032024-04-21 09:50:04

Judging History

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

  • [2024-04-21 09:50:04]
  • 评测
  • 测评结果:TL
  • 用时:5975ms
  • 内存:209644kb
  • [2024-04-21 09:50:03]
  • 提交

answer

#include <bits/stdc++.h>

#define sz(x)  (int)x.size()
#define bigint __int128
#define el '\n'
#define ll long long
//#define int long long
#define ld long double
using namespace std;
const int N = 1e5 + 4, M = 1e5 + 10, mod = 998244353;
int szz = 1;

struct seg_tree {
    vector<vector<pair<int, int>>> tree;
    // weight , idx
    vector<pair<int, int>> neutral = {};
    int k;

    seg_tree(int n, vector<vector<pair<int, int>>> &v, int k) {
        szz = 1;
        while (szz < n)szz <<= 1;
        tree.assign(2 * szz, neutral);
        this->k = k;
        build(v);
    }

    vector<pair<int, int>> merge(vector<pair<int, int>> val1, vector<pair<int, int>> val2) {
        // vector 2*n 3,3
        for (auto x: val2)val1.push_back(x);
        sort(val1.rbegin(), val1.rend());
        while (sz(val1) > k)val1.pop_back();
        return val1;
    }

    void build(vector<vector<pair<int, int>>> &v, int node = 0, int lx = 0, int rx = szz - 1) {
        if (lx == rx) {
            if (lx < sz(v)) {
                tree[node] = v[lx];
            }
            return;
        }
        int mid = lx + rx >> 1;
        build(v, (node << 1) + 1, lx, mid);
        build(v, (node << 1) + 2, mid + 1, rx);
        tree[node] = merge(tree[(node << 1) + 1], tree[(node << 1) + 2]);
    }

    void set(int idx, vector<pair<int, int>> val, int node = 0, int lx = 0, int rx = szz - 1) {
        if (lx == rx) {
            tree[node] = val;
            return;
        }
        int mid = (lx + rx) >> 1;
        if (mid >= idx) {
            set(idx, val, (node << 1) + 1, lx, mid);
        } else {
            set(idx, val, (node << 1) + 2, mid + 1, rx);
        }
        tree[node] = merge(tree[(node << 1) + 1], tree[(node << 1) + 2]);
    }

    vector<pair<int, int>> query(int l, int r, int node = 0, int lx = 0, int rx = szz - 1) {
        if (lx > r or rx < l)return neutral;
        if (lx >= l and rx <= r) {
            return tree[node];
        }
        int mid = lx + rx >> 1;
        vector<pair<int, int>> q1 = query(l, r, (node << 1) + 1, lx, mid);
        vector<pair<int, int>> q2 = query(l, r, (node << 1) + 2, mid + 1, rx);
        return merge(q1, q2);
    }

};

void acc() {
    int n, k;
    cin >> n >> k;
    // 0
    vector<pair<int, int>> points(n);
    // x-axis  , idx
    //  -5 -1 0 1 2 3
    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;
        points[i] = {x, i};
    }
    points.push_back({0, -1});
    sort(points.begin(), points.end());
    n++;
    vector<vector<pair<int, int>>> v(n);
    for (int i = 0; i < n; i++) {
        v[i].push_back({points[i].second, i});
    }
    seg_tree sg(n, v, k);
    int lg = 29;
    vector<int> adj[n + 1][lg];
    vector<int> pw(lg + 1, 1);
    pw[0] = 0;
    for (int i = 2; i < lg; i++)pw[i] = pw[i - 1] * 2;
    for (int bt = 0; bt < lg; bt++) {
//        cout << "with bt = " << bt << el;
//        cout << "---------" << el;
        for (int i = 0; i < n; i++) {
            int l = points[i].first - pw[bt], r = points[i].first + pw[bt];
            auto idxl = std::lower_bound(points.begin(), points.end(), make_pair(l, -1)) - points.begin();
            auto idxr = std::lower_bound(points.begin(), points.end(), make_pair(r, -1)) - points.begin();
            if (idxr == n or points[idxr].first > r)idxr--;
//            cout << points[i].first << " " << idxl << " " << idxr << el;
            vector<pair<int, int>> ret = sg.query(idxl, idxr);
            // weight , idx
            for (auto [val, idx]: ret) {
                adj[i][bt].push_back(idx);
//                cout << idx << " ";
            }
//            cout << "------" << el;
        }
    }
//    for (int i = 0; i < n; i++) {
//        cout << points[i].first << el;
//        for (int j = 0; j < 4; j++) {
//            cout << "with scoop == " << pw[j] << el;
//            for (auto child: adj[i][j]) {
//                cout << child << " ";
//            }
//            cout << el;
//        }
//    }

    int idx_zero = -1;
    for (int i = 0; i < sz(points); i++) {
        if (points[i].first == 0)idx_zero = i;
    }
    priority_queue<array<int, 3>> pq;
    // num_of operation , lst ,  bt
    pq.push({0, idx_zero, 1});
    vector<int> lst(n, 1e9);
    vector<vector<int>> dist(n + 1, vector<int>(lg, 1e9));
    dist[0][1] = 0;
    while (sz(pq)) {
        array<int, 3> arr = pq.top();
        pq.pop();
        int op = -arr[0], node = arr[1], bt = arr[2];
        for (int i = 0; i < lg; i++) {
            int nw_cost = abs(bt - i) + op;
            for (auto child: adj[node][i]) {
                if (dist[child][i] > nw_cost + (child != node)) {
                    dist[child][i] = nw_cost + (child != node);
                    pq.push({-dist[child][i], child, i});
                }
                if (child == node)lst[child] = min(lst[child], nw_cost);
            }
        }
    }
    vector<int> ans(n - 1, 11223344);
    for (int i = 0; i < sz(points); i++) {
        if (points[i].first == 0)continue;
        ans[points[i].second] = lst[i];
    }
    for (int i = 0; i < sz(ans); i++) {
        if (ans[i] >= 1e9)cout << "-1" << el;
        else cout << ans[i] << el;
    }
//    cout << el;
}

signed main() {
    int t = 1;
    cout.tie(0);
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    //  while (t--)acc();
    acc();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

4 2
-2
-1
-3
2

output:

3
1
3
2

result:

ok 4 lines

Test #2:

score: 0
Accepted
time: 5936ms
memory: 209552kb

input:

100000 4
-81032899
-96819000
35569221
67767248
74154777
75473369
3463319
18339682
-77145048
-26587182
-22902096
-55717109
-47237134
-44620229
-45753774
33183854
-15569807
72648777
-33842373
44545672
-48017305
71506102
33448860
-20688896
6205313
21527590
24985350
-14688810
29596074
-47051603
-9258675...

output:

56
49
48
47
47
50
38
42
-1
-1
51
55
51
52
51
46
41
48
46
44
47
47
49
44
37
-1
44
44
45
51
49
49
45
49
44
43
51
54
46
43
-1
44
49
57
46
45
43
47
46
53
48
44
47
-1
49
55
53
47
52
51
49
47
53
56
43
50
43
29
43
50
52
52
47
45
47
50
52
47
49
47
50
49
48
47
49
45
62
37
47
48
51
39
47
46
48
50
47
53
44
52
...

result:

ok 100000 lines

Test #3:

score: 0
Accepted
time: 5975ms
memory: 209508kb

input:

100000 4
70680198
97317334
39546330
-54313602
-76555335
61941724
-8221943
-40467311
-38387353
-79352984
-85695884
-116744
-8014898
-90838613
21642942
-13906694
61034002
41083341
22631959
70997275
75121931
90712180
-50976024
-89714113
62620386
30945015
37854774
2964535
80028136
-28328113
-32257258
78...

output:

-1
59
-1
48
49
43
44
49
46
52
48
-1
45
49
40
45
47
43
46
54
-1
49
46
50
46
44
45
39
45
45
46
52
-1
37
52
49
33
42
42
-1
40
34
50
-1
47
56
50
54
-1
46
-1
44
50
37
-1
50
60
48
51
49
-1
53
47
44
41
-1
47
46
-1
50
47
47
48
51
46
47
44
51
50
48
-1
-1
40
48
-1
43
42
58
51
43
54
43
48
50
41
-1
45
48
48
55
...

result:

ok 100000 lines

Test #4:

score: 0
Accepted
time: 5962ms
memory: 209644kb

input:

100000 4
-22911754
64514931
-46579963
-52307990
-28419918
-35238
66903391
-87096465
15648262
-88916963
96268224
-39583622
-33334243
76123224
30449894
-60512561
92205726
96701672
12953603
70508490
88259167
-12584810
35245713
2029713
19820003
91618921
-66864990
-96291681
73438998
6296068
-26195246
780...

output:

-1
50
45
48
45
22
46
52
46
59
52
52
47
50
52
49
50
48
-1
49
47
55
49
40
50
49
54
-1
48
41
43
41
46
55
-1
50
49
46
48
48
46
47
49
48
50
42
45
55
44
55
54
47
56
-1
52
55
-1
52
-1
54
48
-1
54
41
45
47
50
-1
50
56
48
57
44
48
50
50
48
-1
-1
55
52
53
57
51
45
57
51
45
49
54
52
42
48
57
49
49
51
57
47
51
...

result:

ok 100000 lines

Test #5:

score: -100
Time Limit Exceeded

input:

100000 4
-288
-318
-339
-525
-666
-688
714
-1068
1248
-1318
1453
1485
-1503
1767
1844
1893
-1903
-1942
-1959
1995
2114
-2150
-2209
2256
-2302
2317
-2414
-2423
2693
-2702
2736
-2948
2974
2986
3013
-3019
3197
-3300
3493
3495
3612
-3633
-3657
3702
3727
-3753
3758
4031
-4063
4131
-4172
-4284
4298
4409
4...

output:


result: