結果
| 問題 | No.2065 Sum of Min |
| コンテスト | |
| ユーザー |
Manuel1024
|
| 提出日時 | 2023-09-14 20:12:17 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,955 bytes |
| 記録 | |
| コンパイル時間 | 1,027 ms |
| コンパイル使用メモリ | 86,056 KB |
| 実行使用メモリ | 8,064 KB |
| 最終ジャッジ日時 | 2024-07-02 03:06:30 |
| 合計ジャッジ時間 | 30,125 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 1 WA * 6 RE * 3 TLE * 10 |
ソースコード
#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;
}
Manuel1024