QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#687796 | #9528. New Energy Vehicle | ir101 | TL | 2ms | 11856kb | C++20 | 2.4kb | 2024-10-29 21:19:14 | 2024-10-29 21:19:14 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define PII pair<int,int>
#define endl '\n'
#define int long long
#define lc p<<1
#define rc p<<1|1
using namespace std;
const int N=1e6+10;
int cnt;
struct node{
int l,r;
ll cnt;
}tr[N<<2];
int pp[N];
void push_up(int p){
tr[p].cnt=tr[lc].cnt+tr[rc].cnt;
}
void build(int p,int l,int r){
tr[p]={l,r,0};
if(l==r){
pp[l]=p;
return;
}
int mid=l+r>>1;
build(lc,l,mid);
build(rc,mid+1,r);
push_up(p);
}
void update(int p,int x,int k){
// cout<<"d"<<endl;
if(tr[p].l==x and tr[p].r==x){
tr[p].cnt+=k;
return;
}
int mid=tr[p].l+tr[p].r>>1;
if(x<=mid){
update(lc,x,k);
}
if(x>mid)update(rc,x,k);
push_up(p);
}
int query(int p,ll syc){
if(tr[p].l==tr[p].r){
return tr[p].l;
}
if(tr[lc].cnt>=syc){
return query(lc,syc);
}
else{
syc-=tr[lc].cnt;
if(tr[rc].cnt>=syc)return query(rc,syc);
return -1;
}
}
int L[N],R[N];
vector<int>nxt[N];
ll a[N];
PII b[N];
void solve(){
int n,m;
cin>>n>>m;
// ::srand(time(0));
ll s=0;
for (int i = 1; i <=n ; ++i) {
cin>>a[i];
s+=a[i];
nxt[i].clear();
L[i]=0;
}
build(1,1,m+1);
for (int i = 1; i <=m ; ++i) {
cin>>b[i].first>>b[i].second;
nxt[b[i].second].push_back(i);
}
for (int i = 1; i <=n ; ++i) {
// cout<<nxt[i].size()<<endl;
if(nxt[i].size()){
update(1,nxt[i][0],a[i]);
}
else{
update(1,m+1,a[i]);
}
}
int qs=0;
ll ans=0;
int pos=1;
for (int i = 1; i <=m ; ++i) {
auto [u,v]=b[i];
int p= query(1,u-qs);
if(p==-1)break;
int sum=u-qs;
for (int j = pos; j <p ; ++j) {
sum-=tr[pp[j]].cnt;
update(1,j,-tr[pp[j]].cnt);
}
update(1,p,-sum);
update(1,i,-tr[pp[i]].cnt);
L[v]++;
if(nxt[v].size()<=L[v]){
update(1,m+1,a[v]);
}
else update(1,nxt[v][L[v]],a[v]);
pos=p;
qs=u;
}
ans=qs+tr[1].cnt;
cout<<ans<<endl;
}
signed main(){
ios::sync_with_stdio(false),cin.tie(0);
int t;
cin>>t;
while(t--){
solve();
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 11768kb
input:
2 3 1 3 3 3 8 1 2 2 5 2 1 2 2 1
output:
12 9
result:
ok 2 lines
Test #2:
score: 0
Accepted
time: 2ms
memory: 11856kb
input:
6 3 2 2 2 2 6 1 7 1 2 2 3 3 2 1 6 2 2 3 2 2 5 1 7 2 9 1 2 2 3 3 2 1 6 2 1 1 999999999 1000000000 1 1 1 1000000000 1000000000 1
output:
9 11 4 11 999999999 2000000000
result:
ok 6 lines
Test #3:
score: -100
Time Limit Exceeded
input:
10 230 8042 599 1039 69 1011 1366 824 14117 1523 806 5002 332 55 3769 996 359 1040 255 1135 3454 3609 6358 2509 3695 8785 3890 1304 3394 14611 33 89 2245 508 22 1043 10411 628 1279 714 903 585 7413 5099 845 148 4689 2110 8683 1613 143 3263 2599 110 244 3297 4742 1571 425 1822 15692 572 9397 328 1691...