离散化+树状数组
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
#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; }