結果
| 問題 |
No.151 セグメントフィッシング
|
| コンテスト | |
| ユーザー |
Nachia
|
| 提出日時 | 2020-10-24 17:29:16 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 44 ms / 5,000 ms |
| コード長 | 2,261 bytes |
| コンパイル時間 | 1,470 ms |
| コンパイル使用メモリ | 170,584 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-21 15:19:49 |
| 合計ジャッジ時間 | 3,173 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 19 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)
template<
class S,
S(*op)(S a, S b),
S(*e)()
>
struct segtree {
private:
int N;
vector<S> V;
public:
segtree(int n) {
N = 1; while (N < n) N *= 2;
V.assign(N * 2, e());
}
segtree(vector<S> A) {
N = 1; while (N < A.size()) N *= 2;
V.assign(N * 2, e());
rep(i, A.size()) V[N + i] = A[i];
for (int i = N - 1; i >= 1; i--)
V[i] = op(V[i * 2], V[i * 2 + 1]);
}
void set(int p, S v) {
p += N;
V[p] = v;
while (p != 1) {
p /= 2;
V[p] = op(V[p * 2], V[p * 2 + 1]);
}
}
S get(int p) {
p += N;
return V[p];
}
S prod(int l, int r) {
S ans_l = e(), ans_r = e();
l += N; r += N;
while (l < r) {
if (l & 1) ans_l = op(ans_l, V[l++]);
if (r & 1) ans_r = op(V[--r], ans_r);
l /= 2; r /= 2;
}
return op(ans_l, ans_r);
}
};
struct S { LL x; };
S op(S l, S r) { return { l.x + r.x }; }
S e() { return { 0 }; }
using RQ = segtree<S, op, e>;
int main() {
int N, Q; cin >> N >> Q;
RQ G(N * 2);
auto applyT = [N](int p, int t)->int { return (p + t) % (2 * N); };
for (int q = 1; q <= Q; q++) {
char c; cin >> c;
if (c == 'L') {
int y, z; cin >> y >> z;
y = applyT(y, q);
G.set(y, op(G.get(y), S{ z }));
}
if (c == 'R') {
int y, z; cin >> y >> z;
y = applyT(N * 2 - 1 - y, q);
G.set(y, op(G.get(y), S{ z }));
}
if (c == 'C') {
int l, r; cin >> l >> r;
LL ans = 0;
int xl = applyT(l, q);
int xr = applyT(r, q);
if (xl < xr) ans += G.prod(xl, xr).x;
else ans += G.prod(0, xr).x + G.prod(xl, 2 * N).x;
xl = applyT(2 * N - r, q);
xr = applyT(2 * N - l, q);
if (xl < xr) ans += G.prod(xl, xr).x;
else ans += G.prod(0, xr).x + G.prod(xl, 2 * N).x;
cout << ans << endl;
}
}
return 0;
}
Nachia