1843E - Tracking Segments - CodeForces Solution


binary search data structures

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
#define ll long long
#define ull unsigned long long
#define umap unordered_map 
#define nel cout << "\n"
#define sz(s) s.size()
#define all(v) v.begin(),v.end()
#define T int ts; cin >> ts; while (ts--)
#define loop(c,n) for(int i=c;i<n;i++)
#define mem(a,n) memset((a),n,sizeof (a))
ll gcd(ll x, ll y) { return(!y ? x : gcd(y, x % y)); }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
int dx[] = { 0,0,-1,1,-1,1,-1,1,0 };
int dy[] = { -1,1,0,0,-1,-1,1,1,0 };
//.......................
int n, m;
vector<int>changes;
vector<pair<int, int>>qur;
//........................
bool valid(int mid)
{
    vector<int>vec(n + 2);
    for (int i = 0; i < mid; i++)
    {
        vec[changes[i]] = 1;
    }

    for (int i = 1; i <= n; i++)
    {
        vec[i] += vec[i - 1];
    }

    for (int i = 0; i < m; i++)
    {
        int x = qur[i].first, y = qur[i].second;
        int ones = vec[y] - vec[x - 1];
        int zeros = ((y - x) + 1 ) - ones;
        if (ones > zeros) return 1;
    }
    return 0;
}
//....................... 
void Joseph();
int main()
{
    Joseph();

    T{
    cin >> n >> m;
    qur.clear();
    qur.resize(m);
    for (int i = 0; i < m; i++)
    {
        cin >> qur[i].first >> qur[i].second;
    }

    int q; cin >> q;
    changes.clear();
    changes.resize(q);
    for (int i = 0; i < q; i++)
    {
        cin >> changes[i];
    }

    int l = 1, r = q, ans = -1;
    while (l <= r)
    {
        ll mid = (l + r) / 2;
        if (valid(mid))
        {
            r = mid - 1;
            ans = mid;
        }
        else
        {
            l = mid + 1;
        }
    }
    cout << ans; nel;
}


}
void Joseph() {
    // freopen("halfnice.in", "r", stdin);
    // freopen("output.txt", "w", stdout);      
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}


Comments

Submit
0 Comments
More Questions

1472B - Fair Division
1281C - Cut and Paste
141A - Amusing Joke
112A - Petya and Strings
677A - Vanya and Fence
1621A - Stable Arrangement of Rooks
472A - Design Tutorial Learn from Math
1368A - C+=
450A - Jzzhu and Children
546A - Soldier and Bananas
32B - Borze
1651B - Prove Him Wrong
381A - Sereja and Dima
41A - Translation
1559A - Mocha and Math
832A - Sasha and Sticks
292B - Network Topology
1339A - Filling Diamonds
910A - The Way to Home
617A - Elephant
48A - Rock-paper-scissors
294A - Shaass and Oskols
1213A - Chips Moving
490A - Team Olympiad
233A - Perfect Permutation
1360A - Minimal Square
467A - George and Accommodation
893C - Rumor
227B - Effective Approach
1534B - Histogram Ugliness