Submission #57767602


Source Code Expand

#include <bits/stdc++.h>
#include <atcoder/all>

using namespace std;
using namespace atcoder;

// template {{{
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define lb lower_bound
#define ub upper_bound
#define f first
#define s second
#define resz resize

#define sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto& a : x)

#define sort_by(x, y) sort(all(x), [&](const auto& a, const auto& b) { return y; })

using ll = int64_t;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vd = vector<double>;
using vs = vector<string>;

using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;

using vpii = vector<pii>;
using vvpii = vector<vpii>;
using vpll = vector<pll>;
using vvpll = vector<vpll>;
using vpdd = vector<pdd>;
using vvpdd = vector<vpdd>;

template<typename T> void ckmin(T& a, const T& b) { a = min(a, b); }
template<typename T> void ckmax(T& a, const T& b) { a = max(a, b); }

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

namespace __algorithm {
    template<typename T> void dedup(vector<T>& v) {
        sort(all(v)); v.erase(unique(all(v)), v.end());
    }
    template<typename T> typename vector<T>::const_iterator find(vector<T>& v, const T& x) {
        auto it = lower_bound(all(v), x); return it != v.end() && *it == x ? it : v.end();
    }
    template<typename T> int index(vector<T>& v, const T& x) {
        auto it = find(v, x); assert(it != v.end() && *it == x); return int(it - v.begin());
    }
    template<typename C, typename T, typename OP> vector<T> prefixes(const C& v, T id, OP op) {
        vector<T> r(sz(v)+1, id); F0R (i, sz(v)) r[i+1] = op(r[i], v[i]); return r;
    }
    template<typename C, typename T, typename OP> vector<T> suffixes(const C& v, T id, OP op) {
        vector<T> r(sz(v)+1, id); F0Rd (i, sz(v)) r[i] = op(v[i], r[i+1]); return r;
    }
}
using namespace __algorithm;

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
struct __monostate {
    friend istream& operator>>(istream& is, const __monostate& ms) { return is; }
    friend ostream& operator<<(ostream& os, const __monostate& ms) { return os; }
    friend __monostate operator+(const __monostate& a, const __monostate& b) { return a; }
} ms;
#pragma GCC diagnostic pop

namespace __input {
    template<class T1, class T2> void re(pair<T1,T2>& p);
    template<class T> void re(vector<T>& a);
    template<class T, size_t SZ> void re(array<T,SZ>& a);
 
    template<class T> void re(T& x) { cin >> x; }
    void re(double& x) { string t; re(t); x = stod(t); }
    template<class Arg, class... Args> void re(Arg& first, Args&... rest) {
        re(first); re(rest...);
    }
 
    template<class T1, class T2> void re(pair<T1,T2>& p) { re(p.f,p.s); }
    template<class T> void re(vector<T>& a) { F0R(i,sz(a)) re(a[i]); }
    template<class T, size_t SZ> void re(array<T,SZ>& a) { F0R(i,SZ) re(a[i]); }
}
using namespace __input;
 
namespace __output {
    template<class T1, class T2> void pr(const pair<T1,T2>& x);
    template<class T, size_t SZ> void pr(const array<T,SZ>& x);
    template<class T> void pr(const vector<T>& x);
    template<class T> void pr(const set<T>& x);
    template<class T1, class T2> void pr(const map<T1,T2>& x);
 
    template<class T> void pr(const T& x) { cout << x; }
    template<class Arg, class... Args> void pr(const Arg& first, const Args&... rest) {
        pr(first); pr(rest...);
    }
 
    template<class T1, class T2> void pr(const pair<T1,T2>& x) {
        pr("{",x.f,", ",x.s,"}");
    }
    template<class T, bool pretty = true> void prContain(const T& x) {
        if (pretty) pr("{");
        bool fst = 1; for (const auto& a: x) pr(!fst?pretty?", ":" ":"",a), fst = 0;
        if (pretty) pr("}");
    }
    template<class T> void pc(const T& x) { prContain<T, false>(x); pr("\n"); }
    template<class T, size_t SZ> void pr(const array<T,SZ>& x) { prContain(x); }
    template<class T> void pr(const vector<T>& x) { prContain(x); }
    template<class T> void pr(const set<T>& x) { prContain(x); }
    template<class T1, class T2> void pr(const map<T1,T2>& x) { prContain(x); }
 
    void ps() { pr("\n"); }
    template<class Arg> void ps(const Arg& first) {
        pr(first); ps();
    }
    template<class Arg, class... Args> void ps(const Arg& first, const Args&... rest) {
        pr(first," "); ps(rest...);
    }
}
using namespace __output;
 
#define TRACE(x) x
#define __pn(x) pr(#x, " = ")
#define pd(...) __pn((__VA_ARGS__)), ps(__VA_ARGS__), cout << flush

struct S {
    int num;
    ll val;
};

S op(S a, S b) {
    return {a.num + b.num, a.val + b.val};
}

