1789C - Serval and Toxel's Arrays - CodeForces Solution


combinatorics implementation

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
#define pob pop_back
#define b back
#define cout(x) cout << x << endl
#define lb long double
#define soa(arr, n) sort(arr,arr+n)
#define inputVector(v, n) for(ll i = 0; i < n; i++) cin >> v[i]
#define sov(v) sort(v.begin(), v.end())
#define rev(v) reverse(v.begin(), v.end())
#define pl pair<ll, ll>
#define all(a)    a.begin(), a.end()
#define vpp vector<pair<ll, ll>>
#define fastIo ios_base::sync_with_stdio(false), cin.tie(0)
#define endl '\n'
#define No cout << "NO\n"; 
#define Yes cout << "YES\n";
typedef long long int  ll;
ll P = 1e9 + 7;

long long solve() {
   ll n, m;
   cin >> n >> m;
   vector<ll> v(n);
   vector<ll> freq(n + m + 1);
   vector<ll> firstOccurrence(n + m + 1);
   for(ll i = 0; i < n; i ++) {
    cin >> v[i];
    firstOccurrence[v[i]] = 0;
   }
   ll a, b;
   
   ll currRepititions = 0;
   vector<ll> temp(n);
   ll ans = 0;
   for(ll i = 0; i < m; i ++) {
    cin >> a >> b;
    
    a--;

    ll prevVal = v[a];

    ll prevFreq = i + 1 - firstOccurrence[prevVal];
    freq[prevVal] += prevFreq;
    v[a] = b;
    firstOccurrence[b] = i + 1;
    
    currRepititions -= temp[a];
    temp[a] = i + 1 - freq[b];
    
    currRepititions += temp[a];
    ans += currRepititions;
   }

   ll vals = (m + 1) * m;
   vals /= 2;
   cout << ans + vals * n << endl;
   return 0;
}

 int main() {
    ll t=1,i=1, n;
    fastIo;
    cin >> t;
    while(t--) {
        solve();
        i ++;
    }
    return 0;
}



Comments

Submit
0 Comments
More Questions

844B - Rectangles
1591A - Life of a Flower
1398C - Good Subarrays
629A - Far Relative’s Birthday Cake
1166A - Silent Classroom
1000B - Light It Up
218B - Airport
1463B - Find The Array
1538C - Number of Pairs
621B - Wet Shark and Bishops
476B - Dreamoon and WiFi
152C - Pocket Book
1681D - Required Length
1725D - Deducing Sortability
1501A - Alexey and Train
721B - Passwords
1263D - Secret Passwords
1371B - Magical Calendar
1726E - Almost Perfect
1360C - Similar Pairs
900A - Find Extra One
1093D - Beautiful Graph
748A - Santa Claus and a Place in a Class
1511B - GCD Length
676B - Pyramid of Glasses
597A - Divisibility
1632A - ABC
1619D - New Year's Problem
242B - Big Segment
938A - Word Correction