There exists a straight line along which cities are built.
Each city is given a number starting from 1. So if there are 10 cities, city 1 has a number 1, city 2 has a number 2,... city 10 has a number 10.
Different buses (named GBus) operate within different cities, covering all the cities along the way. The cities covered by a GBus are represented as 'first_city_number last_city_number' So, if a GBus covers cities 1 to 10 inclusive, the cities covered by it are represented as '1 10'
We are given the cities covered by all the GBuses. We need to find out how many GBuses go through a particular city.
The first line contains the number of test cases (T), after which T cases follow each separated from the next with a blank line.
For each test case,
The first line contains the number of GBuses.(N)
Second line contains the cities covered by them in the form
a1 b1 a2 b2 a3 b3...an bn
where GBus1 covers cities numbered from a1 to b1, GBus2 covers cities numbered from a2 to b2, GBus3 covers cities numbered from a3 to b3, upto N GBuses.
Next line contains the number of cities for which GBus count needs to be determined (P).
The below P lines contain different city numbers.
For each test case, output one line containing "Case #Ti:" followed by P numbers corresponding to the number of cities each of those P GBuses goes through.
1 <= T <= 10
ai and bi will always be integers.
1 <= N <= 50
1 <= ai <= 500, 1 <= bi <= 500
1 <= P <= 50
1 <= N <= 500
1 <= ai <= 5000, 1 <= bi <= 5000
1 <= P <= 500
1 GBus goes through city 25 (GBus1 [15 25])
类型:线段树 难度:2
题意:在一条直线上,有若干城市,城市序号为顺序。给出一些公交线路,以及每条线路覆盖的起始城市序号和终止城市序号[ai,bi]。再给出一些查询,每个查询为一个城市序号,求这个城市有多少条公交线路经过。
分析:线段树模板题。由于城市数量为[1,5000],所以适合用线段树解决。需要用到线段树的成段更新,在给出每条公交线路时,将[ai,bi]区间内+1。查询时,返回查询点的值。
代码如下:
#include <iostream> #include <vector> #include <algorithm> #include <cstdint> #include <cstdio> #include <cstring> #include <string> using namespace std; #define lson l , m , rt << 1 #define rson m + 1 , r , rt << 1 | 1 const int N = 5010; const int maxN = 5010; int add[N<<2]; int sum[N<<2]; void PushUp(int rt) { sum[rt] = sum[rt<<1] + sum[rt<<1|1]; } void PushDown(int rt,int m) { if (add[rt]) { add[rt<<1] += add[rt]; add[rt<<1|1] += add[rt]; sum[rt<<1] += add[rt] * (m - (m >> 1)); sum[rt<<1|1] += add[rt] * (m >> 1); add[rt] = 0; } } void build(int l,int r,int rt) { add[rt] = 0; if (l == r) { sum[rt] = 0; return ; } int m = (l + r) >> 1; build(lson); build(rson); PushUp(rt); } void update(int L,int R,int c,int l,int r,int rt) { //³É¶ÎÔö¼õ if (L <= l && r <= R) { add[rt] += c; sum[rt] += c * (r - l + 1); return ; } PushDown(rt , r - l + 1); int m = (l + r) >> 1; if (L <= m) update(L , R , c , lson); if (m < R) update(L , R , c , rson); PushUp(rt); } int srch(int p,int l,int r,int rt) { if (l == r) { return sum[rt]; } PushDown(rt , r - l + 1); int m = (l + r) >> 1; if (p <= m) srch(p , lson); else srch(p , rson); } int main() { freopen("B-large.in", "r", stdin); freopen("B-large.out", "w", stdout); int t; scanf("%d",&t); for(int cnt=1; cnt<=t; ++cnt) { int n,p; build(1,maxN,1); scanf("%d",&n); for(int i=0; i<n; ++i) { int a,b; scanf("%d%d",&a,&b); update(a,b,1,1,maxN,1); } scanf("%d",&p); printf("Case #%d:",cnt); for(int i=0; i<p; ++i) { int q; scanf("%d",&q); printf(" %d",srch(q,1,maxN,1)); } printf("\n"); } }