据说是主席树板子题,然而我并不会。
观察数据范围 ,可以思考一个根号做法,考虑带修莫队。
但是这样离散化之后还是会有 个数,暴力统计答案会 TLE。考虑分块,然后可以做到 查询。
时间复杂度就带修莫队拼上 次查询, 同阶,即 。
然后几乎就是板子题,赛后写个奇偶化排序就过了,赛时没写奇偶化排序 T 飞了。
//#include<bits/stdc++.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<iomanip>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<ctime>
#include<deque>
#include<queue>
#include<stack>
#include<list>
using namespace std;
typedef long long ll;
constexpr const int N=2e5,Q=2e5,V=5e5;
int n,m,sizeQ,sizeOp,init[N+1],a[N+1],real[N+1];
ll value[V+1];
int M,Bv,bc;
int bid[V+1],bl[N+1],br[N+1],cv[V+1],cb[N+1];
ll sv[V+1],sb[N+1];
ll tot;
void build(){
Bv=sqrt(M)+1;
bc=(M+Bv-1)/Bv;
for(int b=1;b<=bc;b++){
bl[b]=(b-1)*Bv+1;
br[b]=min(b*Bv,M);
for(int v=bl[b];v<=br[b];v++)
bid[v]=b;
}
}
struct question{
int l,r;
ll k;
int t,id;
}q[Q+1];
struct operation{
int pos,color,backup;
}op[Q+1];
int ans[Q+1];
int B,size,pos[N+1],edgeL[N+1],edgeR[N+1];
void pre(){
B=pow(n,2/3.0);
for(int i=1;edgeR[i-1]+1<=n;i++){
edgeL[i]=edgeR[i-1]+1;
edgeR[i]=min(edgeL[i]+B-1,n);
for(int j=edgeL[i];j<=edgeR[i];j++)
pos[j]=i;
}
sort(q+1,q+sizeQ+1,[](question a,question b){
if(pos[a.l]!=pos[b.l]){
return pos[a.l]<pos[b.l];
}else if(pos[a.r]!=pos[b.r]){
if(pos[a.l]&1){
return pos[a.r]<pos[b.r];
}else{
return pos[a.r]>pos[b.r];
}
}else{
if(pos[a.r]&1){
return a.t<b.t;
}else{
return a.t>b.t;
}
}
});
}
void addLeft(int &l,int &r,int &t){
l--;
int v=a[l];
cv[v]++;
sv[v]+=value[v];
tot+=value[v];
int b=bid[v];
cb[b]++;
sb[b]+=value[v];
}
void addRight(int &l,int &r,int &t){
r++;
int v=a[r];
cv[v]++;
sv[v]+=value[v];
tot+=value[v];
int b=bid[v];
cb[b]++;
sb[b]+=value[v];
}
void delLeft(int &l,int &r,int &t){
int v=a[l];
cv[v]--;
sv[v]-=value[v];
tot-=value[v];
int b=bid[v];
cb[b]--;
sb[b]-=value[v];
l++;
}
void delRight(int &l,int &r,int &t){
int v=a[r];
cv[v]--;
sv[v]-=value[v];
tot-=value[v];
int b=bid[v];
cb[b]--;
sb[b]-=value[v];
r--;
}
void moveUp(int &l,int &r,int &t){
t++;
int p=op[t].pos;
if(l<=p&&p<=r){
int old=a[p];
cv[old]--;
sv[old]-=value[old];
tot-=value[old];
int b=bid[old];
cb[b]--;
sb[b]-=value[old];
}
a[p]=op[t].color;
if(l<=p&&p<=r){
int nw=a[p];
cv[nw]++;
sv[nw]+=value[nw];
tot+=value[nw];
int b=bid[nw];
cb[b]++;
sb[b]+=value[nw];
}
}
void moveDown(int &l,int &r,int &t){
int p=op[t].pos;
if(l<=p&&p<=r){
int nw=a[p];
cv[nw]--;
sv[nw]-=value[nw];
tot-=value[nw];
int b=bid[nw];
cb[b]--;
sb[b]-=value[nw];
}
a[p]=op[t].backup;
if(l<=p&&p<=r){
int old=a[p];
cv[old]++;
sv[old]+=value[old];
tot+=value[old];
int b=bid[old];
cb[b]++;
sb[b]+=value[old];
}
t--;
}
int calc(ll k){
if(tot<k)return -1;
ll rem=k;
int ans=0;
for(int b=bc;b>=1;b--){
if(rem<=0)break;
if(sb[b]==0)continue;
if(sb[b]>=rem){
for(int v=br[b];v>=bl[b];v--){
if(rem<=0)break;
if(cv[v]==0)continue;
ll s=sv[v];
if(s>=rem){
ans+=(rem+value[v]-1)/value[v];
rem=0;
break;
}else{
rem-=s;
ans+=cv[v];
}
}
break;
}else{
rem-=sb[b];
ans+=cb[b];
}
}
return ans;
}
void discist(){
static int tmp[V+1];
M=0;
for(int i=1;i<=n;i++){
tmp[++M]=init[i];
}
for(int i=1;i<=sizeOp;i++){
tmp[++M]=op[i].color;
tmp[++M]=op[i].backup;
}
sort(tmp+1,tmp+M+1);
M=unique(tmp+1,tmp+M+1)-tmp-1;
for(int i=1;i<=M;i++){
value[i]=tmp[i];
}
for(int i=1;i<=n;i++){
a[i]=lower_bound(tmp+1,tmp+M+1,init[i])-tmp;
}
for(int i=1;i<=sizeOp;i++){
op[i].color=lower_bound(tmp+1,tmp+M+1,op[i].color)-tmp;
op[i].backup=lower_bound(tmp+1,tmp+M+1,op[i].backup)-tmp;
}
}
main(){
/*freopen("test.in","r",stdin);
freopen("test.out","w",stdout);*/
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>init[i];
real[i]=init[i];
}
sizeQ=sizeOp=0;
for(int i=1;i<=m;i++){
int c,x,l,r;ll k;
cin>>c>>x>>l>>r>>k;
op[++sizeOp]={c,x,real[c]};
real[c]=x;
sizeQ++;
q[sizeQ]={l,r,k,sizeOp,sizeQ};
}
discist();
build();
pre();
int l=1,r=0,t=0;
tot=0;
for(int i=1;i<=sizeQ;i++){
while(l>q[i].l){
addLeft(l,r,t);
}
while(r<q[i].r){
addRight(l,r,t);
}
while(l<q[i].l){
delLeft(l,r,t);
}
while(r>q[i].r){
delRight(l,r,t);
}
while(t<q[i].t){
moveUp(l,r,t);
}
while(t>q[i].t){
moveDown(l,r,t);
}
ans[q[i].id]=calc(q[i].k);
}
for(int i=1;i<=sizeQ;i++){
cout<<ans[i]<<'\n';
}
cout.flush();
/*fclose(stdin);
fclose(stdout);*/
return 0;
}