Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // clang-format off
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <vector>
- #include <string>
- #include <algorithm>
- #include <cmath>
- #include <stack>
- #include <iomanip>
- #include <fstream>
- #include <string>
- #include <set>
- #include <deque>
- #include <queue>
- #include <map>
- #include <bitset>
- #include <random>
- #include <list>
- #include <unordered_map>
- #include <unordered_set>
- #include <cassert>
- using namespace std;
- typedef long long ll;
- typedef unsigned long long ull;
- typedef long double ld;
- typedef string str;
- //typedef __int128 ultraint;
- #define sqrt sqrtl
- #define F first
- #define S second
- #define endl '\n'
- #define all(vc666) vc666.begin(), vc666.end()
- #define allr(vc666) vc666.rbegin(), vc666.rend()
- #define int long long
- #define degug(x) cerr (#x) << " " << (x) << endl;
- const ll INF = (ll)1e18;
- const ll inf = 1e9 + 7;
- const ll ONE = 1;
- const ll mod = 998244353;
- const ll LG = 20;
- ld EPS = 1e-12;
- ld PI = 3.1415926535897932384;
- mt19937_64 gen(rand() + rand());
- void solve() {
- int n, m, i, j, k, hsh = 0, B = 10;
- cin >> n >> m;
- unordered_map <int, vector <int> > zxc(2 * n * m);
- vector <vector <int> > a(n, vector <int>(m));
- vector <vector <int> > b;
- unordered_map <int, vector <int> > cursed(2 * n * m);
- unordered_set <int> zxc2(2 * n * m);
- for (i = 0; i < n; i++) {
- hsh = 0;
- for (j = 0; j < m; j++) {
- cin >> a[i][j];
- hsh = hsh * B + a[i][j];
- }
- cursed[hsh].push_back(i);
- if (zxc2.find(hsh) != zxc2.end()) continue;
- zxc2.insert(hsh);
- b.push_back(a[i]);
- }
- for (i = 0; i < b.size(); i++) {
- hsh = 0;
- for (j = 0; j < m; j++) {
- hsh = hsh * B + b[i][j];
- zxc[hsh].push_back(i);
- }
- }
- vector <int> ans(n, 0), ans2(b.size(), 0);
- for (i = 0; i < b.size(); i++) {
- hsh = 0;
- for (k = 1; k <= m; k++) {
- j = find(all(b[i]), k) - b[i].begin() + 1;
- hsh = hsh * B + j;
- if (zxc.find(hsh) != zxc.end()) {
- for (auto it : zxc[hsh]) {
- ans2[it] = max(ans2[it], k);
- }
- }
- else {
- break;
- }
- }
- }
- for (i = 0; i < b.size(); i++) {
- hsh = 0;
- for (j = 0; j < m; j++) {
- hsh = hsh * B + b[i][j];
- }
- for (auto it : cursed[hsh]) {
- ans[it] = ans2[i];
- }
- }
- for (i = 0; i < n; i++) {
- cout << ans[i] << " ";
- }
- cout << endl;
- }
- signed main() {
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- ios_base::sync_with_stdio(0);
- cin.tie(NULL);
- cout.tie(NULL);
- int t = 1;
- cin >> t;
- while (t--) solve();
- }
- //Deisgned by skimono
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement