Submission #166734

#TimeUsernameProblemLanguageResultExecution timeMemory
166734egekabasZamjene (COCI16_zamjene)C++14
28 / 140
3234 ms57024 KiB
#include <bits/stdc++.h> #define ff first #define ss second #define pb push_back #define mp make_pair using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<ll, ll> pll; typedef pair<ull, ull> pull; typedef pair<ll, ll> pii; typedef pair<ld, ld> pld; ll plh[1000009]; ll valh[1000009]; ll prt[1000009]; ll sz[1000009]; ll mod = 1e16+7; unordered_map<ll, ll> sfind; ll n, q; ll ar[1000009]; ll sr[1000009]; vector<ll> un; ll pr = 0; ll find(ll x){ if(prt[x] == x) return x; return prt[x] = find(prt[x]); } void erase(ll x){ ll tmp = (plh[x]-valh[x]+mod)%mod; ll tmp2 = mod-tmp; sfind[tmp] -= sz[x]; if(tmp != 0) pr -= sz[x]*sfind[tmp2]; if(sfind[tmp] == 0) sfind.erase(tmp); if(sfind[tmp2] == 0) sfind.erase(tmp2); } void add(ll y){ ll tmp = (plh[y]-valh[y]+mod)%mod; ll tmp2 = mod-tmp; if(tmp != 0) pr += sz[y]*sfind[tmp2]; sfind[tmp] += sz[y]; if(sfind[tmp] == 0) sfind.erase(tmp); if(sfind[tmp2] == 0) sfind.erase(tmp2); } void merge(ll x, ll y){ x = find(x); y = find(y); if(x == y) return; erase(x); erase(y); prt[x] = y; plh[y] = (plh[y]+plh[x])%mod; valh[y] = (valh[y]+valh[x])%mod; sz[y] += sz[x]; add(y); } ll pw(ll y){ ll ret = 1; ll x = 47; ++y; while(y > 0){ if(y%2) ret = ret*x%mod; x = x*x%mod; y /= 2; } return ret; } ll hashval(ll x){ return pw(lower_bound(un.begin(), un.end(), x)- un.begin()); } ll hashpl(ll x){ return pw(lower_bound(un.begin(), un.end(), sr[x-1])- un.begin()); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); cin >> n >> q; for(ll i = 1; i <= n; ++i){ cin >> ar[i]; sr[i-1] = ar[i]; } sort(sr, sr+n); for(int i = 0; i < n; ++i){ if(un.size() > 0 && *(un.end()-1) == sr[i]) continue; un.pb(sr[i]); } for(ll i = 1; i <= n; ++i){ prt[i] = i; sz[i] = 1; plh[i] = hashpl(i); valh[i] = hashval(ar[i]); //cout << i << " " << hashpl(i) << " " << hashval(ar[i]) << "\n"; add(i); } while(q--){ ll t; cin >> t; if(t == 1){ ll a, b; cin >> a >> b; ll a1 = find(a); ll b1 = find(b); erase(a1); erase(b1); valh[a1] -= hashval(ar[a]); valh[b1] -= hashval(ar[b]); valh[a1] += hashval(ar[b]); valh[b1] += hashval(ar[a]); valh[a1] %= mod; valh[b1] %= mod; if(valh[a1] < 0) valh[a1] += mod; if(valh[b1] < 0) valh[b1] += mod; add(a1); add(b1); swap(ar[a], ar[b]); } if(t == 2){ ll a, b; cin >> a >> b; merge(a, b); } if(t == 3){ if(sfind[0] == n) cout << "DA\n"; else cout << "NE\n"; } if(t == 4){ cout << pr << "\n"; } } }
#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...