P1083 借教室
題面:https://www.luogu.org/problem/P1083
一題簡(jiǎn)單的線段樹(shù)(但在這題看來(lái)似乎是一種玄學(xué)算法) 開(kāi)一個(gè)線段樹(shù)維護(hù)區(qū)間最小值和支持區(qū)間修改即可 只要整個(gè)區(qū)間中有<0的就不行,即教室不夠了.輸出當(dāng)前的訂單號(hào)就行.Code: #include<cstdio> #include<algorithm> #include<cstring> #include<queue> #include<cmath> #include <iostream> #define MAXN 10000010 #define inf 0x3f3f3f3f using namespace std; struct node{ int l,r;int add;int mn; }tree[MAXN<<2]; void pushup(int index){ tree[index].mn = min(tree[index<<1].mn,tree[index<<1|1].mn); } void pushdown(int index){ if(tree[index].add){ tree[index<<1].mn += tree[index].add; tree[index<<1|1].mn += tree[index].add; tree[index<<1].add += tree[index].add; tree[index<<1|1].add += tree[index].add; tree[index].add = 0; } } void build(int l,int r,int index){ tree[index].l = l; tree[index].r = r; tree[index].add = 0;if(l == r){ scanf("%d",&tree[index].mn);return ; } int mid = (l+r)>>1; build(l,mid,index<<1); build(mid+1,r,index<<1|1); pushup(index); } void updata(int l,int r,int index,int val){ if(l <= tree[index].l && r >= tree[index].r){ tree[index].mn += val; tree[index].add += val;return ; } pushdown(index); int mid = (tree[index].l+tree[index].r)>>1; if(l <= mid){ updata(l,r,index<<1,val); } if(r > mid){ updata(l,r,index<<1|1,val); } pushup(index); } int main() { int n,m,x,y,z; scanf("%d%d",&n,&m);build(1,n,1);for(int i=1;i<=m;i++){scanf("%d%d%d",&z,&x,&y);updata(x,y,1,-z);if(tree[1].mn<0){cout<<"-1"<<endl<<i<<endl;return 0;}}cout<<"0"<<endl;return 0; }轉(zhuǎn)載于:https://www.cnblogs.com/ukcxrtjr/p/11382433.html
總結(jié)
- 上一篇: P1983 车站分级
- 下一篇: webpack与vue环境搭建(转载)