Problem R
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 8 Accepted Submission(s) : 4
Wintokk has collected a huge amount of coins at THU. One day he had all his coins fallen on to the ground. Unfortunately, WangDong came by and decided to rob Wintokk of the coins.
They agreed to distribute the coins according to the following rules:
Consider the ground as a plane. Wintokk draws a horizontal line on the plane and then WangDong draws a vertical one so that the plane is divided into 4 parts, as shown below.
Wintokk will save the coins in I and III while those fit in II and IV will be taken away by the robber WangDong.
For fixed locations of the coins owned by Wintokk, they drew several pairs of lines. For each pair, Wintokk wants to know the difference between the number of the saved coins and that of the lost coins.
It's guaranteed that all the coins will lie on neither of the lines drew by that two guys.
2 10 3 29 22 17 14 18 23 3 15 6 28 30 27 4 1 26 7 8 0 11 21 2 25 5 10 19 24 10 5 28 18 2 29 6 5 13 12 20 27 15 26 11 9 23 25 10 0 22 24 16 30 14 3 17 21 8 1 7 4
6 4 4 2 2 4 4 4
思路:(借鉴别人的思路)
先将点和原点以纵坐标为关键字排序,此题目需要用到两个数状数组,分别来维护左上方和右上方的点,一开始时把所有点放在右上数状数组(这也是原点要排序的原因,原点要先处理位置在左上的,再处理右下的),随着处理原点的不同逐渐把右上的删除放到左边的树状数组(因为原点越来越向右)。
代码:
#include <iostream>
#include <algorithm>
#include <cstring>
#define maxn 500010
using namespace std;
int l[maxn],r[maxn];
struct node
{
int x;
int y;
int id;
}point[50005],coin[50005];
int lowbit(int x){
return x&(-x);
}
int cmp(node a,node b)
{
if(a.x!=b.x)
return a.x<b.x;
else
return a.y<b.y;
}
void add(int *s,int pos,int n,int val){
while(pos<=n){
s[pos]+=val;
pos+=lowbit(pos);
}
}
int sum(int *s,int pos)
{
int sum=0;
while(pos>0){
sum+=s[pos];
pos-=lowbit(pos);
}
return sum;
}
int main()
{
ios::sync_with_stdio(false);
int i,t,n,m,ans[50005],y;
cin>>t;
while(t--)
{
memset(l,0,sizeof(l));
memset(r,0,sizeof(r));
memset(ans,0,sizeof(ans));
y=-0x3f;
cin>>n>>m;
for(i=1;i<=n;i++)
{
cin>>coin[i].x>>coin[i].y;
coin[i].x++;
coin[i].y++;
if(y<coin[i].y)
y=coin[i].y;
}
sort(coin+1,coin+1+n,cmp);
for(i=1;i<=m;i++)
{
cin>>point[i].x>>point[i].y;
point[i].x++;
point[i].y++;
point[i].id=i;
if(y<point[i].y)
y=point[i].y;
}
sort(point+1,point+1+m,cmp);
for(i=1;i<=n;i++)
add(r,coin[i].y,y,1);
int ss=1;
for(i=1;i<=m;i++)
{
while(point[i].x>=coin[ss].x&&ss<=n)
{
add(l,coin[ss].y,y,1);
add(r,coin[ss].y,y,-1);
ss++;
}
int a=sum(r,y)-sum(r,point[i].y)+sum(l,point[i].y);
int b=sum(l,y)-sum(l,point[i].y)+sum(r,point[i].y);
ans[point[i].id]=abs(a-b);
}
for(i=1;i<=m;i++)
cout<<ans[i]<<endl;
cout<<endl;
}
return 0;
}
读仔细题目,看准要求!