Putting Boxes Together CodeForces - 1030F (带权中位数)

Posted uid001

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 ‘
‘
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
",ans);
		}
	}
}

 

以上是关于Putting Boxes Together CodeForces - 1030F (带权中位数)的主要内容,如果未能解决你的问题,请参考以下文章

codeforces round 512 F. Putting Boxes Together 树状数组维护区间加权平均数

Putting Boxes Together CodeForces - 1030F (带权中位数)

[CFR512 div 2 F]Putting Boxes Together(线段树)

vim - Putting the current file on the Windows clipboard

Tip of the Week #65: Putting Things in their Place

Tip of the Week #65: Putting Things in their Place