HDU 5125 Magic Ball DP+树状数组

由于只要找1~x 中的最大值,然后线段树又容易MLE,所以这里可以用树状数组搞。

#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);
			pos += lowbit(pos);
		}
	}

	int GetMax(int pos) {
		int ret = 0;
		while(pos >= 1) {
			ret = max(ret, val[pos]);
			pos -= 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) {
	return 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", &n, &m);
		numcnt = 0;
		for(int i = 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;
				prev_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;
}

  

时间: 2024-11-17 23:36:03

HDU 5125 Magic Ball DP+树状数组的相关文章

hdu 1556Color the ball (树状数组,更新区间,查询单点)

Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12566    Accepted Submission(s): 6294 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气

HDU 1556-Color the ball(树状数组-区间修改 单点查询)

Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 15491    Accepted Submission(s): 7731 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"

hdu4605 Magic Ball Game(树状数组)

题目链接:点这里!!!! 题意: 给定一棵N(N<=1e5)节点的树,每个节点要么有两个儿子(左右两个儿子),要么没有儿子,每个节点有一个权值w[i]. 一个权值为X球从根节点开始下落,每落到一个节点的时候, 1.如果X=W[i],或者没有儿子节点了,球停止下落. 2.如果X<W[i],球各有1/2的概率落到左右儿子节点. 3.如果X>W[i],球有1/8的概率落到左儿子,有7/8的概率落到右儿子. 给你q(q<=1e5)个询问,询问中包含v,x.问你权值为x的球从根节点(根节点是

HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences                                  Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)                                             

hdu 1556 Color the ball(树状数组)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色.但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,你能帮他算出每个气

树形DP+树状数组 HDU 5877 Weak Pair

1 //树形DP+树状数组 HDU 5877 Weak Pair 2 // 思路:用树状数组每次加k/a[i],每个节点ans+=Sum(a[i]) 表示每次加大于等于a[i]的值 3 // 这道题要离散化 4 5 #include <bits/stdc++.h> 6 using namespace std; 7 #define LL long long 8 typedef pair<int,int> pii; 9 const double inf = 12345678901234

HDU 6447 - YJJ&#39;s Salesman - [树状数组优化DP][2018CCPC网络选拔赛第10题]

Problem DescriptionYJJ is a salesman who has traveled through western country. YJJ is always on journey. Either is he at the destination, or on the way to destination.One day, he is going to travel from city A to southeastern city B. Let us assume th

hdu 3015 Disharmony Trees (离散化+树状数组)

Disharmony Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 663    Accepted Submission(s): 307 Problem Description One day Sophia finds a very big square. There are n trees in the square. T

HDU 2852 KiKi&#39;s K-Number (树状数组 &amp;&amp; 二分)

题意:给出对容器的总操作次数n, 接下来是这n个操作.这里对于一个容器提供三种操作, 分别是插入.删除和查找.输入0  e表示插入e.输入1  e表示删除e,若元素不存在输出No Elment!.输入2  e  k表示查找比e大且第k大的数, 若不存在则输出Not Find! 分析:这里考虑树状数组做的原因是在第三个操作的时候, 只要我们记录了元素的总数, 那通过求和操作, 便能够高效地知道到底有多少个数比现在求和的这个数要大, 例如 tot - sum(3)就能知道整个集合里面比3大的数到底有