# 【51nod 1685】 第K大区间2

[l,r]表示区间的值
[1]：3
[2]：1
[3]：2
[4]：4
[1,3]：2
[2,4]：2

Input

Output

Input示例

4 3
3 1 2 4

Output示例

2

2 * (sum[R] - sum[L-1]) > R-(L-1)
2 * sum[R] - R > 2 * sum[L-1]-(L-1)

2-i，统计每个b[i]有多少个b[j]<b[i](j<i 且 j和i奇偶性不同)

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll int
using namespace std;
const int maxn = 500000;
int n, k, ans, a[maxn], b[maxn], s[maxn], sum[maxn];
int bit[2][maxn];
int lowbit(int x)
{
return x & -x;
}
void update(int flag, int pos)
{
while(pos <= n*2)
{
bit[flag][pos]++;
pos += lowbit(pos);
}
}
ll query(int flag, int pos)
{
ll res = 0;
while(pos)
{
res += bit[flag][pos];
pos -= lowbit(pos);
}
return res;
}
bool check(int mid)
{
//cout<<mid<<"qwq\n";
memset(bit, 0, sizeof(bit));
memset(sum, 0, sizeof(sum));
ans = 0;
for(int i = 1; i <= n; i++) sum[i] = sum[i-1] + (a[i] >= mid);
for(int i = 1; i <= n; i++) sum[i] = 2*sum[i] - i+n;
update(0, n);
for(int i = 1; i <= n; i++)
{
ans += query(!(i&1), sum[i]-1);
update(i&1,sum[i]);
}
//for(int i = 1; i <= n; i++) cout<<bit[0][i]<<" ";
//cout<<"ans:"<<ans<<endl;
return ans >= k;
}
int main()
{
cin>>n>>k;
for(int i = 1; i <= n; i++) {cin>>a[i]; b[i] = a[i];}
sort(b+1, b+1+n);
int l = 1, r = n;
while(l != r)
{
int mid = (l + r + 1) >> 1;
//cout<<mid<<"qnq\n";
if(check(b[mid])) l = mid;

else r = mid - 1;
}
cout<<b[l];
return 0;
}

posted @ 2018-10-30 18:24  Misaka_Azusa  阅读(81)  评论(0编辑  收藏
Live2D