結果
| 問題 | No.924 紲星 |
| コンテスト | |
| ユーザー |
milanis48663220
|
| 提出日時 | 2023-08-11 00:33:02 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2,606 ms / 4,000 ms |
| コード長 | 4,336 bytes |
| コンパイル時間 | 1,347 ms |
| コンパイル使用メモリ | 131,892 KB |
| 最終ジャッジ日時 | 2025-02-16 00:39:18 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 16 |
ソースコード
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>
#include <atcoder/segtree>
#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
using namespace std;
typedef long long ll;
template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
return vector<vector<T>>(n, vector<T>(m, v));
}
template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}
template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
if(v.empty()) {
cout << endl;
return;
}
for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
cout << v.back() << endl;
}
template<typename T>
class Compress{
public:
vector<T> data;
int offset;
Compress(vector<T> data_, int offset=0): offset(offset){
data = data_;
sort(begin(data), end(data));
data.erase(unique(begin(data), end(data)), end(data));
};
int operator[](T x) {
auto p = lower_bound(data.begin(), data.end(), x);
assert(x == *p);
return offset+(p-data.begin());
}
T inv(int x){
return data[x-offset];
}
int size(){
return data.size();
}
};
template<typename T>
class Cumsum{
public:
Cumsum(vector<T> v): v(v){
n = v.size();
v_cumsum = vector<T>(n+1, T(0));
for(int i = 0; i < n; i++) v_cumsum[i+1] = v_cumsum[i]+v[i];
}
/**
* v[l] + ... + v[r-1]
*/
T sum(int l, int r){
if(r <= l) return T(0);
if(r > n) r = n;
if(l < 0) l = 0;
return v_cumsum[r]-v_cumsum[l];
}
private:
int n;
vector<T> v;
vector<T> v_cumsum;
};
ll op(ll x, ll y){
return x+y;
}
ll e(){
return 0;
}
using Seg = atcoder::segtree<ll, op, e>;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(10) << fixed;
int n, q; cin >> n >> q;
vector<ll> a(n);
for(int i = 0; i < n; i++) cin >> a[i];
vector<int> l(q), r(q);
for(int i = 0; i < q; i++){
cin >> l[i] >> r[i]; l[i]--;
}
auto cp = Compress<ll>(a);
int m = cp.size();
vector<vector<int>> indices(m+1);
for(int i = 0; i < n; i++) indices[cp[a[i]]].push_back(i);
vector<int> lb(q, 0), rb(q, m);
int iter = 30;
while(iter--){
Seg seg(n);
vector<vector<int>> query(m+1);
for(int i = 0; i < q; i++){
int idx = (lb[i]+rb[i])/2;
query[idx].push_back(i);
}
for(int i = m; i >= 0; i--){
for(int idx: indices[i]){
seg.set(idx, 1);
}
for(int idx: query[i]){
int len = r[idx]-l[idx];
int cnt = seg.prod(l[idx], r[idx]);
if(cnt*2 >= len) lb[idx] = i;
else rb[idx] = i;
}
}
}
auto cumsum = Cumsum<ll>(a);
vector<vector<int>> query(m+1);
for(int i = 0; i < q; i++){
query[lb[i]].push_back(i);
}
vector<ll> ans(q);
Seg seg_large(n);
Seg seg_cnt(n);
for(int i = m; i >= 0; i--){
for(int idx: indices[i]){
seg_large.set(idx, a[idx]);
seg_cnt.set(idx, 1);
}
for(int idx: query[i]){
int len = r[idx]-l[idx];
int cnt_large = seg_cnt.prod(l[idx], r[idx]);
int cnt_small = len-cnt_large;
ll sum_large = seg_large.prod(l[idx], r[idx]);
ll sum_small = cumsum.sum(l[idx], r[idx])-sum_large;
ll med = cp.data[i];
// cout << idx << ":" << sum_small << ' ' << sum_large << ' ' << med << ' ' << cnt_large << ' ' << cnt_small << endl;
ans[idx] = sum_large-cnt_large*med + cnt_small*med - sum_small;
}
}
print_vector(ans, '\n');
}
milanis48663220