結果

問題 No.1441 MErGe
ユーザー tnakao0123tnakao0123
提出日時 2021-03-29 18:54:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 192 ms / 1,000 ms
コード長 2,695 bytes
コンパイル時間 1,592 ms
コンパイル使用メモリ 96,108 KB
実行使用メモリ 7,532 KB
最終ジャッジ日時 2023-08-19 17:14:59
合計ジャッジ時間 7,047 ms
ジャッジサーバーID
(参考情報)
judge10 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 5 ms
4,380 KB
testcase_06 AC 6 ms
4,380 KB
testcase_07 AC 5 ms
4,384 KB
testcase_08 AC 42 ms
4,428 KB
testcase_09 AC 41 ms
4,528 KB
testcase_10 AC 70 ms
4,380 KB
testcase_11 AC 64 ms
4,384 KB
testcase_12 AC 70 ms
4,432 KB
testcase_13 AC 155 ms
7,284 KB
testcase_14 AC 159 ms
7,376 KB
testcase_15 AC 168 ms
7,200 KB
testcase_16 AC 154 ms
7,280 KB
testcase_17 AC 150 ms
7,128 KB
testcase_18 AC 121 ms
6,492 KB
testcase_19 AC 134 ms
6,880 KB
testcase_20 AC 107 ms
6,400 KB
testcase_21 AC 97 ms
5,552 KB
testcase_22 AC 139 ms
5,564 KB
testcase_23 AC 190 ms
7,316 KB
testcase_24 AC 192 ms
7,304 KB
testcase_25 AC 190 ms
7,348 KB
testcase_26 AC 189 ms
7,532 KB
testcase_27 AC 188 ms
7,380 KB
testcase_28 AC 146 ms
7,352 KB
testcase_29 AC 144 ms
7,316 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1441.cc:  No.1441 MErGe - 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 = 200000;
const int MAX_E2 = 1 << 19; // = 524288

/* typedef */

typedef long long ll;

template <typename T, const int MAX_E2>
struct SegTreeDelay {
  int e2;
  T nodes[MAX_E2];
  bool dls[MAX_E2];
  SegTreeDelay() {}

  void init(int n) {
    for (e2 = 1; e2 < n; e2 <<= 1);
    //fill(nodes, nodes + MAX_E2, 0);
    //fill(dls, dls + MAX_E2, false);
  }

  T &geti(int i) { return nodes[e2 - 1 + i]; }
  void seti(int i, T v) { geti(i) = v; }

  void setall() {
    for (int j = e2 - 2; j >= 0; j--)
      nodes[j] = nodes[j * 2 + 1] + nodes[j * 2 + 2];
  }

  void __update(int k) {
    if (dls[k]) {
      int k0 = k * 2 + 1, k1 = k0 + 1;
      nodes[k0] = nodes[k1] = 0;
      dls[k0] = dls[k1] = true;
      dls[k] = false;
    }
  }

  void clear_range(int r0, int r1, int k, int i0, int i1) {
    if (r1 <= i0 || i1 <= r0) return;
    if (r0 <= i0 && i1 <= r1) {
      nodes[k] = 0;
      dls[k] = true;
      return;
    }

    __update(k);

    int im = (i0 + i1) / 2;
    int k0 = k * 2 + 1, k1 = k0 + 1;
    clear_range(r0, r1, k0, i0, im);
    clear_range(r0, r1, k1, im, i1);
    nodes[k] = nodes[k0] + nodes[k1];
  }
  void clear_range(int r0, int r1) { clear_range(r0, r1, 0, 0, e2); }

  int upper_bound(T v) {
    int k = 0, i0 = 0, i1 = e2;
    while (i0 + 1 < i1) {
      if (nodes[k] <= v) return i1;

      __update(k);
      int k0 = k * 2 + 1, k1 = k0 + 1;
      int im = (i0 + i1) / 2;
      if (v < nodes[k0]) k = k0, i1 = im;
      else k = k1, i0 = im, v -= nodes[k0];
    }
    return (v < nodes[k]) ? i0 : i1;
  }
};

/* global variables */

ll ass[MAX_N + 1];
SegTreeDelay<int,MAX_E2> st;

/* subroutines */

/* main */

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

  for (int i = 0; i < n; i++) {
    int ai;
    scanf("%d", &ai);
    ass[i + 1] = ass[i] + ai;
  }

  st.init(n);
  for (int i = 0; i < n; i++) st.seti(i, 1);
  st.setall();

  while (qn--) {
    int op, l, r;
    scanf("%d%d%d", &op, &l, &r);
    l--;

    if (op == 1) {
      int li = st.upper_bound(l + 1);
      int ri = st.upper_bound(r);
      st.clear_range(li, ri);
    }
    else {
      int li = st.upper_bound(l);
      int ri = st.upper_bound(r);
      ll v = ass[min(n, ri)] - ass[li];
      printf("%lld\n", v);
    }
  }
  return 0;
}
0