码迷,mamicode.com
首页 > 其他好文 > 详细

2018 Multi-University Training Contest 2 - Naive Operations

时间:2019-05-21 19:11:35      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:sync   ios   数组   线段   with   初始   ring   imu   ret   

线段树

a数组一开始全是0,每次增加1,我们可以发现不一定每一个a[i]/b[i]都是会影响答案的。

也就是说,只有a[i]>b[i]才会影响答案,为了方便比较,我们可以把a的初始值变成b,然后每次区间加1相当于区间减1,当有某个数减为0,就代表影响了一次答案,我们可以暴力找到这个数,将他重置为b的初始值,然后更新答案。

对于没有影响答案的区间,只需要打标记,区间减1则让最小值减1即可。

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
#define FAST_IO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
    int X = 0, w = 0; char ch = 0;
    while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
    while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
    return w ? -X : X;
}
inline int gcd(int a, int b){ return b ? gcd(b, a % b) : a; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
    A ans = 1;
    for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
    return ans;
}
const int N = 100005;
int n, q, a[N], tree[N<<2], minimum[N<<2], lazy[N<<2];

void init(){
    full(a, 0), full(tree, 0);
    full(minimum, 0), full(lazy, 0);
}

void push_up(int rt){
    minimum[rt] = min(minimum[rt << 1], minimum[rt << 1 | 1]);
    tree[rt] = tree[rt << 1] + tree[rt << 1 | 1];
};

void push_down(int rt){
    int l = rt << 1, r = rt << 1 | 1;
    minimum[l] -= lazy[rt], minimum[r] -= lazy[rt];
    lazy[l] += lazy[rt], lazy[r] += lazy[rt];
    lazy[rt] = 0;
}

void buildTree(int rt, int l, int r){
    if(l == r){
        tree[rt] = 0, minimum[rt] = a[l], lazy[rt] = 0;
        return;
    }
    int mid = (l + r) >> 1;
    buildTree(rt << 1, l, mid);
    buildTree(rt << 1 | 1, mid + 1, r);
    push_up(rt);
}

void calc(int rt, int l, int r){
    if(l == r){
        tree[rt] ++, lazy[rt] = 0, minimum[rt] = a[l];
        return;
    }
    push_down(rt);
    int mid = (l + r) >> 1;
    if(!minimum[rt << 1]) calc(rt << 1, l, mid);
    if(!minimum[rt << 1 | 1]) calc(rt << 1 | 1, mid + 1, r);
    push_up(rt);
}

void modify(int rt, int l, int r, int ml, int mr){
    if(l == ml && r == mr){
        lazy[rt] ++, minimum[rt] --;
        if(!minimum[rt]) calc(rt, l, r);
        return;
    }
    push_down(rt);
    int mid = (l + r) >> 1;
    if(mr <= mid) modify(rt << 1, l, mid, ml, mr);
    else if(ml > mid) modify(rt << 1 | 1, mid + 1, r, ml, mr);
    else{
        modify(rt << 1, l, mid, ml, mid);
        modify(rt << 1 | 1, mid + 1, r, mid + 1, mr);
    }
    push_up(rt);
}

int query(int rt, int l, int r, int ql, int qr){
    if(l == ql && r == qr){
        return tree[rt];
    }
    push_down(rt);
    int mid = (l + r) >> 1;
    if(qr <= mid) return query(rt << 1, l, mid, ql, qr);
    else if(ql > mid) return query(rt << 1 | 1, mid + 1, r, ql, qr);
    return query(rt << 1, l, mid, ql, mid) + query(rt << 1 | 1, mid + 1, r, mid + 1, qr);
}

int main(){

    FAST_IO;
    while(cin >> n >> q){
        init();
        for(int i = 1; i <= n; i ++) cin >> a[i];
        buildTree(1, 1, n);
        while(q --){
            string opt; cin >> opt;
            int l, r; cin >> l >> r;
            if(opt[0] == 'a') modify(1, 1, n, l, r);
            else cout << query(1, 1, n, l, r) << endl;
        }
    }
    return 0;
}

2018 Multi-University Training Contest 2 - Naive Operations

标签:sync   ios   数组   线段   with   初始   ring   imu   ret   

原文地址:https://www.cnblogs.com/onionQAQ/p/10901671.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!