zoukankan      html  css  js  c++  java
  • Naive Operations

    PS:照着感觉瞎写了一波。记录每个区间的最小值,当a数组加1时,b数组减一,即对应区间的最小值减1,如果最小值变成了0,就暴力寻找那些位置变成0的,并将值重置为a[i]

    //#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("
    "); }
    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
    ", x); }
    inline void printLong(ll x) { printf("%lld
    ", x); }
    inline void printDouble(double x) { printf("%.7f
    ", 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;
    }
  • 相关阅读:
    hadoop架构
    hdfs存储模型
    C语言编译过程
    linux文件类型和权限
    推荐系统效果评估
    推荐系统冷启动
    Js计算-当月每周有多少天
    3D动画
    固定边栏——淘宝滚动效果
    jquery图片轮播-插件
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/9389124.html
Copyright © 2011-2022 走看看