Codeforces 1053 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的更多相关文章
- CodeForces 1058 F Putting Boxes Together 树状数组,带权中位数
Putting Boxes Together 题意: 现在有n个物品,第i个物品他的位置在a[i],他的重量为w[i].每一个物品移动一步的代价为他的w[i].目前有2种操作: 1. x y 将第x的 ...
- [Codeforces 1053C] Putting Boxes Together
Link: Codeforces 1053C 传送门 Solution: 先推出一个结论: 最后必有一个点不动且其为权值上最中间的一个点 证明用反证证出如果不在中间的点必有一段能用代价少的替代多的 这 ...
- Codeforces 1053C Putting Boxes Together 树状数组
原文链接https://www.cnblogs.com/zhouzhendong/p/CF1053C.html 题目传送门 - CF1053C 题意 有 $n$ 个物品,第 $i$ 个物品在位置 $a ...
- Putting Boxes Together CodeForces - 1030F (带权中位数)
#include <iostream> #include <algorithm> #include <cstdio> #include <math.h> ...
- 2018.09.24 codeforces 1053C. Putting Boxes Together(线段树)
传送门 就是让你维护动态的区间带权中位数. 然而昨晚比赛时并没有调出来. 想找到带权中位数的中点可以二分(也可以直接在线段树上找). 也就是二分出第一个断点,使得断点左边的和恰好大于或等于断点右边的和 ...
- CodeForces 551C - GukiZ hates Boxes - [二分+贪心]
题目链接:http://codeforces.com/problemset/problem/551/C time limit per test 2 seconds memory limit per t ...
- Codeforces 551C GukiZ hates Boxes(二分)
Problem C. GukiZ hates Boxes Solution: 假设最后一个非零的位置为K,所有位置上的和为S 那么答案的范围在[K+1,K+S]. 二分这个答案ans,然后对每个人尽量 ...
- Codeforces 821C - Okabe and Boxes
821C - Okabe and Boxes 思路:模拟.因为只需要比较栈顶和当前要删除的值就可以了,所以如果栈顶和当前要删除的值不同时,栈就可以清空了(因为下一次的栈顶不可能出现在前面那些值中). ...
- Codeforces 260C - Balls and Boxes
260C - Balls and Boxes 思路:模拟.在x前面找到最小值,如果没有,从0跳到n,继续找到最小值,边找最小值路过的点边减1.然后所有值都减去最小值,最小值那个点加上减去的值. 找到x ...
随机推荐
- win10常见问题处理办法
1.当笔记本连接wifi时,提示,无internet,安全,而手机能正常连接wifi时: cmd(需管理员权限)执行命令 netsh winsock reset 出现已重置,重启电脑 解决方法 2.当 ...
- P3380 【模板】二逼平衡树(树套树)(线段树套平衡树)
P3380 [模板]二逼平衡树(树套树) 前置芝士 P3369 [模板]普通平衡树 线段树套平衡树 这里写的是线段树+splay(不吸氧竟然卡过了) 对线段树的每个节点都维护一颗平衡树 每次把给定区间 ...
- 【题解】Luogu P2157 [SDOI2009]学校食堂
原题传送门:P2157 [SDOI2009]学校食堂 一看题目就知道是状压dp 设f[i][j][k]表示第1到i-1个人都吃完了饭,第i个人以及后面的7个人是否打饭的状态为j,当前最后打饭的人的编号 ...
- 尚硅谷面试第一季-12Linux常用服务类相关命令
课堂重点: 实操命令及运行结果: (centos 6) service network status chkconfig --list chkconfig --level 5 network off ...
- 【Python52--爬虫1】
一.Python如何访问互联网 采用urllib包来访问 二.理论 1.请问URL是“统一资源标识符”还是“统一资源定位符” URI是统一资源标识符,URL是统一资源定位符.即:URI是用字符串表示某 ...
- default activity not found的问题
莫名其妙的同一个project下的所有modlue全都出现了这个问题,在网上查了一些解决方法,总结一下就是在运行时把default activity改成nothing,这个把活动都搞没了肯定不行.还有 ...
- Bootstrap3基础 caret 辅助类样式 下拉的小三角
内容 参数 OS Windows 10 x64 browser Firefox 65.0.2 framework Bootstrap 3.3.7 editor ...
- php知识点-1
global 是在函数内部 声明一个 函数外部的变量(即所谓的全局变量, 而所谓的超全局变量是指 像 $_POST, $GLOBALS等之类的自动系统变量) 的一个别名. 在函数内部使用 unset( ...
- 【做题】CF239E. k-d-sequence——线段树
首先,容易得到判断一个子串为"good k-d sequence"的方法: 子串中没有重复元素,且所有元素模d相等. 记mx为除以d的最大值,mn为除以d的最小值,则\(mx-mn ...
- Docker run 出现问题如何调试?
docker run -ti 3f5dd697cc83 /bin/bash #进入image的目录 ls -l #列出所有目录 dotnet run WestWin.Ads.Crawler.WebAp ...