C - Putting Boxes Together

思路:

求带权中位数

用树状数组维护修改

代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define pii pair<int, int>
#define piii pair<pli, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head const int N = 3e5 + ;
const int MOD = 1e9 + ;
int a[N], w[N], n;
struct BIT {
LL bit[N];
int ty;
void add(int x, LL a) {
while(x <= n) {
if(ty == ) bit[x] = (bit[x] + a) % MOD;
else bit[x] = bit[x] + a;
x += x&-x;
}
}
LL sum(int x) {
LL res = ;
while(x) {
if(ty == )res = (res + bit[x]) % MOD;
else res = res + bit[x];
x -= x&-x;
}
return res;
}
}b1, b2; void solve (int x, int y) {
if(x == y) {
printf("0\n");
return ;
}
int l = x, r = y, m = l+r >> ;
LL tot = b1.sum(r) - b1.sum(l-);
LL sub = b1.sum(l-);
while(l < r) {
if((b1.sum(m) - sub)* >= tot) r = m;
else l = m+;
m = l+r >> ;
}
LL ans = , cnt = a[m] - m;
cnt %= MOD;
ans = (ans + cnt * ((b1.sum(m) - b1.sum(x-)) % MOD) % MOD - (b2.sum(m) - b2.sum(x-))) % MOD;
ans = (ans - cnt * ((b1.sum(y) - b1.sum(m)) % MOD) % MOD + (b2.sum(y) - b2.sum(m))) % MOD;
ans = (ans + MOD) % MOD;
printf("%lld\n", ans); }
int main() {
int q, x, y;
scanf("%d %d", &n, &q);
for (int i = ; i <= n; i++) scanf("%d", &a[i]);
for (int i = ; i <= n; i++) scanf("%d", &w[i]);
b1.ty = ;
b2.ty = ;
for (int i = ; i <= n; i++) {
b1.add(i, w[i]);
b2.add(i, 1LL*w[i]*(a[i]-i));
}
while(q--) {
scanf("%d %d", &x, &y);
if(x < ) {
x = -x;
b1.add(x, -w[x]);
b2.add(x, -1LL*w[x]*(a[x]-x)); w[x] = y;
b1.add(x, w[x]);
b2.add(x, 1LL*w[x]*(a[x]-x));
}
else solve(x, y);
}
return ;
}

Codeforces 1053 C - Putting Boxes Together的更多相关文章

  1. CodeForces 1058 F Putting Boxes Together 树状数组,带权中位数

    Putting Boxes Together 题意: 现在有n个物品,第i个物品他的位置在a[i],他的重量为w[i].每一个物品移动一步的代价为他的w[i].目前有2种操作: 1. x y 将第x的 ...

  2. [Codeforces 1053C] Putting Boxes Together

    Link: Codeforces 1053C 传送门 Solution: 先推出一个结论: 最后必有一个点不动且其为权值上最中间的一个点 证明用反证证出如果不在中间的点必有一段能用代价少的替代多的 这 ...

  3. Codeforces 1053C Putting Boxes Together 树状数组

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF1053C.html 题目传送门 - CF1053C 题意 有 $n$ 个物品,第 $i$ 个物品在位置 $a ...

  4. Putting Boxes Together CodeForces - 1030F (带权中位数)

    #include <iostream> #include <algorithm> #include <cstdio> #include <math.h> ...

  5. 2018.09.24 codeforces 1053C. Putting Boxes Together(线段树)

    传送门 就是让你维护动态的区间带权中位数. 然而昨晚比赛时并没有调出来. 想找到带权中位数的中点可以二分(也可以直接在线段树上找). 也就是二分出第一个断点,使得断点左边的和恰好大于或等于断点右边的和 ...

  6. CodeForces 551C - GukiZ hates Boxes - [二分+贪心]

    题目链接:http://codeforces.com/problemset/problem/551/C time limit per test 2 seconds memory limit per t ...

  7. Codeforces 551C GukiZ hates Boxes(二分)

    Problem C. GukiZ hates Boxes Solution: 假设最后一个非零的位置为K,所有位置上的和为S 那么答案的范围在[K+1,K+S]. 二分这个答案ans,然后对每个人尽量 ...

  8. Codeforces 821C - Okabe and Boxes

    821C - Okabe and Boxes 思路:模拟.因为只需要比较栈顶和当前要删除的值就可以了,所以如果栈顶和当前要删除的值不同时,栈就可以清空了(因为下一次的栈顶不可能出现在前面那些值中). ...

  9. Codeforces 260C - Balls and Boxes

    260C - Balls and Boxes 思路:模拟.在x前面找到最小值,如果没有,从0跳到n,继续找到最小值,边找最小值路过的点边减1.然后所有值都减去最小值,最小值那个点加上减去的值. 找到x ...

随机推荐

  1. Android4.0以上版本Http请求的问题

    异常:android.os.NetworkOnMainThreadException Android 4.1项目:使用新浪微博分享时报: android.os.NetworkOnMainThreadE ...

  2. jmeter的使用

    jmeter:java开发的开源的性能测试工具. *jmeter返回中文乱码: 1.在jmeter的bin目录下,找到jmeter的配置文件,jmeter.properties,然后把samplere ...

  3. linux 中部署 rsync 实现文件远程备份及 同步

    客户端:数据源:服务端:数据接收方 rsync官方文档:https://www.samba.org/ftp/rsync/rsync.html 手动测试用“通过远程外壳访问"里的语法: 参考1 ...

  4. centos 7 安装使用 redis

    1.下载redis,用wget就行,版本在这里找: http://download.redis.io/releases/ 2.安装gcc,tcl,用yum 安装. 3.解压,make ,make in ...

  5. 20145315何佳蕾《网络对抗》Web安全基础

    20145315何佳蕾<网络对抗>Web安全基础 1.实验后回答问题 (1)SQL注入攻击原理,如何防御 SQL Injection:就是通过把SQL命令插入到Web表单递交或输入域名或页 ...

  6. Plantuml画图工具

    1,Plantuml画图工具 安装指南: Mac sublimetext http://blog.csdn.net/zhangjikuan/article/details/53365730 win i ...

  7. topcoder srm 515 div1

    problem1 link 暴力枚举即可. problem2 link 一共有24小时,所以最多有24个顾客.设$f[x][y][z]$表示还剩下$x$把刀,现在时间是$y$,以及来过的顾客集合为$z ...

  8. Python3 tkinter基础 Label justify 多行字符串左对齐

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  9. Redis常用操作大全和Python操作Redis

    简单使用 utils.py import redis POOL=redis.ConnectionPool(host='127.0.0.1',port=6379) view.py 第一种方式 (通用方式 ...

  10. Bytom矿池接入协议指南

    矿机配置 https://gist.github.com/HAOYUatHZ/a47400bde4a138825faef415387b532c 固件升级 https://service.bitmain ...