unique去重 懒惰标记pushdown()

链接:https://ac.nowcoder.com/acm/contest/887/E
来源:牛客网
 

Find the median

时间限制:C/C++ 3秒,其他语言6秒
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld

题目描述

Let median of some array be the number which would stand in the middle of this array if it was sorted beforehand. If the array has even length let median be smallest of of two middle elements. For example, median of the array [10,3,2,3,2] is 3 (i.e. [2,2,3‾,3,10][2,2,\underline{3},3,10][2,2,3​,3,10]). Median of the array [1,5,8,1] is 1 (i.e. [1,1‾,5,8][1,\underline{1},5,8][1,1​,5,8]).

At first, you're given an empty sequence. There are N operations. The i-th operation contains two integers LiL_iLi​ and RiR_iRi​. This means that adding Ri−Li+1R_i-L_i+1Ri​−Li​+1 integers Li,Li+1,...,RiL_i, L_i+1, ... , R_iLi​,Li​+1,...,Ri​ into the sequence. After each operation, you need to find the median of the sequence.

输入描述:

The first line of the input contains an integer N (1≤N≤400000)N\ (1 \leq N \leq 400000)N (1≤N≤400000) as described above.

The next two lines each contains six integers in the following format, respectively:

- X1 X2 A1 B1 C1 M1X_1\ X_2\ A_1\ B_1\ C_1\ M_1X1​ X2​ A1​ B1​ C1​ M1​
- Y1 Y2 A2 B2 C2 M2Y_1\ Y_2\ A_2\ B_2\ C_2\ M_2Y1​ Y2​ A2​ B2​ C2​ M2​

These values are used to generate Li,RiL_i, R_iLi​,Ri​ as follows:

We define:
- Xi=(A1×Xi−1+B1×Xi−2+C1) module M1X_i = (A_1 \times X_{i-1} + B_1 \times X_{i-2} + C_1)\ module\ M_1Xi​=(A1​×Xi−1​+B1​×Xi−2​+C1​) module M1​, for i=3 to Ni = 3\ to\ Ni=3 to N
- Yi=(A2×Yi−1+B2×Yi−2+C2) module M2Y_i = (A_2 \times Y_{i-1} + B_2 \times Y_{i-2} + C_2)\ module\ M_2Yi​=(A2​×Yi−1​+B2​×Yi−2​+C2​) module M2​, for i=3 to Ni = 3\ to\ Ni=3 to N


We also define:
- Li=min(Xi,Yi)+1L_i = min(X_i, Y_i) + 1Li​=min(Xi​,Yi​)+1, for i=1 to Ni = 1\ to\ Ni=1 to N.
- Ri=max(Xi,Yi)+1R_i = max(X_i, Y_i) + 1Ri​=max(Xi​,Yi​)+1, for i=1 to Ni = 1\ to\ Ni=1 to N.

Limits:
1≤N≤4000001 \leq N \leq 4000001≤N≤400000
0≤A1<M10 \leq A_1 < M_10≤A1​<M1​
0≤A2<M20 \leq A_2 < M_20≤A2​<M2​
0≤B1<M10 \leq B_1 < M_10≤B1​<M1​
0≤B2<M20 \leq B_2 < M_20≤B2​<M2​
0≤C1<M10 \leq C_1 < M_10≤C1​<M1​
0≤C2<M20 \leq C_2 < M_20≤C2​<M2​
0≤X1<M10 \leq X_1 < M_10≤X1​<M1​
0≤X2<M10 \leq X_2 < M_10≤X2​<M1​
0≤Y1<M20 \leq Y_1 < M_20≤Y1​<M2​
0≤Y2<M20 \leq Y_2 < M_20≤Y2​<M2​
1≤M1≤1091 \leq M_1 \leq 10^91≤M1​≤109
1≤M2≤1091 \leq M_2 \leq 10^91≤M2​≤109

输出描述:

You should output lines. Each line contains an integer means the median.

示例1

输入

复制

5
3 1 4 1 5 9
2 7 1 8 2 9

输出

复制

3
4
5
4
5

说明

