2 solutions

  • 3
    @ 2024-12-19 14:32:22

    我来发一个tree node风格的代码。这个写法必须有建树的过程,因为每个节点管理的区间需要预先算出来。

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    
    const int MAXN = 1e5 + 10;
    struct node {
    	int L, R; // 该节点维护的区间范围
    	LL v; // 该节点维护的值(区间和)
    } tr[MAXN << 2]; // 根节点编号为1
    int tot, MOD;
    
    // 把当前节点的左右子节点的信息更新到
    // (往上层推到)当前节点。
    void pushUp(int p) {
    	tr[p].v = (tr[p << 1].v * tr[p << 1 | 1].v) % MOD;
    }
    
    // 递归建树。
    // p是当前要创建的节点在tr数组中的编号。
    void build(int p, int L, int R) {
    	// 一定不能忘了!!!初始化每个节点管理的区间范围!
    	tr[p].L = L, tr[p].R = R;
    	// 每个节点保存的值要么是1,要么是m_i
    	if (L == R) {
    		tr[p].v = 1LL;
    		return;
    	}
    	int mid = (L + R) >> 1;
    	build(p << 1, L, mid);
    	build(p << 1 | 1, mid + 1, R);
    	pushUp(p); // 回溯时要把左右子树的更改上传到父节点p
    }
    
    // 单点更新。
    // p:当前的递归入口(tr节点编号)
    // x,newv:需要更新的(在原数组中的)位置、新值
    void update(int p, int x, int newv) {
    	if (tr[p].L == tr[p].R) {
    		tr[p].v = (LL)newv;
    		return;
    	}
    	int mid = (tr[p].L + tr[p].R) >> 1;
    	// 在树中往下走到正确的维护区间 [x,x] 的节点
    	if (x <= mid) update(p << 1, x, newv);
    	else update(p << 1 | 1, x, newv);
    	pushUp(p); // 回溯时要把左右子树的更改上传到父节点p
    }
    
    void solve() {
    	int Q;
    	cin >> Q >> MOD;
    	build(1, 1, MAXN - 1);
    
    	for (int i=1; i<=Q; i++) {
    		int op, x;
    		cin >> op >> x;
    		if (op == 1) {
    			int m = x;
    			update(1, i, m);
    		} else {
    			int pos = x;
    			update(1, pos, 1);
    		}
    		cout << tr[1].v % MOD << '\n';
    	}
    }
    
    int main() {
    	int t;
    	cin >> t;
    	while (t--) solve();
    	return 0;
    }
    
    • @ 2024-12-20 12:52:43

      啊,我的代码有救啦......

    • @ 2024-12-20 21:28:41

      啊,我的代码有救啦......

    • @ 2024-12-20 21:33:46

      鸡生体节

      #include<iostream>
      #include<cstring>
      #define int long long
      #define N 100005
      using namespace std;
      int t,q,mod;
      int tree[4*N];
      void btree(int l,int r,int x)
      {
      	if(l==r)
      	{
      		tree[x]=1;
      		return;
      	}
      	int m=(l+r)>>1;
      	btree(l,m,x<<1);
      	btree(m+1,r,x<<1|1);
      	tree[x]=(tree[x<<1]*tree[x<<1|1])%mod;
      }
      void utree(int l,int s,int t,int c,int x)
      {
      	if(l==s && t==l)
      	{
      		tree[x]=c;
      		return;
      	}
      	int m=(s+t)>>1;
      	if(l<=m)
      		utree(l,s,m,c,x<<1);
      	else
      		utree(l,m+1,t,c,x<<1|1);
      	tree[x]=(tree[x<<1]*tree[x<<1|1])%mod;
      }
      signed main()
      {
      	ios::sync_with_stdio(0);
      	cin.tie(0);
      	cout.tie(0);
      	cin>>t;
      	while(t--)
      	{
      		int o,x;
      		cin>>q>>mod;
      		btree(1,q,1);
      		for(int i=1;i<=q;i++)
      		{
      			cin>>o>>x;
      			if(o==1)
      				utree(i,1,q,x,1);
      			else
      				utree(x,1,q,1,1);
      			cout<<tree[1]%mod<<"\n";
      		}
      	}
      	return 0;
      }
      
    • @ 2024-12-20 21:38:27

      @ 没注释

  • 0
    @ 2024-12-18 14:02:51
    #include <bits/stdc++.h>
    #define int long long 
    using namespace std;
    struct str{
    	int l,r,v;
    }tr[400005];
    int a[100005];
    void push(int p,int mod) {
    	tr[p].v=tr[p<<1].v*tr[p<<1|1].v;
    	tr[p].v%=mod;
    }
    void build(int p,int x,int y) {
    	tr[p].l=x;
    	tr[p].r=y;
    	if(x==y) {
    		tr[p].v=1;
    		return;
    	}
    	int mid=(x+y)>>1;
    	build(p<<1,x,mid);
    	build(p<<1|1,mid+1,y);
    }
    void update(int p,int x,int v,int mod) {
    	if(tr[p].l==tr[p].r) {
    		tr[p].v=v;
    		tr[p].v%=mod;
    		return;
    	}
    	int mid=(tr[p].l+tr[p].r)>>1;
    	if(x<=mid){
    		update(p<<1,x,v,mod);
    	}
    	else{
    		update(p<<1|1,x,v,mod);
    	}
    	push(p,mod); 
    }
    int query(int p,int x,int y,int mod) {
    	if(x<=tr[p].l&&tr[p].r<=y) {
    		return tr[p].v%mod;
    	}
    	int mid=(tr[p].l+tr[p].r)>>1;
    	int ans=1;
    	if(x<=mid) {
    		ans*=query(p<<1,x,y,mod);
    	}
    	if(mid<y) {
    		ans*=query(p<<1|1,x,y,mod);
    	}
    	return ans%mod;
    }
    map<int,int>maps;
    signed main() {
    	ios::sync_with_stdio(0);
    	cin.tie(0),cout.tie(0);
    	int t;
    	cin>>t;
    	while(t--){
    		int q,mod;
    		cin>>q>>mod;
    		build(1,1,q);
    		int s=0;
    		for(int i=1;i<=q;i++){
    			int f,m;
    			cin>>f>>m;
    			if(f==1){
    				s++;
    				maps[i]=s;
    				update(1,s,m,mod);
    			} 
    			else{
    				update(1,maps[m],1,mod);
    			}
    			cout<<query(1,1,s,mod)<<"\n";
    		}	
    	} 
    }
    
    • @ 2024-12-19 14:33:13

      用map、写了query函数,差评【手动狗头

  • 1

Information

ID
288
Time
1000ms
Memory
250MiB
Difficulty
7
Tags
# Submissions
54
Accepted
11
Uploaded By