Topic Portal
Test instructions: Give some time for flowers to blossom, ask how many flowers bloom at some time
Analysis: There are several approaches to this problem, the positive solution should be discretized coordinates after the end of the line tree to update and a single point of inquiry. There are sorting after the two points to find the question before the total number of flowers and total flowers fade number, the difference is the current number of flowers, put a diagram easy to understand:
There is a way to use the idea of a ruler, the optimization of the violence, the inquiry point after the sequencing and then scan again, flowers open +1, Flower Xie-1. Look at the code in detail.
Harvest: A lot of harvest: 1. The complexity can be reduced by two points of 2. The line count problem can be marked at endpoint 1 and-1 3. discretization + Line Tree finally will be:) (I hear the data is very water?) )
Code Listing 1: discretization + segment Tree
/************************************************* author:running_time* Created time:2015-8-25 8:55:54* File Nam E:f.cpp ************************************************/#include <cstdio> #include <algorithm># Include <iostream> #include <sstream> #include <cstring> #include <cmath> #include <string > #include <vector> #include <queue> #include <deque> #include <stack> #include <list># Include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime>using namespace std; #define Lson L, Mid, RT << 1#define Rson mid + 1, R, RT << 1 | 1typedef long ll;typedef pair<int, int> p;const int N = 1e5 + 10;const int INF = 0x3f3f3f3f;struct St{int sum[n <<2], col[n<<2];void push_up (int rt) {Sum[rt] = sum[rt<<1] + sum[rt<<1|1];} void Push_down (int rt, int len) {if (Col[rt]) {col[rt<<1] + col[rt];col[rt<<1|1] + = col[rt];sum[RT<<1] + = col[rt] * (Len-(Len >> 1)); sum[rt<<1|1] + col[rt] * (len >> 1); Col[rt] = 0;}} void build (int l, int r, int rt) {Col[rt] = 0;sum[rt] = 0;if (L = = r) return; int mid = (L + R) >> 1;build (Lson); Buil D (Rson);//push_up (RT);} void Updata (int ql, int qr, int l, int r, int rt) {if (QL <= l && r <= qr) {Sum[rt] + = (r-l + 1); Col[rt] + = 1;return;} Push_down (RT, r-l + 1); int mid = (L + R) >> 1;if (QL <= mid) Updata (QL, QR, Lson); if (Qr > Mid) updata (QL, QR, Rson);//push_up (RT);} int query (int ql, int qr, int l, int r, int rt) {if (QL <= l && r <= qr) return sum[rt];p Ush_down (RT, R-l + 1) int mid = (L + R) >> 1;if (QL <= mid) return query (QL, QR, Lson), if (QR > Mid) return query (QL, QR, Rson); }}st;int X1[n], x2[n], q[n];int x[n*3];int my_binary_search (int l, int r, int key) {while (L <= r) {int mid = (L + R) ;> 1;if (X[mid] = key) return mid;if (X[mid] < key) L = mid + 1;elser = mid-1;} Return -1;} int main (void) {int T, cas = 0;scanf ("%d", &t), while (t--) {int n, m;scanf ("%d%d", &n, &m); int tot = 0;for (in T I=1; i<=n; ++i) {scanf ("%d%d", &x1[i], &x2[i]); x[tot++] = X1[i]; x[tot++] = X2[i];} for (int i=1; i<=m; ++i) {scanf ("%d", &q[i]); x[tot++] = Q[i];} Sort (x, x + tot); int k = 1;for (int i=1; i<tot; ++i) {if (X[i]! = X[i-1]) x[k++] = X[i];} St.build (0, K, 1); for (int QL, QR, i=1; i<=n; ++i) {QL = Lower_bound (x, X+k, X1[i])-X;QR = Lower_bound (x, X+k, X2[i ])-x;//ql = My_binary_search (0, K-1, X1[i]),//qr = My_binary_search (0, K-1, X2[i]), St.updata (QL, QR, 0, K, 1);} printf ("Case #%d:\n", ++cas); for (int p, i=1; i<=m; ++i) {p = lower_bound (X, X+k, Q[i])-x;//p = My_binary_search (0, K-1, Q[i]);p rintf ("%d\n", St.query (p, p, 0, K, 1));}} return 0;}
Code Listing 2: two-point lookup
/************************************************* author:running_time* Created time:2015-8-25 8:55:54* File Nam E:f.cpp ************************************************/#include <cstdio> #include <algorithm># Include <iostream> #include <sstream> #include <cstring> #include <cmath> #include <string > #include <vector> #include <queue> #include <deque> #include <stack> #include <list># Include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime>using namespace std; #define Lson L, Mid, RT << 1#define Rson mid + 1, R, RT << 1 | 1typedef long ll;const int n = 1e5 + 10;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;int x1[n], X2[n];int N, M; int main (void) {int T, cas = 0;scanf ("%d", &t), while (t--) {scanf ("%d%d", &n, &m); for (int i=1; i<=n; + +i) {scanf ("%d%d", &x1[i], &x2[i]);} Sort (x1+1, x1+1+n); sort (x2+1, x2+1+n);p rintf("Case #%d:\n", ++cas); for (int i=1; i<=m; ++i) {int p;scanf ("%d", &p);p rintf ("%d\n", Lower_bound (X1+1, X1+1+n, p )-(x1 + 1)-(Lower_bound (x2+1, X2+1+n, p)-(x2 + 1));}} return 0;}
Code Listing 3: Ruler extraction
#include <cstdio> #include <algorithm> #include <cstring>using namespace Std;typedef long ll; typedef pair<int, int> p;const int N = 1e5 + 10;const int INF = 0x3f3f3f3f; P P[n], q[n];int ans[n];int N, m;int main (void) {int T, cas = 0;scanf ("%d", &t), while (t--) {scanf ("%d%d", &n , &m); int tot = 0;for (int A, b, i=1; i<=n; ++i) {scanf ("%d%d", &a, &b);p [++tot] = P (A, 1);p [++tot] = p (b + 1,-1);} Sort (p+1, P+1+tot); for (int i=1; i<=m; ++i) {int x;scanf ("%d", &x); Q[i] = P (x, i);} Sort (q+1, q+1+m);p rintf ("Case #%d:\n", ++cas); for (int j=1, cnt=0, I=1; i<=m; ++i) {while (J <= tot && P[j] . First <= Q[i].first) {cnt + = p[j].second;++j;} Ans[q[i].second] = cnt;} for (int i=1; i<=m; ++i) printf ("%d\n", Ans[i]);} return 0;}
discretization + segment Tree/Two-point lookup/ruler Hdoj 4325 Flowers