HDU 5125 Magic Ball dp+ Tree Array

Source: Internet
Author: User

As long as the maximum value in the 1~x is found, then the segment tree is easily mle, so it can be used in a tree-like array.

#include <cstdio> #include <cstring> #include <algorithm> #include <map> #include <set># Include <bitset> #include <queue> #include <stack> #include <string> #include <iostream># Include <cmath> #include <climits>using namespace std;const int maxn = 1005;class BIT {int VAL[MAXN << 2] , N;inline int lowbit (int x) {return x & (-X);} Public:void init (int __n) {n = __n;memset (val, 0, sizeof (val));} void ADDV (int pos, int v) {while (pos <= n) {Val[pos] = max (Val[pos], v);p OS + = Lowbit (pos);}} int getmax (int pos) {int ret = 0;while (pos >= 1) {ret = max (ret, val[pos]);p os-= Lowbit (POS);} return ret;}}; BIT Bit[maxn];int A[MAXN], B[MAXN], F[maxn][maxn][2];int N, M, NUM[MAXN << 2], numcnt;inline int GetID (int Val) {RET Urn lower_bound (num, num + numcnt, Val)-num + 1;} int main () {int T; scanf ("%d", &t), for (int kase = 1; kase <= T; kase++) {memset (f, 0, sizeof (f)); scanf ("%d%d", &amp n, &m); numcnt = 0;for (inti = 1; I <= N; i++) {scanf ("%d%d", &a[i], &b[i]); num[numcnt++] = a[i];num[numcnt++] = B[i];}  Sort (num, num + numcnt), numcnt = unique (num, num + numcnt)-num;for (int i = 1; I <= n; i++) {A[i] = GetID (A[i]); B[i] = GetID (B[i]);} for (int i = 0; I <= m; i++) Bit[i].init (numcnt), int ans = 0;for (int i = 1; I <= n; i++) {for (int j = m; j >= 0;- -J) {int prev_max = Bit[j]. Getmax (A[i]-1); F[i][j][0] = Prev_max + 1;BIT[J].ADDV (A[i], f[i][j][0]); ans = max (ans, f[i][j][0]); if (j = = 0) continue;pr Ev_max = Bit[j-1]. Getmax (B[i]-1); f[i][j][1] = Prev_max + 1;BIT[J].ADDV (B[i], f[i][j][1]); ans = max (ans, f[i][j][1]);}} printf ("%d\n", ans);} return 0;}

  

HDU 5125 Magic Ball dp+ Tree Array

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.