[Segment tree] Luogu P4560 brick

Title Description

Given a length of  n- n-and the initial value are all  0 sequence zero. You need to support the following two operations:

  • The Add  L, R & lt, h L , R & lt , h: the sequence  [L, R & lt] [ L , R & lt all values less than]  h h Ode as an element  h h, then do not change the height is greater than  h element value of h
  • The Remove  L, R & lt, H L , R & lt , H: the sequence  [L, R & lt] [ L , R & lt all values] is greater than  H H Ode element as  H H, does not change at this time is smaller than the height  H H element values

You need to output  k sequence k times after the operation.

Input and output formats

Input formats:

 

The first line of input contains two positive integers  n-, K n- , K, respectively, and the number represents the number of elements in the sequence of operation, note: the sequence number of the subscript  0 0 ~  n-. 1- n- - . 1.

Then  K K lines contains  . 4 4 integers  T, L, R & lt, H T , L , R & lt , H, if  T =. 1 T = . 1 indicates that for the Add operation, if  T = 2 T = 2 indicates as Remove operation. L, R & lt, H L , R & lt , H have the meaning described in the subject.

 

Output formats:

 

Output comprising  n- n-rows, each row containing  . 1 an integer. The first  i integer i represents a row  k after the k th operation sequence numbered  i - 1 i - value of the element 1.

 

Sample input and output

Input Sample # 1:
10 3
1 3 4 91220
1 5 9 48623
2 3 5 39412
Output Sample # 1:
0
0
0
39412
39412
39412
48623
48623
48623
48623
Input Sample # 2:
10 6
1 1 8 4
2 4 9 1
2 3 6 5
1 0 5 3
1 2 2 5
2 6 7 0
Output Sample # 2: 
3
4
5
4
3
3
0
0
1
0

Explanation

  • Subtask # 1 (8 min): satisfies  1 \ n-Leq \ Leq 10 000, 1 \ K Leq \ Leq. 5 000 1 n- 1 0 0 0 0 , 1 K . 5 0 0 0;
  • Subtask # 2 (24 points): meet  . 1 \ Leq n-\ Leq 100 000,. 1 \ Leq K \ Leq 500 000 . 1 n- . 1 0 0 0 0 0 , . 1 K . 5 0 0 0 0 0, all increase operations are all removed before the operation;
  • Subtask # 3 (29 points): meet  . 1 \ n-Leq \ Leq 100 000,. 1 \ Leq K \ Leq 500 000 . 1 n- . 1 0 0 0 0 0 , . 1 K . 5 0 0 0 0 0;
  • Subtask # 4 (39 points): meet  . 1 \ Leq n-\ Leq 2 000 000,. 1 \ Leq K \ Leq 500 000 . 1 n- 2 0 0 0 0 0 0 , . 1 K . 5 0 0 0 0 0 .

The height of all operations  H H satisfy  0 \ Leq H \ Leq 100 000 0 H 1 0 0 0 0 0.

 

answer

  • Taking interval is to take min and max, a relatively simple segment tree

answer

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #define N 2000010
 5 using namespace std;
 6 int n,m;
 7 struct Tree
 8 {
 9     int mx[N*4],mn[N*4];
10     void Max(int x,int y) { mn[x]=max(mn[x],y),mx[x]=max(mx[x],y); }
11     void Min(int x,int y) { mn[x]=min(mn[x],y),mx[x]=min(mx[x],y); } 
12     void pushdown(int d) { Max(d*2,mx[d]),Max(d*2+1,mx[d]),Min(d*2,mn[d]),Min(d*2+1,mn[d]),mx[d]=0,mn[d]=0x7fffffff; }    
13     void update(int d,int L,int R,int l,int r,int x,int y)
14     {
15         if (L<=l&&r<=R) { if (y==0) Max(d,x); else Min(d,x); return; }
16         int mid=l+r>>1;
17         pushdown(d);
18         if (L<=mid) update(d*2,L,R,l,mid,x,y);
19         if (R>mid) update(d*2+1,L,R,mid+1,r,x,y);
20     } 
21     void query(int d,int l,int r)
22     {
23         if (l==r) { printf("%d\n",mx[d]); return; }
24         int mid=l+r>>1;
25         pushdown(d),query(d*2,l,mid),query(d*2+1,mid+1,r);
26     }    
27 }tree;
28 int main()
29 {
30     scanf("%d%d",&n,&m);
31     for (int i=1;i<=n*4;i++) tree.mn[i]=0x7fffffff;
32     for (int i=1,op,l,r,h;i<=m;i++) scanf("%d%d%d%d",&op,&l,&r,&h),tree.update(1,l+1,r+1,1,n,h,op-1);
33     tree.query(1,1,n);
34 } 

 

Guess you like

Origin www.cnblogs.com/Comfortable/p/11199199.html