結果

問題 No.877 Range ReLU Query
ユーザー tnakao0123tnakao0123
提出日時 2019-09-07 21:51:08
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 130 ms / 2,000 ms
コード長 2,699 bytes
コンパイル時間 950 ms
コンパイル使用メモリ 90,152 KB
実行使用メモリ 12,476 KB
最終ジャッジ日時 2024-04-25 22:12:13
合計ジャッジ時間 3,648 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
8,912 KB
testcase_01 AC 3 ms
8,068 KB
testcase_02 AC 4 ms
8,820 KB
testcase_03 AC 4 ms
8,732 KB
testcase_04 AC 3 ms
8,032 KB
testcase_05 AC 3 ms
9,796 KB
testcase_06 AC 3 ms
8,888 KB
testcase_07 AC 3 ms
8,428 KB
testcase_08 AC 4 ms
8,484 KB
testcase_09 AC 4 ms
8,052 KB
testcase_10 AC 3 ms
7,924 KB
testcase_11 AC 123 ms
11,972 KB
testcase_12 AC 106 ms
11,648 KB
testcase_13 AC 87 ms
10,956 KB
testcase_14 AC 88 ms
11,048 KB
testcase_15 AC 130 ms
12,184 KB
testcase_16 AC 121 ms
12,108 KB
testcase_17 AC 125 ms
12,112 KB
testcase_18 AC 118 ms
12,228 KB
testcase_19 AC 99 ms
12,460 KB
testcase_20 AC 113 ms
12,476 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:108:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  108 |   scanf("%d%d", &n, &qn);
      |   ~~~~~^~~~~~~~~~~~~~~~~
main.cpp:113:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  113 |     scanf("%d", &ai);
      |     ~~~~~^~~~~~~~~~~
main.cpp:118:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  118 |     scanf("%d%d%d%d", &c, &l, &r, &x);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 877.cc:  No.877 Range ReLU Query - 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;
const int MAX_E2 = 1 << 18; // = 262144
const int MAX_QN = 100000;
const int MAX_M = MAX_N + MAX_QN;

/* typedef */

typedef long long ll;

struct Elm {
  ll a, b;
  Elm() {}
  Elm(ll _a, ll _b): a(_a), b(_b) {}
  Elm operator+(const Elm &e) const { return Elm(a + e.a, b + e.b); }
};

template <typename T, const int MAX_E2>
struct SegTreeSum {
  int n, e2;
  T nodes[MAX_E2], z;
  SegTreeSum() {}

  void init(int _n, T _z) {
    n = _n; z = _z;
    for (e2 = 1; e2 < n; e2 <<= 1);
    fill(nodes, nodes + MAX_E2, z);
  }

  T &get(int i) { return nodes[e2 - 1 + i]; }

  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];
    }
  }

  void setall() {
    for (int j = e2 - 2; j >= 0; j--)
      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 z;
    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 Op {
  int c, l, r, x, i;
  Op() {}
  Op(int _c, int _l, int _r, int _x, int _i):
    c(_c), l(_l), r(_r), x(_x), i(_i) {}
  Op(int _x, int _i): c(0), l(0), r(0), x(_x), i(_i) {}
  bool operator<(const Op &o) const {
    return x < o.x || (x == o.x && c < o.c);
  }
};

/* global variables */

Op ps[MAX_M];
SegTreeSum<Elm,MAX_E2> st;
ll ans[MAX_N];

/* subroutines */

/* main */

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

  int m = 0;
  for (int i = 0; i < n; i++) {
    int ai;
    scanf("%d", &ai);
    ps[m++] = Op(ai, i);
  }
  for (int i = 0; i < qn; i++) {
    int c, l, r, x;
    scanf("%d%d%d%d", &c, &l, &r, &x);
    l--;
    ps[m++] = Op(c, l, r, x, i);
  }
  sort(ps, ps + m);

  st.init(n, Elm(0, 0));

  for (int i = m - 1; i >= 0; i--) {
    if (ps[i].c == 0)
      st.set(ps[i].i, Elm(ps[i].x, 1));
    else {
      Elm e = st.sum_range(ps[i].l, ps[i].r);
      ans[ps[i].i] = e.a - e.b * ps[i].x;
    }
  }

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