結果
問題 | No.2101 [Cherry Alpha N] ずっとこの数列だったらいいのに |
ユーザー |
|
提出日時 | 2022-10-15 01:51:02 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,562 ms / 6,000 ms |
コード長 | 4,251 bytes |
コンパイル時間 | 2,580 ms |
コンパイル使用メモリ | 218,720 KB |
最終ジャッジ日時 | 2025-02-08 05:53:42 |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 42 |
ソースコード
#include <bits/stdc++.h>// clang-format offusing namespace std; using ll=long long; using ull=unsigned long long; using pll=pair<ll,ll>; const ll INF=4e18;void print0(){}; template<typename H,typename... T> void print0(H h,T... t){cout<<h;print0(t...);}void print(){print0("\n");}; template<typename H,typename... T>void print(H h,T... t){print0(h);if(sizeof...(T)>0)print0(" ");print(t...);}void perr0(){}; template<typename H,typename... T> void perr0(H h,T... t){cerr<<h;perr0(t...);}void perr(){perr0("\n");}; template<typename H,typename... T>void perr(H h,T... t){perr0(h);if(sizeof...(T)>0)perr0(" ");perr(t...);}void ioinit() { cout<<fixed<<setprecision(15); cerr<<fixed<<setprecision(6); ios_base::sync_with_stdio(0); cin.tie(0); }#define debug1(a) { cerr<<#a<<":"<<a<<endl; }#define debug2(a,b) { cerr<<#a<<":"<<a<<" "<<#b<<":"<<b<<endl; }#define debug3(a,b,c) { cerr<<#a<<":"<<a<<" "<<#b<<":"<<b<<" "<<#c<<":"<<c<<endl; }#define debug4(a,b,c,d) { cerr<<#a<<":"<<a<<" "<<#b<<":"<<b<<" "<<#c<<":"<<c<<" "<<#d<<":"<<d<<endl; }// clang-format onint main() {// 場所ごとに「減り始めるイベント」「減らなくなるイベント」が1回ずつある// 地獄のような平方分割ioinit();ll N;cin >> N;vector<pll> AT(N);for (ll i = 0; i < N; i++) {cin >> AT[i].first >> AT[i].second;}ll Q;cin >> Q;using tll = tuple<ll, ll, ll>;using qll = tuple<ll, ll, ll, ll>;vector<tll> queries;for (ll i = 0; i < Q; i++) {ll d, l, r;cin >> d >> l >> r;l--;r--;queries.push_back({d, l, r});}vector<qll> evt;for (ll i = 0; i < N; i++) {ll t = AT[i].second;evt.push_back({t, 0, -1, i});ll t2 = AT[i].first + AT[i].second - 1;evt.push_back({t2, 0, 1, i});}for (ll i = 0; i < Q; i++) {ll d, l, r;tie(d, l, r) = queries[i];evt.push_back({d, 1, i, i});}const ll SQ = 500;vector<ll> i2b(N);for (ll i = 0; i < N; i++) {i2b[i] = (i / SQ);}ll BK = i2b[N - 1] + 1;vector<vector<ll>> b2i(BK);vector<ll> bleft(BK, INF);vector<ll> bright(BK, -1);for (ll i = 0; i < N; i++) {ll b = i2b[i];b2i[b].push_back(i);bleft[b] = min(bleft[b], i);bright[b] = max(bright[b], i);}vector<ll> sum(BK);vector<ll> diff(BK, 0);for (ll b = 0; b < BK; b++) {ll s = 0;for (auto i : b2i[b]) {s += AT[i].first;}sum[b] = s;}vector<ll> ans(Q);ll pre_tim = 0;sort(evt.begin(), evt.end());for (auto e : evt) {ll tim, kind, _tmp, _tmp2;tie(tim, kind, _tmp, _tmp2) = e;ll tim_diff = tim - pre_tim;if (kind == 1) {ll qid = _tmp;for (ll b = 0; b < BK; b++) {sum[b] += diff[b] * tim_diff;}ll d, l, r;tie(d, l, r) = queries[qid];ll s = 0;{for (ll b = 0; b < BK; b++) {if (l <= bleft[b] && bright[b] <= r) {s += sum[b];continue;}if (r < bleft[b]) continue;if (bright[b] < l) continue;for (auto i : b2i[b]) {if (l <= i && i <= r) {ll now = AT[i].first;if (tim >= AT[i].second) {now -= tim - AT[i].second + 1;if (now < 0) now = 0;}s += now;}}}}ans[qid] = s;} else {ll d = _tmp;ll i = _tmp2;for (ll b = 0; b < BK; b++) {sum[b] += diff[b] * tim_diff;}if (d < 0) sum[i2b[i]]--;diff[i2b[i]] += d;// debug3(tim, diff[0], sum[0]);}pre_tim = tim;}for (ll q = 0; q < Q; q++) {print(ans[q]);}return 0;}