The Child and Sequence 【CodeForces - 438D】【线段树】

版权声明:https://blog.csdn.net/qq_41730082 https://blog.csdn.net/qq_41730082/article/details/86348880

题目链接


  就是有三种更新的方式:

1 u v 对于所有i u<=i<=v,输出a[i]的和;
2 u v t 对于所有i u<=i<=v a[i]=a[i]%t;
3 u v 表示a[u]=v(将v赋值给a[u]);

然后,就是思路了,对于这样的一个区间更新,我们直接暴力更新到底,每次用一个剪枝,区间最大值小于模值的话,就没必要了,并且,区间和为0的话也是没必要的了。


#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define SonG_y main
#define MP(x, y) make_pair(x, y)
#define myself rt, l, r
#define Lson rt<<1, l, mid
#define Rson rt<<1|1, mid+1, r
#define qL Lson, ql, qr, val
#define qR Rson, ql, qr, val
#define HalF (l + r)>>1
#define max_4(a, b, c, d) max(max(a, b), max(c, d))
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 1e5 + 7;
int N, Q;
ll tree[maxN<<2], maxx[maxN<<2];
void pushup(int rt)
{
    tree[rt] = tree[rt<<1] + tree[rt<<1|1];
    maxx[rt] = max(maxx[rt<<1], maxx[rt<<1|1]);
}
void buildTree(int rt, int l, int r)
{
    if(l == r)
    {
        scanf("%lld", &tree[rt]);
        maxx[rt] = tree[rt];
        return;
    }
    int mid = HalF;
    buildTree(Lson);
    buildTree(Rson);
    pushup(rt);
}
void update(int rt, int l, int r, int ql, int qr, ll val)
{
    if(maxx[rt] < val) return;
    if(l == r)
    {
        tree[rt] %= val;
        maxx[rt] = tree[rt];
        return;
    }
    int mid = HalF;
    if(ql > mid) update(qR);
    else if(qr <= mid) update(qL);
    else { update(qL); update(qR); }
    pushup(rt);
}
void change(int rt, int l, int r, int qx, ll val)
{
    if(l == r) { tree[rt] = maxx[rt] = val;  return; }
    int mid = HalF;
    if(qx <= mid) change(Lson, qx, val);
    else change(Rson, qx, val);
    pushup(rt);
}
ll query(int rt, int l, int r, int ql, int qr)
{
    if((ql<=l && qr>=r) || tree[rt] == 0) return tree[rt];
    int mid = HalF;
    if(ql > mid) return query(Rson, ql, qr);
    else if(qr <= mid) return query(Lson, ql, qr);
    else return query(Lson, ql, qr) + query(Rson, ql, qr);
}
int SonG_y()
{
    scanf("%d%d", &N, &Q);
    buildTree(1, 1, N);
    while(Q--)
    {
        int op; scanf("%d", &op);
        if(op == 1)
        {
            int e1, e2; scanf("%d%d", &e1, &e2);
            printf("%lld\n", query(1, 1, N, e1, e2));
        }
        else if(op == 2)
        {
            int e1, e2;     ll e3;  scanf("%d%d%lld", &e1, &e2, &e3);
            update(1, 1, N, e1, e2, e3);
        }
        else if(op == 3)
        {
            int e1; ll e2;  scanf("%d%lld", &e1, &e2);
            change(1, 1, N, e1, e2);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41730082/article/details/86348880