結果

問題 No.2065 Sum of Min
ユーザー KKT89KKT89
提出日時 2022-09-02 21:56:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 91 ms / 2,000 ms
コード長 2,350 bytes
コンパイル時間 1,894 ms
コンパイル使用メモリ 142,316 KB
実行使用メモリ 11,604 KB
最終ジャッジ日時 2023-08-10 03:56:15
合計ジャッジ時間 5,849 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 70 ms
9,692 KB
testcase_05 AC 72 ms
10,104 KB
testcase_06 AC 72 ms
10,192 KB
testcase_07 AC 91 ms
10,132 KB
testcase_08 AC 85 ms
9,668 KB
testcase_09 AC 82 ms
10,452 KB
testcase_10 AC 78 ms
9,824 KB
testcase_11 AC 77 ms
11,424 KB
testcase_12 AC 86 ms
9,416 KB
testcase_13 AC 84 ms
10,340 KB
testcase_14 AC 85 ms
10,544 KB
testcase_15 AC 86 ms
11,604 KB
testcase_16 AC 85 ms
10,200 KB
testcase_17 AC 85 ms
10,372 KB
testcase_18 AC 85 ms
10,548 KB
testcase_19 AC 86 ms
10,540 KB
testcase_20 AC 86 ms
10,340 KB
testcase_21 AC 82 ms
9,788 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <sstream>
#include <utility>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <array>
#include <bitset>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline double time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

// 1-indexed
template<typename T>
struct BIT{
    int n;
    vector<T> bit;
    BIT(int n_=0):n(n_),bit(n+1){}
    T sum(int i){
        T res=0;
        for(;i>0;i-=(i&-i))res+=bit[i];
        return res;
    }
    void add(int i,T a){
        if(i==0)return;
        for(;i<=n;i+=(i&-i)){bit[i]+=a;}
    }
    int lower_bound(T k){ // k<=sum(res)
        if(k<=0)return 0;
        int res=0,i=1;
        while((i<<1)<=n)i<<=1;
        for(;i;i>>=1){
            if(res+i<=n&&bit[res+i]<k)k-=bit[res+=i];
        }
        return res+1;
    }
};

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n,q; cin >> n >> q;
    BIT<ll> bit(n),cnt(n);
    vector<pair<pair<int,int>,pair<int,int>>> v;
    for(int i=0;i<n;i++){
        int a; cin >> a;
        v.push_back({{a,i+1},{-1,-1}});
    }
    vector<ll> res(q);
    for(int i=0;i<q;i++){
        int l,r,x; cin >> l >> r >> x;
        v.push_back({{x,i},{l,r}});
    }
    sort(v.begin(), v.end());
    for(auto &p:v){
        if(p.second.first == -1){
            int idx = p.first.second;
            int a = p.first.first;
            bit.add(idx, a);
            cnt.add(idx, 1);
        }
        else{
            int idx = p.first.second;
            ll cn = cnt.sum(p.second.second) - cnt.sum(p.second.first-1);
            ll su = bit.sum(p.second.second) - bit.sum(p.second.first-1);
            res[idx] = su + (ll)(p.second.second-p.second.first+1-cn)*(ll)p.first.first;
        }
    }
    for(int i=0;i<q;i++){
        cout << res[i] << "\n";
    }
}
0