Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3.  
  4. using namespace std;
  5.  
  6.  
  7. using ll = long long;
  8.  
  9.  
  10. mt19937 mt(736);
  11.  
  12.  
  13. void solve(istream &cin = std::cin, ostream &cout = std::cout)
  14. {
  15. int n, len, m;
  16. cin >> n >> len >> m;
  17.  
  18. vector<string> trains(n);
  19. for (auto &s : trains)
  20. cin >> s;
  21.  
  22. const int let = 26;
  23. mt19937_64 rng;
  24.  
  25. vector<vector<ll>> cost(len, vector<ll>(let));
  26. for (auto &row : cost)
  27. {
  28. for (auto &x : row)
  29. x = rng() % (1LL << 60);
  30. }
  31.  
  32. vector<ll> cur_cost(n);
  33. for (int i = 0; i < n; i++)
  34. {
  35. for (int j = 0; j < len; j++)
  36. cur_cost[i] ^= cost[j][trains[i][j] - 'a'];
  37. }
  38.  
  39. vector<int> best(n), cur(n);
  40.  
  41. for (int i = 0; i < n; i++)
  42. cur[i] = count(cur_cost.begin(), cur_cost.end(), cur_cost[i]);
  43.  
  44. for (int tmr = 0; tmr < m; tmr++)
  45. {
  46. int p1, w1, p2, w2;
  47. cin >> p1 >> w1 >> p2 >> w2;
  48. --p1, --w1, --p2, --w2;
  49.  
  50. for (int it = 0; it < 2; it++)
  51. {
  52. if (it == 1 && p1 == p2)
  53. continue;
  54.  
  55. for (int i = 0; i < n; i++)
  56. cur[i] -= (cur_cost[i] == cur_cost[it == 0 ? p1 : p2]);
  57. }
  58.  
  59. cur_cost[p1] ^= cost[w1][trains[p1][w1] - 'a'];
  60. cur_cost[p2] ^= cost[w2][trains[p1][w1] - 'a'];
  61. cur_cost[p1] ^= cost[w1][trains[p2][w2] - 'a'];
  62. cur_cost[p2] ^= cost[w2][trains[p2][w2] - 'a'];
  63. swap(trains[p1][w1], trains[p2][w2]);
  64.  
  65. cerr << "tmr = " << tmr + 1 << endl;
  66. for (auto x : cur_cost)
  67. cerr << x << ' ';
  68. cerr << endl;
  69.  
  70. for (int it = 0; it < 2; it++)
  71. {
  72. if (it == 1 && p1 == p2)
  73. continue;
  74.  
  75. for (int i = 0; i < n; i++)
  76. cur[i] += (cur_cost[i] == cur_cost[it == 0 ? p1 : p2]);
  77. }
  78.  
  79. for (int i = 0; i < n; i++)
  80. best[i] = max(best[i], cur[i]);
  81. }
  82.  
  83. for (int i = 0; i < n; i++)
  84. cout << best[i] << "\n";
  85. }
  86.  
  87. int main()
  88. {
  89. ios_base::sync_with_stdio(false);
  90. cin.tie(nullptr);
  91.  
  92. cout << fixed;
  93.  
  94. #ifdef LOCAL
  95. auto st = clock();
  96.  
  97. ifstream fin("../input.txt");
  98.  
  99. solve(fin);
  100.  
  101. cout << "clock: " << setprecision(4) << (clock() - st) / (double) CLOCKS_PER_SEC << endl;
  102. #else
  103. solve();
  104. #endif
  105.  
  106. return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement