zoj2706

  1 /*
  2 题意:给n个数,m个操作,每次把区间[l,r]的数用它们的平均值替代,
  3 如果平均值不是整数,且当前n个数的和小于原先的和就向上round,不然就向下round;
  4 
  5 分析:简单线段区间更新;
  6 
  7 trick:关于正数和负数的整除问题,正数整除是自动向下取整的,但负数是向上取整的
  8 即13/3=4.3 ->4  但-13/3=-4.3 ->-4
  9 
 10 */
 11 #include<cstdio>
 12 #include<cstring>
 13 #include<cstdlib>
 14 #include<iostream>
 15 #include<algorithm>
 16 #include<cmath>
 17 #define lson l,m,rt<<1
 18 #define rson m+1,r,rt<<1|1
 19 using namespace std;
 20 typedef long long LL;
 21 const int N=30000+10;
 22 LL col[N<<2],sum[N<<2],a[N];
 23 void pushup(int rt){
 24     sum[rt]=sum[rt<<1]+sum[rt<<1|1];
 25 }
 26 void pushdown(int l,int m,int r,int rt){
 27     if (col[rt]){
 28         col[rt<<1]=col[rt<<1|1]=col[rt];
 29         sum[rt<<1]=(LL)(m-l+1)*col[rt];
 30         sum[rt<<1|1]=(LL)(r-m)*col[rt];
 31         col[rt]=0;
 32     }
 33 }
 34 void build(int l,int r,int rt){
 35      col[rt]=sum[rt]=0;
 36      if (l==r){
 37         sum[rt]=a[l];
 38         return;
 39      }
 40      int m=(l+r)>>1;
 41      build(lson);build(rson);
 42      pushup(rt);
 43 }
 44 void update(int L,int R,int v,int l,int r,int rt){//将[L,R]更新为v;
 45     if (L<=l && r<=R){
 46         sum[rt]=(LL)(r-l+1)*v;
 47         col[rt]=v;
 48         return;
 49     }
 50     int m=(l+r)>>1;
 51     pushdown(l,m,r,rt);
 52     if (L<=m) update(L,R,v,lson);
 53     if (m< R) update(L,R,v,rson);
 54     pushup(rt);
 55 }
 56 LL query(int L,int R,int l,int r,int rt){//询问[L,R]里的SUM
 57     if (L<=l && r<=R){
 58         return sum[rt];
 59     }
 60     int m=(l+r)>>1;
 61     pushdown(l,m,r,rt);
 62     LL ret=0;
 63     if (L<=m) ret+=query(L,R,lson);
 64     if (m< R) ret+=query(L,R,rson);
 65     return ret;
 66 }
 67 void Q(int l,int r,int rt){
 68     if (l==r){
 69         a[l]=sum[rt];
 70         return;
 71     }
 72     int m=(l+r)>>1;
 73     pushdown(l,m,r,rt);
 74     Q(lson);Q(rson);
 75 }
 76 int n,m;
 77 int main(){
 78     while (~scanf("%d%d",&n,&m)){
 79         LL S=0;
 80         for (int i=0;i<n;i++){
 81             cin>>a[i];
 82             S+=a[i];
 83         }
 84         build(0,n-1,1);
 85         for (int i=0;i<m;i++){
 86             int x,y;scanf("%d%d",&x,&y);
 87             x--,y--;
 88             LL S1=query(x,y,0,n-1,1);
 89            // cout<<"** "<<S1<<endl;
 90             if (S1%(y-x+1)!=0){
 91                 //对于正负数更新不同的值
 92                 int flag1=0,flag2=0;
 93                 if (S1>=0)flag1=1;
 94                 if (S1<0) flag2=-1;
 95               
 96                 if (sum[1]<=S) update(x,y,S1/(y-x+1)+flag1,0,n-1,1);
 97                 else update(x,y,S1/(y-x+1)+flag2,0,n-1,1);
 98             }else update(x,y,S1/(y-x+1),0,n-1,1);
 99         }
100         Q(0,n-1,1);
101         for (int i=0;i<n-1;i++) cout<<a[i]<<" ";
102         cout<<a[n-1]<<endl;
103         cout<<endl;
104     }
105 
106     return 0;
107 }

 

posted @ 2013-05-21 21:31  Rabbit_hair  阅读(234)  评论(0编辑  收藏  举报