Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define ONLINE_JUDGE // online judge
- #include <iostream>
- #include <cmath>
- using namespace std;
- const int MXS = 300000;
- int sgt[MXS], sgtl[MXS], sgtr[MXS], sgtlz[MXS];
- int n, s;
- void sgtupdate(int node) {
- if (!sgtlz[node]) return;
- if (sgtlz[node] == 1) sgt[node] = sgtr[node] - sgtl[node] + 1;
- else sgt[node] = 0;
- if (node < s) {
- sgtlz[node * 2 + 1] = sgtlz[node * 2 + 2] = sgtlz[node];
- }
- sgtlz[node] = 0;
- /* while (node) {
- node = node - 1 >> 1;
- sgt[node] = sgt[2 * node + 1] + sgt[2 * node + 2];
- } */
- }
- int sgtset(const int &node, const int &l, const int &r, const bool &v) {
- if (sgtr[node] < l || sgtl[node] > r) return 0;
- sgtupdate(node);
- if (l <= sgtl[node] && sgtr[node] <= r) {
- sgtlz[node] = v ? 1 : 2;
- int oldVal = sgt[node];
- sgtupdate(node);
- // cerr << "update " << node << " from " << oldVal << " to " << sgt[node] << endl;
- return sgt[node] - oldVal;
- }
- int delta = sgtset(node * 2 + 1, l, r, v) + sgtset(node * 2 + 2, l, r, v);
- sgt[node] += delta;
- return delta;
- }
- int sgtget(const int &node, const int &l, const int &r) {
- if (sgtr[node] < l || sgtl[node] > r) return 0;
- sgtupdate(node);
- if (l <= sgtl[node] && sgtr[node] <= r) {
- // cerr << "get " << node << ": " << sgt[node] << endl;
- return sgt[node];
- }
- return sgtget(node * 2 + 1, l, r) + sgtget(node * 2 + 2, l, r);
- }
- signed main() {
- #ifdef ONLINE_JUDGE
- ios::sync_with_stdio(0);
- cin.tie(0); cout.tie(0);
- #endif
- int q, a, b;
- char c;
- cin >> n >> q;
- s = (1 << int(ceil(log2(n)))) - 1;
- for (int i = 0; i < n; i++) {
- cin >> sgt[s + i];
- }
- for (int i = s; i <= s * 2; i++) {
- sgtl[i] = sgtr[i] = i - s;
- }
- for (int i = s - 1; i >= 0; i--) {
- sgt[i] = sgt[2 * i + 1] + sgt[2 * i + 2];
- sgtl[i] = sgtl[2 * i + 1];
- sgtr[i] = sgtr[2 * i + 2];
- }
- while (q--) {
- cin >> c >> a >> b; a--; b--;
- if (c == '+') sgtset(0, a, b, 1);
- if (c == '-') sgtset(0, a, b, 0);
- if (c == '?') cout << sgtget(0, a, b) << '\n';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement