题目大意

区间取模,区间求和,单点修改。

分析

其实算是一道蛮简单的水题。
首先线段树非常好解决后两个操作,重点在于如何解决区间取模的操作。
一开始想到的是暴力单点修改,但是复杂度就飙到了\(mnlogn\),直接爆炸。
但是重新看到了题目中给出的4s的操作,说明,我们可以优化单点修改的操作。
那么我们顺便维护一下区间的最大值,如果当前的区间的最大值是小于mod数的,那么这个区间内的所有数都是没有必要mod的。
后面随着数据的越来越大,那么就可以剪去不必要的操作。

代码

#include <bits/stdc++.h>
#define ll long long
#define N 100005
using namespace std;
struct segment_tree {
    #define lc (nod << 1)
    #define rc (nod << 1 | 1)
    #define mid ((l + r) >> 1)
    struct node {
        ll s;
        int l, r, mx;
        node() {
            mx = s = 0;
        }
    }tr[N << 2];
    void pushup(int nod) {
        tr[nod].s = tr[lc].s + tr[rc].s;
        tr[nod].mx = max(tr[lc].mx, tr[rc].mx);
    }
    void build(int l, int r, int nod, int *a) {
        tr[nod].l = l, tr[nod].r = r;
        if (l == r) {
            tr[nod].mx = tr[nod].s = a[l];
            return;
        }
        build(l, mid, lc, a);
        build(mid + 1, r, rc, a);
        pushup(nod);
    }
    ll query_sec_sum(int nod, int ql, int qr)  {
        ll res = 0;
        int l = tr[nod].l, r = tr[nod].r;
        if (ql <= l && r <= qr) return tr[nod].s;
        if (ql <= mid) res += query_sec_sum(lc, ql, qr);
        if (qr > mid) res += query_sec_sum(rc, ql, qr);
        return res;
    }
    void update_point(int nod, int k, int val) {
        int l = tr[nod].l, r = tr[nod].r;
        if (l == r) {
            tr[nod].mx = tr[nod].s = val;
            return;
        }
        if (k <= mid) update_point(lc, k, val);
        else update_point(rc, k, val);
        pushup(nod);
    }
    void update_sec_mod(int nod, int ql, int qr, int p) {
        int l = tr[nod].l, r = tr[nod].r;
        if (tr[nod].mx < p) return;
        if (l == r) {
            tr[nod].s %= p;
            tr[nod].mx %= p;
            return;
        }
        if (ql <= mid) update_sec_mod(lc, ql, qr, p);
        if (qr > mid) update_sec_mod(rc, ql, qr, p);
        pushup(nod);
    }
}tr;
int a[N];
int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i ++) scanf("%d", &a[i]);
    tr.build(1, n, 1, a);
    while(m --) {
        int opt, x, y, z;
        scanf("%d", &opt);
        if (opt == 1) {
            scanf("%d%d", &x, &y);
            printf("%lld\n", tr.query_sec_sum(1, x, y));
        }
        if (opt == 2) {
            scanf("%d%d%d", &x, &y, &z);
            tr.update_sec_mod(1, x, y, z);
        }
        if (opt == 3) {
            scanf("%d%d", &x, &z);
            tr.update_point(1, x, z);
        }
    }
    return 0;
}

[CF438D]The Child and Sequence【线段树】的更多相关文章

  1. CF438D The Child and Sequence 线段树

    给定数列,区间查询和,区间取模,单点修改. n,m小于10^5 ...当区间最值小于模数时,就直接返回就好啦~ #include<cstdio> #include<iostream& ...

  2. Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间取摸

    D. The Child and Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...

  3. Codeforces 438D The Child and Sequence - 线段树

    At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at ...

  4. Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间求和+点修改+区间取模

    D. The Child and Sequence   At the children's day, the child came to Picks's house, and messed his h ...

  5. cf250D. The Child and Sequence(线段树 均摊复杂度)

    题意 题目链接 单点修改,区间mod,区间和 Sol 如果x > mod ,那么 x % mod < x / 2 证明: 即得易见平凡, 仿照上例显然, 留作习题答案略, 读者自证不难. ...

  6. CF(438D) The Child and Sequence(线段树)

    题意:对数列有三种操作: Print operation l, r. Picks should write down the value of . Modulo operation l, r, x. ...

  7. CodeForces 438D The Child and Sequence (线段树 暴力)

    传送门 题目大意: 给你一个序列,要求在序列上维护三个操作: 1)区间求和 2)区间取模 3)单点修改 这里的操作二很讨厌,取模必须模到叶子节点上,否则跑出来肯定是错的.没有操作二就是线段树水题了. ...

  8. Codeforces Round #250 (Div. 1) D. The Child and Sequence (线段树)

    题目链接:http://codeforces.com/problemset/problem/438/D 给你n个数,m个操作,1操作是查询l到r之间的和,2操作是将l到r之间大于等于x的数xor于x, ...

  9. 2016暑假多校联合---Rikka with Sequence (线段树)

    2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...

随机推荐

  1. 消息队列queue

    一.queue 在多线程编程中,程序的解耦往往是一个麻烦的问题,以及在socket网络编程中也会有这样的问题.recv 和send之间,如果服务端有消息,问题需要发送给客户端,而那边的recv 被主程 ...

  2. spring实例入门

    首先是bean文件: package onlyfun.caterpillar; public class HelloBean {    private String helloWord = " ...

  3. Python之操作Excel

    使用之前先导入三个模块: import xlwt #只能写Excel import xlrd #只能读Excel import xlutils #修改Excel,在原来的基础上修改 一.写EXCEL ...

  4. Python之字符串操作

    一.字符串特点 内容不可修改 password=' #内容不可修改 二.字符串常用方法 1..strip()方法 去字符串两边的空格和换行符 print(password.strip()) #去掉字符 ...

  5. Python之缩进块

    pycharm编辑器识别冒号,当换行后下一行默认是缩进块的位置:

  6. 如何使用RSS

    (转载: http://www.ruanyifeng.com/blog/2006/01/rss.html) 一. 自从我发现很多人不知道什么是RSS以后,我就一直想向大家介绍它,因为它太有用了,将来会 ...

  7. Azure系列2.1.6 —— BlobProperties

    (小弟自学Azure,文中有不正确之处,请路过各位大神指正.) 网上azure的资料较少,尤其是API,全是英文的,中文资料更是少之又少.这次由于公司项目需要使用Azure,所以对Azure的一些学习 ...

  8. 游标cursor案例

  9. 在Laravel中使用数据库事务以及捕获事务失败后的异常

    Description 在Laravel中要想在数据库事务中运行一组操作,则可以在 DB facade 中使用 transaction 方法.如果在事务的闭包内抛出异常,事务将会被自动还原.如果闭包运 ...

  10. 初次启动hive,解决 ls: cannot access /home/hadoop/spark-2.2.0-bin-hadoop2.6/lib/spark-assembly-*.jar: No such file or directory问题

    >>提君博客原创  http://www.cnblogs.com/tijun/  << 刚刚安装好hive,进行第一次启动 提君博客原创 [hadoop@ltt1 bin]$ ...