Created
November 3, 2015 17:35
-
-
Save ctylim/f8c2131a2b6211474550 to your computer and use it in GitHub Desktop.
yukicoder No.259
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <iomanip> | |
#include <vector> | |
#include <algorithm> | |
#include <numeric> | |
#include <functional> | |
#include <cmath> | |
#include <queue> | |
#include <stack> | |
#include <set> | |
#include <map> | |
#include <sstream> | |
#include <string> | |
#define repd(i,a,b) for (int i=(a);i<(b);i++) | |
#define rep(i,n) repd(i,0,n) | |
#define var auto | |
#define mod 1000000007 | |
#define inf 2147483647 | |
typedef long long ll; | |
using namespace std; | |
int inputValue(){ | |
int a; | |
cin >> a; | |
return a; | |
} | |
template <typename T> | |
void output(T a, int precision) { | |
if(precision > 0){ | |
cout << fixed << setprecision(precision) << a << "\n"; | |
} | |
else{ | |
cout << a << "\n"; | |
} | |
} | |
// end of template | |
struct bit { | |
vector<ll> v; // 部分和をbitで管理 仮想配列はa[1...n] | |
bit(int n){ // bit(int n) : v(n + 1) {} | |
v.resize(n + 1); | |
} | |
ll sum(int i){ // 仮想配列a[1]~a[i]の和を求める | |
ll ret = 0; | |
for(; i > 0; i -= i & -i){ // i & -i: iの1が立っている最高位bitをの2^を返す | |
ret += v[i]; | |
} | |
return ret; | |
} | |
void add(int i, ll w) { // index:iにwを加える | |
for (; i < v.size(); i += i & -i) { | |
v[i] += w; | |
} | |
} | |
}; | |
int N; | |
int mod2N(int i){ // return 1 ~ 2N | |
i -= 1; | |
return ((i % (2 * N)) + 2 * N) % (2 * N) + 1; | |
} | |
int main() { | |
// source code | |
N = inputValue(); | |
int Q = inputValue(); | |
bit b(2 * N); | |
rep(i, Q){ | |
char c; | |
cin >> c; | |
int t = inputValue(); | |
int y = inputValue(); | |
int z = inputValue(); | |
if (c == 'R') { // right | |
b.add(mod2N(y - t + 1), z); | |
} | |
if (c == 'L') { // left | |
b.add(mod2N(2 * N - y - t), z); | |
} | |
if (c == 'C') { // count | |
int l1 = mod2N(y - t); | |
int r1 = mod2N(z - t); | |
int l2 = mod2N(2 * N - z - t); | |
int r2 = mod2N(2 * N - y - t); | |
ll ret1 = b.sum(r1) - b.sum(l1); | |
ll ret2 = b.sum(r2) - b.sum(l2); | |
if (l1 > r1) { | |
ret1 += b.sum(2 * N); | |
} | |
if (l2 > r2) { | |
ret2 += b.sum(2 * N); | |
} | |
output(ret1 + ret2, 0); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment