Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- using ll = long long;
- mt19937 mt(736);
- void solve(istream &cin = std::cin, ostream &cout = std::cout)
- {
- int n, len, m;
- cin >> n >> len >> m;
- vector<string> trains(n);
- for (auto &s : trains)
- cin >> s;
- const int let = 26;
- mt19937_64 rng;
- vector<vector<ll>> cost(len, vector<ll>(let));
- for (auto &row : cost)
- {
- for (auto &x : row)
- x = rng() % (1LL << 60);
- }
- vector<ll> cur_cost(n);
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < len; j++)
- cur_cost[i] ^= cost[j][trains[i][j] - 'a'];
- }
- vector<int> best(n), cur(n);
- for (int i = 0; i < n; i++)
- cur[i] = count(cur_cost.begin(), cur_cost.end(), cur_cost[i]);
- for (int tmr = 0; tmr < m; tmr++)
- {
- int p1, w1, p2, w2;
- cin >> p1 >> w1 >> p2 >> w2;
- --p1, --w1, --p2, --w2;
- for (int it = 0; it < 2; it++)
- {
- if (it == 1 && p1 == p2)
- continue;
- for (int i = 0; i < n; i++)
- cur[i] -= (cur_cost[i] == cur_cost[it == 0 ? p1 : p2]);
- }
- cur_cost[p1] ^= cost[w1][trains[p1][w1] - 'a'];
- cur_cost[p2] ^= cost[w2][trains[p1][w1] - 'a'];
- cur_cost[p1] ^= cost[w1][trains[p2][w2] - 'a'];
- cur_cost[p2] ^= cost[w2][trains[p2][w2] - 'a'];
- swap(trains[p1][w1], trains[p2][w2]);
- cerr << "tmr = " << tmr + 1 << endl;
- for (auto x : cur_cost)
- cerr << x << ' ';
- cerr << endl;
- for (int it = 0; it < 2; it++)
- {
- if (it == 1 && p1 == p2)
- continue;
- for (int i = 0; i < n; i++)
- cur[i] += (cur_cost[i] == cur_cost[it == 0 ? p1 : p2]);
- }
- for (int i = 0; i < n; i++)
- best[i] = max(best[i], cur[i]);
- }
- for (int i = 0; i < n; i++)
- cout << best[i] << "\n";
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout << fixed;
- #ifdef LOCAL
- auto st = clock();
- ifstream fin("../input.txt");
- solve(fin);
- cout << "clock: " << setprecision(4) << (clock() - st) / (double) CLOCKS_PER_SEC << endl;
- #else
- solve();
- #endif
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement