1475C - Ball in Berland - CodeForces Solution


combinatorics graphs math *1400

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define ull unsigned long long
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define rep(i, m, n) for (auto i = m; i < n; i++)
#define ppi pair<int, int>
#define ppl pair<ll, ll>
#define um(x, y) unordered_map<x, y>
#define us(x) unordered_set<x>
#define pb push_back
#define endl "\n"
#define all(v) (v).begin(), (v).end()
#define f first
#define INF 1000000000
#define ss second
#define lb lower_bound
#define up upper_bound
#define SET(n) cout << fixed << setprecision(n)
#define pi (double)3.14159265358
#define get(s) getline(cin, s)

ll binexp(ll a, ll b, ll m)
{
    ll res = 1;
    while (b > 0)
    {
        if (b & 1)
        {
            res = (res * 1LL * a) % m;
        }
        a = (a * 1LL * a) % m;
        b >>= 1;
    }
    return res;
}

ll binpow(ll a, ll b)
{
    if (b == 0)
        return 1;
    ll res = binpow(a, b / 2);
    if (b % 2)
        return res * res * a;
    else
        return res * res;
}
bool isPrime(long long int n)
{
    if (n <= 1)
        return false;

    for (long long int i = 2; i <= sqrtl(n); i++)
        if (n % i == 0)
            return false;

    return true;
}
ll modFact(ll n, ll p)
{
    if (n >= p)
        return 0;

    ll result = 1;
    for (int i = 1; i <= n; i++)
        result = (result * i) % p;

    return result;
}
ll count_max_power_of_2_greaterthan_n(ll n)
{
    ll res = 0, ans = 0;
    for (ll i = 1; i <= 60; i++)
    {
        ans = (1LL) << i;
        if (ans > n)
        {
            res = i;
            return res;
        }
    }
    return res;
}

void solve()
{
    ll x,y,k;
    cin>>x>>y>>k;
    ll a[k],b[k];
    map<ll,ll>mp,mpp;
    map<pair<ll,ll>,ll>m;
    rep(i,0,k) cin>>a[i],mp[a[i]]++;
    rep(i,0,k) cin>>b[i],mpp[b[i]]++,m[{a[i],b[i]}]++;
    ll ans=0;
    for(int i=0;i<k-1;i++)
    {
        ll p=mp[a[i]];
        ll q=mpp[b[i]];
        ll r=m[{a[i],b[i]}];
        ll z=(k-i)-(p-1)-(q-1)+r-1;
        if(z>0)
        {
            ans+=z-1;
            // cout<<(z*(z-1))/2<<endl;
        }
        mp[a[i]]--;
        mpp[b[i]]--;
        if(m.find({a[i],b[i]})!=m.end())
        {
           m[{a[i],b[i]}]--;
        }
    }
    cout<<ans<<endl;
}

int main()
{
    fast;
    ll T;
    cin >> T;
    // int T = 1;
    while (T > 0)
    {
        solve();
        T--;
    }

    return 0;
}


Comments

Submit
0 Comments
More Questions

1588. Sum of All Odd Length Subarrays
1662. Check If Two String Arrays are Equivalent
1832. Check if the Sentence Is Pangram
1678. Goal Parser Interpretation
1389. Create Target Array in the Given Order
1313. Decompress Run-Length Encoded List
1281. Subtract the Product and Sum of Digits of an Integer
1342. Number of Steps to Reduce a Number to Zero
1528. Shuffle String
1365. How Many Numbers Are Smaller Than the Current Number
771. Jewels and Stones
1512. Number of Good Pairs
672. Richest Customer Wealth
1470. Shuffle the Array
1431. Kids With the Greatest Number of Candies
1480. Running Sum of 1d Array
682. Baseball Game
496. Next Greater Element I
232. Implement Queue using Stacks
844. Backspace String Compare
20. Valid Parentheses
746. Min Cost Climbing Stairs
392. Is Subsequence
70. Climbing Stairs
53. Maximum Subarray
1527A. And Then There Were K
1689. Partitioning Into Minimum Number Of Deci-Binary Numbers
318. Maximum Product of Word Lengths
448. Find All Numbers Disappeared in an Array
1155. Number of Dice Rolls With Target Sum