結果
問題 | No.151 セグメントフィッシング |
ユーザー |
|
提出日時 | 2015-02-14 00:08:36 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 42 ms / 5,000 ms |
コード長 | 2,034 bytes |
コンパイル時間 | 770 ms |
コンパイル使用メモリ | 93,768 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-06-23 19:52:56 |
合計ジャッジ時間 | 1,938 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 19 |
ソースコード
#include <cstdio> #include <iostream> #include <sstream> #include <fstream> #include <iomanip> #include <algorithm> #include <cmath> #include <string> #include <vector> #include <list> #include <queue> #include <stack> #include <set> #include <map> #include <bitset> #include <numeric> #include <limits> #include <climits> #include <cfloat> #include <functional> using namespace std; // Binary Indexed Tree template <class T = int> class BinaryIndexedTree { private: int n; vector<T> data; public: BinaryIndexedTree(int n){ // コンストラクタ this->n = n; data.assign(n+1, 0); } void add(int k, T x){ // k番目の要素にxを加算する ++ k; while(k <= n){ data[k] += x; k += k & -k; } } T sum(int k){ // 区間[0,k]の総和を返す ++ k; T ret = 0; while(k > 0){ ret += data[k]; k -= k & -k; } return ret; } T sum(int a, int b){ // 区間[a,b]の総和を返す return sum(b) - sum(a-1); } }; int main() { int n, q; cin >> n >> q; BinaryIndexedTree<long long> bit(2*n); for(int t=0; t<q; ++t){ char x; int y, z; cin >> x >> y >> z; if(x == 'R'){ int a = (2 * n - 1 - y + t) % (2 * n); bit.add(a, z); } else if(x == 'L'){ int a = (y + t) % (2 * n); bit.add(a, z); } else{ long long ret = 0; int a = (y + t) % (2 * n); int b = (z - 1 + t) % (2 * n); if(a <= b) ret += bit.sum(a, b); else ret += bit.sum(0, b) + bit.sum(a, 2*n-1); b = (2 * n - 1 - y + t) % (2 * n); a = (2 * n - z + t) % (2 * n); if(a <= b) ret += bit.sum(a, b); else ret += bit.sum(0, b) + bit.sum(a, 2*n-1); cout << ret << endl; } } return 0; }