luoguP1083 借教室(題解)(我用的線段樹)
阿新 • • 發佈:2018-05-23
push col \n div har printf clu pac flag
luoguP1083 借教室 題目
#include<cstdio> #include<iostream> #include<cmath> #include<string> #include<string> #include<algorithm> #define rg register #define N 1000050 #define lc now<<1 #define rc (now<<1)+1 using namespace std; int n,m; bool flag; int a[N]; structtree{ int l,r,minn,lazy; }ljl[N<<2]; inline int read() { int s=0,m=1;char ch=getchar(); while(ch!=‘-‘&&(ch<‘0‘||ch>‘9‘))ch=getchar(); if(ch==‘-‘)m=-1,ch=getchar(); while(ch>=‘0‘&&ch<=‘9‘)s=(s<<3)+(s<<1)+ch-‘0‘,ch=getchar(); return s*m; }void build(int now,int ll,int rr) { if(ll==rr) { ljl[now].l=ljl[now].r=ll; ljl[now].minn=a[ll]; } else { int mid=(ll+rr)>>1; build(lc,ll,mid); build(rc,mid+1,rr); ljl[now].l=ll;ljl[now].r=rr; ljl[now].minn=min(ljl[lc].minn,ljl[rc].minn); } } void push_down(int now) { if(ljl[now].lazy!=0) { ljl[lc].lazy+=ljl[now].lazy; ljl[rc].lazy+=ljl[now].lazy; ljl[lc].minn-=ljl[now].lazy; ljl[rc].minn-=ljl[now].lazy; ljl[now].lazy=0; } } void xiugai(int now,int ll,int rr,int d) { push_down(now); if(ljl[now].l==ll&&ljl[now].r==rr) { ljl[now].lazy+=d; ljl[now].minn-=d; // if(ljl[now].minn<0)flag=1; } else { int mid=(ljl[now].l+ljl[now].r)/2; if(rr<=mid)xiugai(2*now,ll,rr,d); if(ll>mid)xiugai(2*now+1,ll,rr,d); if(ll<=mid&&rr>mid) { xiugai(2*now,ll,mid,d); xiugai(2*now+1,mid+1,rr,d); } ljl[now].minn=min(ljl[lc].minn,ljl[rc].minn); } } int main() { n=read();m=read(); for(rg int i=1;i<=n;++i)a[i]=read(); build(1,1,n); for(rg int i=1;i<=m;++i) { rg int num=read(),st=read(),et=read(); xiugai(1,st,et,num); if(ljl[1].minn<0) { printf("-1\n%d\n",i); exit(0); } } puts("0"); return 0; }
luoguP1083 借教室(題解)(我用的線段樹)