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. Kali系列之aircrack-ng wifi密码穷举

    kali linux安全渗透 网卡:rtl8187 工具aircrack-ng 操作+ 查看无线网卡信息 ifconfig 或者 iwconfig 有个是wlan0的就是您的外置无线网卡 启动网卡监听 ...

  2. centos6.5编译安装php7

    1.安装依赖软件库: yum install -y libxml2-devel libtool* curl-devel libjpeg-devel libpng-devel freetype-deve ...

  3. VC++ 获取Windows系统版本号、CPU名称

    转载:https://blog.csdn.net/sunflover454/article/details/51525179 转载:https://blog.csdn.net/magictong/ar ...

  4. 尚硅谷面试第一季-09SpringMVC中如何解决POST请求中文乱码问题GET的又如何处理呢

    目录结构: 关键代码: web.xml <filter> <filter-name>CharacterEncodingFilter</filter-name> &l ...

  5. selenium css定位方式

  6. Python3基础 response.read 输出网页的源代码

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

  7. Flutter学习指南:UI布局和控件

    Flutter学习指南:UI布局和控件 - IT程序猿  https://www.itcodemonkey.com/article/11041.html

  8. [POI2011]Garbage 欧拉回路

    [POI2011]Garbage 链接 https://www.lydsy.com/JudgeOnline/problem.php?id=2278 https://loj.ac/problem/216 ...

  9. 【做题】hdu5514 Frogs——另类容斥

    题意是给出n个m的约数,问[0,m-1]中至少被其中一个约数整除的整数和.(n<=10000,m<=1000000000) 直接容斥的话,是2^n再拖个log的复杂度,加上当前的数大于m时 ...

  10. 集合03_Map

    Map集合总览 保存映射关系key-value键值对,键唯一,值可以重复,Map和Set的实现类相似 Entry是Map的内部类 Map接口中常用的方法: void clear() Set keySe ...