Time Limit:20 Sec
Memory limit:256 MB
Topic Connection
Http://codeforces.com/gym/100187/problem/M
Description
Heaviside function is defined as the Piecewise constant function whose value are 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 double 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 VA Lue 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
Test instructions
if (sx-a>=0) ans++; ask you each number through this formula, the last ans is how much
Exercises
When S=1, it is clear that the line is monotonous, and we can use the pointer to optimize it.
Complexity O (n+m)
Code
#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>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineTest Freopen ("Test.txt", "R", stdin)#defineMAXN 200101#defineMoD 1000000009#defineEPS 1e-9Const intinf=0x3f3f3f3f;Constll infll =0x3f3f3f3f3f3f3f3fll;inline ll Read () {ll x=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}//**************************************************************************************structnode{intx, y;};BOOLCMP1 (node A,node b) {returna.x<b.x;}BOOLCMP2 (node A,node b) {returna.y<B.y;} Node A[maxn];vector<int>A1;vector<int>A2;node B[MAXN];intANS[MAXN];intMain () {intflag1=0, flag2=0; intn=read (); for(intI=0; i<n;i++) {a[i].x=read (), a[i].y=read (); if(a[i].x==1) Flag1++, A1.push_back (A[I].Y); ElseFlag2++, A2.push_back (A[I].Y); } intm=read (); for(intI=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(inti=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(intI=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(intI=0; i<m;i++) printf ("%d\n", Ans[i]);}
Codeforces Gym 100187M M. Heaviside Function-pointer