-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathamedie.cpp
97 lines (93 loc) · 1.99 KB
/
amedie.cpp
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
85
86
87
88
89
90
91
92
93
94
95
96
97
# include <cstdio>
# include <cstring>
# include <algorithm>
using namespace std;
int i,j,n,m,x,y,q,l,col,VV,mij,active;
int a[805][805], poz[805][805], ant[805*805], urm[805*805];
struct elem
{
int l,c,nr;
}E,v[805*805];
char c;
bool cmp (elem x, elem y)
{
if (x.nr>=y.nr) return 0;
return 1;
}
void procesare ()
{
sort (v+1, v+VV+1, cmp);
mij=(1+VV)/2; active=VV;
for (int i=1; i<=VV; ++i)
ant[i]=i-1, urm[i]=i+1, poz[v[i].l][v[i].c]=i;
}
void eliminare (int l, int c)
{
int i,M=poz[l][c];
urm[ant[M]]=urm[M]; ant[urm[M]]=ant[M];
if (active==1) { mij=0; return; }
if (poz[l][c]==mij)//se elimina chiar elementul median
{
if (active%2==1) mij=ant[mij];
else mij=urm[mij];
--active;
return;
}
if (poz[l][c]<mij)
{
if (active%2==0) mij=urm[mij];
--active;
return;
}
if (poz[l][c]>mij)
{
if (active%2==1) mij=ant[mij];
--active;
return;
}
}
int main ()
{
freopen ("amedie.in", "r", stdin);
freopen ("amedie.out", "w", stdout);
scanf ("%d%d%d", &n, &m, &q);
for (i=1; i<=n; ++i)
for (j=1; j<=m; ++j)
{
scanf ("%d", &a[i][j]);
E.l=i; E.c=j; E.nr=a[i][j];
v[++VV]=E;
}
scanf ("\n");
procesare();
for (i=1; i<=q; ++i)
{
scanf ("%ch", &c);
if (c=='L')
{
scanf (" %d\n", &l);
for (j=1; j<=m; ++j)
if (a[l][j]!=-1)
{
eliminare(l,j);
a[l][j]=-1;
}
}
else if (c=='C')
{
scanf (" %d\n", &col);
for (j=1; j<=n; ++j)
if (a[j][col]!=-1)
{
eliminare(j,col);
a[j][col]=-1;
}
}
else if (c=='Q')
{
scanf ("\n");
printf ("%d\n", v[mij].nr);
}
}
return 0;
}