QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#121301#6675. DS Team Selection 2DenisovRE 7ms4900kbC++2010.9kb2023-07-07 21:22:202023-07-07 21:22:20

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-07 21:22:20]
  • 评测
  • 测评结果:RE
  • 用时:7ms
  • 内存:4900kb
  • [2023-07-07 21:22:20]
  • 提交

answer

//#pragma GCC optimize("Ofast", "unroll-loops")
//#pragma GCC target("sse", "sse2", "sse3", "ssse3", "sse4")
#ifdef LOCAL
#include <iostream>
#include <cmath>
#include <algorithm>
#include <stdio.h>
#include <cstdint>
#include <cstring>
#include <string>
#include <cstdlib>
#include <vector>
#include <bitset>
#include <map>
#include <queue>
#include <ctime>
#include <stack>
#include <set>
#include <list>
#include <random>
#include <deque>
#include <functional>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <complex>
#include <numeric>
#include <cassert>
#include <array>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <thread>
#else
#include <bits/stdc++.h>
#endif

#define all(a) a.begin(),a.end()
#define len(a) (int)(a.size())
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
#define fi first
#define se second

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;

template<typename T>
inline bool umin(T &a, T b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}

template<typename T>
inline bool umax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

#ifdef LOCAL
#define D for (bool _FLAG = true; _FLAG; _FLAG = false)
#define LOG(...) print(#__VA_ARGS__" ::", __VA_ARGS__) << endl
template <class ...Ts> auto &print(Ts ...ts) { return ((cerr << ts << " "), ...); }
#else
#define D while (false)
#define LOG(...)
#endif // LOCAL

//const int max_n = -1, inf = 1000111222;


const ld inf = 1e18;
struct line {

    ll m, b; ld x;
    ll val; bool isQuery;
    line(ll _m = 0, ll _b = 0) :
        m(_m), b(_b), val(0), x(-inf), isQuery(false) {}

    ll eval(ll x) const { return m * x + b;    }
    bool parallel(const line &l) const { return m == l.m; }
    ld intersect(const line &l) const {
        return parallel(l) ? inf : 1.0 * (l.b - b) / (m - l.m);
    }
    bool operator < (const line &l) const {
        if(l.isQuery) return x < l.val;
        else return m < l.m;
    }
};
struct convex_hull_trick {
/// max
    set<line> hull;
    typedef set<line> :: iterator iter;

    bool cPrev(iter it) { return it != hull.begin(); }
    bool cNext(iter it) { return it != hull.end() && next(it) != hull.end(); }

    bool bad(const line &l1, const line &l2, const line &l3) {
        return l1.intersect(l3) <= l1.intersect(l2);
    }
    bool bad(iter it) {
        return cPrev(it) && cNext(it) && bad(*prev(it), *it, *next(it));
    }

    iter update(iter it) {
        if(!cPrev(it)) return it;
        ld x = it -> intersect(*prev(it));
        line tmp(*it); tmp.x = x;
        it = hull.erase(it);
        return hull.insert(it, tmp);
    }

    void addLine(ll m, ll b) {
        m *= -1;
        b *= -1;
        line l(m, b);
        iter it = hull.lower_bound(l);
        if(it != hull.end() && l.parallel(*it)) {
            if(it -> b < b) it = hull.erase(it);
            else return;
        }

        it = hull.insert(it, l);
        if(bad(it)) return (void) hull.erase(it);

        while(cPrev(it) && bad(prev(it))) hull.erase(prev(it));
        while(cNext(it) && bad(next(it))) hull.erase(next(it));

        it = update(it);
        if(cPrev(it)) update(prev(it));
        if(cNext(it)) update(next(it));
    }

    ll query(ll x) const {
        if(hull.empty()) return -inf;
        line q; q.val = x, q.isQuery = 1;
        iter it = --hull.lower_bound(q);
        return -it -> eval(x);
    }
};


template <class T>
struct fenwick {
public:
    int n;
    vector <T> t; /// !!!


    fenwick (int n) : n(n) {
        t.assign(n, T(0));
    }

    inline void upd (int i, T x) {
        for (; i < n; i = i | (i + 1)) t[i] += x;
    }

    inline T sum (int r) {
        T ans = 0;
        for (; r >= 0; r = (r & (r + 1)) - 1) ans += t[r];
        return ans;
    }

    inline T sum (int l, int r) {
        if (l > r) return T(0); /// !!!
        return sum(r) - sum(l - 1);
    }
};

