线段树

来源:互联网 发布:阿里云优惠码在哪 编辑:程序博客网 时间:2024/06/05 06:06

题意是有一排花。 每朵花要开需要浇pi的水,每天stalin会给一段区间【l,r】浇c的水,给q个询问,每个询问问某朵花开花的时间。

一眼瞄去总感觉是个可持久化之类的乱搞数据结构题。但是看了题解发现只是一个简单的区间加减。只要维护一个区间min就可以知道有没有往这个节点查询的必要。蒟蒻数据结构真的弱啊。区间更改都已经不会写了,惨啊。。。‘

#include <bits/stdc++.h>#define N 500010#define inf 0x7fffffffusing namespace std;inline int read(){    int x=0,f=1;char ch=getchar();    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}    return x*f;}int tree[N<<2],minu[N<<2];int n,m,q,k,tim[N],gron[N],now;inline void pushup(int rt){    if(tree[rt<<1]==-inf) tree[rt]=tree[rt<<1|1];    else if(tree[rt<<1|1]==-inf) tree[rt]=tree[rt<<1];    else tree[rt]=min(tree[rt<<1],tree[rt<<1|1]);}inline void pushdown(int rt){   if(minu[rt])   {     tree[rt<<1]-=minu[rt];        tree[rt<<1|1]-=minu[rt];        minu[rt<<1]+=minu[rt];        minu[rt<<1|1]+=minu[rt];        minu[rt]=0;    }}void built(int l,int r,int rt){    if(l==r){tree[rt]=gron[l];return;}    int mid=(l+r)>>1;    built(l,mid,rt<<1);built(mid+1,r,rt<<1|1);    pushup(rt);}void dowith(int l,int r,int rt){    if(l==r){  if(!tim[l])tim[l]=now;        else tim[l]=min(now,tim[l]);tree[rt]=-inf;return;    }int mid=(l+r)>>1;    pushdown(rt);    if(tree[rt<<1]<=0&&tree[rt<<1]!=inf) dowith(l,mid,rt<<1);    if(tree[rt<<1|1]<=0&&tree[rt<<1|1]!=inf) dowith(mid+1,r,rt<<1|1);    pushup(rt);}void update(int L,int R,int mi,int l,int r,int rt){    if(L<=l&&r<=R){    minu[rt]+=mi;        tree[rt]-=mi;        if(tree[rt]<=0)        dowith(l,r,rt);        return;    }int mid=(l+r)>>1;    pushdown(rt);    if(L<=mid&&tree[rt<<1]!=-inf) update(L,R,mi,l,mid,rt<<1);    if(R>mid&&tree[rt<<1|1]!=-inf) update(L,R,mi,mid+1,r,rt<<1|1);    pushup(rt);}int main(){    n=read();m=read();    for(int i=1;i<=n;i++) scanf("%d",&gron[i]);    built(1,n,1);    for(now=1;now<=m;now++)   {  int l=read(),r=read(),kl=read();        if(l<=r){update(l,r,kl,1,n,1);}        else update(1,r,kl,1,n,1),update(l,n,kl,1,n,1);        }            q=read();while(q--){   k=read(); if(!tim[k]) printf("So sad\n");   else printf("%d\n",tim[k]); }    return 0;}



0 0
原创粉丝点击