Crossing
Time Limit: 2000MS | Memory Limit: 65536K |
Total Submissions: 1562 | Accepted: 724 |
Description
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.
Input
The first line contains an integer T, indicating the number of test cases. Then T blocks of test cases follow. For each case, the first line contains two integers N and M, where N is the number of coins on the ground and M indicates how many times they are going to draw the lines. The 2nd to (N+1)-th lines contain the co-ordinates of the coins and the last M lines consist of the M pairs integers (x, y) which means that the two splitting lines intersect at point (x, y).
(N,M ≤ 50000, 0 ≤x,y ≤ 500000)
Output
For each query, output a non-negative integer, the difference described above. Output a blank line between cases.
Sample Input
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
Sample Output
6
4
4
2
2
4
4
4
——————————————————分割線——————————————
題目大意:
有n個硬币,每個硬币的坐标(x,y),有m次的操作,每次給一個點(i,j),做水準線x=i,垂直線y=j,這兩條直線将區域分為4塊,求1,3塊的硬币數跟2,4塊硬币數差的絕對值
思路:
對n個硬币坐标按x升序排列,m次操作也按x升序排列。用兩個樹狀數組分别記錄在x軸左邊的點的個數,在x軸右邊點的個數。初始時都在右邊,然後進行更新時,把比它小的點插入到坐标,把右邊的删除。
統計的時候按平常那樣子就可以了。
對了,下标從0開始,點的坐标要+1
這題數組越界搞得我都醉了。問了大神才知道怎麼錯的,ORZ。
這題讓我學了好多,雖然是個水題!
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
const int M=500001;
using namespace std;
int n,m;
int l[M+10],r[M+10],ans[M];
struct node
{
int x,y,index;
bool operator<(const node&A)const
{
return x<A.x;
}
}a[M],b[M];
void update(int *s,int x,int v)
{
for(int i=x;i<=M;i+=i&-i){
s[i]+=v;
}
}
int getsum(int *s,int x)
{
int sum=0;
for(int i=x;i>0;i-=i&-i){
sum+=s[i];
}
return sum;
}
int main()
{
int T;
cin>>T;
while(T--){
scanf("%d %d",&n,&m);
for(int i=0;i<n;++i){
scanf("%d %d",&a[i].x,&a[i].y);
a[i].x++,a[i].y++;
}
for(int i=0;i<m;++i){
scanf("%d %d",&b[i].x,&b[i].y);
b[i].x++,b[i].y++;
b[i].index=i;
}
sort(a,a+n);sort(b,b+m);
memset(l,0,sizeof(l));memset(r,0,sizeof(r));
for(int i=0;i<n;++i){
update(r,a[i].y,1);
}
int st=0,ed;
for(int i=0;i<m;++i){
for(ed=st;ed<n;++ed){
if(a[ed].x>b[i].x) break;
}
for(int j=st;j<ed;++j){
update(r,a[j].y,-1);
update(l,a[j].y,1);
}
int s13=getsum(l,b[i].y)+getsum(r,M)-getsum(r,b[i].y);
int s24=getsum(r,b[i].y)+getsum(l,M)-getsum(l,b[i].y);
ans[b[i].index]=abs(s13-s24);
st=ed;
}
for(int i=0;i<m;++i){
printf("%d\n",ans[i]);
}
if(T) printf("\n");
}
return 0;
}