码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-10-03 00:26:18      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:sts   update   order   iostream   define   gnu   first   fstream   std   

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, ai?1<aiai?1<ai holds for all valid ii.

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+(r?l)][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 2?109+132?109+13and 2?109+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,q2?1051≤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, ai?1<aiai?1<ai holds for all valid ii.

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?|1?4|+1?|2?5|+1?|6?6|+1?|7?7|+2?|10?8|=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 <iostream>
  2 #include <fstream>
  3 #include <sstream>
  4 #include <cstdlib>
  5 #include <cstdio>
  6 #include <cmath>
  7 #include <string>
  8 #include <cstring>
  9 #include <algorithm>
 10 #include <queue>
 11 #include <stack>
 12 #include <vector>
 13 #include <set>
 14 #include <map>
 15 #include <list>
 16 #include <iomanip>
 17 #include <cctype>
 18 #include <cassert>
 19 #include <bitset>
 20 #include <ctime>
 21 
 22 using namespace std;
 23 
 24 #define pau system("pause")
 25 #define ll long long
 26 #define pii pair<int, int>
 27 #define pb push_back
 28 #define pli pair<ll, int>
 29 #define pil pair<int, ll>
 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 <ext/pb_ds/assoc_container.hpp>
 39 #include <ext/pb_ds/tree_policy.hpp>
 40 using namespace __gnu_pbds;
 41 #define TREE tree<pli, null_type, greater<pli>, 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

 

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

标签:sts   update   order   iostream   define   gnu   first   fstream   std   

原文地址:https://www.cnblogs.com/BIGTOM/p/9738058.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!