LibreOJ #6283. 数列分块入门 7

题目链接:

https://loj.ac/problem/6283

题意:

给你一个 n n n个整数的序列 a a a,让你进行三种操作:

  • [ l , r ] [l,r] [l,r]的之间的数字都加上 x x x
  • [ l , r ] [l,r] [l,r]的之间的数字都乘上 x x x
  • 询问 a x a_{x} ax的值 m o d mod mod  ( 10007 ) (10007) (10007)
分析:

       这里显而易见的是,如果我们分块,那么需要两个标记来存储加法标记和乘法标记,那么这里有个关键问题,就是顺序,顺序不同结果不同;我们修改的时候是有顺序的,而我们对于标记的操作,到最后是统一修改的,并不考虑乘法和加法的顺序;

       假设这里有一个数 t t t,那么它所在块有乘法标记 x + x_+ x+及加法标记 x ∗ x_* x,那么对于 t t t经乘法与加法的若干次运算,我们以 t = t ∗ x ∗ + x + t=t*x_*+x_+ t=tx+x+当作结果(先加后乘很难处理)(即此时的乘法和加法标记);

       如果是先有加法 x + ′ x_{+}^{'} x+再有乘法标记 x ∗ ′ x_{*}^{'} x,那么我们就有 t = ( t ∗ x ∗ + x + + x + ′ ) ∗ x ∗ ′ t=(t*x_*+x_++x_{+}^{'})*x_{*}^{'} t=(tx+x++x+)x,如果先有乘法 x ∗ ′ ′ x_{*}^{''} x,再有加法 x + ′ ′ x_{+}^{''} x+,那么我们有 t = ( t ∗ x ∗ + x + ) ∗ x ∗ ′ ′ + x + ′ ′ t=(t*x_*+x_+)*x_{*}^{''}+x_{+}^{''} t=(tx+x+)x+x+,对比一下发现乘法标记都是相同的(即是此时修改的乘法标记乘上原来的乘法标记),而对于加法标记则不一样,前者是 ( x + + x + ′ ) ∗ x ∗ ′ (x_++x_{+}^{'})*x_{*}^{'} (x++x+)x,后者是 x + ∗ x ∗ ′ ′ + x + ′ ′ x_+*x_{*}^{''}+x_{+}^{''} x+x+x+;前面是先加,后面是先乘,即对于后进行的乘法操作,我们将之前的加法标记乘上这个乘法标记;

       总结一下,它即是这样的,对于乘法操作,我们之前有标记 m u t i p l y mutiply mutiply,加法有标记 a d d add add,如果下一部进行加法操作 a a a,我们就直接 a d d + = a add+=a add+=a来更新,若是乘法操作 m m m,我们就 m u t i p l y ∗ = m , a d d ∗ = m mutiply*=m,add*=m mutiply=m,add=m来更新;
       这题还需要注意取余,在暴力修改的时候要注意取余,不然会wa(发现了负数的存在);

代码:
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include <map>

using namespace std;

#define inf 0x7f7f7f7f
#define maxn 100050
#define N 100100
#define P 2

typedef long long ll;
typedef struct {
    int u, v, next, w;
} Edge;
Edge e[2];
int cnt, head[1];

inline void add1(int u, int v, int w) {
    e[cnt].u = u;
    e[cnt].v = v;
    e[cnt].w = w;
    // e[cnt].f=f;
    e[cnt].next = head[u];
    head[u] = cnt++;
    e[cnt].u = v;
    e[cnt].v = u;
    e[cnt].w = w;
    //    e[cnt].f=-f;
    e[cnt].next = head[v];
    head[v] = cnt++;
}

inline void write(int x) {
    if (x < 0)
        putchar('-'), x = -x;
    if (x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}

inline int read() {
    int x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x * f;
}

int opt, l, r, c, sz, n, pos[maxn];
ll a[maxn], mu[maxn], ad[maxn];
// vector<ll>vec[505];

void update(int l, int r, int val1, int val2) {
    int pl = pos[l], pr = pos[r];
    for (int i = pl * sz + 1; i <= min(n, (pl + 1) * sz); i++) a[i] = (a[i] * mu[pl] + ad[pl]) % 10007;
    mu[pl] = 1, ad[pl] = 0;
    for (int i = l; i <= min((pl + 1) * sz, r); i++) {
        a[i] += val1;
        a[i] *= val2;
        a[i] %= 10007;
    }
    if (pl != pr) {
        for (int i = pr * sz + 1; i <= min(n, (pr + 1) * sz); i++) a[i] = (a[i] * mu[pr] + ad[pr]) % 10007;
        mu[pr] = 1, ad[pr] = 0;
        for (int i = pr * sz + 1; i <= r; i++) {
            a[i] += val1, a[i] *= val2;
            a[i] %= 10007;
        }
    }
    for (int i = pl + 1; i < pr; i++) {
        ad[i] = (ad[i] + val1) % 10007;
        ad[i] = ad[i] * val2 % 10007;
        mu[i] = mu[i] * val2 % 10007;
    }
};
ll query(int x) { return (a[x] * mu[pos[x]] + ad[pos[x]]); }
int main() {
    cin >> n;
    sz = sqrt(n);
    for (int i = 1; i <= n; i++) {
        a[i] = read();
        pos[i] = (i - 1) / sz;
        mu[pos[i]] = 1;
    }
    for (int i = 1; i <= n; i++) {
        opt = read(), l = read(), r = read(), c = read();
        if (!opt)
            update(l, r, c % 10007, 1);
        else if (opt == 1)
            update(l, r, 0, c % 10007);
        else
            printf("%d\n", (a[r] * mu[pos[r]] + ad[pos[r]]) % 10007);
    }
    return 0;
}

我们坚持一件事情,并不是因为这样做了会有效果,而是坚信,这样做是对的。
——哈维尔

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值