6:
      LAZY 线段树有乘法的更新
   #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn = 101000;
long long value[maxn], mod;
struct SegNode {
    int left, right;
    long long sum, add, mul;
    int mid() {
        return (left + right) >> 1;
    }
    int size() {
        return right - left + 1;
    }
};
struct SegmentTree {
    SegNode tree[maxn*5];
    void pushUp(int idx) {
        tree[idx].sum = (tree[idx<<1].sum + tree[idx<<1|1].sum) % mod;
    }
    void pushDown(int idx) {
        tree[idx<<1].add = (tree[idx].mul % mod * tree[idx<<1].add % mod + tree[idx].add) % mod;
        tree[idx<<1|1].add = (tree[idx].mul % mod * tree[idx<<1|1].add % mod + tree[idx].add) % mod;
        tree[idx<<1].mul = tree[idx<<1].mul % mod * tree[idx].mul % mod;
        tree[idx<<1|1].mul = tree[idx<<1|1].mul % mod * tree[idx].mul % mod;
        tree[idx<<1].sum = (tree[idx<<1].sum % mod * tree[idx].mul % mod
            + tree[idx<<1].size() * tree[idx].add % mod) % mod;
        tree[idx<<1|1].sum = (tree[idx<<1|1].sum % mod * tree[idx].mul % mod
            + tree[idx<<1|1].size() * tree[idx].add % mod) % mod;
        tree[idx].add = 0;
        tree[idx].mul = 1;
    }
    void build(int left, int right, int idx) {
        tree[idx].left = left;
        tree[idx].right = right;
        tree[idx].sum = 0;
        tree[idx].mul = 1;
        tree[idx].add = 0;
        if (left == right) {
            tree[idx].sum = value[left] % mod;
            return ;
        }
        int mid = tree[idx].mid();
        build(left, mid, idx<<1);
        build(mid+1, right, idx<<1|1);
        pushUp(idx);
    }
    void update(int left, int right, int idx, int opt, long long val) {
        if (tree[idx].left == left && tree[idx].right == right) {
            if (opt == 1) {
                tree[idx].add = (tree[idx].add + val) % mod;
                tree[idx].sum = (tree[idx].sum + tree[idx].size() % mod * val) % mod;
            } else {
                tree[idx].add = tree[idx].add % mod * val % mod;
                tree[idx].mul = tree[idx].mul % mod * val % mod;
                tree[idx].sum = tree[idx].sum % mod * val % mod;
            }
            return ;
        }
        pushDown(idx);
        int mid = tree[idx].mid();
        if (right <= mid)
            update(left, right, idx<<1, opt, val);
        else if (left > mid)
            update(left, right, idx<<1|1, opt, val);
        else {
            update(left, mid, idx<<1, opt, val);
            update(mid+1, right, idx<<1|1, opt, val);
        }
        pushUp(idx);
    }
    long long query(int left, int right, int idx) {
        if (tree[idx].left == left && tree[idx].right == right) {
            return tree[idx].sum % mod;
        }
        pushDown(idx);
        int mid = tree[idx].mid();
        if (right <= mid)
            return query(left, right, idx<<1);
        else if (left > mid)
            return query(left, right, idx<<1|1);
        else {
            return (query(left, mid, idx<<1) % mod + query(mid+1, right, idx<<1|1));
        }
    }
};
SegmentTree tree;
int n, m;
void init() {
    scanf("%d %lld", &n, &mod);
    for (int i = 1; i <= n; i++)
        scanf("%lld", &value[i]);
    tree.build(1, n, 1);
}

