Submission #166734

# Submission time Handle Problem Language Result Execution time Memory
166734 2019-12-03T14:35:07 Z egekabas Zamjene (COCI16_zamjene) C++14
28 / 140
3234 ms 57024 KB
#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 time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 3 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 3 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 504 KB Output is correct
2 Correct 3 ms 348 KB Output is correct
3 Incorrect 3 ms 376 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Incorrect 3 ms 376 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 3 ms 380 KB Output is correct
3 Incorrect 4 ms 504 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 13 ms 888 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 126 ms 5400 KB Output is correct
2 Incorrect 142 ms 5808 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1265 ms 28040 KB Output is correct
2 Incorrect 3234 ms 42012 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2019 ms 56580 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1417 ms 51976 KB Output is correct
2 Incorrect 2232 ms 57024 KB Output isn't correct
3 Halted 0 ms 0 KB -