結果

問題 No.230 Splarraay スプラレェーイ
ユーザー Min_25Min_25
提出日時 2016-07-18 19:08:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 41 ms / 5,000 ms
コード長 3,624 bytes
コンパイル時間 1,564 ms
コンパイル使用メモリ 96,928 KB
実行使用メモリ 6,016 KB
最終ジャッジ日時 2024-04-23 16:34:27
合計ジャッジ時間 2,481 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 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 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 4 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 20 ms
5,376 KB
testcase_10 AC 18 ms
5,376 KB
testcase_11 AC 13 ms
5,376 KB
testcase_12 AC 21 ms
5,376 KB
testcase_13 AC 4 ms
5,376 KB
testcase_14 AC 15 ms
5,632 KB
testcase_15 AC 30 ms
6,016 KB
testcase_16 AC 35 ms
6,016 KB
testcase_17 AC 41 ms
5,632 KB
testcase_18 AC 23 ms
5,888 KB
testcase_19 AC 25 ms
6,016 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("O3")
#pragma GCC target ("avx")
// #pragma GCC target ("sse4") // SPOJ, codechef

#include <cstdio>
#include <cassert>
#include <cmath>
#include <cstring>

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <tuple>

#define _rep(_1, _2, _3, _4, name, ...) name
#define rep2(i, n) rep3(i, 0, n)
#define rep3(i, a, b) rep4(i, a, b, 1)
#define rep4(i, a, b, c) for (int i = int(a); i < int(b); i += int(c))
#define rep(...) _rep(__VA_ARGS__, rep4, rep3, rep2, _)(__VA_ARGS__)

using namespace std;

using i64 = long long;
using u8 = unsigned char;
using u32 = unsigned;
using u64 = unsigned long long;
using f80 = long double;

struct SegmentTree {
  struct Node {
    int cnts[2];
    int color;
  };
  using elem = Node;

  SegmentTree(int n) : size_(n) {
    tree = new elem[2 * size_];
  }
  ~SegmentTree() { delete [] tree; }

  void build() {
    rep(i, 2 * size_) tree[i] = {{0, 0}, -1};
  }

  void apply(int k) {
    int ks[30], ki = 0, b = 1;
    for (k >>= 1; k >= 1; k >>= 1, b <<= 1) ks[ki++] = k;

    for (; ki; b >>= 1) {
      int k = ks[--ki];
      if (tree[k].color >= 0) {
        tree[k].cnts[tree[k].color ^ 0] = b;
        tree[k].cnts[tree[k].color ^ 1] = 0;
        tree[2 * k + 0].color = tree[k].color;
        tree[2 * k + 1].color = tree[k].color;
        tree[k].color = -1;
      }
    }
  }

  inline void update_count(int cnts[2], int k, int b) {
    if (tree[k].color >= 0) cnts[tree[k].color] += b;
    else cnts[0] += tree[k].cnts[0], cnts[1] += tree[k].cnts[1];
  }

  inline void fix(int k, int b) {
    assert(tree[k].color == -1);
    int cnts[2] = {};
    update_count(cnts, 2 * k + 0, b >> 1);
    update_count(cnts, 2 * k + 1, b >> 1);
    tree[k].cnts[0] = cnts[0];
    tree[k].cnts[1] = cnts[1];
  }

  void update(int l, int r, int col) {
    bool lup = false, rup = false;
    int b = 1;
    l += size_; r += size_;
    apply(l); apply(r - 1);
    for (; l < r; l >>= 1, r >>= 1, b <<= 1) {
      if (lup) fix(l - 1, b);
      if (rup) fix(r, b);
      if (l & 1) tree[l++].color = col, lup = true;
      if (r & 1) tree[--r].color = col, rup = true;
    }
    for (--l; l < r; l >>= 1, r >>= 1, b <<= 1)  {
      if (lup) fix(l, b);
      if (rup) fix(r, b);
    }
    for (; l; l >>= 1, b <<= 1) fix(l, b);
  }

  pair<int, int> query(int l, int r) {
    l += size_;
    r += size_;
    apply(l); apply(r - 1);
    int cnts[2] = {0, 0};
    for (int b = 1; l < r; l >>= 1, r >>= 1, b <<= 1) {
      if (l & 1) update_count(cnts, l++, b);
      if (r & 1) update_count(cnts, --r, b);
    }
    return {cnts[0], cnts[1]};
  }
  int size_;
  elem* tree;
};

#define getchar getchar_unlocked

int get_int() {
  int n, c;
  while ((c = getchar()) < '0') if (c == EOF) return -1;
  n = c - '0';
  while ((c = getchar()) >= '0') n = n * 10 + c - '0';
  return n;
}

void solve() {
  int N;
  while (~(N = get_int())) {
    auto tree = SegmentTree(N);
    tree.build();

    int Q = get_int();

    i64 a = 0, b = 0;
    rep(i, Q) {
      int c = get_int();
      int l = get_int();
      int r = get_int() + 1;
      if (c >= 1) {
        tree.update(l, r, c - 1);
      } else {
        auto p = tree.query(l, r);
        if (p.first > p.second) a += p.first;
        else if (p.first < p.second) b += p.second;
      }
    }
    auto p = tree.query(0, N);
    a += p.first;
    b += p.second;
    printf("%lld %lld\n", a, b);
  }
}

int main() {
  clock_t beg = clock();
  solve();
  clock_t end = clock();
  fprintf(stderr, "%.3f sec\n", double(end - beg) / CLOCKS_PER_SEC);
  return 0;
}
0