Codeforces 438 D. The Child and Sequence (线段树

题意: 三种操作

1.区间求和
3.区间取模
2.单点更新

题解:

直接单点更新暴力维护, 取模的均摊复杂度为 O(nlogn) O ( n l o g n )

#include <bits/stdc++.h>

using namespace std;

#define ll long long 
#define pb push_back
#define ls o<<1
#define rs o<<1|1
#define fi first
#define se second
#define CLR(a, b) memset(a, (b), sizeof(a))
const ll INF = 0x3f3f3f3f;
const ll mod = 1e9+7;
const ll MAXN = 2e5+10;
inline int read() {
    int x = 0, fh = 1; char ch = getchar();
    for (; !isdigit(ch); ch = getchar()) if (ch == '-') fh = -1;
    for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48);
    return x * fh;
}

void F() {
    //ios::sync_with_stdio(false);
    #ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
    #endif
}
ll n, m;
ll arr[MAXN];
struct node {
    ll l, r;
    ll x, mo;
}t[MAXN<<2];
inline void push_up(ll o) {
    t[o].mo = max(t[ls].mo, t[rs].mo);
    t[o].x = t[rs].x+t[ls].x;
} 
inline void build(ll l, ll r, ll o) {
    t[o].l=l, t[o].r=r;
    if(l==r) {
        t[o].x=t[o].mo=arr[l];
        return;
    }
    ll mid=(l+r)>>1;
    build(l,mid,ls);build(mid+1,r,rs);
    push_up(o);
}
inline void update(ll l, ll o, ll x) {
    if(l==t[o].l && t[o].l==t[o].r) {
        t[o].x= t[o].mo = x; return ;
    }
    ll mid=(t[o].l+t[o].r)>>1;
    if(l<=mid) update(l,ls,x);
    else if(l>mid) update(l,rs,x); 
    push_up(o);
}
inline void modif(ll l, ll r, ll o, ll p) {
    if(t[o].mo<p) return;
    if(t[o].l==t[o].r) {
        t[o].x=t[o].mo=t[o].mo%p;
        return;
    }
    ll mid=(t[o].l+t[o].r)>>1;
    if(r<=mid) modif(l,r,ls,p);
    else if(l>mid) modif(l,r,rs,p);
    else {
        modif(l,mid,ls,p); modif(mid+1,r,rs,p);
    }
    push_up(o);
}
inline ll query(ll l, ll r, ll o) {
    if(t[o].l>=l && t[o].r<=r) return t[o].x;
    if(t[o].l>r || t[o].r<l) return 0;
    ll ans = 0;
    ll mid=(t[o].l+t[o].r)>>1;
    if(r<=mid) ans += query(l,r,ls);
    else if(l>mid) ans += query(l,r,rs);
    else {
        ans+=query(l,mid,ls); ans+=query(mid+1,r,rs);
    }
    return ans;
}
int main() {
    F();
    n = read(); m =read();
    for(ll i = 1; i <= n; ++i) 
        arr[i] = read();
    build(1, n, 1);
    while(m--) {
        ll op,l,r,x;
        cin>>op;;
        if(op == 1) {
            cin>>l>>r;
            cout << query(l,r,1) << endl;
        }
        if(op == 2) {
            ll p;
            cin >>l>>r>>p;
            modif(l,r,1,p);
        }
        if(op==3){
            cin>>l>>x;
            update(l,1,x);
        }
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值