Skip to content

Commit 25a46f5

Browse files
post : abc287g
1 parent 3b155ab commit 25a46f5

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

_posts/2025-01-04-abc287-g.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
---
2+
title: 【精進1】ABC287-G
3+
date: 2025-1-04 14:45:00 0900
4+
categories: [競技プログラミング]
5+
tags: [AtCoder,ABC,精進]
6+
math : true
7+
---
8+
<!-- Block math, keep all blank lines -->
9+
10+
黄色以上の問題を【精進】として解いていく。現在解いてない黄色の問題はProblemsによると$367$問残っているようなので1日1問程度解けば年内に黄色を埋められる計算になりそれを目指して進めていく
11+
## 問題
12+
[G - Balance Update Query](https://atcoder.jp/contests/abc287/tasks/abc287_g)\
13+
$N$種類のカードがあり、得点$a_i$と使用可能枚数$b_i$が定まっている。$3$種類のクエリを処理する
14+
- $x$種類目の得点を更新
15+
- $x$種類目の使用可能枚数を更新
16+
- 使用可能枚数を守って$x$枚選ぶときの得点の和の最大を出力
17+
18+
## 解法
19+
得点順にソートすればセグメント木の2分探索でクエリ$3$が処理できる。事前にどのような得点があり得るのかを知っておく必要があるためクエリを先読みし座圧しておく。まじめにやると$\log$ $2$つから$1$つにできるということだけど、ここらへんのアルゴリズムはよく知らない。```atcoder::fenwick_tree```ではその機能は提供されていないけど、セグメント木である必要があるんだっけ?
20+
21+
<details open markdown="1"><summary>コード</summary>
22+
23+
```cpp
24+
#include "/home/y_midori/cp/library/template.hpp"
25+
#include "data_structure/compress.hpp"
26+
#include <atcoder/fenwicktree>
27+
void solve() {
28+
LL(n);
29+
vl a(n), b(n);
30+
Compress<ll> cp;
31+
rep(i, n) {
32+
input(a[i], b[i]);
33+
cp.add(-a[i]);
34+
}
35+
cp.add(-inf);
36+
LL(q);
37+
vl flag(q), x(q), y(q);
38+
rep(i, q) {
39+
input(flag[i]);
40+
if(flag[i] == 1) {
41+
input(x[i], y[i]);
42+
x[i]--;
43+
cp.add(-y[i]);
44+
} else if(flag[i] == 2) {
45+
input(x[i], y[i]);
46+
x[i]--;
47+
} else {
48+
input(x[i]);
49+
}
50+
}
51+
cp.build();
52+
ll m = cp.size();
53+
atcoder::fenwick_tree<ll> sz(m + 1), sum(m + 1);
54+
rep(i, n) {
55+
sz.add(cp.get(-a[i]), b[i]);
56+
sum.add(cp.get(-a[i]), a[i] * b[i]);
57+
}
58+
rep(i, q) {
59+
if(flag[i] == 1) {
60+
sz.add(cp.get(-a[x[i]]), -b[x[i]]);
61+
sum.add(cp.get(-a[x[i]]), -a[x[i]] * b[x[i]]);
62+
a[x[i]] = y[i];
63+
sz.add(cp.get(-a[x[i]]), b[x[i]]);
64+
sum.add(cp.get(-a[x[i]]), a[x[i]] * b[x[i]]);
65+
} else if(flag[i] == 2) {
66+
sz.add(cp.get(-a[x[i]]), y[i] - b[x[i]]);
67+
sum.add(cp.get(-a[x[i]]), a[x[i]] * (y[i] - b[x[i]]));
68+
b[x[i]] = y[i];
69+
} else {
70+
if(sz.sum(0, m) < x[i]) {
71+
print(-1);
72+
continue;
73+
}
74+
ll ok = 0, ng = m;
75+
while(ng > ok + 1) {
76+
ll mid = (ok + ng) / 2;
77+
if(sz.sum(0, mid) > x[i]) {
78+
ng = mid;
79+
} else {
80+
ok = mid;
81+
}
82+
}
83+
ll ans = sum.sum(0, ok) - cp[ok] * (x[i] - sz.sum(0, ok));
84+
print(ans);
85+
}
86+
}
87+
}
88+
89+
int main() {
90+
ios::sync_with_stdio(false);
91+
std::cin.tie(nullptr);
92+
cout << std::setprecision(16);
93+
int t = 1;
94+
rep(_, t) {
95+
solve();
96+
}
97+
}
98+
```
99+
</details>
100+
101+
## 別解
102+
オンラインで解く方法が[ここ](https://kanpurin.hatenablog.com/entry/2023/02/06/230403)で解説されてる。ポインタ木周りの知識が足りてないので勉強する。

0 commit comments

Comments
 (0)