传送门

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

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

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

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

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

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

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

代码:

#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. Activity工作流学习(一)——Activity服务类

    Activity有9个service1.DynamicBpmnService动态Bpmn服务Service providing access to the repository of process ...

  2. Spring mvc 返回json包含双引号问题 解决

    解决方式1: @RequestMapping(value="/shopsList.json", produces = "text/html;charset=UTF-8&q ...

  3. 前端-CSS-11-Z-index

    ---- z-index 这个东西非常简单,它有四大特性,每个特性你记住了,页面布局就不会出现找不到盒子的情况. z-index 值表示谁压着谁,数值大的压盖住数值小的, 只有定位了的元素,才能有z- ...

  4. SecureCRT去除关闭Session的确认窗口提示

  5. mysql启动报错 The server quit without updating PID file

    [root@uz6542 data]# /etc/init.d/mysqld startStarting MySQL... ERROR! The server quit without updatin ...

  6. TCP/IP知识总结(TCP/IP协议族读书笔记四)

    参考:http://blog.chinaunix.net/uid-26275986-id-4109679.html 继续!TCP的流量控制和拥塞控制. TCP相对UDP可靠的地方在于它的拥塞控制.流量 ...

  7. 求数组中的逆序对的数量----剑指offer36题

    在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数: 如数组{7,5,6,4},逆序对总共有5对,{7,5},{7,6},{7, ...

  8. ndarray

    ndarray ndarray-多维数组对象 ndarray数组分两部分 实际数据 描述数据的元数据(数据类型/维度等) ndarray所有元素类型相同,下标从0开始 import numpy as ...

  9. python网络编程——socket基础篇

    python的网络编程比c语言简单许多, 封装许多底层的实现细节, 方便程序员使用的同时, 也使程序员比较难了解一些底层的东西. 1 TCP/IP 要想理解socket,首先得熟悉一下TCP/IP协议 ...

  10. 大型运输行业实战_day09_2_站间互售实现

    1.添加站间互售入口 对应的html代码 <button onclick="otherStation()">站间互售</button> 对应的js发送函数 ...