C29 线段树 P4588 [TJOI2018] 数学计算

视频链接:216 线段树 [TJOI2018] 数学计算_哔哩哔哩_bilibili

 

Luogu P4588 [TJOI2018] 数学计算

复制代码
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

const int N=100005;
#define ls u<<1
#define rs ls|1
int M;
struct Tree{
  int l,r;
  int mul; //区间积
}tr[N*4]; 

void pushup(int u){
  tr[u].mul=(1ll*tr[ls].mul*tr[rs].mul)%M;
}
void build(int u,int l,int r){ //建树
  tr[u]={l,r,1};
  if(l==r) return;
  int mid=(l+r)>>1;
  build(ls,l,mid);
  build(rs,mid+1,r);
  pushup(u);
}
void change(int u,int x,int c){ //点修
  if(x==tr[u].l && x==tr[u].r){
    tr[u].mul=c; return;
  }
  int mid=tr[u].l+tr[u].r>>1;
  if(x<=mid) change(ls,x,c);
  if(x>mid) change(rs,x,c);
  pushup(u);
}
int main(){
  int t; scanf("%d",&t);
  for(int i=1;i<=t;i++){
    int q,op,m,pos; scanf("%d%d",&q,&M);
    build(1,1,q);
    for(int i=1;i<=q;i++){
      scanf("%d",&op);
      if(op==1) scanf("%d",&m), change(1,i,m);
      else scanf("%d",&pos), change(1,pos,1);
      printf("%d\n",tr[1].mul);
    }
  }
}
复制代码

 

posted @   董晓  阅读(306)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示