L = [3, 2 ,4, 1, 7]

R = [4, 8, 8, 3, 9]

题意:有一个初始序列(没有元素),有n次操作,每次操作给序列加这些元素: Li, Li+1,Li+2,,,Ri,然后求序列的中位数是多少,如果序列长度为偶数n,中位数为从小到大排序第n/2个数
解法:裸线段树,如果不卡空间,直接动态开点,如果不能动态开点,那我们把所有查询区间变成左开右闭区间然后离散化搞搞就行,没啥好讲的,线段树简单题
--------------------- 
版权声明:本文为CSDN博主「一只叫橘子的猫」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/ccsu_cat/article/details/98880997

懒惰标记下传:

void pushdown(int o,int l,int r)
{
	if(tag[o]==0)
	{
		return ;
	}
	tag[ls]+=tag[o];
	tag[rs]+=tag[o];
	sum[ls]+=tag[o]*(S[m]-S[l-1]);
	sum[rs]+=tag[o]*(S[r]-S[m]);
	tag[o]=0;
}

去重:unique(S+1,S+1+sz)-S-1;

STL之unique()去重函数

#include<bits/stdc++.h>
using namespace std;
#define ll long long 
#define ls o*2
#define rs o*2+1
#define m (l+r)/2
const int maxn=8e5+10;
ll sum[maxn*4];
ll X[maxn],Y[maxn];
int S[maxn],L[maxn],R[maxn],tag[maxn*4],n,sz;
void pushdown(int o,int l,int r)
{
	if(tag[o]==0)
	{
		return ;
	}
	tag[ls]+=tag[o];
	tag[rs]+=tag[o];
	sum[ls]+=tag[o]*(S[m]-S[l-1]);
	sum[rs]+=tag[o]*(S[r]-S[m]);
	tag[o]=0;
}
int up(int o,int l,int r,int ql,int qr)
{
	if(l>=ql&&r<=qr)
	{
		sum[o]+=S[r]-S[l-1];
		tag[o]++;
		return 0;
	}
	pushdown(o,l,r);
	if(ql<=m)
		up(ls,l,m,ql,qr);
	if(m<qr) 
		up(rs,m+1,r,ql,qr);
	sum[o]=sum[ls]+sum[rs];
}
int qu(int o,int l,int r,ll v)
{
	if(l==r)
	{
		int k=sum[o]/(S[r]-S[l-1]);
		return (v+k-1)/k+S[l-1];
	}
	pushdown(o,l,r);
	if(sum[ls]<v)
		return qu(rs,m+1,r,v-sum[ls]);
	return qu(ls,l,m,v);
}
void init()
{
	ll A1,B1,C1,M1;
	ll A2,B2,C2,M2;
	cin>>n;
	cin>>X[1]>>X[2]>>A1>>B1>>C1>>M1;
	cin>>Y[1]>>Y[2]>>A2>>B2>>C2>>M2;
	for(int i=1;i<=n;i++)
	{
		if(i>2)
		{
			X[i] =(A1*X[i-1]+B1*X[i-2]+C1)%M1;
			Y[i] = (A2 * Y[i - 1] + B2 * Y[i - 2] + C2) % M2;
		}
		L[i]=min(X[i],Y[i]);
		R[i]=max(X[i],Y[i])+1;
		S[++sz]=L[i];
		S[++sz]=R[i];
	}
	sort(S+1,S+1+sz) ;
	sz=unique(S+1,S+1+sz)-S-1;
	
}

int main()
{
	init();
	for(int i=1;i<=n;i++)
	{
		L[i]=lower_bound(S+1,S+1+sz,L[i])-S;
		R[i]=lower_bound(S+1,S+1+sz,R[i])-S;
		up(1,1,sz,L[i]+1,R[i]);
		ll v=sum[1]/2;
		if(sum[1]&1)
		{
			v++;
		}
		printf("%d\n",qu(1,1,sz,v));
	}	
}
发布了44 篇原创文章 · 获赞 6 · 访问量 1196

猜你喜欢

转载自blog.csdn.net/qq_43868883/article/details/99464543
今日推荐