• hdoj_4417Super Mario


    Super Mario

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 1292    Accepted Submission(s): 636


    Problem Description
    Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory. Now the poor princess is in trouble again and Mario needs to save his lover. We regard the road to the boss’s castle as a line (the length is n), on every integer point i there is a brick on height hi. Now the question is how many bricks in [L, R] Mario can hit if the maximal height he can jump is H.
     

    Input
    The first line follows an integer T, the number of test data.
    For each test data:
    The first line contains two integers n, m (1 <= n <=10^5, 1 <= m <= 10^5), n is the length of the road, m is the number of queries.
    Next line contains n integers, the height of each brick, the range is [0, 1000000000].
    Next m lines, each line contains three integers L, R,H.( 0 <= L <= R < n 0 <= H <= 1000000000.)
     

    Output
    For each case, output "Case X: " (X is the case number starting from 1) followed by m lines, each line contains an integer. The ith integer is the number of bricks Mario can hit for the ith query.
     

    Sample Input
    1 10 10 0 5 2 7 5 4 3 8 7 7 2 8 6 3 5 0 1 3 1 1 9 4 0 1 0 3 5 5 5 5 1 4 6 3 1 5 7 5 7 3
     

    Sample Output
    Case 1: 4 0 0 3 1 2 0 1 5 1

    超时代码,应该加个二分的。回宿舍补上

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    using namespace std;
    #pragma warning(disable : 4996)
    const int MAXN = 100010;
    #define mid ((l + r) >> 1)
    int tree[20][MAXN];//记录第i层元素序列  
    int sum[20][MAXN];//记录第i层的1~j划分到左子树的元素个数(包括j)
    int as[MAXN];
    //以下为查找区间第k小划分树
    void build(int p,int l,int r)
    {
    	int lm = 0, i, ls = l, rs = mid + 1;//lm表示应被放入左子树且与中位数相等的数有多少个,ls为左子树的起始位置,rs为右子树的起始位置
    	for(i = mid; i >= l; i--) //求lm
    	{
    		if(as[i] == as[mid])
    		{
    			lm++;
    		}
    		else
    		{
    			break;
    		}
    	}
    	for(i = l; i <= r; i++)
    	{
    		if(i == l)//这里要特殊讨论
    		{
    			sum[p][i] = 0;
    		}
    		else
    		{
    			sum[p][i] = sum[p][i-1];
    		}
    		if(tree[p][i] == as[mid])//若与中位数相等则判断是否应该被放入左子树
    		{
    			if(lm != 0)
    			{
    				lm--;
    				sum[p][i]++;
    				tree[p+1][ls++] = tree[p][i];
    			}
    			else
    			{
    				tree[p+1][rs++] = tree[p][i];
    			}
    		}
    		else if(tree[p][i] < as[mid])//查找区间第K大即为>
    		{
    			sum[p][i]++;
    			tree[p+1][ls++] = tree[p][i];
    		}
    		else
    		{
    			tree[p+1][rs++] = tree[p][i];
    		}
    	}
    	if(l == r)
    	{
    		return;
    	}
    	build(p + 1, l, mid);
    	build(p + 1, mid + 1, r);
    }
    int query(int p, int l, int r, int ql, int qr, int k)
    {
    	int s, ss;//s表示l到ql-1的区间内放入左子树的个数,ss表示区间[ql,qr]被放入左子树的个数
    	if(l == r)//找到所求的数
    	{
    		return tree[p][l];
    	}
    	if(ql == l)
    	{
    		s = 0, ss = sum[p][qr];
    	}
    	else
    	{
    		s = sum[p][ql-1], ss = sum[p][qr] - s;
    	}
    	if(k<=ss)//要找的数在左子树中
    	{
    		return query(p + 1, l, mid, l + s, l + sum[p][qr] - 1, k);
    	}
    	else//要找的数在右子树中
    	{
    		return query(p + 1, mid + 1, r, mid + 1 - l + ql - s, mid + 1 - l + qr - sum[p][qr], k - ss);
    	}
    }
    
    int main()
    {
    	freopen("in.txt", "r", stdin);
    	int t, n, m, ans, x, y, z;
    	scanf("%d", &t);
    	for(int i = 1; i <= t; i++)
    	{
    		printf("Case %d:\n", i);
    		scanf("%d %d", &n, &m);
    		for(int j = 1; j <= n; j++)
    		{
    			scanf("%d", &as[j]);
    			tree[0][j] = as[j];
    		}
    		sort(as + 1, as + n + 1);
    		build(0, 1, n);
    		while (m--)
    		{
    			scanf("%d %d %d", &x, &y, &z);
    			x++;
    			y++;
    			ans = 0;
    			for (int j = 1; j <= y - x + 1; j++)
    			{
    				int temp = query(0, 1, n, x, y, j);
    				if(temp <= z)
    				{
    					ans++;
    				}
    				else
    				{
    					break;
    				}
    			}
    			printf("%d\n", ans);
    		}
    	}
    	return 0;
    }


    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    using namespace std;
    #pragma warning(disable : 4996)
    const int MAXN = 100010;
    #define mid ((l + r) >> 1)
    int tree[20][MAXN];//记录第i层元素序列  
    int sum[20][MAXN];//记录第i层的1~j划分到左子树的元素个数(包括j)
    int as[MAXN];
    //以下为查找区间第k小划分树
    void build(int p,int l,int r)
    {
    	int lm = 0, i, ls = l, rs = mid + 1;//lm表示应被放入左子树且与中位数相等的数有多少个,ls为左子树的起始位置,rs为右子树的起始位置
    	for(i = mid; i >= l; i--) //求lm
    	{
    		if(as[i] == as[mid])
    		{
    			lm++;
    		}
    		else
    		{
    			break;
    		}
    	}
    	for(i = l; i <= r; i++)
    	{
    		if(i == l)//这里要特殊讨论
    		{
    			sum[p][i] = 0;
    		}
    		else
    		{
    			sum[p][i] = sum[p][i-1];
    		}
    		if(tree[p][i] == as[mid])//若与中位数相等则判断是否应该被放入左子树
    		{
    			if(lm != 0)
    			{
    				lm--;
    				sum[p][i]++;
    				tree[p+1][ls++] = tree[p][i];
    			}
    			else
    			{
    				tree[p+1][rs++] = tree[p][i];
    			}
    		}
    		else if(tree[p][i] < as[mid])//查找区间第K大即为>
    		{
    			sum[p][i]++;
    			tree[p+1][ls++] = tree[p][i];
    		}
    		else
    		{
    			tree[p+1][rs++] = tree[p][i];
    		}
    	}
    	if(l == r)
    	{
    		return;
    	}
    	build(p + 1, l, mid);
    	build(p + 1, mid + 1, r);
    }
    int query(int p, int l, int r, int ql, int qr, int k)
    {
    	int s, ss;//s表示l到ql-1的区间内放入左子树的个数,ss表示区间[ql,qr]被放入左子树的个数
    	if(l == r)//找到所求的数
    	{
    		return tree[p][l];
    	}
    	if(ql == l)
    	{
    		s = 0, ss = sum[p][qr];
    	}
    	else
    	{
    		s = sum[p][ql-1], ss = sum[p][qr] - s;
    	}
    	if(k<=ss)//要找的数在左子树中
    	{
    		return query(p + 1, l, mid, l + s, l + sum[p][qr] - 1, k);
    	}
    	else//要找的数在右子树中
    	{
    		return query(p + 1, mid + 1, r, mid + 1 - l + ql - s, mid + 1 - l + qr - sum[p][qr], k - ss);
    	}
    }
    
    int main()
    {
    	freopen("in.txt", "r", stdin);
    	int t, n, m, ans, x, y, z, left, right, Mid, temp;
    	scanf("%d", &t);
    	for(int i = 1; i <= t; i++)
    	{
    		printf("Case %d:\n", i);
    		scanf("%d %d", &n, &m);
    		for(int j = 1; j <= n; j++)
    		{
    			scanf("%d", &as[j]);
    			tree[0][j] = as[j];
    		}
    		sort(as + 1, as + n + 1);
    		build(0, 1, n);
    		while (m--)
    		{
    			scanf("%d %d %d", &x, &y, &z);
    			x++;
    			y++;
    			ans = 0;
    			left = 1, right = y - x + 1;//二分查找
    			if(query(0, 1, n, x, y, right) <= z)
    			{
    				printf("%d\n", right);
    				continue;
    			}
    			else if(query(0, 1, n, x, y, left) > z)
    			{
    				printf("0\n");
    				continue;
    			}
    			while (left <= right)
    			{
    				Mid = (right + left) / 2;
    				temp = query(0, 1, n, x, y, Mid);
    				if(temp > z)
    				{
    					right = Mid - 1;
    				}
    				else
    				{
    					left = Mid + 1;
    				}
    			}
    			ans = right;
    			for (int i = Mid + 1; i <= y - x + 1; i++)
    			{
    				temp = query(0, 1, n, x, y, i);
    				if(temp <= z)
    				{
    					ans++;
    				}
    				else
    				{
    					break;
    				}
    			}
    			printf("%d\n", ans);
    		}	
    	}
    	return 0;
    }


  • 相关阅读:
    javascript数据结构
    uni-app — 一套前端开发跨平台应用的终极解决方案
    从函数式编程到Ramda函数库(二)
    从函数式编程到Ramda函数库(一)
    node.js爬取数据并定时发送HTML邮件
    vue cli3.0 结合echarts3.0和地图的使用方法
    vue加载优化策略
    C#时间格式化
    wpf 调用线程无法访问此对象,因为另一个线程拥有该对象。
    使用oracle数据库开发,异常总结
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5835012.html
Copyright © 2020-2023  润新知