标签:line 多少 stream queue sum www oal ons 树状数组
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#define ll long long
using namespace std;
inline int read(){
int x=0,o=1;char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')o=-1,ch=getchar();
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*o;
}
const int N=3e5+5;
const int inf=1e9;
int n,m,k,L[N],R[N],V[N],ans[N];ll c[N];
struct query{int goal,id;}q[N],ql[N],qr[N];
vector<int>bel[N];
inline int lowbit(int x){return x&-x;}
inline void add(int x,int v){for(;x<=m;x+=lowbit(x))c[x]+=v;}
inline ll ask(int x){ll cnt=0;for(;x;x-=lowbit(x))cnt+=c[x];return cnt;}
inline void update(int i,int opt){
if(R[i]>=L[i])add(L[i],opt*V[i]),add(R[i]+1,-opt*V[i]);
else add(L[i],opt*V[i]),add(R[i]+1,-opt*V[i]),add(1,opt*V[i]);
//如果r<l,说明从节点m加到了节点1,在之前的基础上,再给节点1也加上val
}
inline void solve(int l,int r,int st,int ed){
if(st>ed)return;
if(l==r){
for(int i=st;i<=ed;++i)ans[q[i].id]=l;
return;
}
int mid=(l+r)>>1,lt=0,rt=0;
for(int i=l;i<=mid;++i)update(i,1);
for(int i=st;i<=ed;++i){
ll sum=0;
for(int j=0;j<bel[q[i].id].size();++j){
sum+=ask(bel[q[i].id][j]);
if(sum>=q[i].goal)break;//没加这个break,有一个点会爆long long,有一个点会超时
}
if(sum>=q[i].goal)ql[++lt]=q[i];
else q[i].goal-=sum,qr[++rt]=q[i];
}
for(int i=mid;i>=l;--i)update(i,-1);
for(int i=1;i<=lt;++i)q[st+i-1]=ql[i];
for(int i=1;i<=rt;++i)q[lt+st+i-1]=qr[i];
solve(l,mid,st,st+lt-1);solve(mid+1,r,st+lt,ed);
}
int main(){
n=read();m=read();
for(int i=1;i<=m;++i)bel[read()].push_back(i);
for(int i=1;i<=n;++i)q[i].goal=read(),q[i].id=i;
k=read();
for(int i=1;i<=k;++i)L[i]=read(),R[i]=read(),V[i]=read();
L[++k]=1;R[k]=m;V[k]=inf;//设置一个右边界,保证最后每个国家都能达到目标值
solve(1,k,1,n);
for(int i=1;i<=n;++i){
if(ans[i]==k)puts("NIE");
else printf("%d\n",ans[i]);
}
return 0;
}
标签:line 多少 stream queue sum www oal ons 树状数组
原文地址:https://www.cnblogs.com/PPXppx/p/11688472.html