結果

問題 No.2065 Sum of Min
ユーザー Manuel1024Manuel1024
提出日時 2023-09-15 00:57:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 82 ms / 2,000 ms
コード長 2,347 bytes
コンパイル時間 881 ms
コンパイル使用メモリ 80,080 KB
実行使用メモリ 9,104 KB
最終ジャッジ日時 2023-09-15 00:57:31
合計ジャッジ時間 6,501 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 63 ms
9,080 KB
testcase_05 AC 64 ms
8,800 KB
testcase_06 AC 54 ms
9,056 KB
testcase_07 AC 58 ms
9,048 KB
testcase_08 AC 67 ms
9,104 KB
testcase_09 AC 77 ms
8,884 KB
testcase_10 AC 72 ms
8,980 KB
testcase_11 AC 74 ms
8,800 KB
testcase_12 AC 80 ms
8,912 KB
testcase_13 AC 80 ms
8,920 KB
testcase_14 AC 81 ms
9,044 KB
testcase_15 AC 81 ms
8,784 KB
testcase_16 AC 79 ms
8,912 KB
testcase_17 AC 82 ms
8,848 KB
testcase_18 AC 80 ms
8,984 KB
testcase_19 AC 80 ms
8,840 KB
testcase_20 AC 79 ms
9,080 KB
testcase_21 AC 81 ms
9,100 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
using namespace std;
using ll = long long;

template <typename T>
struct FenwickTree{
    const int n;
    vector<T> arr;
    FenwickTree() = default;
    FenwickTree(int n): n(n), arr(vector<T>(n+1, 0)){}

    void add(int ind, T x){
        for(int i = ind+1; i <= n; i += i & (-i)){
            arr[i] += x;
        }
    }

    T sum_sub(int end){
        T res = 0;
        for(int i = end; i > 0; i -= i & (-i)){
            res += arr[i];
        }
        return res;
    }

    T sum(int start, int end){
        return sum_sub(end) - sum_sub(start);
    }

    int lower_bound(T w){
        if(w <= 0) return 0;
        int x = 0;
        int k = 1 << 30;
        while(k > n) k /= 2;
        for(; k > 0; k /= 2){
            if(x+k <= n && arr[x+k] < w){
                w -= arr[x+k];
                x += k;
            }
        }
        return x;
    }
};

template <typename T>
vector<T> compress(vector<T> &x){
    vector<T> vals = x;
    sort(vals.begin(), vals.end());
    vals.erase(unique(vals.begin(), vals.end()), vals.end());
    for(auto &p: x){
        p = lower_bound(vals.begin(), vals.end(), p) - vals.begin();
    }
    return vals;
}

struct tup{
    int l, r, x, id;
};

int main(){
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n, q; cin >> n >> q;
    vector<int> a(n);
    for(auto &it: a) cin >> it;

    vector<tup> vec(n+q);
    for(int i = 0; i < q; i++){
        cin >> vec[i].l >> vec[i].r >> vec[i].x;
        vec[i].l--;
        vec[i].id = i;
    }
    for(int i = 0; i < n; i++){
        vec[q+i].x = a[i];
        vec[q+i].id = q+i;
    }
    sort(vec.begin(), vec.end(), [](const tup &x, const tup &y){
        return x.x < y.x;
    });

    FenwickTree<ll> cnt(n), tot(n);
    for(int i = 0; i < n; i++){
        cnt.add(i, 1);
    }

    vector<ll> ans(q);
    for(auto &it: vec){
        if(it.id < q){
            ans[it.id] += tot.sum(it.l, it.r);
            ans[it.id] += cnt.sum(it.l, it.r)*it.x;
            // cerr << cnt.sum(it.l, it.r) << " / " << tot.sum(it.l, it.r) << " " << it.x << " " << ans[it.id] << endl;
        }else{
            cnt.add(it.id-q, -1);
            tot.add(it.id-q, it.x);
        }
    }

    for(int i = 0; i < q; i++) cout << ans[i] << '\n';
    return 0;
}
0