結果
| 問題 | No.2065 Sum of Min |
| コンテスト | |
| ユーザー |
Manuel1024
|
| 提出日時 | 2023-09-15 00:57:23 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 82 ms / 2,000 ms |
| コード長 | 2,347 bytes |
| コンパイル時間 | 785 ms |
| コンパイル使用メモリ | 80,844 KB |
| 実行使用メモリ | 9,116 KB |
| 最終ジャッジ日時 | 2024-07-02 06:52:22 |
| 合計ジャッジ時間 | 6,974 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 20 |
ソースコード
#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(n+q);
for(int i = 0; i < q; i++){
cin >> vec[i].l >> vec[i].r >> vec[i].x;
vec[i].l--;
vec[i].id = i;
}
for(int i = 0; i < n; i++){
vec[q+i].x = a[i];
vec[q+i].id = q+i;
}
sort(vec.begin(), vec.end(), [](const tup &x, const tup &y){
return x.x < y.x;
});
FenwickTree<ll> cnt(n), tot(n);
for(int i = 0; i < n; i++){
cnt.add(i, 1);
}
vector<ll> ans(q);
for(auto &it: vec){
if(it.id < q){
ans[it.id] += tot.sum(it.l, it.r);
ans[it.id] += cnt.sum(it.l, it.r)*it.x;
// cerr << cnt.sum(it.l, it.r) << " / " << tot.sum(it.l, it.r) << " " << it.x << " " << ans[it.id] << endl;
}else{
cnt.add(it.id-q, -1);
tot.add(it.id-q, it.x);
}
}
for(int i = 0; i < q; i++) cout << ans[i] << '\n';
return 0;
}
Manuel1024