Repository navigation
Expand file tree
/
Copy pathCHEFBM.cpp
More file actions
85 lines (75 loc) · 1.53 KB
/
Copy pathCHEFBM.cpp
File metadata and controls
85 lines (75 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include<map>
#include<set>
#include<iostream>
#include<functional>
#include<algorithm>
#include<cstdio>
using namespace std;
struct inc //increment
{
int row;
int col;
bool operator < ( inc tmp ) const //for sorting the array
{
if (row==tmp.row) return col<tmp.col;
else return row<tmp.row;
}
};
inc increment[100000];
int ans[100000]; //the final answer to be printed
int main()
{
int tk;
int n,m,p;
cin>>n>>m>>p;
register int i,j;
int t1,t2;
for( i=0; i<n; i++)
{
ans[i]=m-1;
}
for( i=0 ; i<p; i++)
{
cin>>t1>>t2;
increment[i].row=t1;
increment[i].col=t2;
if( t2==m ) ans[t1-1]++;
if( t2==1) ans[t1-1]--;
}
sort(increment, increment+p);
//algo starts from here
int curr=1; //the current row
map<int,int> counter; //to track the additions
set<int> s; //stores the column number at which the element is adde
set<int>::iterator it;
for( i=0;i<p;i++) //for each addition
{
if( curr!=increment[i].row ) //reset all parameters
{
// row changed -> process the previous row
for( it=s.begin() ; it!=s.end() ; it++)
{
// * it is col number and curr is row number
if( (*it)!=m && (counter[*it]-counter[*it+1])>1 ) //error
{
ans[curr-1]=-1;
}
}
counter.clear();
s.clear();
curr=increment[i].row;
}
counter[ increment[i].col ]++;
s.insert( increment[i].col);
}
for( it=s.begin() ; it!=s.end() ; it++)
{
if( (*it)!=m && (counter[*it]-counter[*it+1])>1 ) //error
{
ans[curr-1]=-1;
}
}
for(i=0;i<n;i++)
cout<<ans[i]<<endl;
return 0;
}