码迷,mamicode.com
首页 > 其他好文 > 详细

ZOJ - 4082:Little Sub and his Geometry Problem (双指针)

时间:2019-01-19 22:15:09      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:sha   container   function   multi   block   ati   def   HERE   cas   

Little Sub loves math very much, and has just come up with an interesting problem when he is working on his geometry homework.

It is very kind of him to share this problem with you. Please solve it with your coding and math skills. Little Sub says that even Mr.Potato can solve it easily, which means it won‘t be a big deal for you.

The problem goes as follows:

Given two integers and , and points with their Euclidean coordinates on a 2-dimensional plane. Different points may share the same coordinate.

Define the function

 
where
 

You are required to solve several queries.

In each query, one parameter is given and you are required to calculate the number of integer pairs such that and .


Input

There are multiple test cases. The first line of the input contains an integer (), indicating the number of test cases. For each test case:

The first line contains two positive integers and ().

For the following lines, the -th line contains two integers and (), indicating the coordinate of the -th point.

The next line contains an integer (), indicating the number of queries.

The following line contains integers (), indicating the parameters for each query.

It‘s guaranteed that at most 20 test cases has .

Output

For each test case, output the answers of queries respectively in one line separated by a space.

Please, DO NOT output extra spaces at the end of each line, or your answer may be considered incorrect!

Sample Input
2
4 2
1 1
2 3
5
1 2 3 4 5
15 5
1 1 
7 3 
5 10 
8 6 
7 15
3
25 12 31
Sample Output
2 3 4 1 2
5 11 5

题意:给定二维N*N平面,以及M个点,Q次询问,每次询问给出C,问平面上有多少个点满足:左下方的点到它的距离和为C。

思路:发现符合条件的点在每个X线上最多一个一个点满足,而且这些点具有单调性,即X递增,Y递减。假设当前点(i,j),左下方的点个数为tot,

那么距离和=(i+j)*tot-sum;      我们维护一些轴上的学习,然后双指针去搞就好了。

#include<bits/stdc++.h>
#define ll long long
#define rep(i,w,v) for(int i=w;i<=v;i++)
using namespace std;
const int maxn=200010;
struct in{
    int x,y;
    bool friend operator<(in w,in v){
        if(w.x==v.x) return w.y<v.y; return w.x<v.x;
    }
}s[maxn];
int num[maxn],tot,ans; ll sum,C,d[maxn];
int main()
{
    int T,N,M,Q;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&N,&M);
        rep(i,1,M) {
            scanf("%d%d",&s[i].x,&s[i].y);
        }
        sort(s+1,s+M+1);  scanf("%d",&Q);
        rep(kk,1,Q){
           scanf("%lld",&C);
           int p=0,q=N; rep(i,1,N) num[i]=0,d[i]=0;
           ans=tot=0; sum=0;
           rep(i,1,N){
              while(p+1<=M&&s[p+1].x<=i) {
                 p++; if(s[p].y<=q){
                    tot++; num[s[p].y]++;
                    sum+=s[p].x+s[p].y;
                    d[s[p].y]+=i;
                 }
              }
              while((ll)tot*(i+q)-sum>C){
                 tot-=num[q];
                 sum-=(d[q]+(ll)num[q]*q);
                 q--;
              }
              if((ll)tot*(i+q)-sum==C) ans++;
           }
           if(kk!=1) putchar( );
           printf("%d",ans);
        }
        puts("");
    }
    return 0;
}

 

ZOJ - 4082:Little Sub and his Geometry Problem (双指针)

标签:sha   container   function   multi   block   ati   def   HERE   cas   

原文地址:https://www.cnblogs.com/hua-dong/p/10293394.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!