传送门

就是让你维护动态的区间带权中位数。

然而昨晚比赛时并没有调出来。

想找到带权中位数的中点可以二分(也可以直接在线段树上找)。

也就是二分出第一个断点,使得断点左边的和恰好大于或等于断点右边的和。

现在的问题在于知道断点之后如何统计答案。

我们可以在线段树中维护当前区间全部移到区间最左端点的花费,以及当前区间全部移到区间最右端点的花费。

这样就可以简单合并并轻松统计答案了。

代码:

#include<bits/stdc++.h>
#define ll long long
#define N 200005
#define mod 1000000007
#define lc (p<<1)
#define rc (p<<1|1)
#define mid (T[p].l+T[p].r>>1)
using namespace std;
inline ll read(){
	ll ans=0,w=1;
	char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}
	while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
	return ans*w;
}
inline void write(ll x){
	static int buf[66];
	if(!x){putchar('0');return;}
	if(x<0)putchar('-'),x=-x;
	while(x)buf[++buf[0]]=x%10,x/=10;
	while(buf[0])putchar((buf[buf[0]--])^48);
}
struct nd{int l,r,cnt;ll ls,rs,s,sl,sr;}T[N<<2];
int n,q;
ll a[N],w[N];
inline nd operator+(nd a,nd b){return(nd){a.l,b.r,a.cnt+b.cnt,a.ls,b.rs,a.s+b.s,(a.sl+b.sl+(b.rs-b.cnt-a.rs)*(a.s%mod)%mod),(a.sr+b.sr+(b.ls-a.ls-a.cnt)*(b.s%mod)%mod)};}
inline void newnode(int p,int l,int r){T[p]=(nd){l,r,1,a[l],a[l],w[l],0,0};}
inline void build(int p,int l,int r){
	T[p].l=l,T[p].r=r;
	if(l==r)return newnode(p,l,r);
	build(lc,l,mid),build(rc,mid+1,r),T[p]=T[lc]+T[rc];
}
inline void update(int p,int k){
	if(T[p].l==T[p].r)return newnode(p,T[p].l,T[p].r);
	if(k<=mid)update(lc,k);
	else update(rc,k);
	T[p]=T[lc]+T[rc];
}
inline ll query(int p,int ql,int qr){
	if(ql>T[p].r||qr<T[p].l)return 0;
	if(ql<=T[p].l&&T[p].r<=qr)return T[p].s;
	if(qr<=mid)return query(lc,ql,qr);
	if(ql>mid)return query(rc,ql,qr);
	return query(lc,ql,mid)+query(rc,mid+1,qr);
}
inline nd ask(int p,int ql,int qr){
	if(ql<=T[p].l&&T[p].r<=qr)return T[p];
	if(qr<=mid)return ask(lc,ql,qr);
	if(ql>mid)return ask(rc,ql,qr);
	return ask(lc,ql,mid)+ask(rc,mid+1,qr);
}
int main(){
	n=read(),q=read();
	for(int i=1;i<=n;++i)a[i]=read();
	for(int i=1;i<=n;++i)w[i]=read();
	build(1,1,n);
	while(q--){
		int x=read(),y=read();
		if(x<0)w[-x]=y,update(1,-x);
		else{
			int l=x,r=y,ans=l;
			while(l<=r){
				int midd=l+r>>1;
				ll L=query(1,x,midd),R=query(1,midd+1,y);
				if(L>=R)ans=midd,r=midd-1;
				else l=midd+1;
			}
			write((ask(1,x,ans).sl+ask(1,ans,y).sr)%mod),puts("");
		}
	}
	return 0;
}

