Interval (树状数组)
2017-04-19 18:56
148 查看
Interval
时间限制:2000 ms | 内存限制:65535 KB[align=center]难度:4[/align]
描述
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));
}
}
}
相关文章推荐
- 2015合肥网络赛 HDU 5489 Removed Interval LIS+线段树(树状数组)
- 2015合肥网络赛 HDU 5489 Removed Interval LIS+线段树(树状数组)
- NYOJ-522 Interval【树状数组】
- ZSTU 3113 - Interval Challenge (树状数组 + 离散化)
- 树状数组(interval)
- NYOJ 522 Interval (树状数组)
- UVALive 6139 Interval Product(树状数组)
- nyoj 树状数组 522Interval 116士兵杀敌(二)
- nyoj----522 Interval (简单树状数组)
- hdu 5489 Removed Interval 2015合肥网络赛 树状数组 dp 离散化/dp
- Cogs 1008. 贪婪大陆(树状数组)
- HDU5542(树状数组优化DP)
- 树状数组的区间修改和区间查询模板
- Zoj 3635 <树状数组+二分>
- Minimum Inversion Number [树状数组]
- Bzoj 3289: Mato的文件管理 莫队,树状数组,逆序对,离散化,分块
- HDU 5327 Segment Game(树状数组)
- poj 2299 树状数组求逆序对数+离散化
- UVALive 4329 Ping pong(树状数组)
- HDU 1892 See you~ 【 二维树状数组 】