Many of the code on the web is wrong, because the background data water, they can AC.
such as this set of data
10 3
P 0 9
R 0 5
R 6 9
The output should be 0 1 1
So it's wrong for some people to directly record whether the interval has been overwritten.
The correct method should be to record the minimum height of the interval (that is, the height closest to the initial position), and the longest left and right intervals corresponding to the minimum height.
Open a sum record the number of blocks of the minimum height of this interval, Min_v record the minimum height of the interval
Cover the sub-range of the interval that is pushed down as a lazy marker needs to be pressed several times
| Kinderriven |
C |
Accepted |
6776 |
170 |
C + + (g++ 4.7.2) |
3285 |
|
#include <cstdio> #include <cstring> #include <algorithm>using namespace std; #define Lson (pos< <1) #define RSON (pos<<1|1) const int MAXN = 20005;const int maxd = 55555;struct input{char op[5]; int x, y;} Input[maxn];int hash[maxd],cnt,ret;void Hash_init () {sort (hash,hash + cnt); CNT = unique (Hash,hash + cnt)-Hash; int t = CNT; for (int i = 1; i < T; i++) if (hash[i-1] + 1! = Hash[i]) hash[cnt++] = hash[i-1] + 1; Sort (Hash,hash + cnt);} int hash (int t) {return lower_bound (Hash,hash + cnt,t)-hash;} struct node{int l,r; int lx,rx,min_v,sum,cover; int mid () {return (L + R) >> 1; } int len () {return r-l + 1; }}node[maxd << 2];void Build (int l,int r,int pos) {node[pos].l = l; NODE[POS].R = R; NODE[POS].LX = Node[pos].rx = Node[pos].len (); Node[pos].min_v = 0; Node[pos].sum = 1; Node[pos].cover = 0; if (L = = r) return; int mid = Node[pos].mid (); Build (l,mid,lson); Build (mid + 1,r,rson);} void pushdown (int pos) {if (node[pos].cover) {node[lson].cover + = Node[pos].cover; Node[rson].cover + = Node[pos].cover; Node[lson].min_v + = Node[pos].cover; Node[rson].min_v + = Node[pos].cover; Node[pos].cover = 0; }}void pushup (int pos) {node[pos].min_v = min (Node[lson].min_v,node[rson].min_v); if (Node[lson].min_v = = Node[rson].min_v) {node[pos].lx = NODE[LSON].LX; Node[pos].rx = Node[rson].rx; if (NODE[POS].LX = = Node[lson].len ()) NODE[POS].LX + = NODE[RSON].LX; if (Node[pos].rx = = Node[rson].len ()) Node[pos].rx + = Node[lson].rx; Node[pos].sum = Node[lson].sum + node[rson].sum; if (Node[lson].rx && node[rson].lx) node[pos].sum--; } else if (Node[lson].min_v < Node[rson].min_v) {node[pos].lx = NODE[LSON].LX; Node[pos].rx = 0; Node[pos].sum = Node[lson].sum; } else{Node[pos].rx = Node[rson].rx; Node[pos].LX = 0; Node[pos].sum = Node[rson].sum; }}void Update (int l,int r,int Pos,int d) {if (L <= node[pos].l && NODE[POS].R <= r) {Node[pos].cove R + = D; Node[pos].min_v + = D; Return } pushdown (POS); int mid = Node[pos].mid (); if (l <= mid) update (L,R,LSON,D); if (R > Mid) update (L,R,RSON,D); Pushup (POS);} int main () {int t,case = 1; scanf ("%d", &t); while (t--) {int n,m; CNT = 0; ret = 0; scanf ("%d%d", &n,&m); hash[cnt++] = 0; hash[cnt++] = n-1; for (int i = 0; i < m; i++) {scanf ("%s%d%d", input[i].op,&input[i].x,&input[i].y); hash[cnt++] = input[i].x; hash[cnt++] = input[i].y; } hash_init (); Build (0,cnt-1,1); printf ("Case #%d:\n", case++); for (int i = 0; i < m; i++) {int L = hash (input[i].x), R = Hash (input[i].y), c,t = 0,ans; if (input[i].op[0] = = ' P ') c = 1; else C =-1; Update (L,R,1,C); if (Node[1].min_v = = 0) t = 1; Ans = t * node[1].sum; printf ("%d\n", ans); }} return 0;} /*110 3p 0 9r 0 5r 6 9*/
ZOJ 3324 Machine (segment tree interval merging)