NYOJ-522 Interval
生活随笔
收集整理的這篇文章主要介紹了
NYOJ-522 Interval
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Interval
時間限制:2000?ms ?|? 內存限制:65535?KB 難度:4 描述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.
注意寫樹狀數組時候把區間都擴大到大于0的區間上,不能等于0.否則會一直超時,代碼如下:
?
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 5 using namespace std; 6 7 int num[200005]; 8 9 int lowbit(int i) 10 { 11 return i & (-i); 12 } 13 14 void update(int i, int add) 15 { 16 while(i <= 200001) 17 { 18 num[i] += add; 19 i += lowbit(i); 20 } 21 } 22 23 int getsum(int x) 24 { 25 int ans = 0; 26 while(x > 0) 27 { 28 ans += num[x]; 29 x -= lowbit(x); 30 } 31 return ans; 32 } 33 34 int main() 35 { 36 int T, m, n, s, t, x; 37 scanf("%d", &T); 38 while(T--) 39 { 40 memset(num, 0, sizeof(num)); 41 scanf("%d%d", &m, &n); 42 while(m--) 43 { 44 scanf("%d%d", &s, &t); 45 update(s+100001, 1); 46 update(t+100001+1, -1); 47 } 48 while(n--) 49 { 50 scanf("%d", &x); 51 printf("%d\n", getsum(x+100001)); 52 } 53 } 54 return 0; 55 }?
?
?
?
?總結
以上是生活随笔為你收集整理的NYOJ-522 Interval的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎样修改安卓位置服务器,修改安卓定位服务
- 下一篇: 为自己尝试写点东西吧,程序员们!(转)