Pizza Cutter Gym - 101908C (树状数组求逆序数+离散化)

Grandpa Giuseppe won a professional pizza cutter, the kind of type reel and, to celebrate, baked a rectangle pizza to his grandchildren! He always sliced his pizzas into pieces by making cuts over continuous lines, not necessarily rectilinear, of two types: some begin at the left edge of the pizza, follow continuously to the right and end up in the right edge; other start on lower edge, follow continuously up and end up on the top edge. But Grandpa Giuseppe always followed a property: two cuts of the same type would never intersect. Here is an example with 4 cuts, two of each type, in the left part of the figure, which divide the pizza in 9 pieces.

It turns out that Grandpa Giuseppe simply loves geometry, topology, combinatorics and stuff; so, he decided to show to his grandchildren who could get more pieces with the same number of cuts if cross cuts of the same type were allowed. The right part of the figure shows, for example, that if the two cuts of the type that go from left to right could intercept, the pizza would be divided into 10 pieces.

Grandpa Giuseppe ruled out the property, but will not make random cuts. In addition to being one of the two types, they will comply with the following restrictions:

Two cuts have at most one intersection point and, if they have, it is because the cuts cross each other at that point;
Three cuts do not intersect in a single point;
Two cuts do not intersect at the border of the pizza;
A cut does not intercept a pizza corner.
Given the start and end points of each cut, your program should compute the number of resulting pieces from the cuts of Grandfather Giuseppe.

Input
The first line of the input contains two integers X and Y, (1≤X,Y≤109), representing the coordinates (X,Y) of the upper-right corner of the pizza. The lower left corner has always coordinates (0,0). The second line contains two integers H and V, (1≤H,V≤105), indicating, respectively, the number of cuts ranging from left to right and the number of cuts ranging from bottom to top. Each of the following lines H contains two integers Y1 and Y2, a cut that intercepts the left side with y-coordinate Y1 and the right side at y-coordinate Y2. Each of the following V lines contains two integers X1 and X2, a cut that intercept the bottom side at x-coordinate X1 and the upper side at x-coordinate X2.

Examples
Input
3 4
3 2
1 2
2 1
3 3
1 1
2 2
Output
13
Input
5 5
3 3
2 1
3 2
1 3
3 4
4 3
2 2
Output
19
Input
10000 10000
1 2
321 3455
10 2347
543 8765
Output
6

#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include<vector>
#define  ll long long
#define Low_bit(x) (x&(-x))
using namespace std;
using namespace std;
const int MAXN=310000;
int d[MAXN],n;

void update(int a) {
	while(a<=MAXN) {
		d[a]++;
		a+=Low_bit(a);
	}
}

int get_sum(int a) {
	int sum=0;
	while(a) {
		sum+=d[a];
		a-=Low_bit(a);
	}
	return sum;
}
struct node {
	int a,b;
} A[MAXN];

bool cmp(node a,node b) {
	return a.a<b.a;
}
int B[MAXN];
int C[MAXN];
int main() {
	int x,y;
	cin>>x>>y;
	int h,v;
	cin>>h>>v;
	vector<int>vv;
	int cnt=1;
	for(int i=1; i<=h; i++) {
		cin>>A[i].a>>A[i].b;
		B[cnt]=A[i].b;
		cnt++;
	}
	sort(B+1,B+1+h);
	sort(A+1,A+1+h,cmp);

	for(int i=1; i<=h; i++) {
		C[i]=lower_bound(B+1,B+cnt+1 ,A[i].b) -B ;
	}
	ll ans=0;
	for(int i=1; i<=h; i++) {
		update(C[i]);
		ans+=(ll)(i-get_sum(C[i]));
	}

	memset(C,0,sizeof(C));
	memset(B,0,sizeof(B));
	memset(d,0,sizeof(d));

	cnt=1;
	for(int i=1; i<=v; i++) {
		cin>>A[i].a>>A[i].b;
		B[cnt]=A[i].b;
		cnt++;
	}
	sort(B+1,B+1+v);
	sort(A+1,A+1+v,cmp);

	for(int i=1; i<=v; i++) {
		C[i]=lower_bound(B+1,B+cnt+1 ,A[i].b) -B ;
	}

	for(int i=1; i<=v; i++) {
		update(C[i]);
		ans+=(ll)(i-get_sum(C[i]));
	}
	cout<<ans+(ll)h*(ll)v+(ll)h+(ll)v+1ll<<endl;

	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值