Interval(南阳oj522)(树状数组)

2015-01-27 06:20:38 · 作者: · 浏览: 7

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.
样例输入
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
/*树状数组应用,统计数字在所给区间出现的次数。
TLE哭了,MAX大小:200002是AC,200005是TLE!!!
注意将负数转换到正值区间上,否则还是会TLE!!! 
*/
#include
       
        
#include
        
          #define MAX 200002 int a[MAX]; int lowbit(int N) { return N&(-N); } void asd(int i,int M) { while(i<=MAX) { a[i]+=M; i+=lowbit(i); } } int sum(int j) { int sum=0; while(j>0) { sum+=a[j]; j-=lowbit(j); } return sum; } int main() { int i,n,m,T,t,k,p; scanf("%d",&T); while(T--) { memset(a,0,sizeof(a)); scanf("%d%d",&m,&n); while(m--) { scanf("%d%d",&k,&t); asd(k+100001,1); asd(t+100002,-1); } while(n--) { scanf("%d",&p); printf("%d\n",sum(p+100001)); } } return 0; }
        
       


<script type="text/java script">
<script type="text/java script">BAIDU_CLB_fillSlot("771048");
点击复制链接 与好友分享! 回本站首页
<script> function copyToClipBoard(){ var clipBoardContent=document.title + '\r\n' + document.location; clipBoardContent+='\r\n'; window.clipboardData.setData("Text",clipBoardContent); alert("恭喜您!复制成功"); }
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"24"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];
您对本文章有什么意见或着疑问吗?请到 论坛讨论您的关注和建议是我们前行的参考和动力??
上一篇: leetcode Largest Rectangle in Histogram
下一篇: ListView中按钮监听器 设置 及 优化
相关文章
C/C++中多维数组的指针作为函数参数传
C/C++中数组和指针类型的关系的入门教
<script type="text/java script">BAIDU_CLB_fillSlot("182716");
<script type="text/java script">BAIDU_CLB_fillSlot("517916");
图文推荐
<iframe src="http://www.2cto.com/uapi.php?tid=353394&catid=339&title=SW50ZXJ2YWyjqMTP0fRvajUyMqOpo6jK99e0yv3X6aOp&forward=http://www.2cto.com/kf/201411/353394.html" width="100%" height="100%" id="comment_iframe" name="comment_iframe" frameborder="0" scrolling="no">
<script type="text/java script">BAIDU_CLB_fillSlot("771057");