BZOJ 1798 [Ahoi2009]维护序列seq (线段树)

题意

对于一个给定的序列有3种操作:

1.给一个区间的数乘c

2.给一个区间的数加c

3.查询区间和。

思路

就是普通的线段树区间更新,因为更新操作有两种,维护两个延迟标记就可以了,不过要注意乘和加在更新时相互之间的关系,在更新乘的时候之前加的数也要相应的乘,更新加的时候之前所乘的数没有改变。

代码

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define LL long long
#define Lowbit(x) ((x)&(-x))
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1|1
#define MP(a, b) make_pair(a, b)
const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + 7;
const double eps = 1e-8;
const double PI = acos(-1.0);

LL p;
LL sum[maxn<<2];
LL add[maxn<<2], mul[maxn<<2];

void PushUp(int rt)
{
    sum[rt] = (sum[rt<<1] + sum[rt<<1|1]) % p;
}

void PushDown(int rt, int m)
{
    add[rt<<1] = (add[rt<<1] * mul[rt] + add[rt]) % p;
    add[rt<<1|1] = (add[rt<<1|1] * mul[rt] + add[rt]) % p;
    mul[rt<<1] = (mul[rt<<1] * mul[rt]) % p;
    mul[rt<<1|1] = (mul[rt<<1|1] * mul[rt]) % p;
    sum[rt<<1] = (sum[rt<<1] * mul[rt] + add[rt] * (m - (m >> 1))) % p;
    sum[rt<<1|1] = (sum[rt<<1|1] * mul[rt] + add[rt] * (m >> 1)) % p;
    mul[rt] = 1, add[rt] = 0;
}

void build(int l, int r, int rt)
{
    add[rt] = 0, mul[rt] = 1;
    if (l == r)
    {
        scanf("%lld", &sum[rt]);
        //printf("%d %d %d\n", l, r, sum[rt]);
        return ;
    }
    int mid = (l + r) >> 1;
    build(lson);
    build(rson);
    PushUp(rt);
}

void update(int L, int R, int type, LL c, int l, int r, int rt)
{
    if (L <= l && r <= R)
    {
        if (type == 1)
        {
            mul[rt] = (mul[rt] * c) % p;
            add[rt] = (add[rt] * c) % p;
            sum[rt] = (sum[rt] * c) % p;
        }
        else if (type == 2)
        {
            add[rt] = (add[rt] + c) % p;
            sum[rt] = (sum[rt] + (LL)(r - l + 1) * c) % p;
        }
        return;
    }
    PushDown(rt, r - l + 1);
    int mid = (l + r) >> 1;
    if (L <= mid) update(L, R, type, c, lson);
    if (R > mid) update(L, R, type, c, rson);
    PushUp(rt);
}

LL query(int L, int R, int l, int r, int rt)
{
    if (L <= l && r <= R)
    {
        return sum[rt] % p;
    }
    PushDown(rt, r - l + 1);
    int mid = (l + r) >> 1;
    LL res = 0;
    if (L <= mid) res = (res + query(L, R, lson)) % p;
    if (R > mid) res = (res + query(L, R, rson)) % p;
    return res;
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n, m;
    while (scanf("%d%lld", &n, &p) != EOF)
    {
        build(1, n, 1);
        scanf("%d", &m);
        for (int i = 1; i <= m; i++)
        {
            int t, l, r, c = 0;
            scanf("%d", &t);
            if (t == 1)
            {
                scanf("%d %d %d", &l, &r, &c);
                update(l, r, 1, c, 1, n, 1);
            }
            if (t == 2)
            {
                scanf("%d %d %d", &l, &r, &c);
                update(l, r, 2, c, 1, n, 1);
            }
            if (t == 3)
            {
                scanf("%d %d", &l, &r);
                printf("%lld\n", query(l, r, 1, n, 1));
            }
        }
    }
    return 0;
}
时间: 2024-07-30 22:31:04

BZOJ 1798 [Ahoi2009]维护序列seq (线段树)的相关文章

