[CF1053C]Putting Boxes Together(线段树)
http://codeforces.com/blog/entry/62013
两个结论:
1.一定有一个箱子不用动。
2.不动的箱子一定是加权前缀和为S/2的那个。
1显然,2由1易得。
于是问题变为:求一段区间前缀和>S/2的第一个数的位置。显然先求出S/2,再线段树上二分即可,实现过程见代码。
自定义struct比stl:pair快,注意取模和爆long long的问题。
#include<cstdio>
#include<algorithm>
#define ls (x<<1)
#define rs (ls|1)
#define lson ls,L,mid
#define rson rs,mid+1,R
#define rep(i,l,r) for (int i=(l); i<=(r); i++)
typedef long long ll;
using namespace std; const int N=,inf=1e9,mod=1e9+;
int n,Q,x,y,a[N],w[N];
struct Tr{ ll s1,s2; }v[N<<];
struct P{ ll x,y; }; void upd(int x){ v[x].s1=v[ls].s1+v[rs].s1; v[x].s2=(v[ls].s2+v[rs].s2)%mod; } void build(int x,int L,int R){
if (L==R){ v[x].s1=w[L]; v[x].s2=1ll*a[L]*w[L]%mod; return; }
int mid=(L+R)>>;
build(lson); build(rson); upd(x);
} void mdf(int x,int L,int R,int pos,int k){
if (L==R){ v[x].s1=k; v[x].s2=1ll*a[pos]*k%mod; return; }
int mid=(L+R)>>;
if (pos<=mid) mdf(lson,pos,k); else mdf(rson,pos,k);
upd(x);
} P que(int x,int L,int R,int l,int r){
if (L==l && r==R) return (P){v[x].s1,v[x].s2};
int mid=(L+R)>>;
if (r<=mid) return que(lson,l,r);
else if (l>mid) return que(rson,l,r);
else{
P s1=que(lson,l,mid),s2=que(rson,mid+,r);
return (P){s1.x+s2.x,(s1.y+s2.y)%mod};
}
} P ask(int x,int L,int R,int l,int r,ll k){
if (L==l && r==R){
if (v[x].s1<=k) return (P){inf,v[x].s1};
if (L==R) return (P){l,};
}
int mid=(L+R)>>;
if (r<=mid) return ask(lson,l,r,k);
else if (l>mid) return ask(rson,l,r,k);
else{
ll sm=,res=inf;
P cur=ask(lson,l,mid,k);
res=min(res,cur.x); sm+=cur.y;
if (res<inf) return (P){res,sm};
cur=ask(rson,mid+,r,k-sm);
res=min(res,cur.x); sm+=cur.y;
return (P){res,sm};
}
} int work(int l,int r){
int k=ask(,,n,l,r,que(,,n,l,r).x/).x;
P t1=(k==l) ? (P){,} : que(,,n,l,k-),t2=que(,,n,k,r);
return ((a[k]*(t1.x%mod)-t1.y+t2.y-a[k]*(t2.x%mod))%mod+mod)%mod;
} int main(){
freopen("1058f.in","r",stdin);
freopen("1058f.out","w",stdout);
scanf("%d%d",&n,&Q);
rep(i,,n) scanf("%d",&a[i]),a[i]=a[i]-i;
rep(i,,n) scanf("%d",&w[i]);
build(,,n);
while (Q--){
scanf("%d%d",&x,&y);
if (x<) mdf(,,n,-x,y); else printf("%d\n",work(x,y));
}
return ;
}
[CF1053C]Putting Boxes Together(线段树)的更多相关文章
- 洛谷CF1030F Putting Boxes Together(树状数组)
题意: 现在有n个物品,第i个物品他的位置在a[i],他的重量为w[i].每一个物品移动一步的代价为他的w[i].目前有2种操作: 1. x y 将第x的物品的重量改为y 2.l r 将编号在 [ l ...
- C. Okabe and Boxes 思维 模拟 or 线段树
C. Okabe and Boxes 这个题目是一个有点思维的模拟,当时没有想到, 思维就是这个栈的排序这里,因为每次直接排序肯定会t的,所以不可以这么写,那怎么表示排序呢? 就是直接把栈清空,如果栈 ...
- CodeForces 1058 F Putting Boxes Together 树状数组,带权中位数
Putting Boxes Together 题意: 现在有n个物品,第i个物品他的位置在a[i],他的重量为w[i].每一个物品移动一步的代价为他的w[i].目前有2种操作: 1. x y 将第x的 ...
- Codeforces 834D The Bakery【dp+线段树维护+lazy】
D. The Bakery time limit per test:2.5 seconds memory limit per test:256 megabytes input:standard inp ...
- Codeforces 626G Raffles(贪心+线段树)
G. Raffles time limit per test:5 seconds memory limit per test:256 megabytes input:standard input ou ...
- Codeforces 834D The Bakery - 动态规划 - 线段树
Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredient ...
- Codeforces 1053 C - Putting Boxes Together
C - Putting Boxes Together 思路: 求带权中位数 用树状数组维护修改 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) ...
- Codeforces Round #426 (Div. 1) B The Bakery (线段树+dp)
B. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...
- Codeforces Round #426 (Div. 2) D 线段树优化dp
D. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...
随机推荐
- 在window 8 或windows2012 上用命令行安装framework3.5 方法
找到对应操作系统安装目录的sources文件夹下的sxs文件夹,拷贝到本地电脑,如F:盘 根目录下 CMD(管理员身份)命令: dism.exe /online /enable-feature /fe ...
- UNIX网络编程 第8章 基本UDP套接字编程
UDP是无连接的,不需要accept,TCP通过accept API来接受连接,并且将连接客户端的信息写入到accept将返回的新socket中,该新socket中有服务端和客户端的IP地址和端口,因 ...
- python(13)多线程:线程池,threading
python 多进程:多进程 先上代码: pool = threadpool.ThreadPool(10) #建立线程池,控制线程数量为10 reqs = threadpool.makeRequest ...
- linux下定时器介绍1
POSIX Timer 间隔定时器 setitimer 有一些重要的缺点,POSIX Timer 对 setitimer 进行了增强,克服了 setitimer 的诸多问题: 首先,一个进程同一时刻只 ...
- redis数据淘汰机制
volatile-lru:从已设置过期时间的数据集(server.db[i].expires)中挑选最近最少使用 的数据淘汰 volatile-ttl:从已设置过期时间的数据集(server.db[i ...
- 「要买车网」免费获取汽车电商要买车网购车优惠券 - 持续更新(2016-03-12)www.fortunelab.cn
汽车电商要买车网简介 “要买车”(www.yaomaiche.com)网站是上海运图投资有限公司旗下网站,是首家真正打通交易闭环的汽车电商网站,由中国电子商务成功探索者——卜广齐于2014年10月在上 ...
- Linux下配置镜像源
清华大学地址: https://mirrors.tuna.tsinghua.edu.cn 选择对应ubuntu的版本 在linux下用终端敲 cd /etc/apt/source.list 把里面的内 ...
- CSS--布局模型,颜色值,长度值
目录 布局模型 流动模型(Flow) 浮动模型 (Float) 层模型(Layer) 颜色值 长度值 一.布局模型 网页布局模型:个人理解,就是对html中各个元素进行一个排列.而排列的方法可以分为 ...
- 使用Appium 测试微信小程序和微信公众号方法
由于腾讯系QQ.微信等都是基于腾讯自研X5内核,不是google原生webview,需要打开TBS内核Inspector调试功能才能用Chrome浏览器查看页面元素,并实现Appium自动化测试微信小 ...
- python包安装-centos7/windows
1.修改pip源 临时使用: 可以在使用pip的时候在后面加上-i参数,指定pip源 eg: pip install scrapy -i https://pypi.tuna.tsinghua.edu. ...