CodeForces - 915E dynamic prescription segment tree

 

topic

N-lit night lamp, numbered from 1 to n.

Now there are two modes of operation, as follows:

  • Operation 1, to turn off the reference [l, r] interval lamp
  • 2 operation, open label [l, r] interval lamp

Q interrogation times below, each query performed wherein an operation query format, l, r, k, k type to perform operations. For each asked to answer the current number of open lamp.

Input

A single set of inputs, a first line contains an integer n, a second row an integer q (1≤n≤10 ^ 9,1≤q≤3 · 10 ^ 5)

The next row 3 of each row represents an integer of query q, l, r, k (1 ≤ l ≤ r ≤ n, 1 ≤ k ≤ 2).

Output

For each asked to answer a integer per line represents the answer.

Example

Input
4
6
1 2 1
3 4 1
2 3 2
1 3 2
2 4 1
1 4 2
Output
2
0
2
3
1
4

 

answer:

Because the tree line to open space four times. But the face of huge data we maxn << 2 open space is certainly open the overflow.

At this time we should use dynamic prescription segment tree to save the space. (Or discrete)

 

Dynamic open point that is used when the node is only space allocated to it, or not allocated, on such questions as the size of n is 1e9, then open the static is certainly room for fried ,,, but you find him 3e5 also asked a range, visible range used in a minority, so whenever we give him to use a range of distribution space

 

Code:

 1 /*
 2 动态开点线段树
 3 */
 4 #include<stdio.h>
 5 #include<string.h>
 6 #include<iostream>
 7 #include<algorithm>
 8 #include<queue>
 9 #include<vector>
10 using namespace std;
11 const int maxn=3e5+10;
12 const int INF=0x3f3f3f3f;
13 typedef long long ll;
14 struct Node
15 {
16      int L, R & lt, SUM, the lazy;
 . 17      the Node ()
 18 is      {
 . 19          L = 0 , R & lt = 0 , SUM = 0 , the lazy = 0 ;
 20 is      }
 21 is } Node [MAXN * 50 ];
 22 is  int CNT = . 1 ;   // number of points to open 
23 is  void pushdown ( int L, int R & lt, int K) // K is the parent node number in the array 
24  {
 25      int MID = (L + R & lt) >> . 1 ;
 26 is      IF(l!=r)
27     {
28         if(!node[k].l) node[k].l=++cnt;
29         if(!node[k].r) node[k].r=++cnt;
30 
31         if(node[k].lazy==2)
32         {
33             node[node[k].l].sum=node[node[k].r].sum=0;
34         }
35         else
36         {
37             node[node[k].l].sum=mid-l+1;
38             node[node[k].r].sum=r-mid;
39         }
40         Node [Node [K] .L] = .lazy Node [K] .lazy;
 41 is          Node [Node [K] .r] = .lazy Node [K] .lazy;
 42 is      }
 43 is      Node [K] .lazy = 0 ;
 44  }
 45  // to segment tree is inserted inside an interval [L, R & lt] 
46 is  void the insert ( int L, int R & lt, int & K, int L, int R & lt, int P)
 47  {
 48      IF (K!) K = cnt ++;   // this it is equivalent to k we want to node [k] this node operates
 49      // the node [1] is the root node, because this is a dynamic we open point, so there is no point nodes are open The initial value of 0 
50      IF(L> = R & lt && L <= R & lt)   // if there is no need to satisfy this condition pushdown, because we do not need the following nodes can be obtained [l, r] in the answer section 
51 is      {
 52 is          IF (P == 2 ) Node [K] .sum = 0 ;
 53 is          the else Node [K] = R & lt .sum-L + . 1 ;
 54 is          Node [K] .lazy = P;
 55          return ;
 56 is      }
 57 is      // If not the above determination, then we will enter about recursive child node k, this time around you have to ensure that sub-points have been modified well
 58      // ie, lazy lazy is labeled 0 
59      IF (the node [k] .lazy) pushdown (L, R & lt, K);
 60      int MID = (L + R & lt) >> . 1 ;
 61 is      IF (MID> =L) Insert(l,mid,node[k].l,L,R,p);
62     if (mid<R) Insert(mid+1,r,node[k].r,L,R,p);
63     node[k].sum=node[node[k].l].sum+node[node[k].r].sum;
64 }
65 int main()
66 {
67     //printf("%d %d\n",node[1].l,node[1].r);
68     int n,q;
69     scanf("%d %d",&n,&q);
70     int k=1;
71     for (int i=1; i<=q; i++)
72     {
73         int l,r,p;
74         scanf("%d %d %d",&l,&r,&p);
75         Insert(1,n,k,l,r,p);
76         printf("%d\n",n-node[1].sum);
77     }
78     return 0;
79 }

 

Guess you like

Origin www.cnblogs.com/kongbursi-2292702937/p/12621431.html