Time Limits: 1000 ms Memory Limits: 131072 KB Detailed Limits
Description
Input
Output
Sample Input
5 5 12 41 46 68 69 35 61 82 84 96 2 1 4 3 5 1 0 5 75 2 2 4 3 4 2 3 4 1 5 2 1 4 2 4
Sample Output
68 68 68 61
Data Constraint
Source / Author: median
题解:
考虑分治。
定义solve(l1 , r1 , l2 ,r2 , cur)为此时要找排名为cur的数的答案。
将 cur 折半,放 在两个区间的对应位置 s,t 上,比较 a[s], b[t]
如果a[s] < b[t] ,a序列s位置及之前的都可以舍去了 , cur对应减去你删掉数的个数。 a[s]>=b[t]同理。
因为我们已经把cur折半了 , 设折半后为k ,k + k<= cur,而我们现在又要求第cur大数 , 当a[s] < b[t]时 , 对于a任意小于s的位置 , 都小于b[t] , 而cur大数肯定是这k个比b[t]小的数以后的。
#include
#include
#include
#define mem(a,b) memset(a,b,sizeof(a))
#define mcy(a,b) memcpy(a,b,sizeof(a))
#define N 500010
#define re register
#define inf 2147483647
#define mod (int)(1e9+7)
#define open(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout)
using namespace std;
int in()
{
char ch(0); int f=0, x=0;
while(ch < '0' || ch > '9') ch = getchar() , f |= ch == '-';
while(ch>='0' && ch<='9') x = (x<<1) + (x<<3) + ch - '0' , ch = getchar();
return f ? (x = -x) : x;
}
int in(int &x)
{
char ch(0); int f=0; x=0;
while(ch < '0' || ch > '9') ch = getchar() , f |= ch == '-';
while(ch>='0' && ch<='9') x = (x<<1) + (x<<3) + ch - '0' , ch = getchar();
return f ? (x = -x) : x;
}
int n,m,a[N],b[N],u;
int slove(int l1,int r1,int l2,int r2,int cur)
{
if(l1>r1) return b[l2 + cur - 1];
if(l2>r2) return a[l1 + cur - 1];
int k = cur / 2 , k1(k),k2(k);
if(cur==1) return min(a[l1] ,b[l2]);
int s = l1 + k - 1 , t = l2 + k - 1;
if(s>r1) k1 = k-(s-r1),s = r1 ;
if(t>r2) k2 = k-(t-r2) , t = r2;
if(a[s] < b[t]) return slove(s+1 , r1 , l2, r2 , cur-k1);
else return slove(l1 , r1 , t+1 , r2 , cur-k2);
}
int main()
{
open("median");
in(n),in(m);
int cnt=0;
for(int i=1;i<=n;i++)in(a[i]);
for(int i=1;i<=n;i++)in(b[i]);
for(int i=1;i<=m;i++)
{
int opt=in();
if(opt == 1)
{
int x = in() , y = in() , z = in();
if(x&1) b[y]=z; else a[y]=z;
}
else
if(opt == 2)
{
// ++cnt;
// if(cnt==3)
// {cnt=3;}
int l1 = in() , r1 = in() , l2 = in() , r2 = in() , ans=0;
ans = slove(l1,r1,l2,r2,(r1 - l1 + r2 - l2 + 3) / 2);
printf("%d\n",ans);
}
}
return 0;
}
O(n log n)