const ll linf = inf * 1ll * inf;

struct node {

    ll ans, push, k, b, cnt, m, sum_b, sum_k;

    /// not existing node
    node () : ans(0), push(-linf), cnt(0), k(0), b(0), m(0), sum_b(0), sum_k(0)  {}

    node (ll x, int id, int m) : ans(x), k(id), b(x - m * 1ll * id), push(-linf), m(m), cnt(1), sum_k(id), sum_b(x - m * 1ll * id)  {
        /// set to not existing node if needed

    }

    inline ll val (ll x) const {
        if (k == 0) {
            return -linf;
        }
        return k * x + b;
    }
};



inline node pull (node a, node b) {
    node res;
    res.ans = a.ans + b.ans;
    res.cnt = a.cnt + b.cnt;
    res.sum_b = a.sum_b + b.sum_b;
    res.sum_k = a.sum_k + b.sum_k;
    if (b.k) {
        res.k = b.k;
        res.b = b.b;
    }
    else {
        res.k = a.k;
        res.b = a.b;
    }
    return res;
}

struct segment_tree {
    vector <node> t;
    int n;

    segment_tree () {}

    inline void build (int v, int tl, int tr) {
        if (tl == tr) {
            t[v] = node(); /// think
            return;
        }
        int tm = (tl + tr) >> 1;
        build(v << 1, tl, tm);
        build(v << 1 | 1, tm + 1, tr);
        t[v] = pull(t[v << 1], t[v << 1 | 1]);
    }

    segment_tree (int n) : n(n) {
        t.resize(4 * n);
        build(1, 0, n - 1);
    }

    inline void push (int v, int tl, int tr) {
        if (tl != tr && t[v].push != -linf) {
            t[v << 1].ans = t[v].push * t[v << 1].cnt;
            t[v << 1 | 1].ans = t[v].push * t[v << 1 | 1].cnt;

            t[v << 1].sum_b = t[v].push * t[v << 1].cnt - t[v << 1].sum_k * t[v].m;
            t[v << 1 | 1].sum_b = t[v].push * t[v << 1 | 1].cnt - t[v << 1 | 1].sum_k * t[v].m;

            t[v << 1].b = t[v].push - t[v].m * t[v << 1].k;
            t[v << 1 | 1].b = t[v].push - t[v].m * t[v << 1 | 1].k;

            t[v << 1].push = t[v].push;
            t[v << 1 | 1].push = t[v].push;

            t[v << 1].m = t[v].m;
            t[v << 1 | 1].m = t[v].m;

            t[v].push = -linf;
        }
    }

    inline void update (int v, int tl, int tr, int l, int r, int x) { /// think
        push(v, tl, tr);
        if (l > r) return;
        if (tl == l && tr == r) { /// think
            // t[v]
            push(v, tl, tr);
            return;
        }
        int tm = (tl + tr) >> 1;
        update(v << 1, tl, tm, l, min(r, tm), x);
        update(v << 1 | 1, tm + 1, tr, max(tm + 1, l), r, x);
        t[v] = pull(t[v << 1], t[v << 1 | 1]);
    }

    inline void update1 (int v, int tl, int tr, int pos, ll x, int cur_k) { /// think
        push(v, tl, tr);
        if (tl == tr) {
            t[v] = node(x, tr + 1, cur_k);
            return;
        }
        int tm = (tl + tr) >> 1;
        if (pos <= tm) {
            update1(v << 1, tl, tm, pos, x, cur_k);
        }
        else {
            update1(v << 1 | 1, tm + 1, tr, pos, x, cur_k);
        }
        t[v] = pull(t[v << 1], t[v << 1 | 1]);
    }

    inline node query (int v, int tl, int tr, int l, int r) {
        push(v, tl, tr);
        if (l > r) return node();
        if (tl == l && tr == r) {
            return t[v];
        }
        int tm = (tl + tr) >> 1;
        return pull(query(v << 1, tl, tm, l, min(r, tm)), query(v << 1 | 1, tm + 1, tr, max(tm + 1, l), r));
    }

    inline void upd (int l, int r, int x) {
        update(1, 0, n - 1, l, r, x);
    }

    inline void upd1 (int pos, ll x, int cur_k) {
        update1(1, 0, n - 1, pos, x, cur_k);
    }

    inline node get (int l, int r) {
        return query(1, 0, n - 1, l, r);
    }