BZOJ 1798:的更多相关文章

  1. BZOJ 1798 题解

    1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec  Memory Limit: 64 MBSubmit: 5531  Solved: 1946[Submit ...

  2. bzoj 1798 [Ahoi2009]Seq 维护序列seq

    原题链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1798 线段树区间更新: 1. 区间同同时加上一个数 2. 区间同时乘以一个数 #inclu ...

  3. bzoj 1798 [Ahoi2009]Seq 维护序列seq(线段树+传标)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1798 [题意] 给定一个序列,要求提供区间乘/加,以及区间求和的操作 [思路] 线段树 ...

  4. BZOJ 1798: [Ahoi2009]Seq 维护序列seq( 线段树 )

    线段树.. 打个 mul , add 的标记就好了.. 这个速度好像还挺快的...( 相比我其他代码 = = ) 好像是#35.. ---------------------------------- ...

  5. bzoj 1798: [Ahoi2009]Seq 维护序列seq (线段树 ,多重标记下放)

    1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec  Memory Limit: 64 MBSubmit: 7773  Solved: 2792[Submit ...

  6. bzoj 1798 线段树

    1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec  Memory Limit: 64 MBSubmit: 7163  Solved: 2587[Submit ...

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

    1798: [Ahoi2009]Seq 维护序列seq Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeO ...

  8. bzoj 1798 双标记区间修改线段树

    #include<bits/stdc++.h> using namespace std; #define MAXN 100000 #define M ((L+R)>>1) #d ...

  9. bzoj 1798 维护序列seq

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1798 题解: 高级一点的线段树,加上了区间乘法运算,则需要增加一个数组mulv记录乘的因数 ...

  10. 值得一做》关于双标记线段树两三事BZOJ 1798 (NORMAL-)

    这是一道双标记线段树的题,很让人很好的预习/学习/复习线段树,我不知道它能让别人学习什么,反正让我对线段树的了解更加深刻. 题目没什么好讲的,程序也没什么好讲的,所以也没有什么题解,但是值得一做 给出 ...

随机推荐

  1. CSData

    NSString 转换成NSData 对象 NSData* xmlData = [@"testdata" dataUsingEncoding:NSUTF8StringEncodin ...

  2. Oracle Recycle Bin

    开启回收站RECYCLEBIN=ON,默认开启 ALTER SYSTEM SET RECYCLEBIN=OFF SCOPE=SPFILE; 一.从回收站还原表 还原删除的表和从属对象. 如果多个回收站 ...

  3. ubuntu破解密码方法

    摘要: 开机按住任何键(shift)停住grub菜单,进入advanced option for ubuntu,出现的菜单中,光标移动至…(recovery mode)按E进入编辑,找到ro reco ...

  4. 自定义Jquery分页插件

    /** * 功能说明:jPager 分页插件 * 参数说明:pages:[] 分页的控件个数 @id:显示分页的div ID,@showSelectPage: 是否显示当前分页的条目过滤下拉框 * @ ...

  5. 微信小程序开发系列六:微信框架API的调用

    微信小程序开发系列教程 微信小程序开发系列一:微信小程序的申请和开发环境的搭建 微信小程序开发系列二:微信小程序的视图设计 微信小程序开发系列三:微信小程序的调试方法 微信小程序开发系列四:微信小程序 ...

  6. Hessian矩阵与牛顿法

    Hessian矩阵与牛顿法 牛顿法 主要有两方面的应用: 1. 求方程的根: 2. 求解最优化方法: 一. 为什么要用牛顿法求方程的根? 问题很多,牛顿法 是什么?目前还没有讲清楚,没关系,先直观理解 ...

  7. Java随机产生中文昵称

    有时候我们注册一个网站第一次登陆系统会产生一个随机昵称供用户选择,在项目测试阶段遇到了这个问题,因为注册时没有让用户填写昵称,于是找了两种产生随机中文昵称的方法: 代码如下 package com.u ...

  8. windows下安装python

    1. 进入python官网   https://www.python.org/downloads/windows/ 2.根据我们的电脑位数选择版本,尽量选择  Download Windows x86 ...

  9. Caused by: java.lang.IllegalArgumentException: Parameter Maps collection does not contain value for com.bj186.crm.mapper.UserMapper.Integer

    在使用SSM整合myBatis的过程中遇到了这个问题. 问题的原因: 把parameterType错误的写成了parameterMap 解决办法: 将parameterMap修改为parameterT ...

  10. Proguard配置文件内容

    -injars elec-bendao-1.2.jar-outjars elec-bendao-1.2-end.jar -libraryjars lib\charsets.jar-libraryjar ...