S e() {
    return {0, 0};
}

S mapping(ll f, S x) {
    if (f == -1) return x;
    else return {x.num, x.num * f};
}

ll composition(ll f, ll g) {
    if (f == -1) return g;
    else return f;
}

ll id() { return -1; }

using range_set_st = lazy_segtree<S, op, e, ll, mapping, composition, id>;


// using mint = modint998244353;
// using vmi = vector<mint>;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int N;
    cin >> N;
    vll X(N);
    for (int i=0;i<N;i++) cin >> X[i];
    for (int i=0;i<N;i++) X[i] -= i;

    range_set_st segtree(N);
    for (int i=0;i<N;i++) segtree.set(i, {1, X[i]});

    ll ans = 0;

    auto first_gt = [&](ll val) {
        if (segtree.get(0).val > val) return 0;
        int lo = 0;
        int hi = N-1;
        while (hi - lo > 1) {
            int mid = (lo + hi) / 2;
            if (segtree.get(mid).val > val) hi = mid;
            else lo = mid;
        }
        return hi;
    };

    auto last_lt = [&](ll val) {
        if (segtree.get(N-1).val < val) return N-1;
        int lo = 0;
        int hi = N-1;
        while (hi - lo > 1) {
            int mid = (lo + hi) / 2;
            if (segtree.get(mid).val < val) lo = mid;
            else hi = mid;
        }
        return lo;
    };

    int Q;
    cin >> Q;
    for (int q=0;q<Q;q++) {
        int ind;
        ll dest;
        cin >> ind >> dest;
        ind--;
        dest -= ind;

        if (dest < segtree.get(ind).val) {
            // find last index where segtree[ind] > dest
            int lind = first_gt(dest);
            // want to set segtree[lind...ind] -> dest
            // at cost sum(segtree[lind...ind]) - (ind - lind + 1) * dest
            ll cost = segtree.prod(lind, ind+1).val - (ind - lind + 1) * dest;
            ans += cost;
            segtree.apply(lind, ind+1, dest);
        } else if (dest > segtree.get(ind).val) {
            int rind = last_lt(dest);

            ll cost = (rind - ind + 1) * dest - segtree.prod(ind, rind+1).val;
            ans += cost;
            segtree.apply(ind, rind+1, dest);
        }
    }

    cout << ans << endl;

    return 0;
}

Submission Info

Submission Time
Task F - Takahashi in Narrow Road
User sajninredoc
Language C++ 20 (gcc 12.2)
Score 550
Code Size 7377 Byte
Status AC
Exec Time 396 ms
Memory 18376 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 550 / 550
Status
AC × 3
AC × 38
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 02_handmade_35.txt, 02_handmade_36.txt, 02_handmade_37.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3576 KiB
00_sample_01.txt AC 1 ms 3576 KiB
00_sample_02.txt AC 1 ms 3560 KiB
01_random_03.txt AC 394 ms 18160 KiB
01_random_04.txt AC 391 ms 18168 KiB
01_random_05.txt AC 393 ms 18124 KiB
01_random_06.txt AC 391 ms 18280 KiB
01_random_07.txt AC 394 ms 18144 KiB
01_random_08.txt AC 393 ms 18180 KiB
01_random_09.txt AC 394 ms 18224 KiB
01_random_10.txt AC 396 ms 18244 KiB
01_random_11.txt AC 393 ms 18240 KiB
01_random_12.txt AC 393 ms 18272 KiB
01_random_13.txt AC 179 ms 7108 KiB
01_random_14.txt AC 14 ms 10004 KiB
01_random_15.txt AC 249 ms 18164 KiB
01_random_16.txt AC 239 ms 10144 KiB
01_random_17.txt AC 166 ms 10568 KiB
01_random_18.txt AC 331 ms 18024 KiB
01_random_19.txt AC 151 ms 17928 KiB
01_random_20.txt AC 42 ms 6552 KiB
01_random_21.txt AC 278 ms 18376 KiB
01_random_22.txt AC 129 ms 18236 KiB
01_random_23.txt AC 321 ms 18196 KiB
01_random_24.txt AC 326 ms 18208 KiB
01_random_25.txt AC 325 ms 18204 KiB
01_random_26.txt AC 324 ms 18192 KiB
01_random_27.txt AC 141 ms 18204 KiB
01_random_28.txt AC 278 ms 18204 KiB
01_random_29.txt AC 327 ms 18208 KiB
01_random_30.txt AC 325 ms 18212 KiB
01_random_31.txt AC 321 ms 18196 KiB
01_random_32.txt AC 324 ms 18156 KiB
01_random_33.txt AC 140 ms 18196 KiB
01_random_34.txt AC 128 ms 18292 KiB
02_handmade_35.txt AC 110 ms 18256 KiB
02_handmade_36.txt AC 18 ms 3572 KiB
02_handmade_37.txt AC 23 ms 3568 KiB