Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <algorithm>
- #include <bitset>
- #include <set>
- #include <unordered_set>
- #include <map>
- #include <unordered_map>
- #include <cmath>
- #include <time.h>
- #include <random>
- #include <string>
- #include <cassert>
- #include <vector>
- #include <ostream>
- #include <istream>
- #include <stack>
- #include <deque>
- #include <queue>
- #include <functional>
- #include <chrono>
- #include <stack>
- #include <limits>
- using namespace std;
- #define int long long
- #define pb push_back
- #define all(a) (a).begin(), (a).end()
- #define pii pair<int, int>
- #define ld long double
- istream& operator>> (istream& in, pii& b) {
- in >> b.first >> b.second;
- return in;
- }
- ostream& operator<< (ostream& out, const pii& b) {
- out << "{" << b.first << ", " << b.second << "}";
- return out;
- }
- template<typename T> ostream& operator<< (ostream& out, const vector<T>& a) {
- for (auto k : a) out << k << " ";
- return out;
- }
- template <typename T1, typename T2> inline bool chkmin(T1 &x, const T2 &y) {if (x > y) {x = y; return 1;} return 0;}
- template <typename T1, typename T2> inline bool chkmax(T1 &x, const T2 &y) {if (x < y) {x = y; return 1;} return 0;}
- #ifdef LOCAL
- #define dbg(x) cout << #x << " : " << (x) << endl;
- const long long INF = 1e18;
- // const long long mod = 2600000069;
- // const long long p = 10;
- #else
- #define dbg(x) 57
- const long long INF = 1e18;
- // const long long mod = 2600000069;
- // const long long p = 179;
- #endif
- const ld PI = 4 * atan(1);
- #define time clock() / (double) CLOCKS_PER_SEC
- // #pragma GCC optimize("Ofast,no-stack-protector")
- // #pragma GCC target("sse,sse2,sse3,sse3,sse4")
- // #pragma GCC optimize("unroll-loops")
- // #pragma GCC optimize("fast-math")
- // #pragma GCC target("avx2")
- mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count());
- #ifdef LOCAL
- const int N = 5010;
- #else
- const int N = 301000;
- #endif
- int n, q;
- int a[N];
- // first segtree
- int t1[8 * N], mod1[8 * N];
- int real1(int v, int l, int r) {
- if (mod1[v] != -1) {
- return mod1[v] * (r - l);
- }
- return t1[v];
- }
- void push1(int v, int l, int r) {
- if (mod1[v] != -1) {
- t1[v] = (r - l) * mod1[v];
- mod1[2 * v + 1] = mod1[v];
- mod1[2 * v + 2] = mod1[v];
- mod1[v] = -1;
- }
- }
- void build1(int v, int l, int r) {
- mod1[v] = -1;
- if (l + 1 == r) {
- t1[v] = !a[l];
- return;
- }
- int m = (l + r) / 2;
- build1(2 * v + 1, l, m);
- build1(2 * v + 2, m, r);
- t1[v] = t1[2 * v + 1] + t1[2 * v + 2];
- }
- int sum1(int v, int l, int r, int askl, int askr) {
- push1(v, l, r);
- if (l >= askr || r <= askl) return 0;
- if (l >= askl && r <= askr) return t1[v];
- int m = (l + r) / 2;
- return sum1(2 * v + 1, l, m, askl, askr) + sum1(2 * v + 2, m, r, askl, askr);
- }
- int kth1(int v, int l, int r, int k) {
- push1(v, l, r);
- if (t1[v] < k) return n;
- if (l + 1 == r) {
- return l;
- }
- int m = (l + r) / 2;
- if (real1(2 * v + 1, l, m) >= k) {
- return kth1(2 * v + 1, l, m, k);
- } else {
- return kth1(2 * v + 2, m, r, k - real1(2 * v + 1, l, m));
- }
- }
- void setval1(int v, int l, int r, int askl, int askr, int val) {
- push1(v, l, r);
- if (l >= askr || r <= askl) return;
- if (l >= askl && r <= askr) {
- mod1[v] = val;
- push1(v, l, r);
- return;
- }
- int m = (l + r) / 2;
- setval1(2 * v + 1, l, m, askl, askr, val);
- setval1(2 * v + 2, m, r, askl, askr, val);
- t1[v] = t1[2 * v + 1] + t1[2 * v + 2];
- }
- struct node {
- int sum = 0, mx = 0, mod = -1;
- node() {}
- node(int sum_, int mx_) {
- sum = sum_, mx = mx_;
- }
- node(const node& a, const node& b) {
- sum = a.sum + b.sum;
- mx = max(a.mx, b.mx);
- }
- };
- node t2[8 * N];
- void push2(int v, int l, int r) {
- if (t2[v].mod != -1) {
- t2[v].sum = t2[v].mod * (r - l);
- t2[v].mx = t2[v].mod;
- t2[2 * v + 1].mod = t2[2 * v + 2].mod = t2[v].mod;
- t2[v].mod = -1;
- }
- }
- void setval2(int v, int l, int r, int askl, int askr, int val) {
- push2(v, l, r);
- if (l >= askr || r <= askl) return;
- if (l >= askl && r <= askr) {
- t2[v].mod = val;
- push2(v, l, r);
- return;
- }
- int m = (l + r) / 2;
- setval2(2 * v + 1, l, m, askl, askr, val);
- setval2(2 * v + 2, m, r, askl, askr, val);
- t2[v] = node(t2[2 * v + 1], t2[2 * v + 2]);
- }
- int real2mx(int v) {
- if (t2[v].mod != -1) return t2[v].mod;
- return t2[v].mx;
- }
- int fmore2(int v, int l, int r, int x) {
- push2(v, l, r);
- if (t2[v].mx < x) return n;
- if (l + 1 == r) {
- return l;
- }
- int m = (l + r) / 2;
- if (real2mx(2 * v + 1) >= x) {
- return fmore2(2 * v + 1, l, m, x);
- } else {
- return fmore2(2 * v + 2, m, r, x);
- }
- }
- int sum2(int v, int l, int r, int askl, int askr) {
- push2(v, l, r);
- if (l >= askr || r <= askl) return 0;
- if (l >= askl && r <= askr) return t2[v].sum;
- int m = (l + r) / 2;
- return sum2(2 * v + 1, l, m, askl, askr) + sum2(2 * v + 2, m, r, askl, askr);
- }
- void D1(int v, int l, int r) {
- push1(v, l, r);
- if (l + 1 == r) {
- cout << t1[v] << " ";
- return;
- }
- int m = (l + r) / 2;
- D1(2 * v + 1, l, m);
- D1(2 * v + 2, m, r);
- }
- void D2(int v, int l, int r) {
- push2(v, l, r);
- if (l + 1 == r) {
- cout << t2[v].mx << " ";
- return;
- }
- int m = (l + r) / 2;
- D2(2 * v + 1, l, m);
- D2(2 * v + 2, m, r);
- }
- signed main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- #ifndef LOCAL
- freopen("lamps.in", "r", stdin);
- freopen("lamps.out", "w", stdout);
- #endif
- cin >> n >> q;
- for (int i = 0; i < n; i++) {
- char c;
- cin >> c;
- a[i] = c - '0';
- }
- int lst = n;
- build1(0, 0, n);
- // for (int i = 0; i < n; i++) {
- // cout << a[i];
- // }
- // cout << "\n";
- for (int i = n - 1; i >= 0; i--) {
- if (a[i] == 0) {
- lst = i;
- }
- // dbg(i);
- // dbg(lst);
- setval2(0, 0, n, i, i + 1, lst);
- }
- push2(0, 0, n);
- const int buben = n * (n - 1) / 2;
- cout << t2[0].sum - buben << "\n";
- // D1(0, 0, n);
- // cout << "\n";
- // D2(0, 0, n);
- // cout << "\n";
- while (q--) {
- int l, r, c;
- cin >> l >> r >> c;
- l--, r--;
- setval1(0, 0, n, l, r + 1, !c);
- if (c == 1) {
- int sum = sum1(0, 0, n, 0, r + 1);
- // dbg(sum);
- int pos = kth1(0, 0, n, sum + 1);
- int sumpref = sum1(0, 0, n, 0, l);
- int pospref;
- if (sumpref == 0) {
- pospref = 0;
- } else {
- pospref = kth1(0, 0, n, sumpref) + 1;
- }
- int rset = fmore2(0, 0, n, pos) - 1;
- // dbg(pos);
- chkmin(rset, pos);
- if (pospref <= rset) {
- // dbg(pospref);
- // dbg(rset);
- setval2(0, 0, n, pospref, rset + 1, pos);
- }
- }
- // D1(0, 0, n);
- // cout << "\n";
- // D2(0, 0, n);
- // cout << "\n";
- push2(0, 0, n);
- cout << t2[0].sum - buben << "\n";
- }
- }
- /*
- 7 4
- 1100101
- 4 6 1
- 3 6 0
- 3 4 1
- 5 7 1
- -> 5 13 13 19 28
- 0 2 2 3
- 0 4 4 4
- test 5
- 4 1
- 0100
- 3 4 1
- correct :
- 1
- 6
- wrong :
- 1
- 5
- */
Advertisement
Add Comment
Please, Sign In to add comment