結果

問題 No.2101 [Cherry Alpha N] ずっとこの数列だったらいいのに
ユーザー tnakao0123tnakao0123
提出日時 2022-10-15 04:56:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 559 ms / 6,000 ms
コード長 3,322 bytes
コンパイル時間 1,451 ms
コンパイル使用メモリ 104,452 KB
実行使用メモリ 33,208 KB
最終ジャッジ日時 2023-09-09 02:09:32
合計ジャッジ時間 23,162 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
18,268 KB
testcase_01 AC 5 ms
18,316 KB
testcase_02 AC 5 ms
18,368 KB
testcase_03 AC 223 ms
28,820 KB
testcase_04 AC 377 ms
31,144 KB
testcase_05 AC 206 ms
24,624 KB
testcase_06 AC 337 ms
30,824 KB
testcase_07 AC 384 ms
30,888 KB
testcase_08 AC 369 ms
31,196 KB
testcase_09 AC 182 ms
24,108 KB
testcase_10 AC 166 ms
24,092 KB
testcase_11 AC 271 ms
25,748 KB
testcase_12 AC 282 ms
25,732 KB
testcase_13 AC 196 ms
24,428 KB
testcase_14 AC 170 ms
20,200 KB
testcase_15 AC 443 ms
31,640 KB
testcase_16 AC 330 ms
30,036 KB
testcase_17 AC 133 ms
20,828 KB
testcase_18 AC 557 ms
32,940 KB
testcase_19 AC 541 ms
32,936 KB
testcase_20 AC 537 ms
33,048 KB
testcase_21 AC 538 ms
33,048 KB
testcase_22 AC 538 ms
32,988 KB
testcase_23 AC 537 ms
33,004 KB
testcase_24 AC 537 ms
32,996 KB
testcase_25 AC 542 ms
32,936 KB
testcase_26 AC 538 ms
32,984 KB
testcase_27 AC 536 ms
33,044 KB
testcase_28 AC 542 ms
32,936 KB
testcase_29 AC 531 ms
32,960 KB
testcase_30 AC 558 ms
33,048 KB
testcase_31 AC 559 ms
33,000 KB
testcase_32 AC 548 ms
33,040 KB
testcase_33 AC 444 ms
32,932 KB
testcase_34 AC 436 ms
32,940 KB
testcase_35 AC 458 ms
33,208 KB
testcase_36 AC 456 ms
32,936 KB
testcase_37 AC 479 ms
32,876 KB
testcase_38 AC 59 ms
19,876 KB
testcase_39 AC 190 ms
22,048 KB
testcase_40 AC 338 ms
29,268 KB
testcase_41 AC 6 ms
18,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2101.cc:  No.2101 [Cherry Alpha N] ずっとこの数列だったらいいのに - 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_QN = 200000;
const int INF = 1 << 30;
const long long LINF = 1LL << 60;

/* typedef */

typedef long long ll;
typedef vector<int> vi;
typedef queue<int> qi;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

struct Query {
  int d, l, r, i;
  Query() {}
  void read(int _i) { scanf("%d%d%d", &d, &l, &r), l--, i = _i; }
  bool operator<(const Query &q) const { return d < q.d; }
};

template <typename T>
struct SegTreeOp {
  int e2;
  vector<T> nodes;
  T defv, (*fop)(T,T);
  SegTreeOp() {}

  void init(int n, T _defv, T (*_fop)(T,T)) {
    defv = _defv, fop = _fop;
    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] = fop(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] = fop(nodes[j * 2 + 1], nodes[j * 2 + 2]);
    }
  }

  T op_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 = op_range(r0, r1, k * 2 + 1, i0, im);
    T v1 = op_range(r0, r1, k * 2 + 2, im, i1);
    return fop(v0, v1);
  }
  T op_range(int r0, int r1) { return op_range(r0, r1, 0, 0, e2); }
};

/* global variables */

int as[MAX_N], ts[MAX_N], ds[MAX_QN];
Query qs[MAX_QN];
vi pvs[MAX_QN], rvs[MAX_QN];
SegTreeOp<pll> st;
ll xs[MAX_QN];

/* subroutines */

pll op_add(pll a, pll b) {
  return pll(a.first + b.first, a.second + b.second);
}

/* main */

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d%d", as + i, ts + i);

  int qn;
  scanf("%d", &qn);
  for (int i = 0; i < qn; i++) qs[i].read(i);
  sort(qs, qs + qn);
  for (int i = 0; i < qn; i++) ds[i] = qs[i].d;

  for (int i = 0; i < n; i++)
    if (as[i] > 0) {
      int k = lower_bound(ds, ds + qn, ts[i]) - ds;
      if (k < qn) {
	pvs[k].push_back(i);
	int c = ts[i] + as[i] - 1;
	int k1 = lower_bound(ds, ds + qn, c) - ds;
	if (k1 < qn) rvs[k1].push_back(i);
      }
    }

  st.init(n, pll(0, 0), op_add);
  for (int i = 0; i < n; i++) st.seti(i, pll(0, as[i]));
  st.setall();

  for (int i = 0; i < qn; i++) {
    for (auto u: pvs[i])
      st.set(u, pll(-1, ts[u] + as[u] - 1));
    for (auto u: rvs[i])
      st.set(u, pll(0, 0));

    if (false) {
      printf("%d:", ds[i]);
      for (int j = 0; j < n; j++) {
	pll p = st.geti(j);
	printf(" %lld", p.first * ds[i] + p.second);
      }
      putchar('\n');
    }

    pll p = st.op_range(qs[i].l, qs[i].r);
    ll v = p.first * ds[i] + p.second;
    xs[qs[i].i] = v;
  }
  
  for (int i = 0; i < qn; i++) printf("%lld\n", xs[i]);

  return 0;
}
0