using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("4 3 2");
WillReturn.Add("2 2");
WillReturn.Add("3 3");
//10
}
else if (InputPattern == "Input2") {
WillReturn.Add("5 4 4");
WillReturn.Add("3 2");
WillReturn.Add("3 4");
WillReturn.Add("4 2");
WillReturn.Add("5 2");
//14
}
else if (InputPattern == "Input3") {
WillReturn.Add("200000 200000 0");
//40000000000
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
struct WallPosDef
{
internal long R;
internal long C;
}
static void Main()
{
List<string> InputList = GetInputList();
long[] wkArr = { };
Action<string> SplitAct = pStr =>
wkArr = pStr.Split(' ').Select(pX => long.Parse(pX)).ToArray();
SplitAct(InputList[0]);
long H = wkArr[0];
long W = wkArr[1];
var WallPosList = new List<WallPosDef>();
foreach (string EachStr in InputList.Skip(1)) {
SplitAct(EachStr);
WallPosDef WillAdd;
WillAdd.R = wkArr[0];
WillAdd.C = wkArr[1];
WallPosList.Add(WillAdd);
}
long Result = Solve(H, W, WallPosList);
Console.WriteLine(Result);
}
static long Solve(long pH, long pW, List<WallPosDef> pWallPosList)
{
// 最も左の壁[Y座標]
var LeftPosDict = new Dictionary<long, long>();
// 最も上の壁[X座標]
var TopPosDict = new Dictionary<long, long>();
foreach (WallPosDef EachWallPos in pWallPosList) {
long R = EachWallPos.R;
long C = EachWallPos.C;
if (LeftPosDict.ContainsKey(R) == false || LeftPosDict[R] > C) {
LeftPosDict[R] = C;
}
if (TopPosDict.ContainsKey(C) == false || TopPosDict[C] > R) {
TopPosDict[C] = R;
}
}
// 壁のX座標のList [壁のY座標] なDict
var XListDict = new Dictionary<long, List<long>>();
foreach (WallPosDef EachWallPos in pWallPosList) {
long R = EachWallPos.R;
long C = EachWallPos.C;
if (XListDict.ContainsKey(R) == false) {
XListDict[R] = new List<long>();
}
XListDict[R].Add(C);
}
// 場合の数01 右に移動してから、下に移動
long PatternCnt1 = 0;
for (long LoopX = 1; LoopX <= pW; LoopX++) {
if (LeftPosDict.ContainsKey(1)) {
if (LoopX == LeftPosDict[1]) {
break;
}
}
long PlusCnt = pH;
if (TopPosDict.ContainsKey(LoopX)) {
PlusCnt = TopPosDict[LoopX] - 1;
}
PatternCnt1 += PlusCnt;
}
// 場合の数02 下に移動してから、右に移動だが、場合の数01の補集合のみ
// セグ木の各ノードの初期値は0で、壁に到達したら1にUpdate
var InsLazySegmentTree = new LazySegmentTree(pW);
// 1行目に壁があったら、その壁から右の区間を1にUpdate
if (LeftPosDict.ContainsKey(1)) {
InsLazySegmentTree.RangeUpdate(LeftPosDict[1], pW, 1, 0);
}
long PatternCnt2 = 0;
for (long LoopY = 1; LoopY <= pH; LoopY++) {
if (TopPosDict.ContainsKey(1)) {
if (LoopY == TopPosDict[1]) {
break;
}
}
long MaxX = pW;
if (LeftPosDict.ContainsKey(LoopY)) {
MaxX = LeftPosDict[LoopY] - 1;
}
long PlusCnt = InsLazySegmentTree.Query(1, MaxX, 0);
PatternCnt2 += PlusCnt;
// 壁があったらセグ木の1点更新で1にする
if (XListDict.ContainsKey(LoopY)) {
foreach (long EachX in XListDict[LoopY]) {
InsLazySegmentTree.RangeUpdate(EachX, EachX, 1, 0);
}
}
}
long Answer = PatternCnt1 + PatternCnt2;
return Answer;
}
}
#region LazySegmentTree
// LazySegmentTreeクラス (RSQ and RUQ)
internal class LazySegmentTree
{
private long[] mTreeNodeArr;
private long UB; // 木のノードの配列のUB
private long mLeafCnt; // 葉ノードの数
private long?[] mLazyArr; // 遅延配列
// ノードの添字を引数とし、範囲の開始添字と終了添字を持つ配列
private struct RangeInfoDef
{
internal long StaInd;
internal long EndInd;
}
private RangeInfoDef[] mRangeInfo;
// コンストラクタ
internal LazySegmentTree(long pLeafCnt)
{
// 簡単のため、葉ノード数を2のべき乗に
long ArrLength = 0;
for (long I = 1; I < long.MaxValue; I *= 2) {
ArrLength += I;
mLeafCnt = I;
if (pLeafCnt < mLeafCnt) break;
}
// すべての値を0に
UB = ArrLength - 1;
mTreeNodeArr = new long[UB + 1];
for (long I = 0; I <= UB; I++) {
mTreeNodeArr[I] = 0;
}
// 遅延配列を初期化
mLazyArr = new long?[UB + 1];
// ノードの添字を引数とし、範囲の開始添字と終了添字を持つ配列の作成
mRangeInfo = new RangeInfoDef[UB + 1];
for (long I = 0; I <= UB; I++) {
if (I == 0) {
RangeInfoDef WillSet1;
WillSet1.StaInd = 0;
WillSet1.EndInd = mLeafCnt - 1;
mRangeInfo[I] = WillSet1;
continue;
}
long ParentNode = DeriveParentNode(I);
RangeInfoDef ParentRangeInfo = mRangeInfo[ParentNode];
RangeInfoDef WillSet2;
long Mid = (ParentRangeInfo.StaInd + ParentRangeInfo.EndInd) / 2;
if (I % 2 == 1) { // 奇数ノードの場合
WillSet2.StaInd = ParentRangeInfo.StaInd;
WillSet2.EndInd = Mid;
}
else { // 偶数ノードの場合
WillSet2.StaInd = Mid + 1;
WillSet2.EndInd = ParentRangeInfo.EndInd;
}
mRangeInfo[I] = WillSet2;
}
}
// 親ノードの添字を取得
private long DeriveParentNode(long pTarget)
{
return (pTarget - 1) / 2;
}
// 子ノードの添字(小さいほう)を取得
private long DeriveChildNode(long pTarget)
{
return pTarget * 2 + 1;
}
// カレントノードを引数として、遅延評価を行う
void LazyEval(long pCurrNode)
{
// 遅延配列が空なら何もしない
if (mLazyArr[pCurrNode].HasValue == false) return;
long CurrNodeStaInd = mRangeInfo[pCurrNode].StaInd;
long CurrNodeEndInd = mRangeInfo[pCurrNode].EndInd;
// 遅延配列の値を反映する
mTreeNodeArr[pCurrNode] = mLazyArr[pCurrNode].Value * (CurrNodeEndInd - CurrNodeStaInd + 1);
long ChildNode1 = DeriveChildNode(pCurrNode);
long ChildNode2 = ChildNode1 + 1;
if (ChildNode1 <= UB) mLazyArr[ChildNode1] = mLazyArr[pCurrNode];
if (ChildNode2 <= UB) mLazyArr[ChildNode2] = mLazyArr[pCurrNode];
// 伝播が終わったので、自ノードの遅延配列を空にする
mLazyArr[pCurrNode] = null;
}
// 開始添字と終了添字とカレントノードを引数として、区間更新を行う
internal void RangeUpdate(long pSearchStaInd, long pSearchEndInd, long pUpdateVal, long pCurrNode)
{
// カレントノードの遅延評価を行う
LazyEval(pCurrNode);
long CurrNodeStaInd = mRangeInfo[pCurrNode].StaInd;
long CurrNodeEndInd = mRangeInfo[pCurrNode].EndInd;
// OverLapしてなければ、何もしない
if (CurrNodeEndInd < pSearchStaInd || pSearchEndInd < CurrNodeStaInd) {
return;
}
// 完全に含んでいれば、遅延配列に値を入れた後に評価
if (pSearchStaInd <= CurrNodeStaInd && CurrNodeEndInd <= pSearchEndInd) {
mLazyArr[pCurrNode] = pUpdateVal;
LazyEval(pCurrNode);
return;
}
// そうでなければ、2つの区間に再帰呼出し
long ChildNode1 = DeriveChildNode(pCurrNode);
long ChildNode2 = ChildNode1 + 1;
RangeUpdate(pSearchStaInd, pSearchEndInd, pUpdateVal, ChildNode1);
RangeUpdate(pSearchStaInd, pSearchEndInd, pUpdateVal, ChildNode2);
mTreeNodeArr[pCurrNode] = mTreeNodeArr[ChildNode1] + mTreeNodeArr[ChildNode2];
}
// 開始添字と終了添字とカレントノードを引数として、Sumを返す
internal long Query(long pSearchStaInd, long pSearchEndInd, long pCurrNode)
{
// 該当ノードを遅延評価する
LazyEval(pCurrNode);
long CurrNodeStaInd = mRangeInfo[pCurrNode].StaInd;
long CurrNodeEndInd = mRangeInfo[pCurrNode].EndInd;
// OverLapしてなければ、0
if (CurrNodeEndInd < pSearchStaInd || pSearchEndInd < CurrNodeStaInd)
return 0;
// 完全に含んでいれば、このノードの値
if (pSearchStaInd <= CurrNodeStaInd && CurrNodeEndInd <= pSearchEndInd)
return mTreeNodeArr[pCurrNode];
// そうでなければ、2つの子のSum
long ChildNode1 = DeriveChildNode(pCurrNode);
long ChildNode2 = ChildNode1 + 1;
long ChildVal1 = Query(pSearchStaInd, pSearchEndInd, ChildNode1);
long ChildVal2 = Query(pSearchStaInd, pSearchEndInd, ChildNode2);
return ChildVal1 + ChildVal2;
}
internal void DebugPrint()
{
for (long I = 0; I <= UB; I++) {
if (mLazyArr[I].HasValue) {
Console.WriteLine("mTreeNodeArr[{0}] = {1} , mLazyArr[{0}] = {2}",
I, mTreeNodeArr[I], mLazyArr[I]);
}
else {
Console.WriteLine("mTreeNodeArr[{0}] = {1}", I, mTreeNodeArr[I]);
}
}
}
}
#endregion