HDU 5101 Select --离散化+树状数组

题意:n 组,每组有一些值,求 在不同的两组中每组选一个使值的和大于k的方法数。

解法:n * Cnt[n] <= 1000*100 = 100000, 即最多10^5个人,所以枚举每个值x,求他的后面那些组中有多少数大于 k-x即可, 求有多少数大于k-x可以先求有多少数小于等于k-x,然后总数减一下即可。 可以用树状数组求。

先把所有数离散地存入一个树状数组中,然后每次枚举完一组的数后,将这组的数去掉。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define lll __int64
using namespace std;
#define N 101107

int c[N],num[1006][104],b[2*N],T[1007];

int lowbit(int x) { return x&-x; }
void modify(int x,int val)
{
    while(x <= N-10)
    {
        c[x] += val;
        x += lowbit(x);
    }
}

int getsum(int x)
{
    int res = 0;
    while(x > 0)
    {
        res += c[x];
        x -= lowbit(x);
    }
    return res;
}

int main()
{
    int t,n,k,m,i,j;
    cin>>t;
    while(t--)
    {
        scanf("%d%d",&n,&k);
        int cnt = 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++)
            {
                int id = 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++)
            {
                int id = lower_bound(b+1,b+cnt+1,num[i][j])-b;
                modify(id,-1);
            }
            for(j=1;j<=num[i][0];j++)
            {
                int now = 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;
}

时间: 2025-01-04 00:10:27

HDU 5101 Select --离散化+树状数组的相关文章

HDU 5862(离散化+树状数组)

Problem Counting Intersections 题目大意 给定n条水平或竖直的线段,统计所有线段的交点个数. (n<=100000) 解题分析 首先将线段离散化. 然后将所有线段按照横坐标的顺序,按照先插入再查找再删除的顺序进行操作. 对于横线 如果是左端点,则将其纵坐标加1,右端点则减1,对于竖线直接求和就行了. 参考程序 1 #include <map> 2 #include <set> 3 #include <stack> 4 #include

hdu 3030 Increasing Speed Limits (离散化+树状数组+DP思想)

Increasing Speed Limits Time Limit: 2000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 481    Accepted Submission(s): 245 Problem Description You were driving along a highway when you got caught by the road p

hdu 3015 Disharmony Trees (离散化+树状数组)

Disharmony Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 663    Accepted Submission(s): 307 Problem Description One day Sophia finds a very big square. There are n trees in the square. T

hdu 2227(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) Total Submission(s): 1466    Accepted Submission(s): 521 Problem Description

2018中国大学生程序设计竞赛 - 网络选拔赛 1010 YJJ&#39;s Salesman 【离散化+树状数组维护区间最大值】

题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6447 YJJ's Salesman Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 919    Accepted Submission(s): 290 Problem Description YJJ is a salesman who h

poj 2299 Ultra-QuickSort 离散化 + 树状数组

题目链接:http://poj.org/problem?id=2299 离散化 + 树状数组 教科书例题般的题目 #include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <cmath> #include <vector> #include <stack> #include <set> #include

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

CodeForces 540E - Infinite Inversions(离散化+树状数组)

花了近5个小时,改的乱七八糟,终于A了. 一个无限数列,1,2,3,4,...,n....,给n个数对<i,j>把数列的i,j两个元素做交换.求交换后数列的逆序对数. 很容易想到离散化+树状数组,但是发现那些没有交换的数也会产生逆序对数,但我没有算. 经明神提示, 把没有用到的数字段化成点.然后用树状数组算一下就好了. 然后我用一个数组记录每个点的长度.比如 <1,2><5,6>,1,2,3,4,5,6只有1,2,5,6用到了,那么离散化为1,2,3,4,5,f[1]=

HDU 3854 Glorious Array(树状数组)

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