結果

問題 No.151 セグメントフィッシング
ユーザー koyumeishi
提出日時 2015-02-14 03:21:32
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 15 ms / 5,000 ms
コード長 2,276 bytes
コンパイル時間 669 ms
コンパイル使用メモリ 80,848 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-23 19:54:07
合計ジャッジ時間 1,636 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 19
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:73:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   73 |         scanf("%d %d\n", &N, &Q);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~
main.cpp:84:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   84 |                 scanf("%s %d %d\n", c, &y, &z);
      |                 ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

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;
	scanf("%d %d\n", &N, &Q);
	//cin >> N >> Q;

	int NN = 2*N;

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

	for(int t=1; t<=Q; t++){
		string x;
		int y,z;
		char c[10];
		scanf("%s %d %d\n", c, &y, &z);
		//cin >> x >> y >> z;
		x = c;

		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