bzoj 1798: [Ahoi2009]Seq 维护序列seq 线段树 区间乘法区间加法 区间求和

1798: [Ahoi2009]Seq 维护序列seq Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/problem.php?id=1798 Description 老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成. 有长为N的数列,不妨设为a1,a2,…,aN .有如下三种操作形式: (1)把数列中的一段数全部乘一个值; (2)把数列中的一段数全部加一个值; (3)询问数列

BZOJ 1798 [Ahoi2009]Seq 维护序列seq 线段树

题意:链接 方法:线段树 解析: 俩标记sb题 更新乘的时候更新加 完了 代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define lson l,mid,rt<<1 #define rson mid+1,r,rt<<1|1 #define N 100010 using namespace std; typedef

bzoj1798: [Ahoi2009]Seq 维护序列seq 线段树

题目传送门 这道题就是线段树 先传乘法标记再传加法 #include<cstdio> #include<cstring> #include<algorithm> #define LL long long using namespace std; const int M=400010; LL read(){ LL ans=0,f=1,c=getchar(); while(c<'0'||c>'9'){if(c=='-') f=-1; c=getchar();}

洛谷 P2023 BZOJ 1798 [AHOI2009]维护序列

题目描述 老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成. 有长为N的数列,不妨设为a1,a2,…,aN .有如下三种操作形式: (1)把数列中的一段数全部乘一个值; (2)把数列中的一段数全部加一个值; (3)询问数列中的一段数的和,由于答案可能很大,你只需输出这个数模P的值. 输入输出格式 输入格式: 第一行两个整数N和P(1≤P≤1000000000).第二行含有N个非负整数,从左到右依次为a1,a2,…,aN, (0≤ai≤1000000000,1≤i≤N).第三行有一个整

P2023 [AHOI2009] 维护序列(线段树水题)

题目描述 老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成. 有长为N的数列,不妨设为a1,a2,…,aN .有如下三种操作形式: (1)把数列中的一段数全部乘一个值; (2)把数列中的一段数全部加一个值; (3)询问数列中的一段数的和,由于答案可能很大,你只需输出这个数模P的值. 输入输出格式 输入格式: 第一行两个整数N和P(1≤P≤1000000000).第二行含有N个非负整数,从左到右依次为a1,a2,…,aN, (0≤ai≤1000000000,1≤i≤N).第三行有一个整

P2023 [AHOI2009]维护序列 (线段树区间修改查询)

题目链接:https://www.luogu.org/problemnew/show/P2023 一道裸的线段树区间修改题,懒惰数组注意要先乘后加 #include<bits/stdc++.h> using namespace std; typedef long long LL; const int maxx = 400010; LL tree[maxx],lazy1[maxx],lazy2[maxx],a[maxx],mod; int n; void build(int l,int r,in

bzoj 1798 [Ahoi2009]Seq 维护序列seq ——线段树

题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1798 先乘后加,就可给加法标记乘上乘法标记. 注意可能有 *0 的操作,所以 pshd 时不是 cg[ cr ]>1 而是 cg[ cr ]!=1 . #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define ll long long #de

bzoj 1798 Seq 维护序列seq —— 线段树

题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1798 这题还4A... 注意:cnt 从1开始:各种模 p:乘法标记初始值是 1:可能乘 0. 代码如下: #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define mid ((l+r)>>1) using namespace std;

BZOJ_1798_[AHOI2009]维护序列_线段树

题意:老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成. 有长为N的数列,不妨设为a1,a2,-,aN .有如下三种操作形式: (1)把数列中的一段数全部乘一个值; (2)把数列中的一段数全部加一个值; (3)询问数列中的一段数的和,由于答案可能很大,你只需输出这个数模P的值. 分析:线段树上要打两个标记.要注意下传的顺序.显然先乘后加和先加后乘是不一样的.我们发现如果是先加后乘的话更改子树值的式子里会出现除法.不妨规定任何时候都先乘后加.推出的式子即为 t[lson]=(t[lso