QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#356082#5459. Goose, goose, DUCK?ucup-team1001RE 32ms58828kbC++235.0kb2024-03-17 15:25:572024-03-17 15:25:57

Judging History

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

  • [2024-03-17 15:25:57]
  • 评测
  • 测评结果:RE
  • 用时:32ms
  • 内存:58828kb
  • [2024-03-17 15:25:57]
  • 提交

answer

#include "bits/stdc++.h"

using namespace std;

#ifndef ONLINE_JUDGE
#include "test.h"
#else
#define debug(...) 42
#define debug_assert(...) 42
#endif
#define IOS ios::sync_with_stdio(0),cin.tie(0)
using ll = long long;
using ull = unsigned long long;
#define irep(i, l, r) for(int i = l; i <= r; ++ i)
#define endl '\n'
#define int ll

using VI = vector<int>;
using VII = vector<VI>;
using PII = pair<int, int>;
const int inf = 1e18;
const int mod = 1e9 + 7;

struct nodes {
    int len, val, sum;

    nodes() {
        len = 0;
        val = 0;
        sum = 0;
    }
};

vector<int> node[1000000 + 7];

void init() {
    int n, k;
    cin >> n >> k;
    VI arr(n + 1);
    // map<int, vector<int>> node;
    for (int i = 1; i <= n; i++) {
        cin >> arr[i];
        node[arr[i]].emplace_back(i);
    }
    // 前两个存储删除空间,后两个存储添加的空间
    // vector<array<int, 4>> e(n + 1);
    // irep(i, 1, n){
    //
    // }
    // for (const auto&[_, v]: node) {
    //     for (int i = 0; i < v.size(); i++) {
    //         int len = v[i];
    //         if (i == 0) {
    //             e[len][0] = e[len][1] = 0;
    //         }
    //         else {
    //             int before = v[i - 1];
    //             e[len][0] = e[before][2];
    //             e[len][1] = e[before][3];
    //         }
    //         if (i + k - 1 > v.size() - 1) {
    //             e[len][2] = n + 1;
    //         }
    //         else {
    //             e[len][2] = v[i + k - 1];
    //             if (i + k > v.size() - 1) {
    //                 e[len][3] = n + 1;
    //             }
    //             else {
    //                 e[len][3] = v[i + k];
    //             }
    //         }
    //     }
    // }

    vector<nodes> _sg(4 << __lg(n+10000));

    auto pushup = [&](int q) {
        if (_sg[q].val) _sg[q].sum = _sg[q].len;
        else _sg[q].sum = _sg[q << 1].sum + _sg[q << 1 | 1].sum;
    };
    function<void(int,int)> addval = [&](int q,int v) {
        _sg[q].val += v;
        if (_sg[q].val < 0) {
            // addval(q << 1, _sg[q].val);
            // addval(q << 1 | 1, _sg[q].val);
            _sg[q].val = 0;
        }
        pushup(q);
    };

    auto pushdown = [&](int q) {
        addval(q << 1, _sg[q].val);
        addval(q << 1 | 1, _sg[q].val);
        _sg[q].val = 0;
    };

    function<void(int,int,int)> build = [&](int q,int l,int r) {
        if (l == r) {
            _sg[q].len = 1;
            return;
        }
        int mid = (l + r) / 2;
        build(q << 1, l, mid);
        build(q << 1 | 1, mid + 1, r);
        _sg[q].len = _sg[q << 1].len + _sg[q << 1 | 1].len;
    };

    function<void(int,int,int,int,int,int)> add = [&](int q,int v,int l,int r,int L,int R) {
        if (L <= l && r <= R) {
            addval(q, v);
            return;
        }
        if (r < L || l > R)return;
        // pushdown(q);
        int mid = (l + r) / 2;
        if (mid >= L) {
            add(q << 1, v, l, mid, L, R);
        }
        if (mid < R) {
            add(q << 1 | 1, v, mid + 1, r, L, R);
        }
        pushup(q);
    };

    // debug(e);
    int ans = 0;
    build(1, 1, n);

    // for (int i = 1; i <= n; i++) {
    //     auto [f1,t1,f2,t2] = e[i];
    //     if (f1 != 0 && f1 != n + 1) {
    //         debug("add", -1, f1, min(t1, n));
    //         add(1, -1, 1, n, f1, min(t1, n));
    //     }
    //     if (f2 != 0 && f2 != n + 1) {
    //         debug("add", 1, f2, min(t2, n));
    //         add(1, 1, 1, n, f2, min(t2, n));
    //     }
    //     ans += _sg[1].sum;
    //     debug(_sg[1].sum);
    // }
    // cout << ans << endl;
    vector<array<int, 4>> opt; // time = 0, add (3) from (1) to (2)
    irep(i, 1, 1e6) {
        node[i].emplace_back(n + 1);
        int sz = node[i].size();
        // cerr << sz << endl;
        int lst = 0;
        for (int p = 0, q = p + k - 1; q + 1 < sz; ++q, lst = node[i][p], ++p) {
            // int il = lst + 1, ir = node[i][q] - 1;
            // lst + 1 , node[i][p], node[i][q] + 1, node[i][q + 1] - 1
            if (node[i][q] > node[i][q + 1] - 1)continue;
            opt.push_back({lst + 1, node[i][q], node[i][q + 1] - 1, 1});
            opt.push_back({node[i][p] + 1, node[i][q], node[i][q + 1] - 1, -1});
        }
    }
    // int ans = 0;
    sort(opt.begin(), opt.end());
    auto idx = opt.begin();
    debug(opt);
    irep(i, 1, n) {
        while (idx != opt.end() && (*idx)[0] == i) {
            auto [tim, l, r, v] = *idx;
            add(1, v, 1, n, l, r);
            debug(v, l, r);
            ++idx;
        }
        debug(_sg[1].sum);
        // cerr << _sg[1].sum << endl;
        ans += (n - i + 1) - _sg[1].sum;
    }
    cout << ans;
}


