#pragma GCC optimize ("O3") #pragma GCC target ("avx") // #pragma GCC target ("sse4") // SPOJ, codechef #include #include #include #include #include #include #include #include #include #include #include #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 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; }