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−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+(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−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|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 <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

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 内容概要 《计算机试卷1》是一份综合性的计算机基础和应用测试卷,涵盖了计算机硬件、软件、操作系统、网络、多媒体技术等多个领域的知识点。试卷包括单选题和操作应用两大类,单选题部分测试学生对计算机基础知识的掌握,操作应用部分则评估学生对计算机应用软件的实际操作能力。 ### 适用人群 本试卷适用于: - 计算机专业或信息技术相关专业的学生,用于课程学习或考试复习。 - 准备计算机等级考试或职业资格认证的人士,作为实战演练材料。 - 对计算机操作有兴趣的自学者,用于提升个人计算机应用技能。 - 计算机基础教育工作者,作为教学资源或出题参考。 ### 使用场景及目标 1. **学习评估**:作为学校或教育机构对学生计算机基础知识和应用技能的评估工具。 2. **自学测试**:供个人自学者检验自己对计算机知识的掌握程度和操作熟练度。 3. **职业发展**:帮助职场人士通过实际操作练习,提升计算机应用能力,增强工作竞争力。 4. **教学资源**:教师可以用于课堂教学,作为教学内容的补充或学生的课后练习。 5. **竞赛准备**:适合准备计算机相关竞赛的学生,作为强化训练和技能检测的材料。 试卷的目标是通过系统性的题目设计,帮助学生全面复习和巩固计算机基础知识,同时通过实际操作题目,提高学生解决实际问题的能力。通过本试卷的学习与练习,学生将能够更加深入地理解计算机的工作原理,掌握常用软件的使用方法,为未来的学术或职业生涯打下坚实的基础。
### 内容概要 这份《计算机试卷1》包含多个部分,主要覆盖了计算机基础知识、操作系统应用、文字处理、电子表格、演示文稿制作、互联网应用以及计算机多媒体技术。试卷以单选题开始,涉及计算机历史、基本概念、硬件组成、软件系统、网络协议等。接着是操作应用部分,要求考生在给定的软件环境中完成一系列具体的计算机操作任务。 ### 适用人群 本试卷适用于计算机科学与技术、信息技术相关专业的学生,以及准备计算机水平考试或职业资格认证的人士。它适合那些希望检验和提升自己计算机操作能力的学习者,也适用于教育工作者作为教学评估工具。 ### 使用场景及目标 1. **学习评估**:作为教育机构的课程评估工具,帮助教师了解学生对计算机基础知识的掌握程度。 2. **自学检验**:供个人自学者检验自己的计算机操作技能和理论知识,为进一步学习提供方向。 3. **职业发展**:为职场人士提供计算机技能的自我提升途径,增强其在信息时代的竞争力。 4. **考试准备**:为准备计算机相关考试的考生提供实战演练的机会,加强考试自信。 5. **教学资源**:教师可以将其作为教学资源,设计课程和实验,提高教学效果。 试卷的目标是通过理论知识的测试和实践技能的操作,全面提升考生的计算机应用能力。考生应掌握从基础的计算机组成原理到复杂的数据处理、演示文稿制作、网络应用以及多媒体技术处理等多方面技能。通过本试卷的学习与练习,考生将能够更加熟练地使用计算机解决实际问题,为未来的学术或职业生涯打下坚实的基础。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值