結果
| 問題 | No.876 Range Compress Query |
| コンテスト | |
| ユーザー |
tnakao0123
|
| 提出日時 | 2019-09-07 19:23:32 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 71 ms / 2,000 ms |
| コード長 | 2,306 bytes |
| 記録 | |
| コンパイル時間 | 665 ms |
| コンパイル使用メモリ | 87,532 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-06-27 01:54:37 |
| 合計ジャッジ時間 | 2,240 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 18 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:89:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
89 | scanf("%d%d", &n, &qn);
| ~~~~~^~~~~~~~~~~~~~~~~
main.cpp:94:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
94 | scanf("%d", &pa);
| ~~~~~^~~~~~~~~~~
main.cpp:98:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
98 | scanf("%d", &ai);
| ~~~~~^~~~~~~~~~~
main.cpp:107:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
107 | scanf("%d%d%d", &op, &l, &r);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
main.cpp:112:12: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
112 | scanf("%d", &x);
| ~~~~~^~~~~~~~~~
ソースコード
/* -*- coding: utf-8 -*-
*
* 876.cc: No.876 Range Compress 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
/* typedef */
typedef long long ll;
template <typename T, const int MAX_E2>
struct SegTreeSum {
int n, e2;
T nodes[MAX_E2];
SegTreeSum() {}
void init(int _n) {
n = _n;
for (e2 = 1; e2 < n; e2 <<= 1);
memset(nodes, 0, sizeof(nodes));
}
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 0;
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); }
};
/* global variables */
SegTreeSum<int,MAX_E2> st;
ll ds[MAX_N];
/* subroutines */
inline int g(ll d) { return (d == 0) ? 0 : 1; }
/* main */
int main() {
int n, qn;
scanf("%d%d", &n, &qn);
st.init(n - 1);
int pa;
scanf("%d", &pa);
for (int i = 0; i + 1 < n; i++) {
int ai;
scanf("%d", &ai);
ds[i] = ai - pa;
st.get(i) = g(ds[i]);
pa = ai;
}
st.setall();
while (qn--) {
int op, l, r;
scanf("%d%d%d", &op, &l, &r);
l--, r--;
if (op == 1) {
int x;
scanf("%d", &x);
if (l > 0) {
ds[l - 1] += x;
int vl = g(ds[l - 1]);
if (st.get(l - 1) != vl) st.set(l - 1, vl);
}
if (r < n - 1) {
ds[r] -= x;
int vr = g(ds[r]);
if (st.get(r) != vr) st.set(r, vr);
}
}
else {
printf("%d\n", st.sum_range(l, r) + 1);
}
}
return 0;
}
tnakao0123