Codeforces 431E

1.假如没有修改,直接排序,
然后二分答案,然后找到比答案小的最大的位置,然后求前缀和怼怼
2.有了修改,离散化权值,建线段树,剩下的一样

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e5+10;
typedef __int64 LL;
LL cc[N*2];
int num[N*2];
LL aa[N],bb[N*2],top;
void update(int x,LL v1,int v2){
    while(x<=top){
        cc[x]+=v1;
        num[x]+=v2;
        x+=x&-x;
    }
}
LL sum;int cnt;
void query(int x){
    sum=0;cnt=0;
    while(x){
        cnt+=num[x];sum+=cc[x];
        x-=x&-x;
    }
}
int op[N],pp[N];LL xx[N];
int main(){
    #ifdef DouBi
    freopen("in.cpp","r",stdin);
    #endif // DouBi
    int n,q;
    while(scanf("%d%d",&n,&q)!=EOF){
        top=0;
        for(int i=1;i<=n;i++){
            scanf("%I64d",&aa[i]);
            bb[top++]=aa[i];
        }
        for(int i=0;i<q;i++){
            scanf("%d",&op[i]);
            if(op[i]==1){
                scanf("%d%I64d",&pp[i],&xx[i]);
                bb[top++]=xx[i];
            }
            else {
                scanf("%I64d",&xx[i]);
            }
        }
        bb[top++]=1e16;
        sort(bb,bb+top);top=unique(bb,bb+top)-bb;
        memset(cc,0,sizeof(cc));
        memset(num,0,sizeof(num));
        for(int i=1;i<=n;i++){
            int x=lower_bound(bb,bb+top,aa[i])-bb+1;
            update(x,aa[i],1);
        }
//        for(int i=0;i<top;i++){
//            printf("%I64d ",bb[i]);
//        }printf("bb\n");
        for(int i=0;i<q;i++){
            if(op[i]==1){
                int x=lower_bound(bb,bb+top,xx[i])-bb+1;
                update(x,xx[i],1);
                x=lower_bound(bb,bb+top,aa[pp[i]])-bb+1;
                update(x,-aa[pp[i]],-1);
                aa[pp[i]]=xx[i];
            }
            else {
                int l=1,r=top;
                while(l+1<r){
                    int m=(l+r)/2;
                    query(m);
                    //printf("%d %I64d %I64d %d\n",m,bb[m],sum,cnt);
                    if(sum+xx[i]>bb[m-1]*cnt)l=m;
                    else r=m;
                }
                r--;
                query(r);
                //printf("%d %I64d %I64d %d\n",r-1,bb[r-1],sum,cnt);
                printf("%.10lf\n",(double)(sum+xx[i])/cnt);
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值