結果

問題 No.2065 Sum of Min
ユーザー rogi52
提出日時 2022-10-21 02:20:56
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 424 ms / 2,000 ms
コード長 3,084 bytes
コンパイル時間 2,460 ms
コンパイル使用メモリ 215,244 KB
最終ジャッジ日時 2025-02-08 09:32:01
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
template < class comm_monoid > class fenwick_tree {
public:
using T = typename comm_monoid::set;
private:
int n, n2;
vector< T > data;
int ceil_pow2(int n) {
int x = 1;
while(x < n) x <<= 1;
return x;
}
public:
fenwick_tree() : fenwick_tree(0) {}
fenwick_tree(int n) : n(n), n2(ceil_pow2(n)), data(n + 1, comm_monoid::id) { assert(comm_monoid::comm); }
fenwick_tree(const vector< T > &a) : n(a.size()), n2(ceil_pow2(n)), data(a) {
assert(comm_monoid::comm);
data.insert(data.begin(), {comm_monoid::id});
for(int i = 1; i <= n; i++) {
int p = i + (i & -i);
if(p <= n) data[p] = comm_monoid::op(data[i], data[p]);
}
}
void add(int i, T x) {
for(int p = i + 1; p <= n; p += p & -p) data[p] = comm_monoid::op(data[p], x);
}
// [0, r)
T fold(int r) {
T s = comm_monoid::id;
for(int p = r; p > 0; p -= p & -p) s = comm_monoid::op(data[p], s);
return s;
}
// [l, r)
T fold(int l, int r) {
return comm_monoid::op(comm_monoid::inv(fold(l)), fold(r));
}
T get(int i) {
return fold(i, i + 1);
}
void set(int i, T x) {
add(i, comm_monoid::op(comm_monoid::inv(get(i)), x));
}
template< class func > int search(const func &f) {
T s = comm_monoid::id;
if(f(s)) return 0;
int i = 0, k = n2;
while(k >>= 1) {
int p = i | k;
if(p <= n && !f(comm_monoid::op(s, data[p]))) s = comm_monoid::op(s, data[i = p]);
}
return i;
}
};
namespace algebra {
template < class T > class PLUS {
public:
using set = T;
static constexpr T op(const T &l, const T &r) { return l + r; }
static constexpr T id = T(0);
static constexpr T inv(const T &x) { return -x; }
static constexpr T pow(const T &x, const int n) { return x * n; }
static constexpr bool comm = true;
};
}
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
int N,Q; cin >> N >> Q;
vector<ll> A(N);
rep(i,N) cin >> A[i];
vector<tuple<int,int,ll>> query(Q);
vector<vector<ll>> px(N + 1);
vector<ll> V = A;
for(auto &[l, r, x] : query) {
cin >> l >> r >> x; l--;
px[l].push_back(x);
px[r].push_back(x);
V.push_back(x);
}
sort(V.begin(), V.end());
V.erase(unique(V.begin(), V.end()), V.end());
fenwick_tree< algebra::PLUS<ll> > ft(V.size()), cnt(V.size());
map<pair<int,int>,ll> f;
rep(i,N+1) {
for(ll x : px[i]) {
int itx = lower_bound(V.begin(), V.end(), x) - V.begin();
f[{i, x}] = ft.fold(itx + 1) + x * cnt.fold(itx + 1, V.size());
}
if(i == N) break;
int ita = lower_bound(V.begin(), V.end(), A[i]) - V.begin();
ft.add(ita, A[i]);
cnt.add(ita, +1);
}
for(auto &[l, r, x] : query) cout << f[{r, x}] - f[{l, x}] << "\n";
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0