Google中國2015校園招聘筆試Round D APAC Test Problem B. GBus count

Problem

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.

Input

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. 

Output

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. 

Limits

1 <= T <= 10 
ai and bi will always be integers.

Small dataset

1 <= N <= 50 
1 <= ai <= 500, 1 <= bi <= 500 
1 <= P <= 50

Large dataset

1 <= N <= 500 
1 <= ai <= 5000, 1 <= bi <= 5000 
1 <= P <= 500

Sample


Input 
 
 
2
4
15 25 30 35 45 50 10 20
2
15
25

10
10 15 5 12 40 55 1 10 25 35 45 50 20 28 27 35 15 40 4 5
3
5
10
27


Output 
 
Case #1: 2 1
Case #2: 3 3 4



Explanation for case 1:
2 GBuses go through city 15 (GBus1 [15 25] and GBus4 [10 20]) 

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


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