牛牛的等差数列【线段树】

牛牛的等差数列【线段树】_第1张图片


题目链接


  这里的突破口在于小于等于25且大于等于3的质数连乘在1e8左右,所以,我们可以在操作上,将其看作对1e8去求模,而不是对每个都进行预处理。

时间复杂度O(N log(N) * 8)\rightarrow O(N log(N))。也就是说,我们排除这个预处理之后,直接就是降了10倍左右的复杂度。

  然后,给区间一个等差数列,可以看成给这段区间赋一个基础值和递增一个值,所以我们在线段树上操作的时候,维护两个懒标记,分别是基础值,和等差值。因为存在累加(线性)关系,所以直接利用累加即可。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
//#include 
//#include 
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define Big_INF 0x3f3f3f3f3f3f3f3f
#define eps 1e-8
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MP(a, b) make_pair(a, b)
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 2e5 + 7;
const ull mod[8] = {3, 5, 7, 11, 13, 17, 19, 23};
const ull MOD = 3 * 5 * 7 * 11 * 13 * 17 * 19 * 23;
inline int _ID(ull mx) { return (int)(lower_bound(mod, mod + 8, mx) - mod); }
int N, Q;
ull a[maxN], tree[maxN << 2], sum[maxN << 2], del[maxN << 2];
bool lazy[maxN << 2] = {false};
inline void pushup(int rt) { tree[rt] = (tree[lsn] + tree[rsn]) % MOD; }
void buildTree(int rt, int l, int r)
{
    if(l == r) { tree[rt] = a[l] % MOD; return; }
    int mid = HalF;
    buildTree(Lson); buildTree(Rson);
    pushup(rt);
}
inline void pushdown(int rt, int l, int r)
{
    if(lazy[rt])
    {
        int mid = HalF; ull len_L = mid - l + 1, len_R = r - mid;
        lazy[lsn] = lazy[rsn] = true;
        sum[lsn] = (sum[lsn] + sum[rt]) % MOD;
        del[lsn] = (del[lsn] + del[rt]) % MOD;
        tree[lsn] = (tree[lsn] + sum[rt] * len_L + (len_L - 1LL) * len_L / 2LL % MOD * del[rt] % MOD) % MOD;
        sum[rsn] = (sum[rsn] + sum[rt] + del[rt] * len_L) % MOD;
        del[rsn] = (del[rsn] + del[rt]) % MOD;
        tree[rsn] = (tree[rsn] + (sum[rt] + del[rt] * len_L) % MOD * len_R + (len_R - 1LL) * len_R / 2LL % MOD * del[rt]) % MOD;
        sum[rt] = del[rt] = 0;
        lazy[rt] = false;
    }
}
void update(int rt, int l, int r, int ql, int qr, ull val, ull d)
{
    if(ql <= l && qr >= r)
    {
        ull fir_v = (val + d * (l - ql)) % MOD;
        lazy[rt] = true;
        sum[rt] = (sum[rt] + fir_v) % MOD;
        del[rt] = (del[rt] + d) % MOD;
        tree[rt] = (tree[rt] + fir_v * (r - l + 1) % MOD + (ull)(r - l) * (r - l + 1) / 2LL * d) % MOD;
        return;
    }
    pushdown(myself);
    int mid = HalF;
    if(qr <= mid) update(QL, val, d);
    else if(ql > mid) update(QR, val, d);
    else { update(QL, val, d); update(QR, val, d); }
    pushup(rt);
}
ull query(int rt, int l, int r, int ql, int qr, int op)
{
    if(ql <= l && qr >= r) return tree[rt] % mod[op];
    pushdown(myself);
    int mid = HalF;
    if(qr <= mid) return query(QL, op);
    else if(ql > mid) return query(QR, op);
    else return (query(QL, op) + query(QR, op)) % mod[op];
}
int main()
{
    scanf("%d", &N);
    for(int i=1; i<=N; i++) scanf("%lld", &a[i]);
    buildTree(1, 1, N);
    scanf("%d", &Q); int op, l, r, v, d;
    while(Q--)
    {
        scanf("%d", &op);
        if(op == 1)
        {
            scanf("%d%d%d%d", &l, &r, &v, &d);
            update(1, 1, N, l, r, v, d);
        }
        else
        {
            scanf("%d%d%d", &l, &r, &d);
            printf("%lld\n", query(1, 1, N, l, r, _ID(d)));
        }
    }
    return 0;
}

 

你可能感兴趣的:(数据结构,线段树)