码迷,mamicode.com
首页 > 编程语言 > 详细

HDOJ 5101 Select 树状数组

时间:2014-12-05 10:54:00      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   ar   color   os   sp   


离散化+树状数组

Select

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1149    Accepted Submission(s): 329


Problem Description
One day, Dudu, the most clever boy, heard of ACM/ICPC, which is a very interesting game. He wants to take part in the game. But as we all know, you can‘t get good result without teammates.
So, he needs to select two classmates as his teammates. 
In this game, the IQ is very important, if you have low IQ you will WanTuo. Dudu‘s IQ is a given number k. We use an integer v[i] to represent the IQ of the ith classmate. 
The sum of new two teammates‘ IQ must more than Dudu‘s IQ.
For some reason, Dudu don‘t want the two teammates comes from the same class.
Now, give you the status of classes, can you tell Dudu how many ways there are.
 

Input
There is a number T shows there are T test cases below. (T20)
For each test case , the first line contains two integers, n and k, which means the number of class and the IQ of Dudu. n ( 0n1000 ), k( 0k<231 ).
Then, there are n classes below, for each class, the first line contains an integer m, which means the number of the classmates in this class, and for next m lines, each line contains an integer v[i], which means there is a person whose iq is v[i] in this class. m( 0m100 ), v[i]( 0v[i]<231 )
 

Output
For each test case, output a single integer.
 

Sample Input
1 3 1 1 2 1 2 2 1 1
 

Sample Output
5
 

Source
 


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long int LL;
const int maxn=100100;

int n,sum;
int tree[maxn];
int cnt[1111],cnum[1111][111];
LL b[maxn];
int len;

int lowbit(int x)
{
	return x&(-x);
}

void Add(int p)
{
	for(int i=p;i<maxn;i+=lowbit(i))
		tree[i]++;
}

int Sum(int p)
{
	int ret=0;
	for(int i=p;i;i-=lowbit(i))
		ret+=tree[i];
	return ret;
}

void init()
{
	len=0; memset(tree,0,sizeof(tree));
}

int main()
{
	int T_T;
	scanf("%d",&T_T);
	while(T_T--)
	{
		scanf("%d%d",&n,&sum);
		sum++;
		init();
		for(int i=0;i<n;i++)
		{
			scanf("%d",cnt+i);
			for(int j=0;j<cnt[i];j++)
			{
				scanf("%d",&cnum[i][j]);
				b[len++]=cnum[i][j];
			}
		}

		b[len++]=(1LL<<60);
		sort(b,b+len);
		len=unique(b,b+len)-b;

		LL ans=0,nb=0;

		/// add first line 
		for(int i=0;i<cnt[0];i++)
		{
			int k=lower_bound(b,b+len,cnum[0][i])-b+1;
			nb++;
			Add(k);
		}

		/// add other line
		for(int i=1;i<n;i++)
		{
			/// get ans
			for(int j=0;j<cnt[i];j++)
			{
				int m=sum-cnum[i][j];
				int k=lower_bound(b,b+len,m)-b;
				if(k==len-1) continue;
				ans+=nb-Sum(k);
			}
			/// add this line
			for(int j=0;j<cnt[i];j++)
			{
				int k=lower_bound(b,b+len,cnum[i][j])-b+1;
				nb++;
				Add(k);
			}
		}

		cout<<ans<<endl;
	}
	return 0;
}



HDOJ 5101 Select 树状数组

标签:des   style   blog   http   io   ar   color   os   sp   

原文地址:http://blog.csdn.net/ck_boss/article/details/41744051

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!