結果

問題 No.833 かっこいい電車
ユーザー IKyoproIKyopro
提出日時 2019-05-24 22:31:23
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 195 ms / 2,000 ms
コード長 2,610 bytes
コンパイル時間 1,268 ms
コンパイル使用メモリ 73,988 KB
実行使用メモリ 5,332 KB
最終ジャッジ日時 2023-09-14 20:53:54
合計ジャッジ時間 5,050 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 192 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,384 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 139 ms
4,692 KB
testcase_11 AC 195 ms
4,908 KB
testcase_12 AC 59 ms
4,380 KB
testcase_13 AC 35 ms
4,380 KB
testcase_14 AC 165 ms
4,920 KB
testcase_15 AC 80 ms
4,500 KB
testcase_16 AC 80 ms
4,604 KB
testcase_17 AC 43 ms
4,380 KB
testcase_18 AC 165 ms
4,460 KB
testcase_19 AC 73 ms
4,644 KB
testcase_20 AC 18 ms
4,376 KB
testcase_21 AC 123 ms
4,384 KB
testcase_22 AC 134 ms
5,060 KB
testcase_23 AC 82 ms
4,388 KB
testcase_24 AC 129 ms
5,252 KB
testcase_25 AC 153 ms
4,380 KB
testcase_26 AC 89 ms
4,672 KB
testcase_27 AC 116 ms
4,464 KB
testcase_28 AC 76 ms
4,380 KB
testcase_29 AC 97 ms
4,380 KB
testcase_30 AC 193 ms
5,332 KB
testcase_31 AC 186 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <climits>
#include <cmath>
using namespace std;
typedef long long ll;

const ll INF = (1LL << 31) - 1;
struct RUQ {
  int N, K;
  vector<ll> data;
  vector<ll> lazyUpdate; // -1ならupdateしない
  RUQ(int N): N(N) {
    K = sqrt(N) + 1;
    data.resize(K * K + 1, INF);
    lazyUpdate.resize(K + 1, -1);
  }
 
  // kは分割した配列のindex
  void evil(int k) {
    if(lazyUpdate[k] != -1) {
      for(int i = k * K; i < (k + 1) * K; i++) {
        data[i] = lazyUpdate[k];
      }
      lazyUpdate[k] = -1;
    }
  }
 
  // [s, t)
  void update(int s, int t, int x) {
    for(int k = 0; k < K; k++) {
      int l = k * K, r = (k + 1) * K;
      // 区間でない
      if(r <= s || t <= l) continue;
      // 完全に含まれている
      if(s <= l && r <= t) {
        lazyUpdate[k] = x;
      } else {
        evil(k);
        for(int i = max(s, l); i < min(t, r); i++) {
          data[i] = x;
        }
      }
    }
  }
 
  int find(int i) {
    int k = i / K;
    evil(k);
    return data[i];
  }
};

struct BIT{
private:
    vector<ll> bit;
    int N;
public:
    BIT(){init();}
    BIT(int N):N(N){init();}
    void init(){
        bit.clear();
        bit.resize(N+1,0);
    }
    ll sum(int i){
        ll s = 0;
        while(i>0){
            s += bit[i];
            i -= i&-i;
        }
        return s;
    }
    ll sum_range(int l,int r){
        return sum(r)-sum(l-1);
    }
    void add(int i,ll x){
        while(i<=N){
        bit[i] += x;
        i += i&-i;
        }
    }
    ll sum0(int i){
        return sum(i+1);
    }
    void add0(int i,ll x){
        add(i+1,x);
    }
};

int N,Q;
int main(){
    cin >> N >> Q;
    RUQ segL(N+1),segR(N+1);
    ll a;
    for(int i=1;i<=N;i++){
        segL.update(i,i+1,i);
        segR.update(i,i+1,i);
    }
    BIT bit(N+1);
    for(int i=1;i<=N;i++){
        cin >> a;
        bit.add(i,a);
    }
    int q,x;
    for(int i=1;i<=Q;i++){
        cin >> q;
        if(q==1){
            cin >> x;
            int newr = segR.find(x+1);
            int newl = segL.find(x);
            segR.update(newl,x+1,newr);
            segL.update(x+1,newr+1,newl);       
        }else if(q==2){
            cin >> x;
            int newr = segR.find(x+1);
            int newl = segL.find(x);
            segR.update(newl,x+1,x);
            segL.update(x+1,newr+1,x+1);
        }else if(q==3){
            cin >> x;
            bit.add(x,1);
        }else{
            cin >> x;
            cout << bit.sum_range(segL.find(x),segR.find(x)) << endl;
        }
    }
}
0