結果

問題 No.2065 Sum of Min
ユーザー Manuel1024Manuel1024
提出日時 2023-09-14 19:51:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,926 bytes
コンパイル時間 1,082 ms
コンパイル使用メモリ 86,168 KB
実行使用メモリ 10,748 KB
最終ジャッジ日時 2023-09-14 19:51:42
合計ジャッジ時間 36,215 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 147 ms
8,844 KB
testcase_05 AC 149 ms
9,024 KB
testcase_06 AC 251 ms
7,252 KB
testcase_07 AC 142 ms
7,472 KB
testcase_08 AC 381 ms
7,192 KB
testcase_09 AC 209 ms
10,608 KB
testcase_10 AC 186 ms
10,440 KB
testcase_11 AC 187 ms
10,596 KB
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 TLE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
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(){
    int n, q; cin >> n >> q;
    vector<int> a(n);
    for(auto &it: a) cin >> it;

    vector<tup> vec(q);
    for(int i = 0; i < q; i++){
        cin >> vec[i].l >> vec[i].r >> vec[i].x;
        vec[i].l--; vec[i].r--;
        vec[i].id = i;
    }

    vector<int> vals(n+q);
    for(int i = 0; i < n; i++) vals[i] = a[i];
    for(int i = 0; i < q; i++) vals[n+i] = vec[i].x;
    auto vals2 = compress(vals);
    for(int i = 0; i < n; i++) a[i] = vals[i];
    for(int i = 0; i < q; i++) vec[i].x = vals[n+i];

    const int bsize = 500;
    sort(vec.begin(), vec.end(), [&](const tup &x, const tup &y){
        if(x.l/bsize < y.l/bsize) return true;
        else if(x.l/bsize == y.l/bsize && x.r < y.r) return true;
        else return false;
    });

    int l = 0, r = 0;
    FenwickTree<ll> tot(vals2.size()+10);
    FenwickTree<ll> cnt(vals2.size()+10);
    const auto lpop = [&](){
        tot.add(a[l], -vals2[a[l]]);
        cnt.add(a[l], -1);
        l++;
    };
    const auto lpush = [&](){
        l--;
        tot.add(a[l], vals2[a[l]]);
        cnt.add(a[l], 1);
    };
    const auto rpop = [&](){
        r--;
        tot.add(a[r], -vals2[a[r]]);
        cnt.add(a[r], -1);
    };
    const auto rpush = [&](){
        tot.add(a[r], vals2[a[r]]);
        cnt.add(a[r], 1);
        r++;
    };

    vector<ll> ans(q);
    for(auto &it: vec){
        while(it.r+1 < r) rpop();
        while(r <= it.r) rpush();

        while(l < it.l) lpop();
        while(it.l < l) lpush();
        ans[it.id] = tot.sum(0, it.x)+cnt.sum(it.x, vals2.size()+10)*vals2[it.x];
    }
    for(int i = 0; i < q; i++) cout << ans[i] << '\n';
    return 0;
}
0