about summary refs log tree commit diff
path: root/tht/C/TP-2018/mab.cpp
blob: 7b8dbb84fbce1f9cf9de84490d2b91bfa712c29e (plain) (blame)
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
#include <iostream>
#include <fstream>
#include <set>
#include <vector>
using namespace std;

int
main()
{
  ifstream infile;
  infile.open("MAB.INP");
  int m, n, k;
  infile >> m >> n >> k;

  int i, j;
  long tmp;
  vector<vector<long>> a;
  a.resize(m);
  vector<set<long>> r;
  r.resize(m);
  vector<set<long>> c;
  c.resize(n);
  for (i = 0; i < m; i++)
    for (j = 0; j < n; j++)
      {
        infile >> tmp;
        a[i].push_back(tmp);
        r[i].insert(tmp);
        c[j].insert(tmp);
      }

  vector<vector<long>> rows, columns;
  rows.resize(m);
  for (i = 0; i < m; i++)
    for (auto& item : r[i])
      rows[i].push_back(item);
  columns.resize(n);
  for (i = 0; i < n; i++)
    for (auto& item : c[i])
      columns[i].push_back(item);

  int alpha, beta;
  vector<vector<long>> mem;
  mem.resize(m);
  for (i = 0; i < m; i++)
    mem[i].resize(n);
  for (i = 0; i < m; i++)
    for (j = 0; j < n; j++)
      {
        tmp = a[i][j];
        alpha = lower_bound(rows[i].begin(), rows[i].end(), tmp) - rows[i].begin();
        beta = lower_bound(columns[j].begin(), columns[j].end(), tmp) - columns[j].begin();
        mem[alpha][n - beta - 1]++;
      }
  for (i = 0; i < m; i++)
    for (j = 1; j < n; j++)
      mem[i][j] += mem[i][j - 1];
  for (i = 1; i < m; i++)
    for (j = 0; j < n; j++)
      mem[i][j] += mem[i - 1][j];

  ofstream outfile;
  outfile.open("MAB.OUT");
  while (k--)
    {
      infile >> alpha >> beta;
      if (alpha == m)
        alpha--;
      if (beta == n)
        beta--;
      outfile << mem[alpha][beta] << endl;
    }

  infile.close();
  outfile.close();
  return 0;
}