zoukankan      html  css  js  c++  java
  • 数列分块入门 3

    分块训练

    用multiset或者vector都行,还是直接二分。。

    不过vector好像会快很多

    #include <bits/stdc++.h>
    #define INF 0x3f3f3f3f
    #define full(a, b) memset(a, b, sizeof a)
    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 = 200005;
    int n, t, lt[N], rt[N], pos[N];
    ll a[N], add[N];
    multiset<ll, myalloc<ll>> s[1005];
    
    inline void modify(int l, int r, ll d){
        int p = pos[l], q = pos[r];
        if(p == q){
            for(int i = l; i <= r; i ++){
                s[p].erase(s[p].find(a[i]));
                a[i] += d;
                s[p].insert(a[i]);
            }
        }
        else{
            for(int i = p + 1; i <= q - 1; i ++) add[i] += d;
            for(int i = l; i <= rt[p]; i ++){
                s[p].erase(s[p].find(a[i]));
                a[i] += d;
                s[p].insert(a[i]);
            }
            for(int i = lt[q]; i <= r; i ++){
                s[q].erase(s[q].find(a[i]));
                a[i] += d;
                s[q].insert(a[i]);
            }
        }
    }
    
    inline ll query(int l, int r, ll x){
        int p = pos[l], q = pos[r];
        ll ret = -1;
        if(p == q){
            for(int i = l; i <= r; i ++){
                if(a[i] + add[p] < x) ret = max(ret, a[i] + add[p]);
            }
        }
        else{
            for(int i = p + 1; i <= q - 1; i ++){
                auto it = s[i].lower_bound(x - add[i]);
                if(it == s[i].begin()) continue;
                ret = max(ret, *(--it) + add[i]);
            }
            for(int i = l; i <= rt[p]; i ++){
                if(a[i] + add[p] < x) ret = max(ret, a[i] + add[p]);
            }
            for(int i = lt[q]; i <= r; i ++){
                if(a[i] + add[q] < x) ret = max(ret, a[i] + add[q]);
            }
        }
        return ret;
    }
    
    int main(){
    
        //freopen("data.txt", "r", stdin);
        ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    
        cin >> n;
        for(int i = 1; i <= n; i ++) cin >> a[i];
        t = (int)sqrt(n);
        for(int i = 1; i <= t; i ++){
            lt[i] = (i - 1) * t + 1;
            rt[i] = i * t;
        }
        if(rt[t] < n) t ++, lt[t] = rt[t - 1] + 1, rt[t] = n;
        for(int i = 1; i <= t; i ++){
            for(int j = lt[i]; j <= rt[i]; j ++){
                pos[j] = i;
                s[i].insert(a[j]);
            }
        }
        for(int i = 1; i <= n; i ++){
            int opt, l, r; ll c;
            cin >> opt >> l >> r >> c;
            if(!opt) modify(l, r, c);
            else cout << query(l, r, c) << endl;
        }
        return 0;
    }
    
  • 相关阅读:
    yii2中的url美化
    一级域名301重定向到www二级域名
    使用meta来控制浏览器的渲染方式
    同一页面不同编码的提交处理
    Yii2.0 UrlManager
    sqlsever连接两个不同服务器上的数据库进行查询
    php 实现传入参数的传出
    xcode如何修改项目名称
    ios如何实现应用之间的跳转
    ios程序如何实现系统自带的分享
  • 原文地址:https://www.cnblogs.com/onionQAQ/p/10872537.html
Copyright © 2011-2022 走看看