这道题,用单调队列优化掉不在当前时间段内的任务
单调队列对于pair使用优先判断first 然后判断second
#include<bits/stdc++.h>
#define ll long long
#define x first
#define y second
using namespace std;
typedef pair<int,int>PII; //first 存放起始时间+消耗时间,second 存放消耗算力
const int N=2e5+10;
int n,m,k;
int s[N];//剩余算力
priority_queue<PII,vector<PII>,greater<PII> >q[N];//优先队列
int main() {
cin>>n>>m;
for(int i=1; i<=n; i++)cin>>s[i];
while(m--){
int a,b,c,d;
cin>>a>>b>>c>>d;
//时间不在当前范围内,出队
while(q[b].size()&&q[b].top().x<=a) {
s[b]+=q[b].top().y;//回复算力
q[b].pop(); //出队
}
if(s[b]<d)cout<<"-1"<<endl;
else{
q[b].push({a+c,d});
s[b]-=d;
cout<<s[b]<<endl;
}
}
return 0;
}