제출 #550964

#제출 시각아이디문제언어결과실행 시간메모리
550964BungmintZamjene (COCI16_zamjene)C++17
0 / 140
3001 ms98984 KiB
//Copyright © 2022 Youngmin Park. All rights reserved.
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
using vpi = vector<pii>;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vpl = vector<pll>;
using ld = long double;
template <typename T, size_t SZ>
using ar = array<T, SZ>;

#define all(v) (v).begin(), (v).end()
#define pb push_back
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i, a) ROF(i, 0, a)
#define REP(a) F0R(_, a)

const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1e9 + 7; //998244353;
const ld PI = acos((ld)-1.0);
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T>
using pqg = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <typename T>
bool ckmax(T &a, const T &b) { return b > a ? a = b, 1 : 0; }

template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p)
{
    return os << '(' << p.first << ", " << p.second << ')';
}
template <typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type>
ostream &operator<<(ostream &os, const T_container &v)
{
    os << '{';
    string sep;
    for (const T &x : v)
        os << sep << x, sep = ", ";
    return os << '}';
}
void dbg_out()
{
    cerr << endl;
}
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T)
{
    cerr << ' ' << H;
    dbg_out(T...);
}
#ifdef LOCAL
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...) 42
#endif

inline namespace RecursiveLambda{
	template <typename Fun>
	struct y_combinator_result{
		Fun fun_;
		template <typename T> 
		explicit y_combinator_result(T &&fun): fun_(forward<T>(fun)){}
		template <typename...Args>
		decltype(auto) operator()(Args &&...args){
			return fun_(ref(*this), forward<Args>(args)...);
		}
	};
	template <typename Fun>
	decltype(auto) y_combinator(Fun &&fun){
		return y_combinator_result<decay_t<Fun>>(forward<Fun>(fun));
	}
};

void setIO(string s) // USACO
{
	#ifndef LOCAL
	    freopen((s + ".in").c_str(), "r", stdin);
	    freopen((s + ".out").c_str(), "w", stdout);
	#endif
}

constexpr uint64_t mod = (1ull<<61) - 1;
constexpr uint64_t B = 1e9 + 7;
uint64_t modmul(uint64_t a, uint64_t b){
	uint64_t l1 = (uint32_t)a, h1 = a>>32, l2 = (uint32_t)b, h2 = b>>32;
	uint64_t l = l1*l2, m = l1*h2 + l2*h1, h = h1*h2;
	uint64_t ret = (l&mod) + (l>>61) + (h << 3) + (m >> 29) + (m << 35 >> 3) + 1;
	ret = (ret & mod) + (ret>>61);
	ret = (ret & mod) + (ret>>61);
	return ret-1;
}
const int N = 1e6 + 19;
int p[N], comp[N];
vi ind, a[N];
int co;
int par[N], SZ[N];
using ull = unsigned long long;
ull hsh[N], inv[N];
map<ull, int> freq;
ll ans{};


ull bin_pow(ull a, ull b) {
	ull res = 1;
	while (b) {
		if (b & 1) res = modmul(res, a);
		a = modmul(a, a);
		b >>= 1LL;
	}
	return res;
}
ull inverse(ull a) {
	return bin_pow(a, mod - 2);
}
int get(int u) {return par[u] == u ? u : par[u] = get(par[u]);}
void unite(int u, int v) {
	u = get(u), v = get(v);
	if (u == v) return;
	if (SZ[u] > SZ[v]) swap(u, v);
	freq[hsh[u]] -= SZ[u], freq[hsh[v]] -= SZ[v];
	if (hsh[u] != 1 && hsh[v] != 1 && modmul(hsh[u], hsh[v]) == 1) {
		ans -= 1LL * SZ[u] * SZ[v];
	}else{
		if (hsh[u] != 1) {
			ull z = inverse(hsh[u]);
			if (freq.count(z)) ans -= 1LL * SZ[u] * freq[z];
		}
		else if (hsh[v] != 1) {
			ull z = inverse(hsh[v]);
			if (freq.count(z)) ans -= 1LL * SZ[v] * freq[z];
		}
	}
	hsh[v] = modmul(hsh[v], hsh[u]);
	SZ[v] += SZ[u];
	par[u] = v;
	freq[hsh[v]] += SZ[v];
	if (hsh[v] != 1) {
		ull z = inverse(hsh[v]);
		if (freq.count(z)) ans += 1LL * SZ[v] * freq[z];
	}
	
	
}
void swaping(int u, int v) {
	u = get(u), v = get(v);
	if (u == v) return;
	freq[hsh[u]] -= SZ[u], freq[hsh[v]] -= SZ[v];
	if (hsh[u] != 1 && hsh[v] != 1 && modmul(hsh[u], hsh[v]) == 1) {
		ans -= 1LL * SZ[u] * SZ[v];
	}else{
		if (hsh[u] != 1) {
			ull z = inverse(hsh[u]);
			if (freq.count(z)) ans -= 1LL * SZ[u] * freq[z];
		}
		else if (hsh[v] != 1) {
			ull z = inverse(hsh[v]);
			if (freq.count(z)) ans -= 1LL * SZ[v] * freq[z];
		}
	}
	hsh[u] = modmul(hsh[u], modmul(inv[p[v]], B - p[u]));
	hsh[v] = modmul(hsh[v], modmul(inv[p[u]], B - p[v]));
	swap(p[u], p[v]);
	freq[hsh[u]] += SZ[u], freq[hsh[v]] += SZ[v];
	if (hsh[u] != 1 && hsh[v] != 1 && modmul(hsh[u], hsh[v]) == 1) {
		ans += 1LL * SZ[u] * SZ[v];
	}else{
		if (hsh[u] != 1) {
			ull z = inverse(hsh[u]);
			if (freq.count(z)) ans += 1LL * SZ[u] * freq[z];
		}
		else if (hsh[v] != 1) {
			ull z = inverse(hsh[v]);
			if (freq.count(z)) ans += 1LL * SZ[v] * freq[z];
		}
	}
}
void yes() {
	cout << "DA\n";
}
void no() {
	cout << "NE\n";
}


void solve()
{
	int n, q;
	cin >> n >> q;
	F0R(i, n) cin >> p[i], ind.pb(p[i]);
	sort(all(ind)), ind.resize(unique(all(ind)) - ind.begin());
	F0R(i, n) {
		p[i] = lb(all(ind), p[i]) - ind.begin();
		a[p[i]].pb(i);
	}
	F0R(i, N) {
		inv[i] = bin_pow((B - i), mod - 2);
	}
	iota(par, par + n, 0);
	fill(SZ, SZ + n, 1);
	F0R(i, sz(ind)) {
		REP(sz(a[i])) {
			comp[co++] = ind[i];
			dbg(ind[i]);
		}
	}
	F0R(i, sz(ind)) {
		int last = -1;
		for (auto &e : a[i]) {
			// p[e] = co++;
			dbg(comp[e], p[e]);
			p[e] = ind[p[e]];
			hsh[e] = modmul(B - comp[e], inv[p[e]]);
			freq[hsh[e]]++;
			if (hsh[e] != 1) {
				ull z = inverse(hsh[e]);
				if (freq.count(z)) {
					ans += freq[z];
				}
			}
		}
	}
	dbg(freq);
	REP(q) {
		int t;
		cin >> t;
		if (t == 1) {
			int i, j;
			cin >> i >> j, i--, j--;
			swaping(i, j);
		}else if (t == 2) {
			int i, j;
			cin >> i >> j, i--, j--;
			unite(i, j);
		}else if (t == 3) {
			if (freq.count(1) && freq[1] == n) {
				yes();
			}else{
				no();
			}
		}else{
			cout << ans << '\n';
		}
		dbg(freq);
	}
}

int main()
{
    cin.tie(0)->sync_with_stdio(0);
    cin.exceptions(cin.failbit);
    int testcase=1;
    // cin >> testcase;
    while (testcase--)
    {
        solve();
    }
}

컴파일 시 표준 에러 (stderr) 메시지

zamjene.cpp: In function 'void solve()':
zamjene.cpp:71:18: warning: statement has no effect [-Wunused-value]
   71 | #define dbg(...) 42
      |                  ^~
zamjene.cpp:220:4: note: in expansion of macro 'dbg'
  220 |    dbg(ind[i]);
      |    ^~~
zamjene.cpp:71:18: warning: statement has no effect [-Wunused-value]
   71 | #define dbg(...) 42
      |                  ^~
zamjene.cpp:227:4: note: in expansion of macro 'dbg'
  227 |    dbg(comp[e], p[e]);
      |    ^~~
zamjene.cpp:224:7: warning: unused variable 'last' [-Wunused-variable]
  224 |   int last = -1;
      |       ^~~~
zamjene.cpp:71:18: warning: statement has no effect [-Wunused-value]
   71 | #define dbg(...) 42
      |                  ^~
zamjene.cpp:239:2: note: in expansion of macro 'dbg'
  239 |  dbg(freq);
      |  ^~~
zamjene.cpp:71:18: warning: statement has no effect [-Wunused-value]
   71 | #define dbg(...) 42
      |                  ^~
zamjene.cpp:260:3: note: in expansion of macro 'dbg'
  260 |   dbg(freq);
      |   ^~~
zamjene.cpp: In function 'void setIO(std::string)':
zamjene.cpp:94:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   94 |      freopen((s + ".in").c_str(), "r", stdin);
      |      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
zamjene.cpp:95:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   95 |      freopen((s + ".out").c_str(), "w", stdout);
      |      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...