4364: [IOI2014]wall砖墙

4364: [IOI2014]wall砖墙

链接

分析:

  线段树,维护一个最大值,一个最小值。

代码:

 1 #include<bits/stdc++.h>
 2 
 3 char buf[100000],*p1 = buf,*p2 = buf;
 4 #define nc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2) ? EOF :*p1++)
 5 inline int read() {
 6     int x=0,f=1;char ch=nc();for(;!isdigit(ch);ch=nc())if(ch=='-')f=-1;
 7     for (;isdigit(ch);ch=nc())x=x*10+ch-'0';return x*f;
 8 }
 9 
10 const int N = 2001000;
11 int mn[N<<2],mx[N<<2];
12 
13 #define max(a,b) a>b?a:b
14 #define min(a,b) a>b?b:a
15 #define Root 1,n,1
16 #define lson l,mid,rt<<1
17 #define rson mid+1,r,rt<<1|1
18 
19 inline void pushup(int rt) {
20     mn[rt] = min(mn[rt<<1],mn[rt<<1|1]);
21     mx[rt] = max(mx[rt<<1],mx[rt<<1|1]);
22 }
23 inline void pushdown(int rt) { // 使左右儿子的最小最大值 也在 根节点的最小最大值之间。 
24     if (mn[rt] > mx[rt<<1]) mn[rt<<1] = mx[rt<<1] = mn[rt]; 
25     else if (mn[rt] > mn[rt<<1]) mn[rt<<1] = mn[rt];
26     if (mn[rt] > mx[rt<<1|1]) mn[rt<<1|1] = mx[rt<<1|1] = mn[rt];
27     else if (mn[rt] > mn[rt<<1|1]) mn[rt<<1|1] = mn[rt];
28     
29     if (mx[rt] < mn[rt<<1]) mn[rt<<1] = mx[rt<<1] = mx[rt];
30     else if (mx[rt] < mx[rt<<1]) mx[rt<<1] = mx[rt];
31     if (mx[rt] < mn[rt<<1|1]) mn[rt<<1|1] = mx[rt<<1|1] = mx[rt];
32     else if (mx[rt] < mx[rt<<1|1]) mx[rt<<1|1] = mx[rt];    
33 }
34 void update1(int l,int r,int rt,int L,int R,int h) { // 提高 
35     if (L <= l && r <= R) {
36         mn[rt] = max(mn[rt],h); // 取max:高于h的不更新 
37         mx[rt] = max(mx[rt],h);
38         return ;
39     }
40     pushdown(rt);
41     int mid = (l + r) >> 1;
42     if (L <= mid) update1(lson,L,R,h);
43     if (R > mid)  update1(rson,L,R,h);
44     pushup(rt);
45 }
46 void update2(int l,int r,int rt,int L,int R,int h) { // 降低 
47     if (L <= l && r <= R) {
48         mn[rt] = min(mn[rt],h); // 取min:低于h的不更新 
49         mx[rt] = min(mx[rt],h);  
50         return ;
51     }
52     pushdown(rt);
53     int mid = (l + r) >> 1;
54     if (L <= mid) update2(lson,L,R,h); // -- update1
55     if (R > mid)  update2(rson,L,R,h);
56     pushup(rt);
57 }
58 void print(int l,int r,int rt) {
59     if (l == r) {
60         printf("%d\n",mn[rt]);
61         return; 
62     }
63     int mid = (l + r) >> 1;
64     pushdown(rt);
65     print(lson);print(rson);
66 }
67 int main() {
68     int n = read(),m = read();
69     while (m--) {
70         int opt = read(),l = read() + 1,r = read() + 1,h = read();
71         if (opt==1) update1(Root,l,r,h);
72         else update2(Root,l,r,h);
73     }
74     print(Root);
75     return 0; 
76 }

猜你喜欢

转载自www.cnblogs.com/mjtcn/p/9247154.html