結果
| 問題 |
No.3078 Difference Sum Query
|
| コンテスト | |
| ユーザー |
👑 |
| 提出日時 | 2025-02-24 08:44:19 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 685 ms / 2,000 ms |
| コード長 | 4,020 bytes |
| コンパイル時間 | 4,691 ms |
| コンパイル使用メモリ | 261,820 KB |
| 実行使用メモリ | 56,152 KB |
| 最終ジャッジ日時 | 2025-02-24 22:52:37 |
| 合計ジャッジ時間 | 13,388 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 19 |
ソースコード
#include<bits/stdc++.h>
#include<atcoder/all>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<vector<int>> vvi;
typedef vector<vector<long long>> vvl;
typedef long double ld;
typedef pair<int, int> P;
template <int m> ostream& operator<<(ostream& os, const static_modint<m>& a) {os << a.val(); return os;}
template <int m> ostream& operator<<(ostream& os, const dynamic_modint<m>& a) {os << a.val(); return os;}
template <int m> istream& operator>>(istream& is, static_modint<m>& a) {long long x; is >> x; a = x; return is;}
template <int m> istream& operator>>(istream& is, dynamic_modint<m>& a) {long long x; is >> x; a = x; return is;}
template<typename T> istream& operator>>(istream& is, vector<T>& v){int n = v.size(); assert(n > 0); rep(i, n) is >> v[i]; return is;}
template<typename U, typename T> ostream& operator<<(ostream& os, const pair<U, T>& p){os << p.first << ' ' << p.second; return os;}
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : " "); return os;}
template<typename T> ostream& operator<<(ostream& os, const vector<vector<T>>& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : ""); return os;}
template<typename T> ostream& operator<<(ostream& os, const set<T>& se){for(T x : se) os << x << " "; os << "\n"; return os;}
template<typename T> ostream& operator<<(ostream& os, const unordered_set<T>& se){for(T x : se) os << x << " "; os << "\n"; return os;}
template<typename S, auto op, auto e> ostream& operator<<(ostream& os, const atcoder::segtree<S, op, e>& seg){int n = seg.max_right(0, [](S){return true;}); rep(i, n) os << seg.get(i) << (i == n - 1 ? "\n" : " "); return os;}
template<typename S, auto op, auto e, typename F, auto mapping, auto composition, auto id> ostream& operator<<(ostream& os, const atcoder::lazy_segtree<S, op, e, F, mapping, composition, id>& seg){int n = seg.max_right(0, [](S){return true;}); rep(i, n) os << seg.get(i) << (i == n - 1 ? "\n" : " "); return os;}
template<typename T> void chmin(T& a, T b){a = min(a, b);}
template<typename T> void chmax(T& a, T b){a = max(a, b);}
// Arranged from
// https://atcoder.jp/contests/abc339/submissions/49979258
struct MergeSortTree{
int n;
vector<vector<long long>> a, s;
bool initialized;
MergeSortTree(int _n){
initialized = false;
n = 1;
while(n < _n) n <<= 1;
a.resize(2 * n);
s.resize(2 * n, vector<long long>{0});
}
void set(int i, long long val){
a[n + i].push_back(val);
s[n + i].push_back(val);
}
void init(){
initialized = true;
for(int i = n - 1; i >= 0; i--){
int l = (i << 1), r = l + 1;
merge(a[l].begin(), a[l].end(), a[r].begin(), a[r].end(), back_inserter(a[i]));
s[i].resize(int(a[i].size()) + 1);
rep(j, int(a[i].size())) s[i][j + 1] = s[i][j] + a[i][j];
}
}
// [l, r)
// get #(>= x) - #(<x)
// get \sigma(>= x) - \sigma(<x)
pair<int, long long> get(int i, long long x) {
int sz = a[i].size();
int j = lower_bound(a[i].begin(), a[i].end(), x) - a[i].begin();
return {(sz - j) - (j - 0), (s[i][sz] - s[i][j]) - (s[i][j] - s[i][0])};
}
pair<int, long long> query(int l, int r, long long x){
if(!initialized) init();
l += n;
r += n;
int num = 0;
long long sum = 0;
while(l < r){
if(l & 1){
auto [num_sub, sum_sub] = get(l, x);
num += num_sub;
sum += sum_sub;
l++;
}
if(r & 1){
r--;
auto [num_sub, sum_sub] = get(r, x);
num += num_sub;
sum += sum_sub;
}
l >>= 1;
r >>= 1;
}
return {num, sum};
}
};
int main(){
int n, q;
cin >> n >> q;
vector<long long> a(n);
cin >> a;
MergeSortTree mst(n);
rep(i, n) mst.set(i, a[i]);
rep(_, q){
int l, r;
long long x;
cin >> l >> r >> x;
l--; r--;
auto [num, sum] = mst.query(l, r + 1, x);
long long ans = sum - (long long)num * x;
cout << ans << "\n";
}
return 0;
}