fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const int MAX_SIZE = 1000;
  5.  
  6. int main() {
  7. int n, m, q, mt[MAX_SIZE + 1][MAX_SIZE + 1], v[MAX_SIZE + 1];
  8. cin >> n >> m;
  9. for (int i = 1; i <= n; ++i) {
  10. for (int j = 1; j <= m; ++j) {
  11. cin >> mt[i][j] ;
  12. }
  13. }
  14. cin >> q;
  15. while (q) {
  16. for (int j = 1; j <= m; ++j) {
  17. cin >> v[j];
  18. }
  19. int stanga = 1, dreapta = n, midle;
  20. while (stanga < dreapta) {
  21. midle = (stanga + dreapta) / 2 + 1;
  22. if (mt[midle][1] > v[1]) {
  23. dreapta = midle - 1;
  24. } else {
  25. stanga = midle;
  26. }
  27. }
  28. // cout << mt[stanga][1] <<" " << v[1] <<" ";
  29. int counter = 0;
  30. // if (mt[stanga][1] == v[1]) {
  31. while (mt[stanga][1] == v[1] && counter < m) {
  32. for (int index = 1; index <= m; ++index) {
  33. if (mt[stanga][index] == v[index]) {
  34. ++counter;
  35. // cout << counter <<" "<< mt[stanga][index] <<" " << v[index] <<" \n";;
  36. } else {
  37. index = m;
  38. }
  39.  
  40. }
  41.  
  42. if (counter == m) {
  43. counter = m;
  44. } else {
  45. --stanga;
  46. counter = 0;
  47. }
  48. }
  49. //}
  50. //cout << counter <<" ";
  51. if (counter == m) {
  52. cout << "DA\n";
  53. } else {
  54. cout << "NU\n";
  55. }
  56. --q;
  57. }
  58. return 0;
  59. }
Success #stdin #stdout 0.01s 5272KB
stdin
5 4 
1 2 3 4
2 1 3 5
3 3 3 3
4 1 1 6
4 2 1 8
3
4 2 1 8
2 1 3 4
3 3 3 3
stdout
DA
NU
DA