HDU 4343 Interval Query (multiplication thought + greed)

Source: Internet
Author: User



Test instructions: given n (n<=100000) intervals (left closed right open) and M (m<=100000) queries [L, R], ask all the maximum number of 22 disjoint intervals within the [L, R] interval. ,

Idea: First greedy thought, to remove the large interval containing other intervals, this will certainly not affect the results.

Then, for all intervals, the left endpoint is sorted in ascending order, so that when all the intervals are not mutually inclusive, their right endpoints are incremented.

Then for each inquiry, must be from left to right to go as many intervals, this greedy easy to think of.

Discretization of data, recording the nearest distance from each point through the I interval, this step uses the idea of multiplication, because if a point order is found, then the time complexity and space complexity will not be able to bear.

In particular, using f[i][j] to record the nearest point through the 2^j interval from I, a recursive relationship can be derived

F[I][J] = f[f[i][j-1]][j-1];

So for each inquiry, we will ask QL,QR also discretization, just find out how many intervals from the QL so that it does not exceed the QR.

PS: Sleep in the middle of the night is really heartache, up the problem ...

#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <iostream > #include <algorithm> #include <vector> #include <map> #include <queue> #include <stack& Gt #include <string> #include <map> #include <set> #define EPS 1e-6 #define LL Long Long #define PII (pair<  int, int>)//#pragma comment (linker, "/stack:1024000000,1024000000") using namespace std; const int MAXN = 100000 + 100;const int INF = 0x3f3f3f3f;int N, m;struct Node {int L, R;bool operator < (const NODE&AMP ; A) Const {if (L = = A.L) return r > A.r;return l < a.l;}} NODE[MAXN]; int discret[2*maxn];int fa[2*maxn][20];int Solve (int l, int r) {int ans = 0;for (int i = n; I >= 0; i--) {int t = Fa[l] [I];//cout << t << endl;if (t = r) return ans+ (1<<i); else if (T < r) L = t, ans + = (1<<i); } return ans; int main () {//freopen ("Input.txt", "R", stdin), while (scanf ("%d%d", &n, &m) = = 2) {MemseT (FA, INF, sizeof (FA)); for (int i = 0; i < n; i++) {scanf ("%d%d", &discret[2*i], &discret[2*i+1]); node[i].l = di Scret[2*i], NODE[I].R = discret[2*i+1];}  Sort (discret, discret+2*n); int cnt = unique (Discret, discret+2*n)-discret;//cout << cnt << endl;for (int i = 0; I < n; i++) {node[i].l = Lower_bound (Discret, discret+cnt, node[i].l)-DISCRET;NODE[I].R = Lower_bound (Discret, discret+cnt, no DE[I].R)-discret;//cout << node[i].r << Endl;} Sort (node, node+n); int cnt2 = n-1;for (int i = cnt-1; I >= 0; i--) {//cout << node[i].l << endl;if (i = = node [CNT2].L] {fa[i][0] = min (NODE[CNT2].R, fa[i+1][0]); while (i = = NODE[CNT2].L) cnt2--;} else fa[i][0] = fa[node[cnt2+1].l][0];} for (int i = 1; i < n; i++) {for (int j = 0; J < CNT; J + +) if (fa[j][i-1]! = INF) Fa[j][i] = fa[fa[j][i-1]][i-1];} for (int i = 0; i < m; i++) {int L, R; scanf ("%d%d", &l, &r); l = Lower_bound (Discret, discret+cnt, L)-discret; R = Upper_bound (Discret, discret+cnt, R)-discret-1;//cout << L << "" << R << endl;printf ("%d\n", Solve (L, R));} cout << fa[0][0] << Endl;} return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 4343 Interval Query (multiplication thought + greed)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.