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. QTableWidget表头样式

    转载请注明出处:http://www.cnblogs.com/dachen408/p/7742680.html QTableView { background-color: rgba(255, 255 ...

  2. [Python學習筆記] 利用 Python在Excel 插入註解

    用Python 來處理excel 檔 用過了 openpyxl 還有 pyexcel目前覺得除了讀寫如果還要使用另外的功能 (像是讀取格子裡的公式)可以用 xlwings  他的首頁標題 " ...

  3. PHP一句话后门过狗姿势万千之传输层加工

    既然木马已就绪,那么想要利用木马,必然有一个数据传输的过程,数据提交是必须的,数据返回一般也会有的,除非执行特殊命令. 当我们用普通菜刀连接后门时,数据时如何提交的,狗狗又是如何识别的,下面结合一个实 ...

  4. Pygame - Python游戏编程入门

    >>> import pygame>>> print(pygame.ver)1.9.2a0 如果没有报错,应该是安装好了~ 如果报错找不到模块,很可能是安装版本的问 ...

  5. 暑假集训 || 树DP

    树上DP通常用到dfs https://www.cnblogs.com/mhpp/p/6628548.html POJ 2342 相邻两点不能同时被选 经典题 f[0][u]表示不选u的情况数,此时v ...

  6. vue中状态管理vuex的使用分享

    一.main.js中引入 store import store from './store' window.HMO_APP = new Vue({ router, store, render: h = ...

  7. saltstack入门个人笔记

    offical website reference1 reference2 install apt-get install python-software-properties apt install ...

  8. 牛客网练习赛25 C 再编号

    链接:https://www.nowcoder.com/acm/contest/158/C来源:牛客网 定义对 a 的再编号为 a' ,满足 . 现在有 m 次询问,每次给定 x,t ,表示询问经过 ...

  9. IO之Object流举例

    import java.io.*; public class TestObjectIO { public static void main(String args[]) throws Exceptio ...

  10. git 项目相关

    工具篇:Sourcetree 和 Git Bash Sourcetree Git一款非常好用的可视化工具,方便管理项目.下载地址 https://www.sourcetreeapp.com/ Git ...