hdu 5372 Segment Game(树状数组+离散化)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5372

题意:有两种操作,输入a b,①a==0,插入第i条线段[b,b+i],输出[b,b+i]内有多少条完全包含于[b,b+i]的线段②a==1,删除插入的第b条线段。

分析:由于插入的线段长度是递增的,那么就不存在包含[b,b+i]的线段。那么完全包含于[b,b+i]的线段的数目=右端点小于等于b+i的线段的数目-左端点小于b的线段的数目。由于输入的数比较大,离散化一下就行了。

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 4e5+8;
int L[maxn],R[maxn],lowbit[maxn],x[maxn],y[maxn],f[maxn*2],Kind,ax[maxn],ay[maxn];
void getlowbit()
{
	for(int i=1;i<maxn;i++)
		lowbit[i]=i&-i;
}
void update(int a[],int x,int v)
{
	for(int i=x;i<maxn;i+=lowbit[i])
		a[i]+=v;
}
int query(int a[],int x)
{
	int ret(0);
	for(int i=x;i>0;i-=lowbit[i])
		ret+=a[i];
	return ret;
}
int Find(int x)
{
	int down=0,up=Kind-1,mid;
	while(down<=up)
	{
		mid=(down+up)>>1;
		if(f[mid]>x)
			up=mid-1;
		else if(f[mid]<x)
			down=mid+1;
		else
			return mid+2;
	}
	return 0;
}
int main()
{
	getlowbit();
	int ncase=1,q,a,b,i,j;
	while(scanf("%d",&q)!=EOF)
	{
		int cnt=0,c=0,aq=0;
		for(i=1;i<=q;i++)
		{
			scanf("%d%d",&x[i],&y[i]);
			if(x[i]==0)
			{
				aq++;
				ax[aq]=y[i];
				ay[aq]=y[i]+aq;

				f[cnt++]=y[i];
				f[cnt++]=y[i]+aq;
			}
		}
		sort(f,f+cnt);
		Kind=unique(f,f+cnt)-f;
		memset(L,0,sizeof(L));
		memset(R,0,sizeof(R));
		printf("Case #%d:\n",ncase++);
		c=0;
		for(i=1;i<=q;i++)
		{
			if(x[i]==0) //add
			{
				c++;
				printf("%d\n",query(R,Find(y[i]+c))-query(L,Find(y[i])-1));
				update(L,Find(y[i]),1);
				update(R,Find(y[i]+c),1);
			}
			else
			{
				update(L,Find(ax[y[i]]),-1);
				update(R,Find(ay[y[i]]),-1);
			}
		}
	}
	return 0;
}
时间: 2024-11-02 02:10:16

hdu 5372 Segment Game(树状数组+离散化)的相关文章

HDOJ 5372 Segment Game 树状数组+离散化

因为这题的线段长度是递增的....所以: 题解:对于新插入的线段,查询有多少个线段左端点大于等于该线段的左端点. 再查询有多少个线段的右端点大于该线段右端点, 两者之差就是答案.用两个树状数组搞定.时间复杂度nlog Segment Game Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 975    Accepted Submiss

HDU 5372 Segment Game 树状数组

链接 Segment Game Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 273    Accepted Submission(s): 48 Problem Description Lillian is a clever girl so that she has lots of fans and often receives gi

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 2492 Ping pong (树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Ping pong Problem Description N(3<=N<=20000) ping pong players live along a west-east street(consider the street as a line segment). Each player has a unique skill rank. To improve their skill rank

hdu4325 树状数组+离散化

http://acm.hdu.edu.cn/showproblem.php?pid=4325 Problem Description As is known to all, the blooming time and duration varies between different kinds of flowers. Now there is a garden planted full of flowers. The gardener wants to know how many flower

HDU 1541 Stars (树状数组)

Problem Description Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given

HDU 3854 Glorious Array(树状数组)

题意:给一些结点,每个结点是黑色或白色,并有一个权值.定义两个结点之间的距离为两个结点之间结点的最小权值当两个结点异色时,否则距离为无穷大.给出两种操作,一种是将某个结点改变颜色,另一个操作是询问当前距离小于K的结点有多少对,K是一个定值. 思路:先求最初时候小于k的结点有多少对,然后每次改变颜色的时候,统计该点左侧和右侧各有多少同色和异色的结点(这一步使用树状数组),分别处理就行.另外需要预处理离某个结点最近的两个距离小于K的结点的位置. 代码写的略乱. #include<cstdio> #

HDU 3333 Turing Tree 树状数组 离线查询

题意: 给你一个数列,然后有n个查询,问你给定区间中不同数字的和是多少. 思路还是比较难想的,起码对于蒟蒻我来说. 将区间按照先右端点,后左端点从小到大排序之后,对于每个查询,我只要维护每个数字出现的最后一次就可以了(这个结论稍微想一下就可以证明是正确的). 然后就是简单的点更新,区间求和问题了- #include <cstdio> #include <cstring> #include <iostream> #include <map> #include

HDU 2689 Sort it (树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2689 Sort it Problem Description You want to processe a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. Then how many times it