結果

問題 No.259 セグメントフィッシング+
ユーザー koyumeishikoyumeishi
提出日時 2015-07-31 23:03:08
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 155 ms / 2,000 ms
コード長 2,189 bytes
コンパイル時間 618 ms
コンパイル使用メモリ 80,412 KB
実行使用メモリ 12,752 KB
最終ジャッジ日時 2023-09-24 23:00:07
合計ジャッジ時間 5,083 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 142 ms
12,464 KB
testcase_09 AC 140 ms
12,464 KB
testcase_10 AC 145 ms
12,428 KB
testcase_11 AC 144 ms
12,356 KB
testcase_12 AC 140 ms
12,408 KB
testcase_13 AC 153 ms
12,348 KB
testcase_14 AC 152 ms
12,488 KB
testcase_15 AC 151 ms
12,520 KB
testcase_16 AC 151 ms
12,528 KB
testcase_17 AC 154 ms
12,696 KB
testcase_18 AC 154 ms
12,536 KB
testcase_19 AC 155 ms
12,400 KB
testcase_20 AC 154 ms
12,464 KB
testcase_21 AC 154 ms
12,400 KB
testcase_22 AC 154 ms
12,424 KB
testcase_23 AC 82 ms
4,376 KB
testcase_24 AC 109 ms
12,416 KB
testcase_25 AC 110 ms
12,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <set>
using namespace std;

template<class T>
class BinaryIndexedTree_0_indexed{
	void init(const vector<T> &A){
		for(int i=0; i<N; i++){
			add(i+1, A[i]);
		}
	}
	
public:
	vector<T> tree;
	int N;
	
	BinaryIndexedTree_0_indexed(const int n) : tree(n+1,0), N(n){
		
	}
	
	BinaryIndexedTree_0_indexed(const vector<T> &A) : tree(A.size()+1,0), N(A.size()){
		init(A);
	}

	//caution : position "i" must be 0-indexed
	void add(int i, const T x){
		i += 1;
		while(i <= N){
			tree[i] += x;
			i += i & -i;
		}
	}

	//get sums [0,i]
	T get_sum(int i){
		i += 1;
		T ret=0;
		while(i>0){
			ret += tree[i];
			i -= i & -i;
		}
		return ret;
	}

	//get sums [from,to]
	T get_sums_range(const int from, const int to){
		return get_sum(to) - get_sum(from-1);
	}

	//get at [i]
	T get_at(const int i){
		return get_sum(i) - get_sum(i-1);
	}

	void print(){
		for(int i=0; i<N; i++){
			cerr << get_at(i) << " ";
		}
		cerr << endl;
	}
};

int main(){
	int N,Q;
	cin >> N >> Q;

	int NN = 2*N;

	vector<BinaryIndexedTree_0_indexed<long long>> BIT(2, BinaryIndexedTree_0_indexed<long long>(NN));

	while(Q--){
		string x;
		int t;
		int y,z;
		cin >> x >> t >> y >> z;

		if(x=="R"){
			y += N;
			y = (y - t%(NN) + NN) % (NN);
			BIT[0].add(y, z);

		}else if(x=="L"){
			y += N;
			y = (y + t%(NN) + NN) % (NN);
			BIT[1].add(y, z);

		}else{
			z--;

			long long ans = 0;
			for(int k=0; k<2; k++){
				vector<int> L(2);
				vector<int> R(2);
				L[0] = (-z + (N-1) + (k%2==0?-1:+1)*t%(NN) + NN)%(NN) ;
				R[0] = (-y + (N-1) + (k%2==0?-1:+1)*t%(NN) + NN)%(NN) ;

				L[1] = (+y + N + (k%2==0?-1:+1)*t%(NN) + NN)%(NN) ;
				R[1] = (+z + N + (k%2==0?-1:+1)*t%(NN) + NN)%(NN) ;

				for(int i=0; i<2; i++){

					if(L[i] > R[i]){
						ans += BIT[k].get_sums_range(L[i],NN-1);
						ans += BIT[k].get_sums_range(0,R[i]);
					}else{
						ans += BIT[k].get_sums_range(L[i],R[i]);
					}
				}
			}
			//cout << ans << endl;
			printf("%lld\n", ans);
		}
/*
		for(int i=0; i<2; i++){
			BIT[i].print();
		}
*/
	}
	return 0;
}
0