HDOJ 5101 Select 树状数组
2014-12-05 10:13
155 查看
离散化+树状数组
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. (T≤20)
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 ( 0≤n≤1000 ),
k( 0≤k<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( 0≤m≤100 ),
v[i]( 0≤v[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
BestCoder Round #17
#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;
}
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. (T≤20)
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 ( 0≤n≤1000 ),
k( 0≤k<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( 0≤m≤100 ),
v[i]( 0≤v[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
BestCoder Round #17
#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;
}
相关文章推荐
- HDU 5101 Select --离散化+树状数组
- hdu 5101 Select(树状数组)
- HDOJ 5101 Select(upper_bound的运用)
- hdoj 3333 Turing Tree 【树状数组】
- 线段树(单点更新)/树状数组 HDOJ 1166 敌兵布阵
- HDOJ 1556 Color the ball(树状数组 & 线段树)
- HDOJ 11166 敌兵布阵(树状数组)
- HDOJ-1556(线段树||树状数组,区间更新+点查询)
- HDOJ 题目1166 敌兵布阵(树状数组单点跟新)
- hdoj 1394 Minimum Inversion Number【树状数组求逆序数--逆序数的性质】
- hdoj 1394 Minimum Inversion Number 线段树||树状数组||分治
- HDOJ 题目4046 Panda(树状数组)
- HDOJ 1156 - Color the ball 水题复习线段树..线段树与树状数组的对比..
- 【树状数组】 HDOJ 4638 Group
- hdoj 1394 Minimum Inversion Number(树状数组求逆序对)
- HDOJ 敌兵布阵 1166(树状数组)
- [HDOJ 4918] Query on the subtree [树的分治+树状数组]
- hdoj 1166 敌兵布阵 【线段树&&二分树】【树状数组】
- 【树状数组】HDOJ 2492 Ping pong
- 【BestCoder】 HDOJ 5101 Select