URAL 1855 区间更新线段树

来源:互联网 发布:网络平台贷款逾期 编辑:程序博客网 时间:2024/06/06 20:33
//-K^2 + K(R-L-1) - R(L-1),即a*K^2+b*K+c。而对于每个查询,K的值都是不变的,变的只是系数a、b、c。//我们用三棵线段树分别去维护区//间内seg[K]*K^2,seg[K]*K,seg[K]的和,乘以每次询问对应的系数,就知道总费用,除以次数,就是答案了。#include<iostream>#include<cstdio>#include<cstdlib>#include<algorithm>#include<vector>#include<map>#define maxr 1000000#define maxn 200005#define ll __int64#define MID(a,b) (a+((b-a)>>1))using namespace std;//哪里有问题?????//维护每一次的系数。。//   * value k 我该维护什么。。//每次pushdown完都要pushup//mul维护k或k^2的和;sum维护所有的和;ll base[maxn][3];//我都不知道怎么过的。。。。。。。struct node{    struct miao    {        ll seg,val,lazy;        int l,r;    } tree[maxr];//总权值和每一段权值分开记录。。?加变量    void func(int root,ll value)//父节点居然在遍历到的时候就直接更新了。lazy是指他更新了但他的孩子还没有更新的东西    {        tree[root].val+=value*tree[root].seg;        tree[root].lazy+=value;//    cout<<tree[type][root].val<<endl;    }    void pushup(int root)    {        tree[root].val=tree[root*2].val+tree[root*2+1].val;    }    void pushdown(int root)    {        if(tree[root].lazy)        {            func(2*root+1,tree[root].lazy);            func(2*root,tree[root].lazy);            tree[root].lazy=0;        }    }    void build(int root,int left,int right,int type)    {        tree[root].l=left;        tree[root].r=right;        tree[root].val=0;        tree[root].lazy=0;        tree[root].seg=0;        if(left==right)        {            tree[root].seg=base[left][type];        }        else        {            int mid=MID(left,right);            build(root*2,left,mid,type);            build(root*2+1,mid+1,right,type);            tree[root].seg=tree[root*2+1].seg+tree[root*2].seg;        }    }    void update(int root,int x,int s,int t)    {        int left=tree[root].l;        int right=tree[root].r;        if(left>=s && right<=t)        {            func(root,x);        }        else        {            pushdown(root);            int mid=MID(left,right);            if(s<=mid) update(2*root,x,s,t);            if(t>mid) update(2*root+1,x,s,t);            pushup(root);        }    }    ll query(int root,int s,int t)    {        int left=tree[root].l;        int right=tree[root].r;        if(left>=s && right<=t) return tree[root].val;        else        {            pushdown(root);            ll sum1=0;            ll sum2=0;            int mid=MID(left,right);            if(t>mid) sum1=query(root*2+1,s,t);            if(s<=mid) sum2=query(root*2,s,t);            pushup(root);            return sum1+sum2;        }    }} seg[3];int main(){    int m,n;//    freopen("rr.txt","r",stdin);    while(scanf("%d%d",&n,&m)!=EOF)    {        for(int i=1; i<=n; i++)        {            base[i][0]=1;            base[i][1]=(ll)i;            base[i][2]=(ll)i*(ll)i;        }        n--;///用线段建树,所以会少一个点。        for(int i=0; i<3; i++)            seg[i].build(1,1,n,i);        char index[50];        for(int i=1; i<=m; i++)        {            scanf("%s",&index);            if(index[0]=='c')            {                int s,t;                int x;                scanf("%d%d%d",&s,&t,&x);                for(int i=0; i<3; i++)                    seg[i].update(1,x,s,t-1);            }            else            {                int s,t;                scanf("%d%d",&s,&t);                ll c=seg[0].query(1,s,t-1);                ll b=seg[1].query(1,s,t-1);                ll a=-seg[2].query(1,s,t-1);//                cout<<a<<endl;                ll tmp=t-s+1;                double ans=a+b*(s+t-1)+c*t*(1-s);                ans*=2;                ans/=(tmp);                ans/=(tmp-1);                printf("%.10lf\n",ans);            }        }    }    return 0;}

0 0