Template prototype:
Solves the number of times that a scattered shard occurs on a known line segment. The idea is to overwrite a line segment with a long line and convert it into a line segment tree. Use the weight value to record the number of occurrences of each shard, and then perform the query. For code explanations, see annotations.
1 # include <bits/stdc ++. h> 2 using namespace STD; 3 4 const int maxn = 3e4 + 10; 5 Int N, M, L, R; // length N, number of line segments M 6 7 8 struct line {9 int left, right; // left: left boundary right: right boundary 10 int N; // The weight of the node 11} A [maxn]; 12 13 void buildt (int l, int R, int step) {14 A [STEP]. left = L; // create the left boundary of the current node and assign a value of 15 A [STEP]. right = r; // The right boundary of the current node is set to 16 A [STEP]. n = 0; // initialize the current node's weight 17 if (L = r) {return ;}// if it is a leaf node, do not perform the following recursive build operations 18 buildt (L, (L + r)/2, step * 2); // recursively create the left Node 19 buildt (L + r) /2 + 1, R, step * 2 + 1); // recursively create right node 20} 21 22 void DFS (INT step) {23 cout <step <"" // current node subscript 24 <A [STEP]. left <"" // node left boundary 25 <A [STEP]. right <"" // The right boundary of the node 26 <A [step]. n <Endl; // the weight of the node 27 if (a [STEP]. left = A [STEP]. right) return; // If the node is a leaf node, the current node is searched for 28 DFS (Step * 2). // The left node is searched for 29 DFS recursively (Step * 2 + 1 ); // recursive search for 30 return; 31} 32 33 void insert (int s, int T, int step) {34 if (S = A [STEP]. left & t = A [STEP]. right) {35 ++ A [STEP]. n; // If the Inserted Line Segment matches, the record of the Line Segment + 136 return; // if (a [STEP]. left = A [STEP]. right) // if the current line segment does not have a subnode, return 40 return; 41 42 int mid = (a [STEP]. left + A [STEP]. right)/2; // The binary thought establishes the median mid43 44 If (mid> = T) {// If the median is on the right of T, 45 insert (S, T, step * 2); // insert to the left son 46 47} else if (mid <s) {// if the value is in the left of s 48 insert (S, T, step * 2 + 1); // insert to right son 49 50} else {51 insert (S, mid, step * 2); // otherwise, the line segment is divided, place the left-end to the midpoint part into the left node 52 insert (Mid + 1, t, step * 2 + 1); // place the center-to-right part into the right node 53} 54 return; 55} 56 57 int main () {58 cout <maxn <Endl; 59 CIN> N> m; 60 buildt (0, N, 1 ); 61 // DFS (1); 62 for (INT I = 0; I <m; I ++) {63 CIN> L> r; 64 insert (L, r, 1); 65} 66 DFS (1); 67 return 0; 68}