結果

問題 No.2065 Sum of Min
ユーザー Manuel1024Manuel1024
提出日時 2023-09-14 20:12:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,955 bytes
コンパイル時間 960 ms
コンパイル使用メモリ 85,324 KB
実行使用メモリ 8,008 KB
最終ジャッジ日時 2023-09-14 20:12:48
合計ジャッジ時間 30,747 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
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(){
    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(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;
    }

    auto vals2 = compress(a);

    const int bsize = 320;
    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, 0);
    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();
        
        int ac = -1, wa = n;
        while(wa-ac > 1){
            int wj = (ac+wa)/2;
            if(vals2[a[wj]] <= it.x) ac = wj;
            else wa = wj;
        }
        ans[it.id] += tot.sum(0, ac+1);
        ans[it.id] += cnt.sum(ac+1, vals2.size()+10)*it.x;
    }
    for(int i = 0; i < q; i++) cout << ans[i] << '\n';
    return 0;
}
0