Codeforces Gym 100187M M. Heaviside Function two pointer

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=0,f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;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=0,flag2=0;
    int n=read();
    for(int i=0;i<n;i++)
    {
        a[i].x=read(),a[i].y=read();
        if(a[i].x==1)
            flag1++,a1.push_back(a[i].y);
        else
            flag2++,a2.push_back(a[i].y);
    }
    int m=read();
    for(int i=0;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-1;i>=0;i--)
    {
        if(flag1==0)
            break;
        while(b[i].x<a1[flag1-1]&&flag1>0)
            flag1--;
        if(flag1==0)
            break;
        ans[b[i].y]+=flag1;
    }
    for(int i=0;i<m;i++)
    {
        if(flag2==0)
            break;
        while(b[i].x>-a2[flag2-1]&&flag2>0)
            flag2--;
        if(flag2==0)
            break;
        ans[b[i].y]+=flag2;
    }
    for(int i=0;i<m;i++)
        printf("%d\n",ans[i]);
}
时间: 2025-01-12 00:25:19

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

codeforces gym 100187M Heaviside Function

//大概就是没想起来怎么做 //解法稍后补上 1 #include<cstdio> 2 #include<iostream> 3 #include<cmath> 4 #include<algorithm> 5 #include<cstring> 6 #include<cstdlib> 7 #include<queue> 8 #include<vector> 9 #include<map> 10 #i

Codeforces gym Hello 2015 Div1 B and Div2 D

Codeforces gym 100571 problem D Problem 给一个有向图G<V,E>和源点S,边的属性有长度L和颜色C,即E=<L,C>.进行Q次询问,每次给定一个点X,输出S到X的最短路的长度(不存在则输出 -1).但要求S到X的路径中相邻两条边颜色不一样. Limits Time Limit(ms): 1000 Memory Limit(MB): 256 |V|, |E|: [1, 10^5] X, S: [1, |V| ] L: [1, 10^9] |C|

Codeforces gym Hello 2015 Div1 E

Codeforces gym 100570 problem E (一种处理动态最长回文子串问题的方法) Problem 给一个长度为N的字符串S,字符集是'a'-'z'.进行Q次操作,操作分三种.一,修改位置X的字符为C:二,查询以P位置为中心的最长回文子串的长度,并输出:三,查询以P与P+1的中间位置为中心的最长回文子串的长度,并输出. More 第二种操作子串长度为奇数,一定存在:第三种操作子串长度为偶数,若不存在,输出 -1. Limits Time Limit(ms): 4000(1s足

Codeforces gym Hello 2015 Div1 C and Div2 E

Codeforces gym 100570 problem C Codeforces gym 100571 problem E Problem 给一个N行M列的矩阵Ma,进行Q次(Q<=10)查询,每次给定一个K,问有多少子矩阵,满足最大值max - 最小值min <=K. Limits Time Limit(ms): 8000 Memory Limit(MB): 512 N, M: [1, 400] Q: [1, 10] Ma(i, j), K: [1, 10^9] Solution (Th

【模拟】ECNA 2015 I What&#39;s on the Grille? (Codeforces GYM 100825)

题目链接: http://codeforces.com/gym/100825 题目大意: 栅栏密码.给定N(N<=10),密钥为一个N*N的矩阵,'.'代表空格可以看到,'X'代表被遮挡,还有密文字符串S,长度为N*N 每次将这个矩阵顺时针旋转90°,把矩阵中空格对应的位置按照从上到下从左到右的顺序依次填充上密文字符,求最终这个密文字符能否填满N*N的矩阵,能按顺序输出得到的答案,不能输出"invalid grille" 题目思路: [模拟] 直接模拟即可.旋转的坐标公式很好推.

Codeforces gym Hello 2015 Div2 B

Codeforces gym 100571 problem B Problem 设函数F(x),F(1)与F(2)已知,且当 i>=3,F(i)=a*F(i-2)+b*F(i-1).再给一个长度为N的数列A,进行Q次如下操作:每次给一个区间[L, R],对于每个k(L=<k<=R),将A[k]=A[k]+F[k-L+1].最后输出数列A(mod 10^9+7). Limits Time Limit(ms): 1000 Memory Limit(MB): 256 N, Q: [1, 10^

Codeforces Gym - 101147J Whistle&#39;s New Car

Discription Statements Whistle has bought a new car, which has an infinite fuel tank capacity. He discovered an irregular country since it has n cities and there are exactly n?-?1roads between them, of course, all cities are connected. He is so much

Codeforces Gym 101174 A Within Arm&#39;s Reach 贪心 手臂

#include<iostream> #include<stdio.h> #include <string.h> #include <algorithm> #include <vector> #include <math.h> using namespace std; #define LL long long const int maxn=25; double a[maxn],l[maxn],r[maxn]; double ex,ey

CF Gym 100187M Heaviside Function

题意:给你一个函数和一些系数,给你一堆询问,求函数值. 一个一个算肯定T,根据s的符号,分成两组,然后,二分就行啦. 我写的迷之四分,当时比较沙茶,写了个四分 #include<cstdio> #include<algorithm> using namespace std; const int maxn = 200000+1; int A[maxn]; int B[maxn]; int sz1,sz2; int main() { // freopen("in.txt&qu