Submission #88457

#TimeUsernameProblemLanguageResultExecution timeMemory
88457jasony123123Zamjene (COCI16_zamjene)C++11
28 / 140
6095 ms62916 KiB
#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 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...