Bzoj 2752 Expressway (expected, segment tree)
Topic links
This problem is obviously for the side, because the title is a chain, so the direct use of the edge of the number. As a sequence
\ (1\)And\ (2\)The edge of the dot is even. Number is\ (1\)The point of the query.\ (r-1\)Just fine.
The expectation here is obviously the average of the path.
Expectations:\[\dfrac{\sum_{i=l}^r\sum_{j=l}^{r}dis[i][j]}{c_{r-l+1}^2}\]
The following sections can be calculated directly:
This part of the above is more difficult to maintain.
Consider how many times each side will be traversed.
\[ans = \sum_{i=l}^ra[i]* (r-i+1) (i-l+1) \]
is equivalent to enumerating the two paths around this point.
Then disassemble.
Re-simplify the formula.
form the following shape.
\[ans = (r-l + 1-r * L) * sum1 + (R + L) * sum2-sum3\]
which
\ (sum1 = \sum_{i=l}^r a[i]\)
\ (sum2 = \sum_{i=l}^r a[i]*i\)
\ (sum3 = \sum_{i=l}^r a[i] * i * i\)
Then we'll use the line tree to maintain it.
Consider merging.
\ (sum1 = lson_{sum1} + rson_{sum1}\)
\ (sum2 = lson_{sum2} + rson_{sum2}\)
\ (sum3 = lson_{sum3} + rson_{sum3}\)
Merging is relatively straightforward.
How do I add when I add it?
Set the value added to\ (k\)
Then the formula becomes.
\ (sum1\)It's simple, just add the length of the interval multiplied by\ (k\)Can.
\ (sum2\)To add\ (k*\sum i\)and maintain the interval.\ (i\)The and. We call it\ (sum5\), or consider the method of arithmetic progression summation is also possible.
\ (sum3\)To add\ (k * \sum i ^2\)We have to maintain it here.\ (sum4\), it represents\ (\sum i^2\)
\ (sum4\)And\ (sum5\)is a fixed value. In the time of the achievement of the update on the line.
Then the problem is complete.
Special attention is given to the fact that weset Edge as Point. So to\ (R-= 1\)
If you directly\ (r-=1\), the following denominator should be changed.\ (c_{r-l + 2}^2\)
CODE:
#include <iostream> #include <cstdio> #define Lson now << 1#define Rson now << 1 | 1#define ll long longconst ll maxn = 100000 + 7;inline ll read () {ll x = 0,f = 1;char c = GetChar (); while (C < ' 0 ' | | c > ' 9 ') {if (c = = '-') F = -1;c = GetChar ();} while (c >= ' 0 ' && C <= ' 9 ') {x = x * + C-' 0 '; c = GetChar ();} return x * f;} ll GCD (ll A,ll b) {return!b a:gcd (b,a% b);} struct Node {ll sum[6]; ll lazy; ll L,r;} TREE[MAXN << 2];ll sum1,sum2,sum3;void updata (ll now) {tree[now].sum[1] = Tree[lson].sum[1] + tree[rson].sum[1]; TREE[NOW].SUM[2] = tree[lson].sum[2] + tree[rson].sum[2]; TREE[NOW].SUM[3] = tree[lson].sum[3] + tree[rson].sum[3]; return;} void Build (ll l,ll r,ll now) {TREE[NOW].L = L;TREE[NOW].R = R; if (L = = r) {Tree[now].sum[4] = L * l; TREE[NOW].SUM[5] = l; return; } ll mid = (L + r) >> 1; Build (L,mid,lson); Build (mid + 1,r,rson); Tree[now]. sum[4] = Tree[lson].sum[4] + tree[rson].sum[4]; TREE[NOW].SUM[5] = tree[lson].sum[5] + tree[rson].sum[5]; return;} void work (ll now,ll k) {tree[now].sum[1] + = (TREE[NOW].R-TREE[NOW].L + 1) * k; Tree[now].sum[2] + = k * Tree[now].sum[5]; Tree[now].sum[3] + = k * Tree[now].sum[4]; Tree[now].lazy + = k;} void Pushdown (ll now) {work (lson,tree[now].lazy); Work (Rson,tree[now].lazy); Tree[now].lazy = 0; return;} void Modify (ll l,ll R,ll now,ll val) {if (tree[now].l >= l && tree[now].r <= R) {work (now,val); return; } if (Tree[now].lazy) pushdown (now); ll mid = (tree[now].l + tree[now].r) >> 1; If (Mid >= L) modify (L,r,lson,val); if (Mid < R) Modify (L,r,rson,val); Updata (now); return;} void query (ll l,ll r,ll now) {if (tree[now].l >= l && tree[now].r <= r) {sum1 + = Tree[now].sum[1 ]; Sum2 + = tree[now].sum[2]; SUM3 + = tree[now].sum[3]; return; } if (trEe[now].lazy) Pushdown (now); ll mid = (tree[now].l + tree[now].r) >> 1; If (Mid >= L) query (L,r,lson); if (Mid < R) query (L,r,rson); return;} int main () {ll n,m,l,r,v; Char s[3]; n = read (); m = read (); Build (1,n,1); while (M-) {scanf ("%s", &s); L = read (); r = Read ()-1; if (s[0] = = ' C ') {v = read (); Modify (L,R,1,V); } else {ll A; sum1 = sum2 = sum3 = 0; Query (l,r,1); A = (r-l + 1-r * L) * sum1 + (R + L) * SUM2-SUM3; ll B = (r-l + 2) * (R-l + 1)/2; ll g = gcd (A, b); printf ("%lld/%lld\n", a/g,b/g); }} return 0;}
Bzoj 2752 Expressway (expected, segment tree)