CF438D The Child and Sequence(线段树区间取模)

时间:2021-08-01
本文章向大家介绍CF438D The Child and Sequence(线段树区间取模),主要包括CF438D The Child and Sequence(线段树区间取模)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

思路

维护区间的最大值,取模的时候如果最大值小于模数就跳过此区间;否则,对每个点暴力修改。由于每次取模后 \(x \bmod p<2/x\),所以每个点最多被修改\(logx\)次。

代码

// Problem: D. The Child and Sequence
// Contest: Codeforces - Codeforces Round #250 (Div. 1)
// URL: https://codeforces.com/problemset/problem/438/D
// Memory Limit: 256 MB
// Time Limit: 4000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PLL;
typedef pair<int, int>PII;
typedef pair<double, double>PDD;
#define I_int ll
inline ll read()
{
    ll x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9')
    {
        if(ch == '-')f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9')
    {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}
  
inline void out(ll x){
    if (x < 0) x = ~x + 1, putchar('-');
    if (x > 9) out(x / 10);
    putchar(x % 10 + '0');
}
  
inline void write(ll x){
    if (x < 0) x = ~x + 1, putchar('-');
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}
  
#define read read()
#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define perr(i,a,b) for(int i=(a);i>(b);i--)
ll ksm(ll a, ll b)
{
    ll res = 1;
    while(b)
    {
        if(b & 1)res = res * a ;
        a = a * a ;
        b >>= 1;
    }
    return res;
}

const int inf = 0x3f3f3f3f;
const int maxn=1e5+7,maxm=210000;

int n,m,a[maxn];
struct node{
	int l,r;
	ll sum,maxx;
}tr[maxn<<2];

void pushup(int u){
	tr[u].sum=tr[u<<1].sum+tr[u<<1|1].sum;
	tr[u].maxx=max(tr[u<<1].maxx,tr[u<<1|1].maxx);
}

void build(int u,int l,int r){
	tr[u]={l,r};
	if(l==r){
		tr[u].sum=tr[u].maxx=a[l];
		return ;
	}
	int mid=(l+r)/2;
	build(u<<1,l,mid);build(u<<1|1,mid+1,r);
	pushup(u);
}

ll query_sum(int u,int l,int r){
	if(tr[u].l>=l&&tr[u].r<=r) return tr[u].sum;
	int mid=(tr[u].l+tr[u].r)/2;
	ll res=0;
	if(l<=mid) res+=query_sum(u<<1,l,r);
	if(r>mid) res+=query_sum(u<<1|1,l,r);
	return res;
}

void update_mod(int u,int l,int r,ll x){
	if(tr[u].l>=l&&tr[u].r<=r&&tr[u].maxx<x) return ;
	if(tr[u].l>=l&&tr[u].r<=r&&tr[u].l==tr[u].r){
		tr[u].maxx%=x;tr[u].sum%=x;
		return ;
	}
	int mid=(tr[u].l+tr[u].r)/2;
	if(l<=mid) update_mod(u<<1,l,r,x);
	if(r>mid) update_mod(u<<1|1,l,r,x);
	pushup(u);
}

void update_pos(int u,int l,int r,int x){
	if(tr[u].l==l&&tr[u].r==r){
		tr[u].sum=tr[u].maxx=x;
		return ;
	}
	int mid=(tr[u].l+tr[u].r)/2;
	if(l<=mid) update_pos(u<<1,l,r,x);
	if(r>mid) update_pos(u<<1|1,l,r,x);
	pushup(u);
}

int main(){
	n=read,m=read;
	rep(i,1,n) a[i]=read;
	build(1,1,n);
	while(m--){
		int op=read;
		if(op==1){
			int l=read,r=read;
			printf("%lld\n",query_sum(1,l,r));
		}
		else if(op==2){
			int l=read,r=read;
			ll x=read;
			update_mod(1,l,r,x);
		}
		else{
			int k=read,x=read;
			update_pos(1,k,k,x);
		}
	}
	
	
	
	
	
	
	
	return 0;
}

原文地址:https://www.cnblogs.com/OvOq/p/15086520.html