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<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));
}
}
}