結果
| 問題 | No.2065 Sum of Min | 
| コンテスト | |
| ユーザー |  Manuel1024 | 
| 提出日時 | 2023-09-14 20:14:14 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                TLE
                                 
                             | 
| 実行時間 | - | 
| コード長 | 2,982 bytes | 
| コンパイル時間 | 1,031 ms | 
| コンパイル使用メモリ | 86,140 KB | 
| 実行使用メモリ | 8,064 KB | 
| 最終ジャッジ日時 | 2024-07-02 03:08:22 | 
| 合計ジャッジ時間 | 18,742 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 10 TLE * 10 | 
ソースコード
#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(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 = vals2.size();
        while(wa-ac > 1){
            int wj = (ac+wa)/2;
            if(vals2[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;
}
            
            
            
        