HDU 5101 Select--discretization + Tree Array

Source: Internet
Author: User

Test instructions: n groups, each group has a number of values, for each group in different two groups to select a value and greater than K method.

Solution: N * cnt[n] <= 1000*100 = 100000, that is, up to 10^5 individuals, so enumerate each value x, ask him how many of those groups are greater than k-x, how many more than k-x can first ask how many of the number is less than or equal to K-x, and then a total reduction can be. You can use a tree-like array.

All the numbers are stored in a tree array, and then each time the number of a group is enumerated, the number of the group is removed.

Code:

#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>#defineLLL __int64using namespacestd;#defineN 101107intc[n],num[1006][104],b[2*n],t[1007];intLowbit (intx) {returnx&-x;}voidModifyintXintval) {     while(x <= NTen) {C[x]+=Val; X+=lowbit (x); }}intGetsum (intx) {    intres =0;  while(X >0) {res+=C[x]; X-=lowbit (x); }    returnRes;}intMain () {intt,n,k,m,i,j; CIN>>T;  while(t--) {scanf ("%d%d",&n,&k); intCNT =0;  for(i=1; i<=n;i++) {scanf ("%d", &num[i][0]);  for(j=1; j<=num[i][0];j++) scanf ("%d", &num[i][j]), b[++cnt] =Num[i][j]; Sort (Num[i]+1, num[i]+num[i][0]+1); } t[n+1] =0;  for(i=n;i>=1; i--) T[i]= t[i+1] + num[i][0]; Sort (b+1, b+cnt+1); Memset (c,0,sizeof(c));  for(i=1; i<=n;i++)        {             for(j=1; j<=num[i][0];j++)            {                intid = lower_bound (b +1, b+cnt+1, Num[i][j])-b; Modify (ID,1); }} lll sum=0;  for(i=1; i<=n;i++)        {             for(j=1; j<=num[i][0];j++)            {                intid = lower_bound (b +1, b+cnt+1, Num[i][j])-b; Modify (ID,-1); }             for(j=1; j<=num[i][0];j++)            {                intnow =Num[i][j],id; if(K-now <0) id =0; Else{ID= Lower_bound (b +1, b+cnt+1, K-now)-b; if(B[id]! = K-now) id--; } Sum+ = t[i+1]-getsum (ID); }} cout<<sum<<Endl; }    return 0;}
View Code

HDU 5101 Select--discretization + Tree Array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.