結果
問題 | No.674 n連勤 |
ユーザー | ei1333333 |
提出日時 | 2018-04-13 23:34:38 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 165 ms / 2,000 ms |
コード長 | 3,603 bytes |
コンパイル時間 | 2,603 ms |
コンパイル使用メモリ | 221,640 KB |
実行使用メモリ | 21,832 KB |
最終ジャッジ日時 | 2024-06-26 21:57:19 |
合計ジャッジ時間 | 4,408 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 2 ms
6,944 KB |
testcase_03 | AC | 2 ms
6,944 KB |
testcase_04 | AC | 2 ms
6,940 KB |
testcase_05 | AC | 2 ms
6,940 KB |
testcase_06 | AC | 2 ms
6,940 KB |
testcase_07 | AC | 2 ms
6,940 KB |
testcase_08 | AC | 2 ms
6,940 KB |
testcase_09 | AC | 2 ms
6,940 KB |
testcase_10 | AC | 2 ms
6,940 KB |
testcase_11 | AC | 12 ms
6,940 KB |
testcase_12 | AC | 9 ms
6,944 KB |
testcase_13 | AC | 28 ms
6,940 KB |
testcase_14 | AC | 102 ms
21,736 KB |
testcase_15 | AC | 73 ms
13,620 KB |
testcase_16 | AC | 120 ms
21,832 KB |
testcase_17 | AC | 116 ms
21,692 KB |
testcase_18 | AC | 90 ms
13,520 KB |
testcase_19 | AC | 165 ms
13,620 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; template< typename Monoid, typename OperatorMonoid = Monoid > struct LazySegmentTree { using F = function< Monoid(Monoid, Monoid) >; using G = function< Monoid(Monoid, OperatorMonoid, int) >; using H = function< OperatorMonoid(OperatorMonoid, OperatorMonoid) >; int sz; vector< Monoid > data; vector< OperatorMonoid > lazy; const F f; const G g; const H h; const Monoid M1; const OperatorMonoid OM0; LazySegmentTree(int n, const F f, const G g, const H h, const Monoid &M1, const OperatorMonoid OM0) : f(f), g(g), h(h), M1(M1), OM0(OM0) { sz = 1; while(sz < n) sz <<= 1; data.assign(2 * sz, M1); lazy.assign(2 * sz, OM0); } void set(int k, const Monoid &x) { data[k + sz] = x; } void build() { for(int k = sz - 1; k > 0; k--) { data[k] = f(data[2 * k + 0], data[2 * k + 1]); } } void propagate(int k, int len) { if(lazy[k] != OM0) { if(k < sz) { lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]); lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]); } data[k] = g(data[k], lazy[k], len); lazy[k] = OM0; } } Monoid update(int a, int b, const OperatorMonoid &x, int k, int l, int r) { propagate(k, r - l); if(r <= a || b <= l) { return data[k]; } else if(a <= l && r <= b) { lazy[k] = h(lazy[k], x); propagate(k, r - l); return data[k]; } else { return data[k] = f(update(a, b, x, 2 * k + 0, l, (l + r) >> 1), update(a, b, x, 2 * k + 1, (l + r) >> 1, r)); } } Monoid update(int a, int b, const OperatorMonoid &x) { return update(a, b, x, 1, 0, sz); } Monoid query(int a, int b, int k, int l, int r) { propagate(k, r - l); if(r <= a || b <= l) { return M1; } else if(a <= l && r <= b) { return data[k]; } else { return f(query(a, b, 2 * k + 0, l, (l + r) >> 1), query(a, b, 2 * k + 1, (l + r) >> 1, r)); } } Monoid query(int a, int b) { return query(a, b, 1, 0, sz); } Monoid operator[](const int &k) { return query(k, k + 1); } }; using int64 = long long; struct node { int64 left, right, ans, len; }; int main() { int64 D, Q, A[30000], B[30000]; scanf("%lld %lld", &D, &Q); vector< int64 > vs; for(int i = 0; i < Q; i++) { scanf("%lld %lld", &A[i], &B[i]); for(int j = -1; j <= 1; j++) vs.push_back(A[i] + j); for(int j = -1; j <= 1; j++) vs.push_back(B[i] + j); } sort(begin(vs), end(vs)); vs.erase(unique(begin(vs), end(vs)), end(vs)); node np = (node) {0, 0, 0, 0}; auto f = [](const node &x, const node &y) { node z; z.len = x.len + y.len; z.left = x.left; if(x.left == x.len) z.left += y.left; z.right = y.right; if(y.right == y.len) z.right += x.right; z.ans = max(x.right + y.left, max(x.ans, y.ans)); z.ans = max(z.ans, max(z.left, z.right)); return z; }; auto gg = [](const node &x, bool y, int len) { node z = x; z.ans = z.left = z.right = x.len; return z; }; auto h = [&](bool x, bool y) { return x || y; }; LazySegmentTree< node, bool > seg(vs.size(), f, gg, h, np, 0); for(int i = 0; i + 1 < vs.size(); i++) { seg.set(i, (node) {0, 0, 0, vs[i + 1] - vs[i]}); } seg.build(); for(int i = 0; i < Q; i++) { A[i] = lower_bound(begin(vs), end(vs), A[i]) - begin(vs); B[i] = upper_bound(begin(vs), end(vs), B[i]) - begin(vs); seg.update(A[i], B[i], true); printf("%lld\n", seg.query(0, vs.size()).ans); } }