数列分块入门 7 LibreOJ - 6283

数列分块入门7

题意

给一个长度为 n n n 的序列,以及 n n n 个操作,涉及区间加法,区间乘法,单点查询。

解法

分块。

小块暴力一定要先清空两个标记数组!

  • 某个点的真实值为 a [ i ] ⋅ m u l [ i d ] + b a s e [ i d ] a[i] \cdot mul[id]+base[id] a[i]mul[id]+base[id]

  • 那么对于大块的加法操作,只要 b a s e [ i d ] = b a s e [ i d ] + x base[id]=base[id]+x base[id]=base[id]+x 即可

  • 对于大块的乘法操作,只要 b a s e [ i d ] = b a s e [ i d ] ∗ x , m u l [ i d ] = m u l [ i d ] ∗ x base[id]=base[id]*x,mul[id]=mul[id]*x base[id]=base[id]x,mul[id]=mul[id]x 即可。

  • 小块暴力更新的时候,一定要先清空这个大块的 b a s e [ i d ] base[id] base[id] m u l [ i d ] mul[id] mul[id] ,获得真实值后再去暴力更新。

代码
#pragma region
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define tr t[root]
#define lson t[root << 1]
#define rson t[root << 1 | 1]
#define rep(i, a, n) for (int i = a; i <= n; ++i)
#define per(i, a, n) for (int i = n; i >= a; --i)
namespace fastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
//fread->R
bool IOerror = 0;
//inline char nc(){char ch=getchar();if(ch==-1)IOerror=1;return ch;}
inline char nc() {
    static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE;
    if (p1 == pend) {
        p1 = buf;
        pend = buf + fread(buf, 1, BUF_SIZE, stdin);
        if (pend == p1) {
            IOerror = 1;
            return -1;
        }
    }
    return *p1++;
}
inline bool blank(char ch) { return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; }
template <class T>
inline bool R(T &x) {
    bool sign = 0;
    char ch = nc();
    x = 0;
    for (; blank(ch); ch = nc())
        ;
    if (IOerror)
        return false;
    if (ch == '-')
        sign = 1, ch = nc();
    for (; ch >= '0' && ch <= '9'; ch = nc())
        x = x * 10 + ch - '0';
    if (sign)
        x = -x;
    return true;
}
inline bool R(double &x) {
    bool sign = 0;
    char ch = nc();
    x = 0;
    for (; blank(ch); ch = nc())
        ;
    if (IOerror)
        return false;
    if (ch == '-')
        sign = 1, ch = nc();
    for (; ch >= '0' && ch <= '9'; ch = nc())
        x = x * 10 + ch - '0';
    if (ch == '.') {
        double tmp = 1;
        ch = nc();
        for (; ch >= '0' && ch <= '9'; ch = nc())
            tmp /= 10.0, x += tmp * (ch - '0');
    }
    if (sign)
        x = -x;
    return true;
}
inline bool R(char *s) {
    char ch = nc();
    for (; blank(ch); ch = nc())
        ;
    if (IOerror)
        return false;
    for (; !blank(ch) && !IOerror; ch = nc())
        *s++ = ch;
    *s = 0;
    return true;
}
inline bool R(char &c) {
    c = nc();
    if (IOerror) {
        c = -1;
        return false;
    }
    return true;
}
template <class T, class... U>
bool R(T &h, U &... t) { return R(h) && R(t...); }
#undef OUT_SIZE
#undef BUF_SIZE
};  // namespace fastIO
using namespace fastIO;
template <class T>
void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template <class T, class U>
void _W(const pair<T, U> &x) { _W(x.F), putchar(' '), _W(x.S); }
template <class T>
void _W(const vector<T> &x) {
    for (auto i = x.begin(); i != x.end(); _W(*i++))
        if (i != x.cbegin()) putchar(' ');
}
void W() {}
template <class T, class... U>
void W(const T &head, const U &... tail) { _W(head), putchar(sizeof...(tail) ? ' ' : '\n'), W(tail...); }
#pragma endregion
const int maxn = 1e5 + 5;
const ll mod = 10007;
int n, B;
ll a[maxn];
ll base[maxn], mult[maxn];
ll query(int pos) {
    return (a[pos] * mult[pos / B] + base[pos / B]) % mod;
}
void add(int l, int r, ll x) {
    int idl = l / B, idr = r / B;
    if (idl == idr) {
        rep(i, max(1, idl * B), min(n, (idl + 1) * B - 1)) a[i] = query(i);
        base[idl] = 0, mult[idl] = 1;
        rep(i, l, r) a[i] = (a[i] + x) % mod;
    } else {
        rep(i, max(1, idl * B), min(n, (idl + 1) * B - 1)) a[i] = query(i);
        base[idl] = 0, mult[idl] = 1;
        rep(i, l, (idl + 1) * B - 1) a[i] = (a[i] + x) % mod;
        rep(id, idl + 1, idr - 1) base[id] = (base[id] + x) % mod;
        rep(i, max(1, idr * B), min(n, (idr + 1) * B - 1)) a[i] = query(i);
        base[idr] = 0, mult[idr] = 1;
        rep(i, idr * B, r) a[i] = (a[i] + x) % mod;
    }
}
void mul(int l, int r, ll x) {
    int idl = l / B, idr = r / B;
    if (idl == idr) {
        rep(i, max(1, idl * B), min(n, (idl + 1) * B - 1)) a[i] = query(i);
        base[idl] = 0, mult[idl] = 1;
        rep(i, l, r) a[i] = a[i] * x % mod;
    } else {
        rep(i, max(1, idl * B), min(n, (idl + 1) * B - 1)) a[i] = query(i);
        base[idl] = 0, mult[idl] = 1;
        rep(i, l, (idl + 1) * B - 1) a[i] = a[i] * x % mod;
        rep(id, idl + 1, idr - 1) base[id] = base[id] * x % mod, mult[id] = mult[id] * x % mod;
        rep(i, idr * B, min(n, (idr + 1) * B - 1)) a[i] = query(i);
        base[idr] = 0, mult[idr] = 1;
        rep(i, idr * B, r) a[i] = a[i] * x % mod;
    }
}
int main() {
    R(n);
    B = sqrt(n);
    rep(i, 1, n) R(a[i]), mult[i / B] = 1;
    rep(i, 1, n) {
        int op, l, r, c;
        R(op, l, r, c);
        if (op == 0) add(l, r, c);
        if (op == 1) mul(l, r, c);
        if (op == 2) W(query(r));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值