M. Heaviside Function

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/gym/100187/problem/M

Description

Heaviside function is defined as the piecewise constant function whose value is zero for negative argument and one for non-negative argument:

You are given the function f(x) = θ(s1x - a1) + θ(s2x - a2) + ... + θ(snx - an), where si =  ± 1. Calculate its values for argument values x1, x2, ..., 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 by space — si and ai (si =  ± 1,  - 109 ≤ ai ≤ 109) — parameters of the i-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 the function for.

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

Output

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

Sample Input

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

Sample Output

0
3
0
2
1
3
2
3

HINT

题意

if(sx-a>=0)ans++;问你每个数通过这个公式,最后的ans是多少

题解:

当s=1时,很显然这个直线是单调向上的,我们可以利用two pointer来优化就好了

复杂度O(n+m)

代码

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 200101
#define mod 1000000009
#define eps 1e-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************** struct node
{
int x,y;
}; bool cmp1(node a,node b)
{
return a.x<b.x;
}
bool cmp2(node a,node b)
{
return a.y<b.y;
}
node a[maxn];
vector<int> a1;
vector<int> a2;
node b[maxn];
int ans[maxn]; int main()
{
int flag1=,flag2=;
int n=read();
for(int i=;i<n;i++)
{
a[i].x=read(),a[i].y=read();
if(a[i].x==)
flag1++,a1.push_back(a[i].y);
else
flag2++,a2.push_back(a[i].y);
}
int m=read();
for(int i=;i<m;i++)
b[i].x=read(),b[i].y=i;
sort(b,b+m,cmp1);
sort(a1.begin(),a1.end());
sort(a2.begin(),a2.end()); for(int i=m-;i>=;i--)
{
if(flag1==)
break;
while(b[i].x<a1[flag1-]&&flag1>)
flag1--;
if(flag1==)
break;
ans[b[i].y]+=flag1;
}
for(int i=;i<m;i++)
{
if(flag2==)
break;
while(b[i].x>-a2[flag2-]&&flag2>)
flag2--;
if(flag2==)
break;
ans[b[i].y]+=flag2;
}
for(int i=;i<m;i++)
printf("%d\n",ans[i]);
}

Codeforces Gym 100187M M. Heaviside Function two pointer的更多相关文章

  1. codeforces gym 100187M Heaviside Function

    //大概就是没想起来怎么做 解法:首先观察seitan方程,发现我们要找的是满足seitan(si*x-ai)=1的方程数,即si*x-ai>=0的方程数,因为si=1 or -1,于是分类讨论 ...

  2. Codeforces Gym 101252D&&floyd判圈算法学习笔记

    一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...

  3. Codeforces Gym 101190M Mole Tunnels - 费用流

    题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...

  4. Codeforces Gym 101623A - 动态规划

    题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...

  5. 【Codeforces Gym 100725K】Key Insertion

    Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...

  6. Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】

     2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...

  7. codeforces gym 100553I

    codeforces gym 100553I solution 令a[i]表示位置i的船的编号 研究可以发现,应是从中间开始,往两边跳.... 于是就是一个点往两边的最长下降子序列之和减一 魔改树状数 ...

  8. CodeForces Gym 100213F Counterfeit Money

    CodeForces Gym题目页面传送门 有\(1\)个\(n1\times m1\)的字符矩阵\(a\)和\(1\)个\(n2\times m2\)的字符矩阵\(b\),求\(a,b\)的最大公共 ...

  9. Codeforces GYM 100876 J - Buying roads 题解

    Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...

随机推荐

  1. SPF详解

    什么是SPF? 这里的SPF不是防晒指数,而是指Sender Policy Framework.翻译过来就是发信者策略架构,比较拗口,通常都直接称为SPF. SPF是跟DNS相关的一项技术,它的内容写 ...

  2. Microsoft Visual Studio Web 创作组件安装失败的解决方法

    在网上查一下说是Office2007的问题.我把Office2007卸载了还是不行. 然后用Windows Install Clean Up工具清理,还是不行. 郁闷了.然后在安装包中的下面路径下找到 ...

  3. SoapUI Property

    1. Test Suite(Case) Property 选择Test Suite(Case),switch to Custom properties 在request中的引用方式: ${[scope ...

  4. DzzOffice共享文件夹、共享目录设置

    dzzoffice中共享目录的设置,是通过机构部门建立的. 首先打开机构用户管理.建立需要的机构和部门.这里机构和部门可以理解为共享目录的名称.也可以根据自己需要起名,并不一定是机构和部门的名字. 而 ...

  5. 九度 Online Judge 之《剑指 Offer》一书相关题目解答

    前段时间准备华为机试,正好之前看了一遍<剑指 Offer>,就在九度 Online Judge 上刷了书中的题目,使用的语言为 C++:只有3题没做,其他的都做了. 正如 Linus To ...

  6. NServiceBus教程-持久化配置

    当配置在NServiceBus v5持久性,秩序是非常重要的.最后赢得持久性配置选项.我们看看一些例子. 示例1 在本例中最后一个配置选项将会覆盖前面的所有选项. v5.2 v5.0 编辑 var c ...

  7. css 样式大全

    字体属性:(font) 大小 {font-size: x-large;}(特大) xx-small;(极小) 一般中文用不到,只要用数值就可以,单位:PX.PD 样式 {font-style: obl ...

  8. gradle 及 git 环境下利用hook及gradle脚本自动添加versioncode和versionname的方法

    在 app/build.gradle 文件里添加几行代码: def gitCommitShortHash = 'git log -1 --pretty=%h'.execute([], project. ...

  9. 第三百四十二天 how can I 坚持

    再问世间都去哪儿了,天气预报没搞完,计划没制定,又周三了. 今天回到家八点,吃完饭接近九点,和老妈开了会视频,这就九点半多了,发了呆洗了个碗就到这时候了,整天浑浑噩噩的,该如何是好. 又有点上火,舌头 ...

  10. 安装完 MySQL 后必须调整的 10 项配置(转)

    英文原文:10 MySQL settings to tune after installation 译文原文:安装完 MySQL 后必须调整的 10 项配置 当我们被人雇来监测MySQL性能时,人们希 ...