• 数列分块入门 5


    分块训练

    参考hzwer的做法,每个数在经过数次向下取整的开方后都会变成0或1。

    我们在维护块内信息的时候,可以将整块全是0或1的块跳过,减少复杂度。

    #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 = 50005;
    ll a[N], sum[N];
    int n, t, lt[N], rt[N], pos[N];
    bool vis[N];
    
    void reset(int k){
        if(vis[k]) return;
        vis[k] = true, sum[k] = 0;
        for(int i = lt[k]; i <= rt[k]; i ++){
            a[i] =(int)sqrt(a[i]);
            sum[k] += a[i];
            if(a[i] > 1) vis[k] = false;
        }
    }
    
    void modify(int l, int r){
        int p = pos[l], q = pos[r];
        if(p == q){
            for(int i = l; i <= r; i ++){
                sum[p] -= a[i], a[i] = (int)sqrt(a[i]), sum[p] += a[i];
            }
        }
        else{
            for(int i = p + 1; i <= q - 1; i ++)
                reset(i);
            for(int i = l; i <= rt[p]; i ++){
                sum[p] -= a[i], a[i] = (int)sqrt(a[i]), sum[p] += a[i];
            }
            for(int i = lt[q]; i <= r; i ++){
                sum[q] -= a[i], a[i] = (int)sqrt(a[i]), sum[q] += a[i];
            }
        }
    }
    
    ll query(int l, int r){
        int p = pos[l], q = pos[r];
        ll ret = 0;
        if(p == q){
            for(int i = l; i <= r; i ++){
                ret += a[i];
            }
        }
        else{
            for(int i = p + 1; i <= q - 1; i ++) ret += sum[i];
            for(int i = l; i <= rt[p]; i ++) ret += a[i];
            for(int i = lt[q]; i <= r; i ++) ret += a[i];
        }
        return ret;
    }
    
    int main(){
    
        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, sum[i] += 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);
            else cout << query(l, r) << endl;
        }
        return 0;
    }
    
  • 相关阅读:
    深入理解Guava Cache的refresh和expire刷新机制
    单机、分布式、集群的区别与联系
    理解Word2Vec
    Struts2 XML配置详解
    目标检测中的数据增强方法(附详细代码讲解)
    做笔记就用印象笔记,支持录音做笔记( 附剪藏 插件下载,一键将网页内容保存笔记中进行编辑)
    教你用OpenCV 和 Python给证件照换底色(蓝底 <->红底->白底)
    一招教你如何用Word直接打开PDF进行编辑,无需下载转换软件
    小白也能弄得懂的目标检测YOLO系列之YOLOv1网络训练
    小白也能弄懂的目标检测YOLO系列之YOLOV1
  • 原文地址:https://www.cnblogs.com/onionQAQ/p/10877583.html
Copyright © 2020-2023  润新知