2018.09.24 codeforces 1053C. Putting Boxes Together(线段树)的更多相关文章

  1. [Codeforces 1053C] Putting Boxes Together

    Link: Codeforces 1053C 传送门 Solution: 先推出一个结论: 最后必有一个点不动且其为权值上最中间的一个点 证明用反证证出如果不在中间的点必有一段能用代价少的替代多的 这 ...

  2. Codeforces 1053C Putting Boxes Together 树状数组

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF1053C.html 题目传送门 - CF1053C 题意 有 $n$ 个物品,第 $i$ 个物品在位置 $a ...

  3. 2018.09.24 codeforces 1051F. The Shortest Statement(dijkstra+lca)

    传送门 这真是一道一言难尽的题. 首先比赛的时候居然没想出来正解. 其次赛后调试一直调不出来最后发现是depth传错了. 其实这是一道简单题啊. 对于树边直接lca求距离. 由于非树边最多21条. 因 ...

  4. [CF1053C]Putting Boxes Together(线段树)

    http://codeforces.com/blog/entry/62013 两个结论: 1.一定有一个箱子不用动. 2.不动的箱子一定是加权前缀和为S/2的那个. 1显然,2由1易得. 于是问题变为 ...

  5. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

  6. Codeforces 834D The Bakery - 动态规划 - 线段树

    Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredient ...

  7. codeforces 22E XOR on Segment 线段树

    题目链接: http://codeforces.com/problemset/problem/242/E E. XOR on Segment time limit per test 4 seconds ...

  8. Codeforces 588E. A Simple Task (线段树+计数排序思想)

    题目链接:http://codeforces.com/contest/558/problem/E 题意:有一串字符串,有两个操作:1操作是将l到r的字符串升序排序,0操作是降序排序. 题解:建立26棵 ...

  9. Codeforces Gym 100803G Flipping Parentheses 线段树+二分

    Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...

随机推荐

  1. IOS CFBundleIdentifier

    CFBundleIdentifier  CFBundleIdentifier 必须是com.12306.aaa 这样的格式吗       AppID   用通配符格式的AppID方便.   AppSt ...

  2. bootstrap 移动自适应界面

    移动设备优先 在 Bootstrap 2 中,我们对框架中的某些关键部分增加了对移动设备友好的样式.而在 Bootstrap 3 中,我们重写了整个框架,使其一开始就是对移动设备友好的.这次不是简单的 ...

  3. FD 设置字体大小

    英文版: 依次选择菜单 Tools ->Syntax Coloring 中文版本: 如依次选择菜单 工具 ->语法配色器

  4. 13 python logging模块

    原文:http://www.cnblogs.com/dahu-daqing/p/7040764.html 1 logging模块简介 logging模块是Python内置的标准模块,主要用于输出运行日 ...

  5. NISP视频知识点总结

    身份认证访问控制安全审计本章实验 ===密码学=====古典密码 算法本身的保密性近代密码 机械密码\机电 密码打字密码机轮转机现代密码 基于密钥公钥密码 公钥==================对称 ...

  6. PPT汇报 评审表

    评审表 团队编号 团队名称 团队项目名称 格式评审 内容评审 PPT评审 演讲评审 优点 存在问题(至少提3点) 建议 01 牛肉面不要牛肉不要面 02 正义联盟 我是一个图书小平台 03 什么队 & ...

  7. KNN识别手写数字

    一.问题描述 手写数字被存储在EXCEL表格中,行表示一个数字的标签和该数字的像素值,有多少行就有多少个样本. 一共42000个样本 二.KNN KNN最邻近规则,主要应用领域是对未知事物的识别,即判 ...

  8. a标签伪类的LOVE HATE原则

    a标签伪类的LOVE HATE原则 a标签有四个伪类,分别是: a:link 未访问的链接 a:visited 已访问的链接 a:hover 鼠标移动到链接上 a:active 选定的链接 遇到的问题 ...

  9. jquery 显示 隐藏

    参考 http://www.w3school.com.cn/jquery/jquery_hide_show.asp $("#a").hide(); $("#a" ...

  10. HibernateTemplate实现CRUD操作

    ---------------------siwuxie095 HibernateTemplate 实现 CRUD 操作 1.在 SSH 框架中使用 HibernateTemplate 模板类实现 C ...