Asika391 @ 2019-08-24 20:00:38
代码:
#include<bits/stdc++.h>
#define lowbit() i&-i
using namespace std;
typedef long long ll;
const int N=200010;
priority_queue<int> q;
struct Section{
int l,r;
bool operator<(const Section& a)const{
return l<a.l;
}
}s[200005];
int a[N],tree[N];
int T,n,m,k,a1,ans,minn;
int read(){
int x=0,f=1;
char c=getchar();
while(c<'0' || c>'9'){if(c=='-') f=-1;c=getchar();}
while(c>='0' && c<='9') {x=(x<<3)+(x<<1)+c-'0';c=getchar();}
return x*f;
}
void add(int x,int k){
for(int i=x;i<=n;i+=lowbit()) tree[i]+=k;
}
int ask(int x){
int num=0;
for(int i=x;i;i-=lowbit()) num+=tree[i];
return num;
}
bool judge(int mid){
memset(tree,0,sizeof(tree));
while(q.size()) q.pop();
int now=1,times=0;
for(int i=1;i<=n;++i){
while(q.size() && q.top()<i) q.pop();
if(a[i]>=mid) continue;
int num=a[i]+ask(i);
if(num<mid){
while(now<=m && s[now].l<=i) q.push(s[now++].r);
while(num<mid && q.size()){
num+=a1,times++;
if(times>k) return false;
add(i+1,a1),add(q.top()+1,-a1);
q.pop();
}
if(num<mid) return false;
}
}
return true;
}
int main(){
for(T=read();T;T--){
minn=1<<29;
n=read(),m=read(),k=read(),a1=read();
for(int i=1;i<=n;++i){
a[i]=read();
minn=min(minn,a[i]);
}
for(int i=1;i<=m;++i){
s[i].l=read(),s[i].r=read();
if(s[i].l>s[i].r) swap(s[i].l,s[i].r);
}
sort(s+1,s+1+m);
int l=minn,r=minn+k*a1,ans=minn;
while(l<=r){
int mid=(l+r)>>1;
if(judge(mid)){
ans=mid;
l=mid+1;
}else r=mid-1;
}
printf("%d\n",ans);
}
return 0;
}