Submission #419281

# Submission time Handle Problem Language Result Execution time Memory
419281 2021-06-06T18:03:54 Z SirCovidThe19th Zamjene (COCI16_zamjene) C++14
140 / 140
3596 ms 164976 KB
#include <bits/stdc++.h>
using namespace std;
 
#define ll long long
 
const ll mx = 1e6+5, p = 6291469;
 
int n, q; int vals[mx], sorted[mx];
int par[mx], sz[mx]; ll pPow[mx], cnOrig[mx], cnSort[mx];
unordered_map<ll, ll> diff; ll cnt;
 
int find(int node) { return (node == par[node]) ? node : par[node] = find(par[node]); }
void upd(int i, int change){
	change *= sz[i];
	if (cnOrig[i] != cnSort[i]) cnt += diff[cnSort[i]-cnOrig[i]]*(ll)change;
	diff[cnOrig[i]-cnSort[i]] += change;
}
void merge(int a, int b){
	a = find(a); b = find(b);
    if (a == b) return; 
    if (sz[a] > sz[b]) swap(a, b);
	upd(a, -1); upd(b, -1);
	par[a] = b; sz[b] += sz[a]; cnOrig[b] += cnOrig[a]; cnSort[b] += cnSort[a];
	upd(b, 1);
}
void Swap(int a, int b){
    int parA = find(a), parB = find(b);
	upd(parA, -1); upd(parB, -1);
	cnOrig[parA] = (cnOrig[parA]-pPow[vals[a]]+pPow[vals[b]]); 
	cnOrig[parB] = (cnOrig[parB]-pPow[vals[b]]+pPow[vals[a]]); 
	upd(parA, 1); upd(parB, 1);
	swap(vals[a], vals[b]);
}
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); 
	cin >> n >> q; pPow[0] = 1; 
	for (int i = 0; i < n; i++){ 
		cin >> vals[i]; sorted[i] = vals[i]; 
		pPow[i+1] = (pPow[i]*p); 
	}
	sort(sorted, sorted+n); iota(par, par+n, 0); fill(sz, sz+n, 1);
	for (int i = 0; i < n; i++){
		cnOrig[i] = pPow[vals[i]], cnSort[i] = pPow[sorted[i]]; 
		diff[cnOrig[i]-cnSort[i]]++;
	}
    unordered_map<int, bool> visited;
	for (int i = 0; i < n; i++){
		if (cnOrig[i] != cnSort[i] and !visited[cnOrig[i]-cnSort[i]]){
			cnt += diff[cnOrig[i]-cnSort[i]]*diff[cnSort[i]-cnOrig[i]];
            visited[cnOrig[i]-cnSort[i]] = true; visited[cnSort[i]-cnOrig[i]] = true;
        }
    }
	for (int i = 0; i < q; i++){
		int t, a, b; cin >> t; if (t <= 2) { cin >> a >> b; a--; b--; }
		if (t == 1) Swap(a, b);
		if (t == 2) merge(a, b);
		if (t == 3) cout<<((diff[0] == n) ? "DA":"NE")<<endl;
		if (t == 4) cout<<cnt<<endl;
	}
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 2 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 828 KB Output is correct
2 Correct 15 ms 828 KB Output is correct
3 Correct 13 ms 844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 141 ms 4540 KB Output is correct
2 Correct 148 ms 5788 KB Output is correct
3 Correct 158 ms 7952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1561 ms 32488 KB Output is correct
2 Correct 3167 ms 126744 KB Output is correct
3 Correct 3596 ms 164976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1901 ms 67708 KB Output is correct
2 Correct 2628 ms 94156 KB Output is correct
3 Correct 1884 ms 80840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1583 ms 45664 KB Output is correct
2 Correct 2059 ms 70404 KB Output is correct
3 Correct 1903 ms 80908 KB Output is correct