codeforce-191E-Thwarting Demonstrations(树状数组+二分+离散)

题意:

求第K 大连续区间

分析:

二分答案,再n * log(n)判断有几个区间的区间和大于mid,然后调整上下界,使这个值不断的接近k。

判断符合条件的区间总数:线性扫描sum[n](前n项和)  每次判断以i结尾的区间有几个区间和大于等于mid,累加即可

// File Name: 191-E.cpp
// Author: Zlbing
// Created Time: 2013年08月03日 星期六 15时10分13秒 #include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
using namespace std;
#define CL(x,v); memset(x,v,sizeof(x));
#define INF 0x3f3f3f3f
#define LL long long
#define REP(i,r,n) for(int i=r;i<=n;i++)
#define RREP(i,n,r) for(int i=n;i>=r;i--)
const int MAXN=1e5+;
int tree[MAXN];
int n;
int lowbit(int x)
{
return x&(-x);
}
void add(int pos,int val)
{
while(pos<=n)
{
tree[pos]+=val;
pos+=lowbit(pos);
}
}
int read(int x)
{
int s=;
while(x>)
{
s+=tree[x];
x-=lowbit(x);
}
return s;
}
LL sum[MAXN];
LL num[MAXN];
int N;
LL M;
LL solve(LL mid)
{
CL(tree,);
LL ans=;
for(int i=;i<=N;i++)
{
if(sum[i]>=mid)ans++;
LL t=sum[i]-mid;
int a=upper_bound(num+,num+n+,t)-num-;
int b=lower_bound(num+,num+n+,sum[i])-num;
ans+=read(a);
add(b,);
}
//cout<<"ans="<<ans<<endl;
return ans;
}
int main()
{
while(~scanf("%d%lld",&N,&M))
{
sum[]=;
LL l,r;
REP(i,,N)
{
scanf("%lld",&sum[i]);
sum[i]+=sum[i-];
num[i]=sum[i];
}
sort(num+,num+N+);
n=unique(num+,num+N+)-num-;
//cout<<"l="<<l<<"r="<<r<<endl;
LL best=-;
l=-1e18;
r=1e18;
while(l<=r)
{
LL mid=l+(r-l+)/;
//cout<<"l="<<l<<"r="<<r<<"mid="<<mid<<endl;
if(solve(mid)>=M)
{
best=mid;
l=mid+;
}
else r=mid-;
}
cout<<best<<endl;
}
return ;
}
上一篇:Android 利用日志消息调试程序


下一篇:mysql 游标嵌套循环实例