void solve() {
}

signed main() {
    IOS;
    init();
    // debug(1);
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
}

详细

Test #1:

score: 100
Accepted
time: 32ms
memory: 58604kb

input:

6 2
1 2 2 1 3 3

output:

10

result:

ok 1 number(s): "10"

Test #2:

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

input:

6 1
1 2 3 4 5 6

output:

0

result:

ok 1 number(s): "0"

Test #3:

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

input:

100 10
142826 764475 142826 986320 764475 142826 142826 986320 764475 986320 764475 764475 764475 142826 142826 986320 764475 986320 764475 764475 142826 764475 142826 764475 986320 986320 764475 142826 764475 764475 142826 764475 764475 986320 142826 142826 142826 142826 764475 986320 986320 764475...

output:

4309

result:

ok 1 number(s): "4309"

Test #4:

score: 0
Accepted
time: 22ms
memory: 58688kb

input:

1000 100
144044 144044 606320 144044 144044 653289 606320 606320 606320 144044 653289 606320 144044 606320 144044 653289 606320 653289 144044 144044 606320 606320 606320 144044 606320 653289 653289 144044 606320 606320 606320 606320 606320 606320 606320 606320 653289 606320 653289 606320 653289 6532...

output:

494331

result:

ok 1 number(s): "494331"

Test #5:

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

input:

1000 2
37158 712133 695735 352502 37158 111876 979836 322207 850 170255 712133 37158 68113 170255 269273 322207 644692 127744 575843 269273 352502 68113 166126 413274 111876 575843 704107 695735 37158 604776 127744 269273 166126 704107 850 111876 352502 979836 850 850 712133 850 979836 575843 704107...

output:

382010

result:

ok 1 number(s): "382010"

Test #6:

score: 0
Accepted
time: 30ms
memory: 58828kb

input:

1000 1
439144 11249 309118 842057 842057 938195 823723 842057 589439 914850 938195 455713 91595 938195 553566 761181 553566 70966 455713 439144 704849 11249 11249 236580 320993 933831 872377 589439 435476 558991 914850 690853 482410 690853 715546 155003 435476 155003 435476 435476 842057 236580 7096...

output:

341042

result:

ok 1 number(s): "341042"

Test #7:

score: 0
Accepted
time: 30ms
memory: 58764kb

input:

1000 1
675227 756097 647871 465683 473815 162625 396802 61313 758469 968915 283015 326083 86904 672294 145904 621859 346020 237440 669779 282692 626616 722910 999031 141389 440533 971280 807058 346648 405103 669779 463968 214284 50833 661085 826455 991362 984243 704592 426668 998421 949882 147122 72...

output:

1

result:

ok 1 number(s): "1"

Test #8:

score: -100
Runtime Error

input:

1000000 1
420242 544222 171447 777913 987553 724009 569564 35971 931705 898245 672004 267555 967920 126950 627536 979230 592634 345070 437097 838283 124796 525563 988560 362647 502828 510728 538451 742569 327219 207978 943947 684879 278534 669721 356340 258535 85663 265714 956295 291196 419333 19692...

output:


result: