Gym 100187M - Heaviside Function

Heaviside function is defined as the piecewiseconstant function whose value is zero for negative argument and one fornon-negative argument:

You are given the function f(x) = θ(s1x - a1) + θ(s2x - a2) + ... + θ(snx - an), where si =  ± 1. Calculate its values for argument values x1x2, ..., xm.

Input

The first line contains a single integer n (1 ≤ n ≤ 200000) —the number of the summands in the function.

Each of the next n lines contains two integers separated byspace — si and ai (si =  ± 1 - 109 ≤ ai ≤ 109) — parameters of thei-th summand.

The next line contains a single integer m (1 ≤ m ≤ 200000) —the number of the argument values you should calculate the value of thefunction for.

The last line contains m integers x1, ..., xm ( - 109 ≤ xi ≤ 109) separated by spaces — the argument valuesthemselves.

Output

Output m lines. i-th line should contain the value of f(xi).

Sample test(s)

input

6
1 3
-1 2
1 9
-1 2
1 7
-1 2
8
0 12 2 8 4 -3 7 9

output

0
3
0
2
1
3
2
3

 

思路:

将s=1与s=-1的分开分别用a1,a2两个数组存储,分别从小到大排序;

x用结构存储,记录值和输入顺序,并按数值从小到大排序;

对于s=1,即对于a1数组,则统计x-a1[i]>=0的个数,我们可以枚举x,a1[i]从最大的开始,当a[i]足够小时,统计a1[i]的剩余个数。因为我们事先已经排序,所以直接加就可以;

对于s=-1的情况,同理;


代码:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;

const int N = 200005;
int n, s;
int a1[N], a2[N];
int l1, l2;
int m;

struct node
{
	int value, id;
}x[N];

__int64 ans[N];

bool cmp(node a, node b)
{
	return a.value<b.value;
}

bool cmp2(node a, node b)
{
	return a.id<b.id;
}

int main()
{
	memset(ans, 0, sizeof(ans));
	scanf("%d", &n);

	int i, temp;
	l1 = l2 = 0;
	for (i = 0; i<n; i++)
	{
		scanf("%d%d", &s, &temp);
		if (s == 1)
			a1[l1++] = temp;
		else
			a2[l2++] = temp;
	}

	scanf("%d", &m);
	for (i = 0; i<m; i++)
	{
		scanf("%d", &x[i].value);
		x[i].id = i;
	}

	sort(a1, a1 + l1);
	sort(a2, a2 + l2);
	sort(x, x + m, cmp);

	for (i = m - 1; i >= 0; i--)
	{
		if (l1 == 0)
			break;
		while (x[i].value<a1[l1 - 1] && l1>0)
			l1--;
		if (l1 == 0)
			break;
		ans[x[i].id] += l1;
	}

	for (i = 0; i<m; i++)
	{
		if (l2 == 0)
			break;
		while (x[i].value>-a2[l2 - 1] && l2>0)
			l2--;
		if (l2 == 0)
			break;
		ans[x[i].id] += l2;
	}

	sort(x, x + m, cmp2);

	for (i = 0; i<m; i++)
		printf("%I64d\n", ans[i]);

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值