前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >nyoj----522 Interval (简单树状数组)

nyoj----522 Interval (简单树状数组)

作者头像
Gxjun
发布2018-03-22 13:03:30
5030
发布2018-03-22 13:03:30
举报
文章被收录于专栏:mlml

Interval

时间限制:2000 ms  |  内存限制:65535 KB

难度:4

描述

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.样例输入

代码语言:javascript
复制
2
3 4
1 3
1 2
2 3
0
1
2
3
1 3
0 0
-1
0
1

样例输出

代码语言:javascript
复制
0
2
3
2
0
1
0

上传者ACM_赵铭浩代码:

代码语言:javascript
复制
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 #define maxn 200005   //整体平移100001个单位
 5 #define lowbit(x) ((x)&(-x))
 6 int aa[maxn+5];
 7 void ope(int x,int val)
 8 {
 9      x+=100001;
10     while(x<=maxn)
11     {
12         aa[x]+=val;
13         x+=lowbit(x);
14     }
15 }
16 long long getsum(int x)
17 {
18    long long ans=0;
19     while(x>0)
20     {
21         ans+=aa[x];
22         x-=lowbit(x);
23     }
24     return ans;
25 }
26 int main()
27 {
28     int test,nn,m,i,a,b;
29     scanf("%d",&test);
30     while(test--)
31     {
32       scanf("%d%d",&nn,&m);
33       memset(aa,0,sizeof(aa));
34       for(i=0;i<nn;i++)
35       {
36        scanf("%d%d",&a,&b);
37        ope(a,1);
38        ope(b+1,-1);
39       }
40       for(i=0;i<m;i++)
41       {
42        scanf("%d",&a);
43        a+=100001;
44        printf("%I64d\n",getsum(a));
45       }
46     }
47   return 0;
48 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014-04-18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Interval
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档