    inline void make_push (int v, int tl, int tr, ll val, int m) {
        t[v].ans = t[v].cnt * val;
        t[v].b = val - t[v].k * m;
        t[v].sum_b = val * t[v].cnt - t[v].sum_k * m;
        t[v].push = val;
        t[v].m = m;
        push(v, tl, tr);
    }

    inline void go (int v, int tl, int tr, ll val, int m) {
        push(v, tl, tr);
        if (!t[v].k) {
            return;
        }
        if (tl == tr) {
            if (t[v].val(m) > val) {
                make_push(v, tl, tr, val, m);
            }
            return;
        }
        int tm = (tl + tr) >> 1;
        push(v << 1, tl, tm);
        push(v << 1 | 1, tm + 1, tr);
        if (t[v << 1].val(m) > val) {
            make_push(v << 1 | 1, tm + 1, tr, val, m);
            go(v << 1, tl, tm, val, m);
        }
        else {
            go(v << 1 | 1, tm + 1, tr, val, m);
        }
        t[v] = pull(t[v << 1], t[v << 1 | 1]);
    }
};

inline ll sum (ll a0, ll n) {
    return ((2 * a0 + n - 1) * n) / 2ll;
}

int main() {
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);


    int n, q;
    cin >> n >> q;
    vector <ll> a(n);
    for (auto &i : a) cin >> i;
    vector <ll> cnt(n), t(n), l(n), r(n);
    for (int i = 0; i < q; i++) {
        cin >> t[i];
        if (i) {
            cnt[i] = cnt[i - 1];
        }
        if (t[i] == 1) {
            cin >> r[i];
        }
        else if (t[i] == 3) {
            cin >> l[i] >> r[i];
            --l[i], --r[i];
        }
        else {
            ++cnt[i];
        }
    }
    vector <int> L(n, 0), R(n, q);
    while (true) {
        bool ok = false;
        vector <vector <int> > have(q + 1);
        for (int i = 0; i < n; i++) {
            if (L[i] != R[i]) {
                int mid = (L[i] + R[i]) >> 1;
                have[mid].pb(i);
                ok = true;
            }
        }
        if (!ok) {
            break;
        }
        convex_hull_trick cht;
        for (int i = 0; i < q; i++) {
            if (t[i] == 1) {
                cht.addLine(-cnt[i], r[i]);
            }
            for (int id : have[i]) {
                if (!cht.hull.empty() && cht.query(id + 1) <= a[id]) {
                    R[id] = i;
                }
                else {
                    L[id] = i + 1;
                }
            }
        }
    }
    vector <vector <int> > have(q);
    fenwick <ll> T(n);
    for (int i = 0; i < n; i++) {
        if (R[i] < q) {
            have[R[i]].pb(i);
        }
        T.upd(i, a[i]);
    }
    segment_tree tr(n);
    for (int i = 0; i < q; i++) {
        if (t[i] == 1) {
            tr.go(1, 0, n - 1, r[i], cnt[i]);
            for (int id : have[i]) {
                tr.upd1(id, r[i], cnt[i]);
                T.upd(id, -a[id]);
            }
        }
        else if (t[i] == 2) {

        }
        else {
            node res = tr.get(l[i], r[i]);
            ll ans = T.sum(l[i], r[i]) + cnt[i] * sum(l[i] + 1, r[i] - l[i] + 1) + res.sum_b;
            cout << ans << '\n';
        }
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

13 11
6 14 14 6 3 6 4 13 10 3 12 5 11
1 2
2
2
2
1 11
3 4 6
2
1 6
2
1 9
3 2 13

output:

33
107

result:

ok 2 number(s): "33 107"

Test #2:

score: 0
Accepted
time: 7ms
memory: 4840kb

input:

5000 5000
29940 259997 53132 912489 608312 594283 432259 344137 889466 383028 320097 337418 571199 372832 563110 542407 133378 998389 238387 120880 477310 634888 191990 133585 935315 558139 141724 893331 190118 991968 843042 384930 935256 891482 123419 91431 955722 376987 197566 106433 234494 645967...

output:

512185934
455189773
121665669
408693244
291779262
45671866
242375008
302245547
222004631
41963113
343434445
347127029
183849524
2144625
278637672
220461451
20719635
108759503
22099550
34631220
55848925
92362584
36949030
86469096
43509864
50829332
1334865
76069109
114623436
13564322
79974466
15230088...

result:

ok 1671 numbers

Test #3:

score: 0
Accepted
time: 3ms
memory: 4864kb

input:

5000 5000
754848159362 799142221874 945332296572 929342054343 220343371940 207059247564 870301066785 609144766745 830351478389 198801101804 768950635554 592202774571 800496073014 730985048260 581401590014 934021096780 587980626010 77068543347 206074783770 390850923112 122794404396 281461236458 11092...

output:

116508179221533
546749128093796
194349368397972
39703523008217
175276724949769
115828286259777
53486037590699
32085609121169
79863137176116
53634397678952
11984901865039
53065256000101
29045072084569
26415198892331
75111789355520
75384800485844
34569350111656
133340053405484
51324651695791
973372919...

result:

ok 1647 numbers

Test #4:

score: 0
Accepted
time: 2ms
memory: 4820kb

input:

5000 5000
2572389899 2379766449 7410864819 2850299650 5617053831 3824312403 9814616879 8435146331 612453351 6111276232 7985477358 2776770282 2475123938 8017080204 7914003508 932800576 1394850160 1615934603 2716519725 6482033755 4787594046 7431658437 9394772703 5567857454 8294657000 2254310003 522061...

output:

10215584720705
1705389861
345800268
2011341781
2308552335
292528530
262542520
978988815
1435722498
941192965
986070818
873158540
1424652645
128856850
1383766618
1238322582
1869688874
820493117
1165528560
1332973518
2307494520
999182443
932104492
823277168
1062040845
1227853390
1624003330
1769087853
...

result:

ok 1677 numbers

Test #5:

score: 0
Accepted
time: 7ms
memory: 4824kb

input:

5000 5000
999809 999517 999488 999380 999248 998857 998620 998604 998567 998308 998186 997997 997810 997577 997322 997020 996746 996534 996197 995855 995826 995640 995600 995210 995081 994967 994685 994625 994443 994442 994262 993784 993725 993555 993306 993103 993081 992995 992414 991911 991909 991...

output:

16773634
412440775
172002115
859804112
771558481
736128415
379732036
26069114
230547108
124250573
144893959
16706694
204005800
25688354
45410890
426029778
358724432
507488261
286534500
26563180
474284154
22346892
56002662
236061693
295241526
137542524
22942480
293165763
323726733
160884504
192510432...

result:

ok 1650 numbers

Test #6:

score: 0
Accepted
time: 7ms
memory: 4900kb

input:

5000 5000
29989 29976 29976 29973 29966 29964 29960 29959 29950 29950 29943 29942 29939 29934 29931 29921 29914 29898 29873 29853 29845 29839 29833 29823 29793 29789 29779 29767 29765 29763 29759 29743 29742 29727 29724 29717 29711 29711 29710 29709 29709 29705 29695 29692 29687 29683 29678 29673 29...

output:

1677454
842408
7914094
20958765
1919270
1922628
25681578
7752212
4645740
4828824
44635274
4507503
10332327
943492
14007222
24519700
6369696
10888484
8301980
21731014
46268553
396
671
5698
12727
10692
121644
1954398
26911503
6554385
672220
8506939
3468479
3589800
9368964
17529036
9895900
4797296
6049...

result:

ok 1647 numbers

Test #7:

score: 0
Accepted
time: 3ms
memory: 4588kb

input:

5000 500
29995 29990 29989 29988 29985 29981 29976 29971 29964 29937 29930 29922 29919 29910 29883 29883 29866 29864 29855 29855 29850 29843 29842 29836 29834 29828 29827 29821 29820 29819 29819 29818 29817 29814 29800 29799 29798 29794 29783 29777 29769 29765 29758 29754 29750 29745 29741 29741 297...

output:

29842583
9940497
16817240
29922210
13110978
400953
90403379
2520804
5008146
8960380
3019016
10404797
2657305
48660920
16149760
15343500
41648193
29264570
13432320
11089916
14534100
24971099
6748971
9015984
10408641
16352180
4234968
14245138
15540872
8479523
1054657
11951303
1314467
20398020
3903982
...

result:

ok 159 numbers

Test #8:

score: -100
Dangerous Syscalls

input:

500 5000
29998 29894 29881 29844 29816 29792 29788 29741 29403 29329 29188 29184 29044 29039 28923 28878 28667 28624 28578 28567 28397 28374 28266 28233 28185 28120 28078 27835 27806 27778 27770 27769 27501 27475 27387 27320 27318 27209 27188 27183 27173 27125 27005 26977 26972 26949 26927 26784 267...

output:


result: