Putting Boxes Together CodeForces - 1030F (带权中位数)
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head const int N = 2e5+10;
int n, q, pos;
int a[N], w[N];
ll s1[N<<2], s2[N<<2], sum, tot;
void pu(int o) {
s1[o]=s1[lc]+s1[rc];
s2[o]=(s2[lc]+s2[rc])%P;
}
void update(int o, int l, int r, int x, int v) {
if (l==r) {
s1[o] = v;
s2[o] = (ll)v*a[l]%P;
return;
}
if (mid>=x) update(ls,x,v);
else update(rs,x,v);
pu(o);
}
ll qry1(int o, int l, int r, int ql, int qr) {
if (ql<=l&&r<=qr) return s1[o];
ll ans = 0;
if (mid>=ql) ans+=qry1(ls,ql,qr);
if (mid<qr) ans+=qry1(rs,ql,qr);
return ans;
}
int qry2(int o, int l, int r, int ql, int qr) {
if (ql<=l&&r<=qr) return s2[o];
ll ans = 0;
if (mid>=qr) return qry2(ls,ql,qr);
if (mid<ql) return qry2(rs,ql,qr);
return (qry2(ls,ql,qr)+qry2(rs,ql,qr))%P;
} void find(int o, int l, int r, int ql, int qr) {
if (sum<=0) return;
if (ql<=l&&r<=qr) {
if (l==r) {
sum -= s1[o];
pos = r;
return;
}
if (sum>s1[lc]) sum-=s1[lc],find(rs,ql,qr);
else find(ls,ql,qr);
return;
}
if (mid>=ql) find(ls,ql,qr);
if (mid<qr) find(rs,ql,qr);
}
void build(int o, int l, int r) {
if (l==r) {
s1[o]=w[l],s2[o]=(ll)a[l]*w[l]%P;
return;
}
build(ls),build(rs);
pu(o);
}
int qry(int l, int r) {
int t = qry2(1,1,n,l,r)-qry1(1,1,n,l,r)%P*a[pos]%P;
if (t<0) t += P;
return t;
}
signed main() {
scanf("%d%d", &n, &q);
REP(i,1,n) scanf("%d", a+i),a[i]-=i;
REP(i,1,n) scanf("%d", w+i);
build(1,1,n);
REP(i,1,q) {
int x, y;
scanf("%d%d", &x, &y);
if (x<0) update(1,1,n,-x,y);
else {
sum = (1+qry1(1,1,n,x,y))/2, pos = 0;
find(1,1,n,x,y);
int ans = qry(pos,y)-qry(x,pos);
if (ans<0) ans+=P;
printf("%d\n",ans);
}
}
}
Putting Boxes Together CodeForces - 1030F (带权中位数)的更多相关文章
- 2019.03.28 bzoj3598: [Scoi2014]方伯伯的商场之旅(带权中位数+数位dp)
传送门 题意咕咕咕自己读吧挺简单的 思路: 由带权中位数的性质可以得到对于每个数放在每个二进制位的代价一定是个单调或者单峰函数,因此我们先把所有的数都挪到第一个位置,然后依次向右枚举峰点(极值点)把能 ...
- poj 1723 SOLDIERS 带权中位数
题目 http://poj.org/problem?id=1723 题解 带权中位数类型的题目~ 可以先考虑降维,最后集合的y坐标,明显是y坐标的中位数的位置,容易求出y方向的贡献res_y.比较麻烦 ...
- CodeForces 1058 F Putting Boxes Together 树状数组,带权中位数
Putting Boxes Together 题意: 现在有n个物品,第i个物品他的位置在a[i],他的重量为w[i].每一个物品移动一步的代价为他的w[i].目前有2种操作: 1. x y 将第x的 ...
- Codeforces 1156D 带权并查集
题意:给你一颗树,树边的权值可能是0或1,问先走0边,再走1边,或者只走1边的路径有多少条? 思路:对于一个点,假设通过0边相连的点一共有x个(包括自己),通过1边相连的有y个(包括自己),那么对答案 ...
- 数学 + 带权中位数 - SGU 114 Telecasting station
Telecasting station Problem's Link Mean: 百慕大的每一座城市都坐落在一维直线上,这个国家的政府决定建造一个新的广播电视台. 经过了许多次试验后,百慕大的科学家们 ...
- Codeforces 1053 C - Putting Boxes Together
C - Putting Boxes Together 思路: 求带权中位数 用树状数组维护修改 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) ...
- Codeforces 1053C Putting Boxes Together 树状数组
原文链接https://www.cnblogs.com/zhouzhendong/p/CF1053C.html 题目传送门 - CF1053C 题意 有 $n$ 个物品,第 $i$ 个物品在位置 $a ...
- 2018.09.24 codeforces 1053C. Putting Boxes Together(线段树)
传送门 就是让你维护动态的区间带权中位数. 然而昨晚比赛时并没有调出来. 想找到带权中位数的中点可以二分(也可以直接在线段树上找). 也就是二分出第一个断点,使得断点左边的和恰好大于或等于断点右边的和 ...
- CF1030F Putting Boxes Together
昨晚的比赛题.(像我这种蒟蒻只能打打div2) 题意 给你$n$个物品,每一个物品$i$,有一个权值$w_i$和一个位置$a_i$,定义移动一个物品$i$到位置$t$的代价为$w_i * \left ...
随机推荐
- jupyter notebook安装/代码补全/支持golang 踩坑记
安装(不要用root) 安装anaconda3,然后ln -s bin目录下的jupyter命令到/usr/bin目录下 生成密码备用 敲ipython进入交互终端 In [1]: from note ...
- 6.0-uC/OS-III软件定时器管理
1.软件定时器管理 uC/OS-III提供了软件定时器服务(相关代码在OS_TMR.C中).当设置OS_CFG.H中的OS_CFG_TMR_EN为1时软件定时器服务被使能. 2.uC/OS-III 定 ...
- 3、Finished with error: FormatException: Bad UTF-8 encoding 0xc3 (at offset 169)
这是由于 app 的版本为 release 找不到 keystore 文件, 我们只需要在 app 下的 build.gradle 文件中修改为 signingConfigs.debug 即可: bu ...
- java框架之SpringBoot(6)-Restful风格的CRUD示例
准备 环境 IDE:Idea SpringBoot版本:1.5.19 UI:BootStrap 4 模板引擎:thymeleaf 3 效果:Restful 风格 CRUD 功能的 Demo 依赖 &l ...
- linux-grep-tail-find
如果在只是想匹配模式的上下几行,grep可以实现. $grep -5 'parttern' inputfile //打印匹配行的前后5行 $grep -C 5 'parttern' inputfile ...
- MySQL行转列与列转行
行转列 例如:把图1转换成图2结果展示 图1 图2 CREATE TABLE `TEST_TB_GRADE` ( `ID` ) NOT NULL AUTO_INCREMENT, `) DEFAULT ...
- cds view join和association
1:创建两张表:ztt_teacher01 和ztt_teacher02 用于 cds view中的join和association 2:创建两个cds view:ztt_teacher01_id_n ...
- Domain Driven Development相关概念
Entity 与 Value Object1,Entity有唯一的身份标识,是可变的对象.Value Object是immutable,创建了就不能改变.2,Value Object可以在多个领域之间 ...
- ORA-00907: 缺失右括号,原因及解决办法整理
ORA-00907: 缺失右括号,原因及解决办法整理 1 union all中order by 导致缺失右括号 在有union all的子查询中使用了order by,会导致缺失右括号的错误,事实上在 ...
- Unity之流光效果
效果如图: shader如下: Shader "Unlit/Walk light" { Properties { _MainTex ("Base (RGB), Alpha ...