結果

問題 No.2065 Sum of Min
ユーザー tnakao0123tnakao0123
提出日時 2022-09-04 03:05:13
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 138 ms / 2,000 ms
コード長 2,390 bytes
コンパイル時間 711 ms
コンパイル使用メモリ 60,496 KB
実行使用メモリ 10,240 KB
最終ジャッジ日時 2024-04-28 22:43:13
合計ジャッジ時間 5,183 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 83 ms
10,112 KB
testcase_05 AC 77 ms
10,112 KB
testcase_06 AC 102 ms
10,112 KB
testcase_07 AC 72 ms
10,112 KB
testcase_08 AC 116 ms
10,112 KB
testcase_09 AC 104 ms
10,240 KB
testcase_10 AC 89 ms
10,240 KB
testcase_11 AC 93 ms
10,240 KB
testcase_12 AC 124 ms
10,240 KB
testcase_13 AC 123 ms
10,112 KB
testcase_14 AC 129 ms
9,984 KB
testcase_15 AC 138 ms
9,984 KB
testcase_16 AC 126 ms
10,112 KB
testcase_17 AC 124 ms
10,112 KB
testcase_18 AC 126 ms
10,240 KB
testcase_19 AC 124 ms
10,240 KB
testcase_20 AC 127 ms
10,112 KB
testcase_21 AC 125 ms
10,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2065.cc:  No.2065 Sum of Min - yukicoder
 */

#include<cstdio>
#include<vector>
#include<algorithm>
#include<utility>
 
using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_QN = 100000;

/* typedef */

typedef long long ll;
typedef pair<int,int> pii;

struct Elm {
  ll a;
  int k;
  Elm() {}
  Elm(ll _a, int _k): a(_a), k(_k) {}

  Elm operator+(const Elm &e) const { return Elm(a + e.a, k + e.k); }
};

template <typename T>
struct SegTreeSum {
  int e2;
  vector<T> nodes;
  T defv;
  SegTreeSum() {}

  void init(int n, T _defv) {
    defv = _defv;
    for (e2 = 1; e2 < n; e2 <<= 1);
    nodes.assign(e2 * 2, defv);
  }

  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 set(int i, T v) {
    int j = e2 - 1 + i;
    nodes[j] = v;
    while (j > 0) {
      j = (j - 1) / 2;
      nodes[j] = nodes[j * 2 + 1] + nodes[j * 2 + 2];
    }
  }

  T sum_range(int r0, int r1, int k, int i0, int i1) {
    if (r1 <= i0 || i1 <= r0) return defv;
    if (r0 <= i0 && i1 <= r1) return nodes[k];

    int im = (i0 + i1) / 2;
    T v0 = sum_range(r0, r1, k * 2 + 1, i0, im);
    T v1 = sum_range(r0, r1, k * 2 + 2, im, i1);
    return v0 + v1;
  }
  T sum_range(int r0, int r1) { return sum_range(r0, r1, 0, 0, e2); }
};

struct Query {
  int l, r, x, i;
  Query() {}
  Query(int _l, int _r, int _x, int _i): l(_l), r(_r), x(_x), i(_i) {}

  void read(int _i) { scanf("%d%d%d", &l, &r, &x), l--, i = _i; }

  bool operator<(const Query &q) const { return x < q.x; }
};

/* global variables */

pii ais[MAX_N];
Query qs[MAX_QN];
SegTreeSum<Elm> st;
ll bs[MAX_QN];

/* subroutines */

/* main */

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

  for (int i = 0; i < n; i++)
    scanf("%d", &ais[i].first), ais[i].second = i;
  sort(ais, ais + n);

  for (int i = 0; i < qn; i++) qs[i].read(i);
  sort(qs, qs + qn);

  st.init(n, Elm(0, 0));
  for (int i = 0, j = 0; i < qn; i++) {
    Query &q = qs[i];

    while (j < n && ais[j].first < q.x) {
      st.set(ais[j].second, Elm(ais[j].first, 1));
      j++;
    }

    Elm e = st.sum_range(q.l, q.r);
    bs[q.i] = e.a + (ll)q.x * (q.r - q.l - e.k);
  }

  for (int i = 0; i < qn; i++) printf("%lld\n", bs[i]);

  return 0;
}
0