Interval (樹狀數組)

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


題解:樹狀數組……




#include<bits/stdc++.h>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<string>
#include<math.h>
#include<map>
#include<queue>
#include<stack>
#define INF 0x3f3f3f3f
#define ll long long
#define For(i,a,b) for(int i=a;i<b;i++)
#define sf(a)  scanf("%d",&a)
#define sfs(a)  scanf("%s",a)
#define sff(a,b)  scanf("%d%d",&a,&b)
#define sfff(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define pf(a) printf("%d\n",a)
#define P()  printf("\n")
#define mem(a,b) memset(a,b,sizeof(a))
#define Max 200002
using namespace std;
int tree[200002];
int lowbit(int x)
{
    return x&(-x);
}
void add(int x,int y)
{
    while(x<=Max)
    {
        tree[x]+=y;
        x+=lowbit(x);
    }
}
int sum(int x)
{
    int s=0;
    while(x)
    {
        s+=tree[x];
        x-=lowbit(x);
    }
    return s;
}
int main()
{
    int t,n,m,a,b,x;
    sf(t);
    while(t--)
    {
        mem(tree,0);
        sff(n,m);
        For(i,0,n)
        {
            sff(a,b);
            add(a+100001,1);
            add(b+100002,-1);
        }
        For(i,0,m)
        {
            sf(x);
            pf(sum(x+100001));
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章