• LOJ6283 数列分块入门 7 (分块 区间加/乘)题解


    题意:区间加,区间乘,单点询问

    思路:假设一个点为a,那么他可以表示为m * a + sum,所以区间加就变为m * a + sum + sum2,区间乘变为m * m2 * a + sum * m2。左右两边的块要先puhs down。

    代码:

    #include<cmath>
    #include<set>
    #include<map>
    #include<queue>
    #include<cstdio>
    #include<vector>
    #include<cstring>
    #include <iostream>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    const int maxn = 1e5 + 10;
    const int M = maxn * 30;
    const ull seed = 131;
    const int INF = 0x3f3f3f3f;
    const int MOD = 1e4 + 7;
    struct Block{
        int l, r;
    }b[maxn];
    int a[maxn], sum[maxn], mult[maxn], belong[maxn];
    int n, block;
    void down(int bl){
        for(int i = b[bl].l; i <= b[bl].r; i++){
            a[i] = (a[i] * mult[bl] + sum[bl]) % MOD;
        }
        mult[bl] = 1;
        sum[bl] = 0;
    }
    void add(int l, int r, int c){
        int bl = belong[l], br = belong[r];
        if(bl == br){
            down(bl);
            for(int i = l; i <= r; i++){
                a[i] = (a[i] + c) % MOD;
            }
        }
        else{
            down(bl);
            for(int i = l; i <= b[bl].r; i++){
                a[i] = (a[i] + c) % MOD;
            }
            for(int i = bl + 1; i <= br - 1; i++){
                sum[i] = (sum[i] + c) % MOD;
            }
            down(br);
            for(int i = b[br].l; i <= r; i++){
                a[i] = (a[i] + c) % MOD;
            }
        }
    }
    void mul(int l, int r, int c){
        int bl = belong[l], br = belong[r];
        if(bl == br){
            down(bl);
            for(int i = l; i <= r; i++){
                a[i] = (a[i] * c) % MOD;
            }
        }
        else{
            down(bl);
            for(int i = l; i <= b[bl].r; i++){
                a[i] = (a[i] * c) % MOD;
            }
            for(int i = bl + 1; i <= br - 1; i++){
                mult[i] = (mult[i] * c) % MOD;
                sum[i] = (sum[i] * c) % MOD;
            }
            down(br);
            for(int i = b[br].l; i <= r; i++){
                a[i] = (a[i] * c) % MOD;
            }
        }
    }
    int main(){
        scanf("%d", &n);
        for(int i = 1; i <= n; i++){
            scanf("%d", &a[i]);
        }
        block = sqrt(n);
        for(int i = 1; i <= n; i++){
            belong[i] = (i - 1) / block + 1;
        }
        for(int i = 1; i <= belong[n]; i++){
            b[i].l = (i - 1) * block + 1;
            b[i].r = min(n, b[i].l + block - 1);
            sum[i] = 0, mult[i] = 1;
        }
        for(int i = 1; i <= n; i++){
            int o, l, r;
            int c;
            scanf("%d%d%d%d", &o, &l, &r, &c);
            if(o == 0){
                add(l, r, c);
            }
            else if(o == 1){
                mul(l, r, c);
            }
            else{
                int ans = (a[r] * mult[belong[r]] % MOD + sum[belong[r]]) % MOD;
                printf("%d
    ", ans);
            }
        }
        return 0;
    }
  • 相关阅读:
    Java基础知识点总结(四)
    Java基础知识点总结(三)
    用easyui实现查询条件的后端传递并自动刷新表格的两种方法
    在JDBC中实现SQL语句的模糊查询
    向DataGrid数据表格增加查询搜索框
    Java基础知识点总结(二)
    easyui中formatter的用法
    腾讯云+阿里云 搭建hadoop + hbase
    -- 记录 -- 问题记录
    -- 1 -- springboot
  • 原文地址:https://www.cnblogs.com/KirinSB/p/10905363.html
Copyright © 2020-2023  润新知