結果

問題 No.833 かっこいい電車
ユーザー tnakao0123tnakao0123
提出日時 2019-05-31 17:34:26
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 2,018 bytes
コンパイル時間 597 ms
コンパイル使用メモリ 85,044 KB
実行使用メモリ 4,660 KB
最終ジャッジ日時 2023-09-14 21:05:08
合計ジャッジ時間 2,923 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 33 ms
4,376 KB
testcase_11 AC 42 ms
4,460 KB
testcase_12 AC 15 ms
4,380 KB
testcase_13 AC 11 ms
4,384 KB
testcase_14 AC 35 ms
4,568 KB
testcase_15 AC 18 ms
4,380 KB
testcase_16 AC 15 ms
4,380 KB
testcase_17 AC 14 ms
4,376 KB
testcase_18 AC 40 ms
4,380 KB
testcase_19 AC 14 ms
4,376 KB
testcase_20 AC 5 ms
4,376 KB
testcase_21 AC 35 ms
4,380 KB
testcase_22 AC 24 ms
4,608 KB
testcase_23 AC 16 ms
4,376 KB
testcase_24 AC 23 ms
4,504 KB
testcase_25 AC 37 ms
4,376 KB
testcase_26 AC 17 ms
4,536 KB
testcase_27 AC 27 ms
4,376 KB
testcase_28 AC 21 ms
4,380 KB
testcase_29 AC 24 ms
4,376 KB
testcase_30 AC 31 ms
4,660 KB
testcase_31 AC 32 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 k = 1;
    while ((k << 1) <= n) k <<= 1;
    int x = 0;
    for (; k > 0; k >>= 1)
      if (x + k <= n && bits[x + k] < v) {
	x += k;
	v -= bits[x];
      }
    return x + 1;
  }
};

/* 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