A1490. osu!(乔明达)

只能过前60%,还在罗

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 #define maxn 550000
 5 #define lson rt<<1,l,mid
 6 #define rson rt<<1|1,mid+1,r
 7 double a[maxn];
 8 
 9 struct node{
10     int l,r;
11     double s0,s1,s2[4];
12     node(){
13         l=r=0;
14         s0=s1=0;
15         for(int i=0;i<4;i++)s2[i]=0;
16     }
17 }tree[maxn<<2];
18 
19 node push_up(node b,node c){
20     node ans;
21     ans.l=b.l,ans.r=c.r;
22     ans.s0=b.s0+c.s0-a[b.r]*a[c.l];
23     ans.s1=b.s1+c.s1;
24     ans.s2[0]=b.s2[0]*c.s2[0];
25     ans.s2[1]=b.s2[0]*c.s2[1]+b.s2[1];
26     ans.s2[2]=b.s2[2]*c.s2[0]+c.s2[2];
27     ans.s2[3]=b.s2[2]*c.s2[1]+b.s2[3]+c.s2[3];
28     return ans;
29 }
30 void build(int rt,int l,int r){
31     if(l==r){
32         tree[rt].l=l,tree[rt].r=r;
33         tree[rt].s0=tree[rt].s1=a[l];
34         for(int i=0;i<4;i++)tree[rt].s2[i]=a[l];
35         tree[rt].s2[1]+=a[l];
36         return;
37     }
38     int mid=(l+r)>>1;
39     build(lson),build(rson);
40     tree[rt]=push_up(tree[rt<<1],tree[rt<<1|1]);
41 }
42 void update(int rt,int pos,double x){
43     if(tree[rt].l==tree[rt].r){
44         tree[rt].s0=tree[rt].s1=x;
45         for(int i=0;i<4;i++)tree[rt].s2[i]=x;
46         tree[rt].s2[1]+=x;
47         return;
48     }
49     int mid=(tree[rt].l+tree[rt].r)>>1;
50     if(pos<=mid)update(rt<<1,pos,x);
51     else update(rt<<1|1,pos,x);
52     tree[rt]=push_up(tree[rt<<1],tree[rt<<1|1]);
53 }
54 node query(int rt,int ql,int qr){
55     if(ql<=tree[rt].l&&qr>=tree[rt].r)return tree[rt];
56     int mid=(tree[rt].l+tree[rt].r)>>1;
57     node ans;
58     if(ql<=mid)ans=query(rt<<1,ql,qr);
59     if(qr>mid)ans=push_up(ans,query(rt<<1|1,ql,qr));
60     return ans;
61 }
62 int main(){
63     int n,m,x,y,op;
64     double z;
65     scanf("%d%d",&n,&m);
66     for(int i=1;i<=n;i++)
67         scanf("%lf",&a[i]);
68     build(1,1,n);
69     for(int i=1;i<=m;i++){
70         scanf("%d",&op);
71         if(!op){
72             scanf("%d%d",&x,&y);
73             node fnl=query(1,x,y);
74             printf("%.2lf\n",fnl.s0+fnl.s1+fnl.s2[3]);
75         }
76         else{
77             scanf("%d%lf",&x,&z);
78             a[x]=z;
79             update(1,x,z);
80         }
81     }
82     return 0;
83 }
View Code

 

posted @ 2015-12-11 12:04  Ngshily  阅读(179)  评论(0编辑  收藏  举报