首页 > 其他分享 >CF438D The Child and Sequence

CF438D The Child and Sequence

时间:2022-09-06 21:24:36浏览次数:107  
标签:取模 ch Sequence int mid CF438D Child 区间 op

CF438D The Child and Sequence

洛谷链接

同一个思路AC四道题太爽了

题目大意:

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

分析:

难点在于区间取模很难实现标记下传以及合并。
思路和线段树区间暴力开根类似。维护一个区间最大值。当要取模的区间最大值比模数小时,给这个区间取模相当于没有取模;否则即使要取模,区间中的每个数都最多只会被操作 \(O(log(x))\) 次,所以单点暴力取模不会超时。

代码:

#include<bits/stdc++.h>
#define int long long
#define MAXN 100086 
using namespace std;

int a[MAXN];
inline int read() {
     int x = 0, fh = 1;
     char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-')
            fh = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    return x * fh;
}

struct T{
int l,r,val,maxn;
}t[MAXN*4];
int n,m;

void update(int node){
  t[node].val=t[node<<1].val+t[node<<1|1].val;
  t[node].maxn=max(t[node<<1].maxn,t[node<<1|1].maxn);
}

void build(int l,int r,int node){
    t[node].l=l;
    t[node].r=r;
    if(l==r){
        t[node].val=a[l];
        t[node].maxn=a[l];
        return ;
    }
    int mid=(l+r)>>1;
    build(l,mid,node<<1);
    build(mid+1,r,node<<1|1);
    update(node);
}


void change(int l,int r,int node,int p,int k){
  if(l==r){
    t[node].val=k;
    t[node].maxn=k;
    return ;
  }
  int mid=(l+r)>>1;
  int ans=0;
  if(p<=mid)change(l,mid,node<<1,p,k);
  else change(mid+1,r,node<<1|1,p,k);
  update(node);
}

int ask(int l,int r,int node,int x,int y){
  if(x<=l&&r<=y){
    return t[node].val;
  }
  int ans=0;
  int mid=(l+r)>>1;
  if(x<=mid)ans+=ask(l,mid,node<<1,x,y);
  if(y>mid)ans+=ask(mid+1,r,node<<1|1,x,y);
  return ans;
}


void change_mod(int l,int r,int node,int x,int y,int p){
  if(t[node].maxn<p)return ;
  if(l==r){
    t[node].val%=p;
    t[node].maxn%=p;
    return ;
  }
  int mid=(l+r)>>1;
  if(x<=mid)change_mod(l,mid,node<<1,x,y,p);
  if(y>mid)change_mod(mid+1,r,node<<1|1,x,y,p);
  update(node);
}

signed main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
  cin>>a[i];
}
build(1,n,1);
int op,x,y,z,k,p;
for(int i=1;i<=m;i++){
  cin>>op>>x>>y;
  if(op==1){
    cout<<ask(1,n,1,x,y)<<'\n';
  }
  if(op==2){
    cin>>z;
    change_mod(1,n,1,x,y,z);
  }
  if(op==3){
    change(1,n,1,x,y);
  }

}
  return 0;
}

标签:取模,ch,Sequence,int,mid,CF438D,Child,区间,op
From: https://www.cnblogs.com/DAIANZE/p/16663327.html

相关文章

  • Yet Another RGB Sequence(排列组合)
    题意问有多少字符串满足如下要求:只包含R、G、B三种字符,并且数量分别是\(A\),\(B\),\(C\)。包含\(K\)个连续子串RG。题目链接:https://atcoder.jp/contests/abc266/tasks......
  • Linq SequenceEqual
    publicclassUnitTest1{publicclassPerson{publicstringName{get;set;}publicintAge{get;set;}......
  • Angular 父组件通过@ViewChild 主动获取子组件的数据和方法
    链接1.父组件中调用子组件<app-footer#footer></app-footer>2.父组件中调用子组件的值<button(click)="getChildMsg()">获取子组件的msg</button>@ViewChild('foot......
  • MathProblem 52 Two children problem
    Awomanischosenatrandomamongallwomenthathavetwochildren.Sheisaskeddoyouhaveatleastoneboy,andsheanswers'yes.'Whatistheprobabilityh......
  • The specified child already has a parent. You must call removeView() on the chil
    报上面的错的意思是已经有了一个父,不能够再有一个父,一个孩子一个父。解决:1、获取view的父2、removeView删除所属的孩子3、然后再使用就可以了。可能的代码:bindingMen......
  • CF1121B Mike and Children 题解
    题意翻译十分简洁,我说几点需要注意的。最多能选几个数?这是错的,要给出最多选出几对数。现在我们就珂以开始了。我的做法理论时间复杂度是 O(n^3)O(n3) 的暴力,但是因......
  • leetcode 594. Longest Harmonious Subsequence 最长和谐子序列(简单).md
    一、题目大意https://leetcode.cn/problems/longest-harmonious-subsequence和谐数组是指一个数组里元素的最大值和最小值之间的差别正好是1。现在,给你一个整数数组......
  • Link with Monotonic Subsequence(构造)
    题意定义lis为最长上升子序列,lds为最长下降子序列。构造一个排列\(p\),使得\(\max(lis(p),lds(p))\)最小。题目链接:https://ac.nowcoder.com/acm/contest/33187/G数据......
  • 936. Stamping The Sequence
    Youaregiventwostrings stamp and target.Initially,thereisastring s oflength target.length withall s[i]=='?'.Inoneturn,youcanplace st......
  • CF815 D2 Xor-Subsequence (hard version)(01trie)
    传送门sb题面误导了我半天。按位考虑,对于\(a[i]\)和\(i\)的一位考虑什么样的\(a[j]\)和\(j\)可以转移过来,发现这一位有一种一定可以一种一定不行,还有两种不确定。考虑......