結果
| 問題 |
No.259 セグメントフィッシング+
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-05-30 17:42:25 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 98 ms / 2,000 ms |
| コード長 | 1,747 bytes |
| コンパイル時間 | 949 ms |
| コンパイル使用メモリ | 101,532 KB |
| 実行使用メモリ | 15,064 KB |
| 最終ジャッジ日時 | 2024-06-12 19:32:05 |
| 合計ジャッジ時間 | 3,973 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 23 |
ソースコード
import std.algorithm, std.conv, std.range, std.stdio, std.string;
void main()
{
auto rd1 = readln.split;
auto n = rd1[0].to!int, n2 = n * 2;
auto q = rd1[1].to!size_t;
auto bt = BiTree!long(n2);
auto summary(int t, int y, int z)
{
auto r = 0L;
auto yl = ((y - t) % n2 + n2) % n2;
auto zl = ((z - t) % n2 + n2) % n2;
if (yl < zl) {
r += bt[yl..zl];
} else {
r += bt[yl..$];
r += bt[0..zl];
}
auto yr = ((n2 - z - t) % n2 + n2) % n2;
auto zr = ((n2 - 1 - y - t) % n2 + n2) % n2 + 1;
if (yr < zr) {
r += bt[yr..zr];
} else {
r += bt[yr..$];
r += bt[0..zr];
}
return r;
}
foreach (_; 0..q) {
auto rd2 = readln.split;
auto x = rd2[0], t = rd2[1].to!int;
switch (x) {
case "C":
auto y = rd2[2].to!int, z = rd2[3].to!int;
writeln(summary(t, y, z));
break;
case "R":
auto y = rd2[2].to!int, z = rd2[3].to!long;
bt[((y - t) % n2 + n2) % n2] += z;
break;
case "L":
auto y = rd2[2].to!int, z = rd2[3].to!long;
bt[((n2 - 1 - y - t) % n2 + n2) % n2] += z;
break;
default:
assert(0);
}
}
}
struct BiTree(T)
{
const size_t n;
T[] buf;
this(size_t n)
{
this.n = n;
this.buf = new T[](n + 1);
}
void opIndexOpAssign(string op: "+")(T val, size_t i)
{
++i;
for (; i <= n; i += i & -i)
buf[i] += val;
}
pure T opSlice(size_t r, size_t l) const
{
return get(l) - get(r);
}
pure size_t opDollar() const { return n; }
pure T opIndex(size_t i) const { return opSlice(i, i+1); }
private:
pure T get(size_t i) const
{
auto s = T(0);
for (; i > 0; i -= i & -i)
s += buf[i];
return s;
}
}