結果

問題 No.151 セグメントフィッシング
ユーザー yaoshimaxyaoshimax
提出日時 2015-05-01 13:14:23
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 41 ms / 5,000 ms
コード長 2,120 bytes
コンパイル時間 2,411 ms
コンパイル使用メモリ 88,364 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-19 04:50:39
合計ジャッジ時間 2,272 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 6 ms
4,380 KB
testcase_09 AC 7 ms
4,380 KB
testcase_10 AC 7 ms
4,376 KB
testcase_11 AC 7 ms
4,380 KB
testcase_12 AC 25 ms
4,376 KB
testcase_13 AC 25 ms
4,376 KB
testcase_14 AC 24 ms
4,376 KB
testcase_15 AC 25 ms
4,380 KB
testcase_16 AC 25 ms
4,380 KB
testcase_17 AC 14 ms
4,380 KB
testcase_18 AC 15 ms
4,376 KB
testcase_19 AC 40 ms
4,376 KB
testcase_20 AC 41 ms
4,380 KB
testcase_21 AC 17 ms
4,376 KB
testcase_22 AC 16 ms
4,376 KB
testcase_23 AC 21 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>

using namespace std;
class BIT{
  /*binary indexed tree*/
 private:
  vector<long long> data;
  int size;
 public:
  BIT(int n): data(n+1,0),size(n){
    return;
  }
  long long sum(int l, int r ){
    // return sum in [l+1,r] or (l,r]
    return sum_(r)-sum_(l);
  }
  long long sum_(int i){
    // return sum (0, i]
    long long ans = 0;
    while( 0 < i ){
      ans += data[i];
      i -= i&-i;   // 11010110 -> 1010100
    }
    return ans;
  }
  void add(int i, int x){
    // add x to i
    while( i <= size ){
      data[i] += x;
      i+= i&-i;
    }
    return;
  }
};


int main(){
  int N,Q;
  cin >> N >> Q;
  int range=2*N;
  BIT b(range); //[1,N],[N+1,2*N] note that 2*N=1
  for( int i = 1 ; i <= Q; i++){
   char c;
   int x,y;
   cin >> c >> x>>y;
   if( c=='R'){
      x-=i;
      x=(x%(range)+(range))%(range)+1;
      //cout << x << endl;
      b.add(x,y);
   }
   if( c=='L'){
      x=2*N-1-x;
      x-=i; 
      x=(x%(range)+(range))%(range)+1;
      //cout << x << endl;
      b.add(x,y);   
   }
   if( c=='C'){
      int x1,y1;
      x1=x;
      y1=y;
      x1-=i;
      y1-=i;
      x1=(x1%(range)+(range))%(range)+1;
      y1=(y1%(range)+(range))%(range)+1;
      long long ans = 0;
      if( x1 > y1 ){
         ans+=b.sum(x1-1,2*N);
         ans+=b.sum(0,y1-1);
      }
      else{
         ans += b.sum(x1-1,y1-1);
      }
      //cout << x1<<"-"<<y1<<":"<<ans<<endl;
      x1=2*N-1-x;
      y1=2*N-1-y;
      x1-=i;
      y1-=i;
      x1=(x1%(range)+(range))%(range)+1;
      y1=(y1%(range)+(range))%(range)+1;
      if( x1 > y1 ){
         ans += b.sum(y1,x1);
      }
      else{
         ans+=b.sum(y1,2*N);
         ans+=b.sum(0,x1);
      }
      //cout << x1<<"-"<<y1<<":"<<ans<<endl;
      cout << ans<<endl;
   }
  }
  return 0;
}
0