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
原本应为(h+1)*(v+1);
每多一个交点,就会+1;
那么我们对于一个端点排完序后,求另一个端点的逆序对即可;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 1000005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-5
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii; inline int rd() {
int x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ int X, Y;
int H, V;
int c[maxn];
int Tot;
void add(int x) {
while (x <= maxn) {
c[x]++; x += x & -x;
}
} int query(int x) {
int ans = 0;
while (x > 0) {
ans += c[x]; x -= x & -x;
}
return ans;
}
ll ans;
struct node {
int l, r;
}a[maxn];
bool cmp(node a, node b) {
return a.l < b.l;
}
int tmpb[maxn];
int tmpc[maxn];
int main()
{
// ios::sync_with_stdio(0);
X = rd(); Y = rd();
H = rd(); V = rd();
ans = 1ll * (H + 1ll)*(V + 1ll);
vector<int>vc; int ct = 0;
for (int i = 1; i <= H; i++) {
a[i].l = rd(); a[i].r = rd();
tmpb[++ct] = a[i].r;
}
sort(tmpb + 1, tmpb + 1 + ct);
sort(a + 1, a + 1 + H, cmp);
for (int i = 1; i <= H; i++) {
tmpc[i] = lower_bound(tmpb + 1, tmpb + 1 + ct, a[i].r) - tmpb;
}
for (int i = 1; i <= H; i++) {
add(tmpc[i]);
ans += (ll)(i - query(lower_bound(tmpb + 1, tmpb + 1 + ct, a[i].r + 1) - tmpb - 1));
}
ms(c); ms(tmpb); ms(tmpc);
ct = 0;
for (int i = 1; i <= V; i++) {
a[i].l = rd(); a[i].r = rd();
tmpb[++ct] = a[i].r;
}
sort(tmpb + 1, tmpb + 1 + ct);
sort(a + 1, a + 1 + V, cmp);
for (int i = 1; i <= V; i++) {
tmpc[i] = lower_bound(tmpb + 1, tmpb + 1 + ct, a[i].r) - tmpb;
}
for (int i = 1; i <= V; i++) {
add(tmpc[i]);
ans += (ll)(i - query(lower_bound(tmpb + 1, tmpb + 1 + ct, a[i].r + 1) - tmpb - 1));
}
cout << (ll)ans << endl;
return 0;
}

Gym - 101908C 树状数组 逆序对的更多相关文章

  1. [树状数组+逆序对][NOIP2013]火柴排队

    火柴排队 题目描述 涵涵有两盒火柴,每盒装有n根火柴,每根火柴都有一个高度.现在将每盒中的火柴各自排成一列,同一列火柴的高度互不相同,两列火柴之间的距离定义为:∑ (ai-bi)2,i=1,2,3,. ...

  2. hdu 5497 Inversion 树状数组 逆序对,单点修改

    Inversion Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5497 ...

  3. Codevs 3286 火柴排队 2013年NOIP全国联赛提高组 树状数组,逆序对

    题目:http://codevs.cn/problem/3286/ 3286 火柴排队  2013年NOIP全国联赛提高组  时间限制: 1 s   空间限制: 128000 KB   题目等级 : ...

  4. Bzoj 2789: [Poi2012]Letters 树状数组,逆序对

    2789: [Poi2012]Letters Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 278  Solved: 185[Submit][Stat ...

  5. Bzoj 3295: [Cqoi2011]动态逆序对 分块,树状数组,逆序对

    3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2886  Solved: 924[Submit][Stat ...

  6. Bzoj 3289: Mato的文件管理 莫队,树状数组,逆序对,离散化,分块

    3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 1539  Solved: 665[Submit][Status][Di ...

  7. Poj 2299 - Ultra-QuickSort 离散化,树状数组,逆序对

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 52306   Accepted: 19194 ...

  8. hdu 2838 Cow Sorting (树状数组+逆序对)

    题目 题意:给你N个排列不规则的数,任务是把它从小到大排好,每次只能交换相邻两个数,交换一次的代价为两数之和,求最小代价 拿到这道题,我根本看不出这道题和树状数组有半毛钱关系,博客之,全说用树状数组做 ...

  9. 【树状数组逆序对】USACO.2011JAN-Above the median

    [题意] 给出一串数字,问中位数大于等于X的连续子串有几个.(这里如果有偶数个数,定义为偏大的那一个而非中间取平均) [思路] 下面的数据规模也小于原题,所以要改成__int64才行.没找到测试数据, ...

随机推荐

  1. ASP.NET Core应用到Windows Service中

    托管到Windows Service中 众所周知,ASP.NET Core采用了和传统ASP.NET不同的托管和HTTP处理方式,即把服务器和托管环境完全解耦. ASP.NET Core内置了两个HT ...

  2. IE6的checkbox, radio是通过defaultChecked决定是否选中

    今天五群提到的BUG,说checked没有生效,一番百度谷歌,发现是它作怪. data.handler = function() { //IE6是通过defaultChecked来实现打勾效果 ele ...

  3. ubuntu 编译并安装resin3.1.12+nginx1.2.6

    一.先装jdk 先建立如下两个目录: mkdir /usr/lib/jvm mkdir /usr/lib/jvm/java 把jdk-6u26-linux-x64.bin文件传到上面目录下: chmo ...

  4. sam格式详细说明

    原文链接 https://www.jianshu.com/p/386f520e5de1 The SAM Format Specification(sam格式说明) 1 The SAM Format S ...

  5. bzoj5392 [Lydsy1806月赛]路径统计

    传送门 分析 我们设sum[x]为小于等于x的点现在有多少联通 于是一个序列合法当且只当sum[R]-sum[L-1]=len且所有点度数不大于2 我们知道如果对于序列[L,R]满足条件则[L+1,R ...

  6. 设计模式11: Flyweight 享元模式(结构型模式)

    Flyweight 享元模式(结构型模式) 面向对象的代价 面向对象很好的解决了系统抽象性的问题,同时在大多数情况下也不会损及系统的性能.但是,在某些特殊应用中,由于对象的数量太大,采用面向对象会给系 ...

  7. Oracle 写存储过程的一个模板还有一些基本的知识点

    我很少用Oracle,也算新手,不过其实入手没有那么难,下面只是一个基本知识,高手绕道,其实数据库基本是相同的,这里提供都是基本知识点 有一个Oracle溢出的问题,容易让新手怀疑到无所怀疑,其实就是 ...

  8. .net List<T>

    List的几个方法 List=>List.Find()List.FindAll()List.Contains() List.ForEach()List.ConvertAll() 1. 先比较Fi ...

  9. React+gulp+browserify模块化开发

    阅读本文需要有React的基础知识,可以在React 入门实例教程和React中文官网进行基础学习. 没有React基础也可以学习本文,本文主要不是学习React,而是gulp+browserify进 ...

  10. 【转】Android系统概览

    这篇文章其实原文叫 <老罗的Android之旅>导读PPT 是罗升阳的博客,我觉得用“Android系统概览”作为标题更贴切些,对于在应用层已经开发了一段时间的人来说,读完之后会有很多体会 ...