1 条题解

  • 0
    @ 2025-4-27 20:40:53

    坑人的从0开始

    #include <bits/stdc++.h>
    using namespace std;
    const int N = 1e5;
    typedef long long ll;
    int a[N];
    struct Node{
        int l,r;
        int data;
        
    }tree[4*N];
    //p: 当前节点的编号  l 表示节点负责的范围的左区间
    void build(int p ,int l,int r)
    {
        tree[p].l = l;
        tree[p].r = r;
        if(l == r)
        {
            tree[p].data = a[l];
            return;
        }
        int mid = (l+r)>>1;
        build(p*2+1,l,mid );
        build(p*2+2 ,mid + 1,r);
        tree[p].data = tree[p*2+1].data + tree[p*2+2].data;
    }
    
    ll query(int p,int x,int y)
    {
        if(tree[p].l>=x && tree[p].r <= y)
        {
            return tree[p].data;
        }
        ll res = 0;
        int mid = (tree[p].l +  tree[p].r)>>1;
        if( x <=mid ) res += query(p*2+1  ,x,y);
        if (y>mid ) res += query(p*2+2,x,y);
        return res;
        
    }
    
    void update(int p,int x,int v)
    {
        if(tree[p].l == tree[p].r)
        {
            tree[p].data = v;
    
            return;
        }
        int mid = (tree[p].l +   tree[p].r)>>1;
        if(x <= mid) update(p*2+1,x,v);
        else update(p*2+2, x,v);
        tree[p].data = tree[p*2+1].data + tree[p*2+2].data;
    }
    
    int main(){
    	int n,q ;
    	cin>>n>>q ;
    	for(int i = 0;i<n;i++)cin>>a[i];
    	build(0,0,n-1);
    	while(q--)
    	{
    	    int x,y,z;
    	    cin>>x>>y>>z;
    	    if(x == 1)
    	    {
    	        update(0,y,z);
    	    }
    	    else if(x == 2)
    	    {
    	      
            	cout<<query(0,y,z)<<endl;
    	        
    	    }
    	    
    	}
    
    
    
    	
    
    }
    
    • 1

    信息

    ID
    8323
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    2
    已通过
    1
    上传者