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. [转]Java Web乱码过滤器

    本文转自http://blog.csdn.net/l271640625/article/details/6388690 大家都知道,在jsp里乱码是最让人讨厌的东西,有些乱码出来的莫名其妙,给开发带来 ...

  2. 骑士周游问题 --- 递归解法 --- java代码

    骑士游历: 定义了向量的数组M,行数组X,列数组Y, 棋盘plane,计数器count,走动步数step 需要注意的是,递归函数的进入前的验证,原先的想法是传入来时的方向参数,可是这样的想法被实践否定 ...

  3. 网易实习笔试真题C/C++

    刚做的时候根本就没有想到解题思路,刚好看到了别人的思路,自己写了一下.里面对unordered_map及vector二维数组的建立很灵活,另外区别了一下map,unordered_map,hash_m ...

  4. 英语之idiom

    1 quick and dirty = Done or constructed in a hasty, approximate, temporarily adequate manner, but no ...

  5. (转)QR二维码生成及原理

    二维码又称QR Code,QR全称Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的Bar Code条形码能存更多的信息,也能表示更多的数据类型:比如:字符,数字, ...

  6. 1、Hadoop架构

    1.Hadoop 是一个能够对大量数据进行分布式处理的软件框架,实现了Google的MapReduce编程模型和框架,能够把应用程序分割成许多小的工作单元放到任何集群节点上执行. 作业(job):一个 ...

  7. c++10 Seattle Clang error

    升级到C++Builder RAD 10 Settle 一些错误解决方法,使用LLVM  CLang编译器,BCC32C http://docwiki.embarcadero.com/RADStudi ...

  8. Android SDK无法更新解决方法

    我这里主要说的是mac下如何设置Android SDK更新,windows下类似 首先说明为什么要这么麻烦,没办法身处在大天朝中,伟大的防火墙,苦逼的程序猿想要查点资料都是非常难的.不废话了,下面进入 ...

  9. SQL2008 SQL Server 代理服务提供的凭据无效

    工作中遇到的问题记录: 安装到服务器配置时出的错误:为 SQL Server 代理服务提供的凭据无效.若要继续操作,请为 SQL Server 代理服务提供有效的帐户和密码. 解决方法:直接在所有的“ ...

  10. Field 'SCHED_TIME' doesn't have a default value

    出现这个情况的原因是: 我jar包使用的是quartz-2.1.7版本,但是初始化集群的dbTables脚本用的却是2.2.1版本的,导致出现这个异常,改用2.1.7的dbTables脚本之后即解决问 ...