标签:
input | output |
---|---|
4 5 change 1 4 2 change 1 2 -1 establish 1 2 establish 2 4 establish 1 4 |
1.00000000 2.66666667 2.83333333
|
分析:设询问区间为[l,r],第k条路编号为k+1;
则第k条路的贡献为(k-l)*(r-k+1)*cost[k];
展开后即[-k*k+(l+1+r)*k-l-l*r]*cost[k];
线段树单点维护好cost[k],k*cost[k],k*k*cost[k]即可;
注意爆int;
代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> #include <climits> #include <cstring> #include <string> #include <set> #include <map> #include <queue> #include <stack> #include <vector> #include <list> #define rep(i,m,n) for(i=m;i<=n;i++) #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++) #define mod 1000000007 #define inf 0x3f3f3f3f #define vi vector<int> #define pb push_back #define mp make_pair #define fi first #define se second #define ll long long #define pi acos(-1.0) #define pii pair<int,int> #define Lson L, mid, rt<<1 #define Rson mid+1, R, rt<<1|1 const int maxn=1e5+10; using namespace std; ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);} ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;} int n,m,k; char op[10]; ll ans[3]; ll gao(int p) { return (ll)p*(p+1)*(2*p+1)/6; } struct Node { ll sum,sum1,sum2,lazy; } T[maxn<<2]; void PushUp(int rt) { T[rt].sum = T[rt<<1].sum + T[rt<<1|1].sum; T[rt].sum1 = T[rt<<1].sum1 + T[rt<<1|1].sum1; T[rt].sum2 = T[rt<<1].sum2 + T[rt<<1|1].sum2; } void PushDown(int L, int R, int rt) { int mid = (L + R) >> 1; ll t = T[rt].lazy; T[rt<<1].sum += t * (mid - L + 1); T[rt<<1|1].sum += t * (R - mid); T[rt<<1].sum1 += t * (mid - L + 1)*(mid + L)/2; T[rt<<1|1].sum1 += t * (R - mid)*(R + mid +1)/2; T[rt<<1].sum2 += t * (gao(mid)-gao(L-1)); T[rt<<1|1].sum2 += t * (gao(R)-gao(mid)); T[rt<<1].lazy += t; T[rt<<1|1].lazy += t; T[rt].lazy = 0; } void Update(int l, int r, ll v, int L, int R, int rt) { if(l==L && r==R) { T[rt].lazy += v; T[rt].sum += v * (R - L + 1); T[rt].sum1 += v * (R - L + 1)*(R + L)/2; T[rt].sum2 += v * (gao(R)-gao(L-1)); return ; } int mid = (L + R) >> 1; if(T[rt].lazy) PushDown(L, R, rt); if(r <= mid) Update(l, r, v, Lson); else if(l > mid) Update(l, r, v, Rson); else { Update(l, mid, v, Lson); Update(mid+1, r, v, Rson); } PushUp(rt); } void Query(int l, int r, int L, int R, int rt) { if(l==L && r== R) { ans[0]+=T[rt].sum; ans[1]+=T[rt].sum1; ans[2]+=T[rt].sum2; return; } int mid = (L + R) >> 1; if(T[rt].lazy) PushDown(L, R, rt); if(r <= mid) Query(l, r, Lson); else if(l > mid) Query(l, r, Rson); else Query(l, mid, Lson) , Query(mid + 1, r, Rson); } int main() { int i,j; scanf("%d%d",&n,&m); while(m--) { int a,b,c; scanf("%s",op); if(op[0]==‘c‘) { scanf("%d%d%d",&a,&b,&c); Update(a+1,b,(ll)c,1,n,1); } else { scanf("%d%d",&a,&b); ans[0]=ans[1]=ans[2]=0; Query(a+1,b,1,n,1); printf("%.10f\n",(double)(-ans[2]+(a+b+1)*ans[1]-(a+(ll)a*b)*ans[0])/(b-a)/(b-a+1)*2); } } //system("Pause"); return 0; }
ural1855 Trade Guilds of Erathia
标签:
原文地址:http://www.cnblogs.com/dyzll/p/5874940.html