codeforces round 512 F. Putting Boxes Together 树状数组维护区间加权平均数

F. Putting Boxes Together
time limit per test
2.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There is an infinite line consisting of cells. There are nn boxes in some cells of this line. The ii-th box stands in the cell aiai and has weight wiwi. All aiai are distinct, moreover, ai1<aiai−1ii.

You would like to put together some boxes. Putting together boxes with indices in the segment [l,r][l,r] means that you will move some of them in such a way that their positions will form some segment [x,x+(rl)][x,x+(r−l)].

In one step you can move any box to a neighboring cell if it isn't occupied by another box (i.e. you can choose ii and change aiai by 11, all positions should remain distinct). You spend wiwi units of energy moving the box ii by one cell. You can move any box any number of times, in arbitrary order.

Sometimes weights of some boxes change, so you have queries of two types:

  1. idid nwnw — weight widwid of the box idid becomes nwnw.
  2. lrr — you should compute the minimum total energy needed to put together boxes with indices in [l,r][l,r]. Since the answer can be rather big, print the remainder it gives when divided by 1000000007=109+71000000007=109+7. Note that the boxes are not moved during the query, you only should compute the answer.

Note that you should minimize the answer, not its remainder modulo 109+7109+7. So if you have two possible answers 2109+132⋅109+13and 2109+142⋅109+14, you should choose the first one and print 109+6109+6, even though the remainder of the second answer is 00.

Input

The first line contains two integers nn and qq (1n,q21051≤n,q≤2⋅105) — the number of boxes and the number of queries.

The second line contains nn integers a1,a2,ana1,a2,…an (1ai1091≤ai≤109) — the positions of the boxes. All aiai are distinct, ai1<aiai−1ii.

The third line contains nn integers w1,w2,wnw1,w2,…wn (1wi1091≤wi≤109) — the initial weights of the boxes.

Next qq lines describe queries, one query per line.

Each query is described in a single line, containing two integers xx and yy. If x<0x<0, then this query is of the first type, where id=xid=−x, nw=ynw=y (1idn1≤id≤n, 1nw1091≤nw≤109). If x>0x>0, then the query is of the second type, where l=xl=x and r=yr=y (1ljrjn1≤lj≤rj≤n). xx can not be equal to 00.

Output

For each query of the second type print the answer on a separate line. Since answer can be large, print the remainder it gives when divided by 1000000007=109+71000000007=109+7.

Example
input
Copy
5 8
1 2 6 7 10
1 1 1 1 2
1 1
1 5
1 3
3 5
-3 5
-1 10
1 4
2 5
output
Copy
0
10
3
4
18
7
Note

Let's go through queries of the example:

  1. 1 11 1 — there is only one box so we don't need to move anything.
  2. 1 51 5 — we can move boxes to segment [4,8][4,8]: 1|14|+1|25|+1|66|+1|77|+2|108|=101⋅|1−4|+1⋅|2−5|+1⋅|6−6|+1⋅|7−7|+2⋅|10−8|=10.
  3. 1 31 3 — we can move boxes to segment [1,3][1,3].
  4. 3 53 5 — we can move boxes to segment [7,9][7,9].
  5. 3 5−3 5 — w3w3 is changed from 11 to 55.
  6. 1 10−1 10 — w1w1 is changed from 11 to 1010. The weights are now equal to w=[10,1,5,1,2]w=[10,1,5,1,2].
  7. 1 41 4 — we can move boxes to segment [1,4][1,4].
  8. 2 52 5 — we can move boxes to segment [5,8][5,8].

思路:

树状数组加二分

  1 #include 
  2 #include 
  3 #include 
  4 #include 
  5 #include 
  6 #include 
  7 #include <string>
  8 #include 
  9 #include 
 10 #include 
 11 #include 
 12 #include 
 13 #include <set>
 14 #include 
 15 #include 
 16 #include 
 17 #include 
 18 #include 
 19 #include 
 20 #include 
 21 
 22 using namespace std;
 23 
 24 #define pau system("pause")
 25 #define ll long long
 26 #define pii pair
 27 #define pb push_back
 28 #define pli pair
 29 #define pil pair
 30 #define clr(a, x) memset(a, x, sizeof(a))
 31 
 32 const double pi = acos(-1.0);
 33 const int INF = 0x3f3f3f3f;
 34 const int MOD = 1e9 + 7;
 35 const double EPS = 1e-9;
 36 
 37 /*
 38 #include 
 39 #include 
 40 using namespace __gnu_pbds;
 41 #define TREE tree, rb_tree_tag, tree_order_statistics_node_update>
 42 TREE T;
 43 */
 44 
 45 int n, q;
 46 ll a[200015], w[200015];
 47 ll c1[200015], c2[200015];
 48 inline int lb(int x) {return x & -x;}
 49 void add1(int p, ll x) {
 50     for (; p <= n; p += lb(p)) c1[p] += x;
 51 }
 52 ll query1(int p) {
 53     ll res = 0;
 54     for (; p; p -= lb(p)) res += c1[p];
 55     return res;
 56 }
 57 void add2(int p, ll x) {
 58     for (; p <= n; p += lb(p)) {
 59         c2[p] += x;
 60         if (c2[p] >= MOD) c2[p] -= MOD;
 61     }
 62 }
 63 ll query2(int p) {
 64     ll res = 0;
 65     for (; p; p -= lb(p)) res += c2[p];
 66     return res % MOD;
 67 }
 68 int get_p(int l, int r) {
 69     ll r1 = query1(l - 1);
 70     ll r2 = query1(r) - r1;
 71     int mi, res = l;
 72     while (l <= r) {
 73         mi = l + r >> 1;
 74         if ((query1(mi) - r1) * 2 >= r2) {
 75             r = (res = mi) - 1;
 76         } else {
 77             l = mi + 1;
 78         }
 79     }
 80     return res;
 81 }
 82 ll cal(int l, int r, int p) {
 83     ll res1 = query2(r) - query2(p - 1) - (query1(r) - query1(p - 1)) % MOD * a[p];
 84     ll res2 = (query1(p) - query1(l - 1)) % MOD * a[p] - (query2(p) - query2(l - 1));
 85     return ((res1 + res2) % MOD + MOD) % MOD;
 86 }
 87 int main() {
 88     scanf("%d%d", &n, &q);
 89     for (int i = 1; i <= n; ++i) {
 90         scanf("%lld", &a[i]);
 91         a[i] -= i;
 92     }
 93     for (int i = 1; i <= n; ++i) {
 94         scanf("%lld", &w[i]);
 95     }
 96     for (int i = 1; i <= n; ++i) {
 97         add1(i, w[i]);
 98         add2(i, w[i] * a[i] % MOD);
 99     }
100     while (q--) {
101         int l, r;
102         scanf("%d%d", &l, &r);
103         if (l < 0) {
104             add1(-l, r - w[-l]);
105             add2(-l, (r - w[-l]) * a[-l] % MOD);
106             w[-l] = r;
107         } else {
108             int p = get_p(l, r);
109             printf("%lld\n", cal(l, r, p));
110         }
111     }
112     return 0;
113 }
View Code

 

转载于:https://www.cnblogs.com/BIGTOM/p/9738058.html

你可能感兴趣的:(codeforces round 512 F. Putting Boxes Together 树状数组维护区间加权平均数)