結果

問題 No.2065 Sum of Min
ユーザー srjywrdnprkt
提出日時 2023-05-05 20:28:58
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 701 ms / 2,000 ms
コード長 2,017 bytes
コンパイル時間 1,309 ms
コンパイル使用メモリ 125,116 KB
最終ジャッジ日時 2025-02-12 17:20:49
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <numeric>
#include <deque>
#include <complex>
#include <cassert>

using namespace std;
using ll = long long;

template <typename T>
map<T, T> compress(vector<T> &A){

    map<T, T> comp;
    int N = A.size(), i=0;
    for (int i=0; i<N; i++) comp[A[i]];

    for (auto &e : comp){
        e.second = i;
        i++;
    }

    return comp;
}

template<class T> struct BIT {
    vector<T> bit;
    long long N;
    T P;

    BIT (long long n, T p = 0){
        N = n;
        P = p;
        bit.resize(N);
    }

    void add(long long loc, T val){
        loc++;
        while(loc <= N){
            bit[loc-1] += val;
            if (P) bit[loc-1] %= P;
            loc += loc & -loc;
        }
    }

    long long sum(long long l, long long r){
        T res = _sum(r) - _sum(l-1);
        if (P) res = (res + P) % P;
        return res;
    }

    long long _sum(long long r){
        r++;
        T res = 0;
        while(r > 0){
            res += bit[r-1];
            if (P) res %= P;
            r -= r & -r;
        }
        return res;
    }
};


int main(){

    ll N, Q, L, R, X, now=0;
    cin >> N >> Q;
    vector<ll> v, A(N), ans(2*Q);
    vector<tuple<ll, ll, ll>> RXM(2*Q);
    BIT<ll> tc(3e5+5), ts(3e5+5);

    for (int i=0; i<N; i++){
        cin >> A[i];
        v.push_back(A[i]);
    }
    for (int i=0; i<Q; i++){
        cin >> L >> R >> X; L--; R--;
        v.push_back(X);
        RXM[i*2] = {L-1, X, i*2};
        RXM[i*2+1] = {R, X, i*2+1};
    }
    sort(RXM.begin(), RXM.end());

    map<ll, ll> mp = compress(v);

    for (auto [R, X, M] : RXM){
        while(now <= R){
            tc.add(mp[A[now]], 1);
            ts.add(mp[A[now]], A[now]);
            now++;
        }
        ans[M] = ts.sum(0, mp[X]-1) +  tc.sum(mp[X], 3e5+4) * X;
    }

    for (int i=0; i<Q; i++) cout << ans[i*2+1] - ans[i*2] << endl;

    return 0;
}
0