# include <stdio.h>
# include <stdlib.h>
# include <stdbool.h>
typedef struct two_together{
int x;
int y;
int count;
}complex_array;
int comparefunc(const void *a,const void *b)
{
complex_array aa = *(complex_array*)a;
complex_array bb = *(complex_array*)b;
if (aa.x>bb.x)
{
return -1;
}
else if(aa.x == bb.x)
{
if(aa.y>bb.y)return 1;
else if(aa.y<bb.y)return -1;
else return 0;
}
else return 1;
}
int main(void)
{
complex_array hold[100000+10] = {{0},{0},{0}};
complex_array
*second_frequency
= malloc((100000+10)*sizeof(complex_array
));for (int i = 0; i < 100000+10; ++i)
{
second_frequency[i].x = 0;
second_frequency[i].y = 0;
second_frequency[i].count = 0;
}
int n,m,p,temp1,temp2,p_copy,i = 1,general_counter,ans = 0;
p_copy = p;
while(p--)
{
scanf("%d %d",&temp1
,&temp2
); hold[i].x = temp1;
hold[i].y = temp2;
hold[i].count++;
i++;
}
qsort(hold
+1,p_copy
,sizeof(complex_array
),comparefunc
); int j = 1;
for (int i = 1; i <=p_copy ;i++)//compressed array
{
int prev = i;
while(((hold[i].x == hold[i+1].x)&&(hold[i].y == hold[i+1].y)))
{
hold[prev].count++;i++;
}
second_frequency[j].x = hold[prev].x;
second_frequency[j].y = hold[prev].y;
second_frequency[j].count = hold[prev].count;
j++;
}
//for (int i = 1; i < j;++i)printf("%d %d %d\n",second_frequency[i].x,second_frequency[i].y,second_frequency[i].count );
int prev_y;
int prev_max_count;
ans =0;
general_counter = j-1;
for (int i = 1; i <=n ; ++i)//row 1 to n ans
{
if((second_frequency[general_counter].x==i)&&(general_counter>=1))
{
if (second_frequency[general_counter].y!=m)
{
if(second_frequency[general_counter].count>1)
{
while(second_frequency[general_counter].x==i)general_counter--;
}
else
{
prev_max_count = second_frequency[general_counter].count;
prev_y = second_frequency[general_counter].y;
general_counter--;
while((second_frequency[general_counter].x==i)&&(general_counter>=1))
{
if (second_frequency[general_counter].y+1 == prev_y)
{
if (!(second_frequency[general_counter].count<=prev_max_count+1))
{
break;
}
}
else if(second_frequency[general_counter].count>1)
{
}
prev_y = second_frequency[general_counter].y;
prev_max_count = second_frequency[general_counter].count;
general_counter--;
}
if (second_frequency[general_counter+1].x == i)//assuring loop doesnt break
{
if (second_frequency[general_counter+1].y == 1)
{
ans-=second_frequency[general_counter+1].count;
}
}
}
}
else
{
ans+=second_frequency[general_counter].count;
prev_max_count = second_frequency[general_counter].count;
prev_y = second_frequency[general_counter].y;
general_counter--;
while((second_frequency[general_counter].x==i)&&(general_counter>=1))
{
if (second_frequency[general_counter].y+1 == prev_y)
{
if (!(second_frequency[general_counter].count<=prev_max_count+1))
{
break;
}
}
else if(second_frequency[general_counter].count>1)
{
}
prev_y = second_frequency[general_counter].y;
prev_max_count = second_frequency[general_counter].count;
general_counter--;
}
if (second_frequency[general_counter+1].x == i)//assuring loop doesnt break
{
if (second_frequency[general_counter+1].y == 1)
{
ans-=second_frequency[general_counter+1].count;
}
}
}
}
else
{
}
ans = 0;
}
return 0;
}
IyBpbmNsdWRlIDxzdGRpby5oPgojIGluY2x1ZGUgPHN0ZGxpYi5oPgojIGluY2x1ZGUgPHN0ZGJvb2wuaD4KdHlwZWRlZiBzdHJ1Y3QgdHdvX3RvZ2V0aGVyewoJaW50IHg7CglpbnQgeTsKCWludCBjb3VudDsKCQp9Y29tcGxleF9hcnJheTsKCmludCBjb21wYXJlZnVuYyhjb25zdCB2b2lkICphLGNvbnN0IHZvaWQgKmIpCnsKCgljb21wbGV4X2FycmF5IGFhID0gKihjb21wbGV4X2FycmF5KilhOwoJY29tcGxleF9hcnJheSBiYiA9ICooY29tcGxleF9hcnJheSopYjsKCWlmIChhYS54PmJiLngpCgl7CgkJCgkJcmV0dXJuIC0xOwoJfQoJZWxzZSBpZihhYS54ID09IGJiLngpCgl7CgkJCgkJaWYoYWEueT5iYi55KXJldHVybiAxOwoJCWVsc2UgaWYoYWEueTxiYi55KXJldHVybiAtMTsKCQllbHNlIHJldHVybiAwOyAKCX0KCWVsc2UgcmV0dXJuIDE7Cn0KCmludCBtYWluKH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