結果
問題 | No.924 紲星 |
ユーザー | shinchan |
提出日時 | 2023-09-12 18:20:59 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 3,770 ms / 4,000 ms |
コード長 | 3,215 bytes |
コンパイル時間 | 3,125 ms |
コンパイル使用メモリ | 231,684 KB |
実行使用メモリ | 50,284 KB |
最終ジャッジ日時 | 2024-06-30 06:02:53 |
合計ジャッジ時間 | 32,682 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 2 ms
6,944 KB |
testcase_03 | AC | 7 ms
6,944 KB |
testcase_04 | AC | 4 ms
6,944 KB |
testcase_05 | AC | 8 ms
6,940 KB |
testcase_06 | AC | 8 ms
6,940 KB |
testcase_07 | AC | 3 ms
6,940 KB |
testcase_08 | AC | 3,490 ms
48,744 KB |
testcase_09 | AC | 3,770 ms
48,968 KB |
testcase_10 | AC | 3,575 ms
46,664 KB |
testcase_11 | AC | 3,451 ms
47,828 KB |
testcase_12 | AC | 3,542 ms
50,284 KB |
testcase_13 | AC | 1,710 ms
26,088 KB |
testcase_14 | AC | 1,815 ms
35,260 KB |
testcase_15 | AC | 1,556 ms
23,688 KB |
testcase_16 | AC | 983 ms
23,348 KB |
testcase_17 | AC | 2,745 ms
42,516 KB |
testcase_18 | AC | 2 ms
6,944 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; #define all(v) (v).begin(),(v).end() #define pb(a) push_back(a) #define rep(i, n) for(int i=0;i<n;i++) #define foa(e, v) for(auto& e : v) using ll = long long; const ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60); #define dout(a) cout<<fixed<<setprecision(10)<<a<<endl; struct SegmentTree { private: ll n; vector<ll> node; ll ID = 0; // min -> INF, max, sum -> 0 inline ll func(ll a, ll b){return a + b;} ///////// public: SegmentTree(vector<ll> v) { int sz = v.size(); n = 1; while(n < sz) n *= 2; node.resize(2*n-1, ID); for(int i=0; i<sz; i++) node[i+n-1] = v[i]; for(int i=n-2; i>=0; i--) node[i] = func(node[2*i+1], node[2*i+2]); } void add(ll x, ll val) { x += (n - 1); node[x] += val; while(x > 0) { x = (x - 1) / 2; node[x] = func(node[2*x+1], node[2*x+2]); } } ll get(ll a, ll b, ll k=0, ll l=0, ll r=-1) { if(r < 0) r = n; if(r <= a || b <= l) return ID; if(a <= l && r <= b) return node[k]; ll vl = get(a, b, 2*k+1, l, (l+r)/2); ll vr = get(a, b, 2*k+2, (l+r)/2, r); return func(vl, vr); } }; struct node { ll a, b, c, d, e, f; bool operator<( const node& right ) const { return a < right.a; } }; using T = tuple<ll, ll, ll, ll, ll, ll>; int main() { cin.tie(0); ios::sync_with_stdio(false); ll n, q; cin >> n >> q; vector<pair<ll, ll>> v; SegmentTree seg_(vector<ll> (n, 0)); rep(i, n) { ll a; cin >> a; seg_.add(i, a); v.push_back({a, i}); } sort(all(v)); vector<node> task; rep(i, q) { ll le, ri; cin >> le >> ri; task.push_back({0LL, -1LL << 30, 1LL << 30, le - 1, ri, i}); } sort(all(task)); bool f = true; vector<tuple<ll, ll, ll, ll>> query; while(f) { vector<node> nx; int id = 0; f = false; SegmentTree seg(vector<ll> (n, 0)); for(auto [mid, Min, Max, le, ri, i] : task) { while(id < n and v[id].first <= mid) { seg.add(v[id].second, 1); id ++; } if(seg.get(le, ri) * 2 >= (ri - le)) Max = mid; else Min = mid; ll m = (Max + Min) / 2; if(Max - Min == 1LL) { query.push_back({Max, le, ri, i}); continue; } f = true; nx.push_back({m, Min, Max, le, ri, i}); } swap(nx, task); sort(all(task)); } sort(all(query)); vector<ll> ans(q); { int id = 0; SegmentTree seg(vector<ll> (n, 0)), seg_cnt(vector<ll> (n, 0)); for(auto [m, le, ri, i] : query) { while(id < n and v[id].first <= m) { seg_cnt.add(v[id].second, 1); seg.add(v[id].second, - v[id].first); id ++; } ans[i] = seg_.get(le, ri) + seg.get(le, ri) * 2 - m * (ri - le) + m * 2 * seg_cnt.get(le, ri); } } rep(i, q) { cout << ans[i] << endl; } return 0; }