This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")
#define F first
#define S second
#define sz(x) int(x.size())
#define pb push_back
#define N 1005
#define M ll(1e9 + 7)
#define inf 1e9 + 1e9
using namespace std;
//using namespace __gnu_pbds;
typedef long double ld;
typedef long long ll;
typedef short int si;
typedef array <int, 2> a2;
//typedef tree <int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
int kol[N][N], id[N], rk[N], pr[N], idr[N], n, q, a[N], srt[N];
vector <int> gr[N];
int fnd(int v) {if (pr[v] != v) pr[v] = fnd(pr[v]); return pr[v];}
void link(int a, int b)
{
if (rk[a] < rk[b]) swap(a, b);
rk[a] += rk[b];
pr[b] = a;
for (int i = 0; i < N; i++) kol[a][i] += kol[b][i];
}
void add(int a, int b)
{
a--; b--;
a = fnd(a); b = fnd(b);
if (a != b) link(a, b);
}
bool can()
{
int koler[N][N];
memset(koler, 0, sizeof(koler));
for (int i = 0; i < n; i++)
{
int j = fnd(i);
if (j != fnd(id[a[i]])) return 0;
if (koler[j][id[a[i]]] == kol[j][id[a[i]]]) return 0;
koler[j][id[a[i]]]++;
}
return 1;
}
int calc()
{
int ans = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (fnd(i) != fnd(j)) ans++;
return ans;
}
int main()
{
//freopen("input.txt", "r", stdin); //freopen("output4.txt", "w", stdout);
ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> q;
for (int i = 0; i < n; i++) {cin >> a[i]; srt[i] = a[i];}
sort(srt, srt + n);
for (int i = 0; i < n; i++) {id[srt[i]] = i; idr[i] = i; if (i != 0 && srt[i] == srt[i - 1]) {idr[i] = idr[i - 1]; id[srt[i]] = id[srt[i - 1]]; }}
for (int i = 0; i < n; i++) {pr[i] = i; rk[i] = 1; kol[i][idr[i]] = 1; gr[i].pb(i);}
for (; q > 0; q--)
{
int tp;
cin >> tp;
if (tp == 1) {int l, r; cin >> l >> r; l--; r--; swap(a[l], a[r]); continue;}
if (tp == 2) {int l, r; cin >> l >> r; l--; r--; add(l, r); continue;}
if (tp == 3) {if (can()) cout << "DA" << '\n'; else cout << "NE" << '\n'; continue;}
cout << calc() << '\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |