Ryuji doesn't want to study-线段树应用(待更改)

#include<bits/stdc++.h>
using namespace std;
#define maxn 5550000
#define ll long long
struct node
{
    ll l,r,sum;
} root[maxn],tree[maxn];
void creat(node *t,ll l,ll r,ll c)
{
    t[c].l=l;
    t[c].r=r;
    if(l==r)
    {
        return ;
        t[c].sum=0;
    }
    ll mid=(l+r)/2;
    creat(t,mid+1,r,c*2+1);
    creat(t,l,mid,c*2);
    t[c].sum=t[c*2].sum+t[c*2+1].sum;
}
void updata(node *t,ll l,ll ad,ll c)
{
    if(t[c].l==l&&t[c].r==l)
    {
        t[c].sum=ad;
        return ;
    }
    ll mid=(t[c].l+t[c].r)/2;
    if(l<=mid)
        updata(t,l,ad,c*2);
    else
        updata(t,l,ad,c*2+1);
    t[c].sum=t[c*2].sum+t[c*2+1].sum;
}
ll query(node *t,ll l,ll r,ll c)
{
    if(l==t[c].l&&r==t[c].r)
        return t[c].sum;
    ll mid=(t[c].l+t[c].r)/2;
    if(r<=mid)
        return query(t,l,r,c*2);
    else if(l>=mid)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
        return query(t,l,r,c*2+1);
    else
    {
        return query(t,l,mid,c*2)+query(t,mid,r,c*2+1);
    }
}
ll n,q,u,v,w,ans;
int main()
{
    while(cin>>n>>q)
    {
        creat(root,1,n,1);
        creat(tree,1,n,1);
        for(int i=1; i<=n; i++)
        {
            cin>>w;
            updata(root,i,w,1);
            updata(tree,i,w*(n-i+1),1);
        }
        while(q--)
        {
            cin>>u>>v>>w;
            if(u==2)
            {
                updata(root,v,w,1);
                updata(tree,v,w*(n-v+1),1);
            }
            else
            {
                ans=query(tree,v,w,1)-(n-w)*query(root,v,w,1);
                cout<<ans<<endl;
            }
        }
    }
    return 0;
}

https://nanti.jisuanke.com/t/31460

猜你喜欢

转载自blog.csdn.net/BePosit/article/details/82721925