結果

問題 No.833 かっこいい電車
ユーザー tnakao0123tnakao0123
提出日時 2019-05-31 02:25:49
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 50 ms / 2,000 ms
コード長 1,978 bytes
コンパイル時間 580 ms
コンパイル使用メモリ 83,056 KB
実行使用メモリ 4,652 KB
最終ジャッジ日時 2023-09-14 21:05:05
合計ジャッジ時間 2,877 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 38 ms
4,376 KB
testcase_11 AC 50 ms
4,456 KB
testcase_12 AC 17 ms
4,380 KB
testcase_13 AC 13 ms
4,380 KB
testcase_14 AC 40 ms
4,536 KB
testcase_15 AC 21 ms
4,376 KB
testcase_16 AC 16 ms
4,376 KB
testcase_17 AC 16 ms
4,376 KB
testcase_18 AC 47 ms
4,376 KB
testcase_19 AC 15 ms
4,380 KB
testcase_20 AC 5 ms
4,376 KB
testcase_21 AC 41 ms
4,376 KB
testcase_22 AC 25 ms
4,548 KB
testcase_23 AC 18 ms
4,376 KB
testcase_24 AC 26 ms
4,512 KB
testcase_25 AC 46 ms
4,376 KB
testcase_26 AC 18 ms
4,376 KB
testcase_27 AC 31 ms
4,380 KB
testcase_28 AC 24 ms
4,376 KB
testcase_29 AC 27 ms
4,376 KB
testcase_30 AC 31 ms
4,652 KB
testcase_31 AC 35 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 833.cc:  No.833 かっこいい電車 - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;

/* typedef */

typedef long long ll;

template <typename T, const int MAX_N>
struct BIT {
  int n;
  T bits[MAX_N + 1];
  BIT() {}

  void init(int _n) {
    n = _n;
    //memset(bits, 0, sizeof(bits));
  }

  T sum(int x) {
    T s = 0;
    while (x > 0) {
      s += bits[x];
      x -= (x & -x);
    }
    return s;
  }

  void add(int x, T v) {
    while (x <= n) {
      bits[x] += v;
      x += (x & -x);
    }
  }

  int lower_bound(T v) {
    int l = 0, r = n + 1;
    while (l + 1 < r) {
      int k = (l + r) / 2;
      if (sum(k) >= v) r = k;
      else l = k;
    }
    return r;
  }
};

/* global variables */

BIT<int,MAX_N + 1> bit0;
BIT<ll,MAX_N + 1> bit1;

/* subroutines */

/* main */

int main() {
  int n, q;
  scanf("%d%d", &n, &q);

  bit0.init(n + 1);
  bit1.init(n + 1);

  for (int i = 1; i <= n; i++) {
    int ai;
    scanf("%d", &ai);
    bit0.add(i, 1);
    bit1.add(i, (ll)ai);
  }

  while (q--) {
    int op, x;
    scanf("%d%d", &op, &x);

    if (op == 1) { // connect
      if (bit0.sum(x) < bit0.sum(x + 1))
	bit0.add(x + 1, -1);
    }
    else if (op == 2) { // separate
      if (bit0.sum(x) == bit0.sum(x + 1))
	bit0.add(x + 1, 1);
    }
    else if (op == 3) { // remodel
      bit1.add(x, 1);
    }
    else { // attractiveness
      int k = bit0.sum(x);
      int i0 = bit0.lower_bound(k);
      int i1 = bit0.lower_bound(k + 1);
      //printf("x=%d, k=%d, i0,i1=%d,%d\n", x, k, i0, i1);
      printf("%lld\n", bit1.sum(i1 - 1) - bit1.sum(i0 - 1));
    }
  }
  return 0;
}
0