zoukankan      html  css  js  c++  java
  • nyist 522 Interval

    描述
    There are n(1 <= n <= 100000) intervals [ai, bi] and m(1 <= m <= 100000) queries, -100000 <= ai <= bi <= 100000 are integers.
    Each query contains an integer xi(-100000 <= x <= 100000). For each query, you should answer how many intervals convers xi.
    输入
    The first line of input is the number of test case.
    For each test case,
    two integers n m on the first line,
    then n lines, each line contains two integers ai, bi;
    then m lines, each line contains an integer xi.
    输出
    m lines, each line an integer, the number of intervals that covers xi.
    样例输入
    2
    3 4
    1 3
    1 2
    2 3
    0
    1
    2
    3
    1 3
    0 0
    -1
    0
    1
    样例输出
    0
    2
    3
    2
    0
    1
    0

    这个题是一个树桩数组题 但是和常规不同的就是这里边有负数
    把负数转换为正数来处理,
    #include <stdio.h>
    #include <string.h>
    
    #define N 100000
    
    int c[N*4];
    
    int bity(int x)
    {
        return x&(-x);
    }
    
    void add(int k,int num)
    {
    
        while(k>0)
        {
            c[k] += num;
            k -= bity(k);
        }
    
    }
    
    int get_sum(int x,int n)
    {
        int sum = 0;
        /*if(x ==0)
         return sum;*/
        // else
         {
        while(x<=n)
        {
           sum += c[x];
           x += bity(x);
        }
        return sum;
         }
    }
    
    int main()
    {
        //printf("%d\n",bity(-1));
        int t;
        scanf("%d",&t);
        while(t--)
        {
               int n,m;
               scanf("%d%d",&n,&m);
               memset(c,0,sizeof(c));
               int i = 0;
               int a = 0,b = 0;
    
               for(i = 0; i < n; i++)
                     {
                          scanf("%d%d",&a,&b);
                          add(b+N+10,1);
                          add(a+N+10-1,-1);
                     }
               while(m--)
               {
                  int x;
                  scanf("%d",&x);
                  printf("%d\n",get_sum(x+N+10,n+N*2+10));
               }
    
        }
        return 0;
    }
    
  • 相关阅读:
    shell数组(产生不同的随机数)
    统计服务连接状况
    子网掩码与子网划分
    oracle 12g sqlplus安装
    MySQL的备份和还原
    mysql日志
    mysql用户和权限管理
    mysql show
    CentOS Linux解决Device eth0 does not seem to be present
    mysqldump命令详解(转载)
  • 原文地址:https://www.cnblogs.com/yyroom/p/2970499.html
Copyright © 2011-2022 走看看