P4560 [IOI2014] Wall brick

Title Description

Given a length of n- n- and the initial value of the full sequence of 00. You need to support the following two operations:

  • L the Add, R & lt, h L , R & lt , h : the sequence [L, R & lt] [ L , R & lt all values within less than h] h element Ode h is h , this time without changing the height h is greater than h of element values
  • L the Remove, R & lt, H L , R & lt , H : the sequence [L, R & lt] [ L , R & lt all values] H is greater than H elements Ode as H H , does not change at this time is smaller than the height H H element values

You need to output K K sequences after the operation times.

Resolve

Apparently every time the range of operation would affect some interval, then we may wish to maintain the value of the interval bounds, you can easily swap A this problem.

The easiest way is to segment tree (but looks like it was a high-level data structures T 233)

So maintenance bounds essentially means preserving the maximum and minimum range, so every time we make a lazytag to preserve affect the operation of the interval on the line. Note that instead of marking maintenance information, we do not have the interval queries, maintains this pile of useless information, but also a waste of time. . .

Finally, we put all the output caused by the impact lazytag well maintained out, once again traverse the whole grain output lazytag segment tree leaf node on the line.

That lazytag how to maintain it? We will focus on how to get pushdown.

Set \ (up (h), down (h) \) respectively correspond to the line segment tree for Add, Remove two operations, changing the height \ (H \) .

For \ (up (H) \) , disposed to change the lower and upper bounds for the interval \ (U, D \) :

  1. If \ (the X-<d \) , does not affect the interval
  2. If \ (D <X <U \) , this time \ (D \) becomes \ (X \) , \ (U \) unchanged
  3. If \ (X> U \) , this time \ (d, u \) both become \ (X \) .

Reference Code

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#define INF 0x3f3f3f3f
#define N 2000010
using namespace std;
inline int read()
{
    int f=1,x=0;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}
struct node{
    int l,r;
    int up,down;
}t[N<<2];
int n,k;
inline void pushdown(int p)
{
    if(p==0) return;
    if(t[p].up!=0){
        t[p<<1].up=max(t[p].up,t[p<<1].up);
        t[p<<1|1].up=max(t[p].up,t[p<<1|1].up);
        if(t[p<<1].down<t[p].up) t[p<<1].down=t[p].up;
        if(t[p<<1|1].down<t[p].up) t[p<<1|1].down=t[p].up;
        t[p].up=0;
    }
    if(t[p].down!=INF){
        t[p<<1].down=min(t[p].down,t[p<<1].down);
        t[p<<1|1].down=min(t[p].down,t[p<<1|1].down);
        if(t[p<<1].up>t[p].down) t[p<<1].up=t[p].down;
        if(t[p<<1|1].up>t[p].down) t[p<<1|1].up=t[p].down;
        t[p].down=INF;
    }
}
inline void build(int p,int l,int r)
{
    t[p].l=l,t[p].r=r;t[p].up=0,t[p].down=INF;
    if(l==r) return;
    int mid=(l+r)>>1;
    build(p<<1,l,mid);
    build(p<<1|1,mid+1,r);
}
inline void up(int p,int l,int r,int val)
{
    if(l<=t[p].l&&t[p].r<=r){
        t[p].up=max(t[p].up,val);
        t[p].down=max(val,t[p].down);
        return;
    }
    pushdown(p);
    int mid=(t[p].l+t[p].r)>>1;
    if(l<=mid) up(p<<1,l,r,val);
    if(r>mid) up(p<<1|1,l,r,val);
}
inline void down(int p,int l,int r,int val)
{
    if(l<=t[p].l&&t[p].r<=r){
        t[p].down=min(t[p].down,val);
        t[p].up=min(t[p].up,val);
        return;
    }
    pushdown(p);
    int mid=(t[p].l+t[p].r)>>1;
    if(l<=mid) down(p<<1,l,r,val);
    if(r>mid) down(p<<1|1,l,r,val);
}
inline void query(int p,int l,int r)
{
    if(t[p].l==t[p].r){
        printf("%d\n",t[p].up);
        return;
    }
    pushdown(p);
    int mid=(t[p].l+t[p].r)>>1;
    if(l<=mid) query(p<<1,l,r);
    if(r>mid) query(p<<1|1,l,r);
}
int main()
{
    n=read(),k=read();
    build(1,1,n);
    for(int i=1;i<=k;++i){
        int cmd=read(),l=read(),r=read(),val=read();
        ++l,++r;
        if(cmd==1) up(1,l,r,val);
        else down(1,l,r,val);
    }
    query(1,1,n);
    return 0;
}

Guess you like

Origin www.cnblogs.com/DarkValkyrie/p/11617998.html