题目链接:
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1672
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#define sf scanf
#define pf printf
#define L (rt << 1)
#define R (rt << 1 | 1)
using namespace std;
typedef long long LL;
const int maxn = 100000 + 5;
LL sum[maxn];
int cover[maxn << 2];
void push_up(int rt){
cover[rt] = cover[L] + cover[R];
}
void Build(){
memset(cover,0,sizeof(cover));
}
void update(int rt,int l,int r,int x){
if(l == r) {
cover[rt]++;
return;
}
int mid = (l + r) >> 1;
if(x <= mid){
update(L,l,mid,x);
}
else{
update(R,mid + 1,r,x);
}
push_up(rt);
}
int Query(int rt,int l,int r,int x){
if(l == r) return r;
else{
int mid = l + r >> 1;
if(cover[R] >= x) return Query(R,mid + 1,r,x);
else return Query(L,l,mid,x - cover[R]);
}
}
struct CC{
int ll,rr;
}CCs[maxn];
bool cmp(const CC& a,const CC& b){
return a.ll < b.ll;
}
int main(){
int n,k,m,temp;
sf("%d%d%d",&n,&k,&m);
sum[0] = 0;
for(int i = 1;i <= n;++i){
sf("%d",&temp);
sum[i] = sum[i - 1] + temp;
}
for(int i = 1;i <= m;++i) sf("%d %d",&CCs[i].ll,&CCs[i].rr);
sort(CCs + 1,CCs + m + 1,cmp);
CCs[n + 1].rr = 1;
Build();
LL ans = 0;
for(int i = 1;i <= k;++i) update(1,1,n,CCs[i].rr);
for(int i = k;i <= m;++i){
int pos = Query(1,1,n,k);
if(pos >= CCs[i].ll){
ans = max(ans,sum[pos] - sum[CCs[i].ll - 1]);
}
update(1,1,n,CCs[i + 1].rr);
}
pf("%lld\n",ans);
return 0;
}