Naive Operations

//#include<bits/stdc++.h>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<vector>
#include<queue>
#include<map>
#include<string>
#include<stack>
#define ll long long
#define P pair<int, int>
#define PP pair<int,pair<int, int>>
#define pb push_back
#define pp pop_back
#define lson root << 1
#define INF (int)2e9 + 7
#define rson root << 1 | 1
#define LINF (unsigned long long int)1e18
#define mem(arry, in) memset(arry, in, sizeof(arry))
using namespace std;

inline void coutSpace() { puts("\n"); }
inline void coutInt(int x) { cout << x << endl; }
inline void coutSpaceInt(int x) { cout << x << " "; }
inline void coutLong(ll x) { cout << x << endl; }
inline void coutSpaceLong(ll x) { cout << x << " "; }
inline void printInt(int x) { printf("%d\n", x); }
inline void printLong(ll x) { printf("%lld\n", x); }
inline void printDouble(double x) { printf("%.7f\n", x); }
inline void readInt(int& x) { scanf("%d", &x); }
inline void readLong(ll& x) { scanf("%lld", &x); }

const int N = 1e5 + 5;

int n, q;
int a[4 * N], lazy[4 * N], Min[4 * N], pos[4 * N], cnt[4 * N];

void Pushup(int root) {
    Min[root] = min(Min[lson] - lazy[lson], Min[rson] - lazy[rson]);
    cnt[root] = cnt[lson] + cnt[rson];
}

void Pushdown(int root) {
    lazy[lson] += lazy[root];
    lazy[rson] += lazy[root];
    lazy[root] = 0;
}

void Build(int l, int r, int root) {
    if(l == r) {
        cnt[root] = 0;
        readInt(Min[root]);
        a[root] = Min[root];
        return;
    }
    int mid = (l + r) >> 1;
    Build(l, mid, lson);
    Build(mid + 1, r, rson);
    Pushup(root);
}

void Find(int l, int r, int root) {
    if(l == r) {
        lazy[root] = 0;
        Min[root] = a[root];
        cnt[root]++;
        return;
    }
    if(lazy[root]) Pushdown(root);
    int mid = (l + r) >> 1;
    if(Min[lson] == lazy[lson]) Find(l, mid, lson);
    if(Min[rson] == lazy[rson]) Find(mid + 1, r, rson);
    Pushup(root);
}

void Update(int l, int r, int root, int L, int R) {
    if(l > R || r < L) return;
    if(L <= l && r <= R) {
        lazy[root]++;
        if(Min[root] == lazy[root]) Find(l, r, root);
        return;
    }
    if(lazy[root]) Pushdown(root);
    int mid = (l + r) >> 1;
    Update(l, mid, lson, L, R);
    Update(mid + 1, r, rson, L, R);
    Pushup(root);
}

int Query(int l, int r, int root, int L, int R) {
    if(l > R || r < L) return 0;
    if(L <= l && r <= R) return cnt[root];
    int mid = (l + r) >> 1;
    int ans = 0;
    ans += Query(l, mid, lson, L, R);
    ans += Query(mid + 1, r, rson, L, R);
    return ans;
}

int main()
{
    while(scanf("%d %d", &n, &q) != EOF) {
        mem(lazy, 0);
        Build(1, n, 1);
        char s[10];
        while(q--) {
            int x, y;
            scanf("%s %d %d", s, &x, &y);
            if(s[0] == 'a') {
                Update(1, n, 1, x, y);
            }
            else {
                printInt(Query(1, n, 1, x, y));
            }
        }
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/zgglj-com/p/9389124.html
今日推荐