题目连接 HDOJ 5700
题意 中文题意
做法 枚举左端点如果小于等于k 那我们直接continue 找满足条件的右端点出现的次数大于等于k次的 进行二分求值
/*
hdoj 5700
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <sstream>
using namespace std;
#define dbg(x) ;
//cout<<#x<<" = "<< (x)<< endl
const int MAX_N = 100024;
long long c_[MAX_N];
int c[MAX_N];
long long a[MAX_N];
struct node {
int l,r;
bool operator < (const node other) const
{
if(l==other.l) return r <other.l;
return l < other.l;
}
}arr[MAX_N];
void add(int x,int v){
for(;x<MAX_N;x+=x&(-x))
c[x]+=v;
}
long long getsum(int x){
long long ans = 0;
for(;x;x-=x&(-x))
ans+=c[x];
return ans;
}
void add_(int x,long long v){
for(;x<MAX_N;x+=x&(-x))
c_[x]+=v;
}
long long getsum_(long long x){
long long ans = 0;
for(;x;x-=x&(-x))
ans+=c_[x];
return ans;
}
int main(){
int n,k,m;
while(scanf("%d%d%d",&n,&k,&m)==3){
memset(c,0,sizeof(c));
memset(c_,0,sizeof(c_));
for(int i = 1;i<=n;++i){
scanf("%lld",&a[i]);
add_(i,a[i]);
}
for(int i = 1;i<=m;++i){
scanf("%d%d",&arr[i].l,&arr[i].r);
}
sort(arr+1,arr+1+m);
long long maxx = 0;
for(int i = 1;i<=m;++i){
add(arr[i].l,1);
add(arr[i].r+1,-1);
if(getsum(arr[i].l)<k) continue;
else {
int l = arr[i].l,r = arr[i].r;
while(l<=r){
int mid = (l+r)>>1;
if(getsum(mid)>=k) l = mid + 1;
else r = mid - 1;
}
dbg(r);
dbg(arr[i].l);
maxx = max(maxx,getsum_(r)-getsum_(arr[i].l-1));
dbg(getsum_(r));
dbg(getsum_(arr[i].l-1));
}
}
printf("%lld\n",maxx);
}
return 0;
}