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 ...
随机推荐
- Golang操作结构体、Map转化为JSON
结构体生成Json package main import ( "encoding/json" "fmt" ) type IT struct { Company ...
- mybatis generator自动生成sqlmap代码的不完善之处以及解决方法
a) 建表时,字段名称建议用"_"分隔多个单词,比如:AWB_NO.REC_ID...,这样生成的entity,属性名称就会变成漂亮的驼峰命名,即:awbNo.recId b)or ...
- oracle 11g SKIP_UNUSABLE_INDEXES参数
SKIP_UNUSABLE_INDEXES的作用是在DML期间跳过对索引的维护,这样可以提交DML的效率,可以事后一次性重建,但是SKIP_UNUSABLE_INDEXES=y对unique inde ...
- vim 快捷键(update)
网上盗的图,233 2018年08月31日14:31:19 update: 批量行操作 esc->ctrl+v->选中批量行->shift+i->operation-> ...
- XcodeProj,使用Ruby更改工程文件
利用xcodeproj修改xcode工程文件 一,Ruby基础 Ruby迭代器each.map.collect.inject each——连续访问集合的所有元素collect—-从集合中获得各个元素传 ...
- vsCode设置中文
1.安装软件之后,关闭欢迎界面,Ctrl+shift+p打开命令窗口,输入lang,选择configuration display language,改为 "locale":&qu ...
- Ubuntu 系统学习
apt 命令 apt-get remove [app] # 删除已安装的软件包(保留配置文件),不会删除依赖软件包,且保留配置文件 apt-get remove --pure [app] # 删除已安 ...
- P4097 [HEOI2013]Segment(李超树)
链接 https://www.luogu.org/problemnew/show/P4097 https://www.lydsy.com/JudgeOnline/problem.php?id=3165 ...
- luogu P4051 [JSOI2007]字符加密
前言 其实就是个后缀数组模板题 可还是有几个的地方不太明白 思路 先将子串复制一遍,组成长度为2*n的子串 给出的子串一定会在前n个后缀 而且后面的优先级不会影响前面的相对大小 然后求得sa输出就好 ...
- Tutorial: Generate BBox or Rectangle to locate the target obejct
Tutorial: Generate BBox or Rectangle to locate the target obejct clc;close all;clear all; Img=imread ...