Submission #88457

# Submission time Handle Problem Language Result Execution time Memory
88457 2018-12-06T02:16:49 Z jasony123123 Zamjene (COCI16_zamjene) C++11
28 / 140
6000 ms 62916 KB
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define FOR(i,start,end) for(int i=start;i<(int)(end);i++)
#define FORE(i,start,end) for(int i=start;i<=(int)end;i++)
#define RFOR(i,start,end) for(int i = start; i>end; i--)
#define RFORE(i,start,end) for(int i = start; i>=end; i--)
#define all(a) a.begin(), a.end()
#define mt make_tuple
#define v vector
#define sf scanf
#define pf printf
#define dvar(x) cout << #x << " := " << x << "\n"
#define darr(x,n) FOR(i,0,n) cout << #x << "[" << i << "]" << " := " << x[i] << "\n"
typedef long long ll;
typedef long double ld;
typedef pair<int, int > pii;
typedef pair<ll, ll> pll;
const ll MOD = 1000000007LL;
const ll PRIME = 105943LL;
const ll INF = 1e18;
//template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<class T> void minn(T &a, T b) { a = min(a, b); }
template<class T> void maxx(T &a, T b) { a = max(a, b); }
void io() {
#ifdef LOCAL_PROJECT 
	freopen("input.in", "r", stdin); freopen("output.out", "w", stdout);
#else 
	/* online submission */
#endif 
	ios_base::sync_with_stdio(false); cin.tie(NULL);
}
/**************************COCI 2016-2017 Contest 2 Problem 5 *************************/

ll modd(ll x) {
	x %= MOD;
	x += MOD;
	x %= MOD;
	return x;
}
ll expo[1000001];

struct Hash {
	ll val = 0;
	int sze = 0;

	Hash() {

	}
	Hash(int p) {
		assert(p >= 0);
		if (p == 0) val = 0;
		else val = (expo[p]);
		sze++;
	}
	int size() {
		return sze;
	}

	void removeElement(int x) {
		val -= expo[x];
		val = modd(val);
	}
	void addElement(int x) {
		val += expo[x];
		val = modd(val);
	}

	Hash operator+ (const Hash &other) {
		Hash nw;
		nw.val = val + other.val;
		nw.val = modd(nw.val);
		nw.sze = sze + other.sze;
		return nw;
	}

	ll operator- (const Hash &other) {
		return modd(val - other.val);
	}

};

int N, Q;

namespace DSU {
	const int SZ = 1000000;
	int p[SZ], q[SZ];
	int par[SZ];
	pair<Hash, Hash> compHash[SZ];
	map<ll, int> cnt;

	void init() {
		FOR(i, 0, N) {
			par[i] = i;
			compHash[i] = { Hash(p[i]), Hash(q[i]) };
			cnt[compHash[i].first - compHash[i].second] += 1;
		}
	}

	int find(int x) {
		if (par[x] != x) par[x] = find(par[x]);
		return par[x];
	}

	void deleteHash(int idx) {
		cnt[compHash[idx].first - compHash[idx].second] -= compHash[idx].first.size();
	}
	void addHash(int idx) {
		cnt[compHash[idx].first - compHash[idx].second] += compHash[idx].first.size();
	}

	void swapPos(int a, int b) {
		int ca = find(a);
		int cb = find(b);
		if (ca == cb) return;
		deleteHash(ca), deleteHash(cb);

		compHash[ca].first.removeElement(p[a]);
		compHash[ca].first.addElement(p[b]);
		compHash[cb].first.removeElement(p[b]);
		compHash[cb].first.addElement(p[a]);

		addHash(ca), addHash(cb);
		swap(p[a], p[b]);
	}

	void connect(int a, int b) {
		a = find(a), b = find(b);
		if (a == b) return;
		par[b] = a;
		deleteHash(a), deleteHash(b);
		compHash[a] = { compHash[a].first + compHash[b].first, compHash[a].second + compHash[b].second };
		addHash(a);
	}
	bool checkSort() {
		return cnt[0LL] == N;
	}
	int checkComp() {
		int ans = 0;
		for (auto& entry : cnt) if(entry.first!=0){
			ans += entry.second*cnt[modd(-entry.first)];
		}
		return ans / 2;
	}
}

int main() {
	io();
	expo[0] = 1;
	FOR(i, 1, 1000001) {
		expo[i] = modd(expo[i - 1] * PRIME);
	}
	cin >> N >> Q;
	FOR(i, 0, N) {
		cin >> DSU::p[i];
		DSU::q[i] = DSU::p[i];
	}
	sort(DSU::q, DSU::q + N);
	DSU::init();


	FOR(i, 0, Q) {
		int t;
		cin >> t;
		if (t == 1) {
			int a, b;
			cin >> a >> b;
			a--, b--;
			DSU::swapPos(a, b);
		}
		else if (t == 2) {
			int a, b;
			cin >> a >> b;
			a--, b--;
			DSU::connect(a, b);
		}
		else if (t == 3) {
			cout << (DSU::checkSort() ? "DA\n" : "NE\n");
		}
		else if (t == 4) {
			cout << DSU::checkComp() << "\n";
		}
		else {
			assert(0);
		}
	}
	return 0;
}
/*
...
*/

# Verdict Execution time Memory Grader output
1 Incorrect 46 ms 39544 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 48 ms 39736 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 46 ms 39804 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 44 ms 39804 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 42 ms 39844 KB Output is correct
2 Correct 49 ms 40048 KB Output is correct
3 Correct 70 ms 40144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 123 ms 40380 KB Output is correct
2 Correct 163 ms 40380 KB Output is correct
3 Correct 292 ms 40660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1161 ms 43032 KB Output is correct
2 Execution timed out 6047 ms 44336 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 6095 ms 50808 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 6011 ms 60224 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 6033 ms 62916 KB Time limit exceeded
2 Halted 0 ms 0 KB -