Codeforces Round #837 (Div. 2) 1771B. Hossam and Friends Solution
Problem Link: https://codeforces.com/problemset/problem/1771/B
Solution in C++:
- /// La ilaha illellahu muhammadur rasulullah
- ///******Bismillahir-Rahmanir-Rahim******///
- /// Abul Hasnat Tonmoy
- /// Department of CSE,23rd batch
- /// Islamic University,Bangladesh
- ///**********ALLAH IS ALMIGHTY************///
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long int
- int main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- ll t;
- cin >> t;
- while (t--) {
- ll n, m, x, y, ans = 0;
- cin >> n >> m;
- std::vector<ll> v(n + 1, -1);
- while (m--) {
- cin >> x >> y;
- if (x > y) swap(x, y);
- v[y] = max(v[y], x);
- }
- ll next = 1;
- for (ll i = 1; i <= n; i++) {
- next = max(next, v[i] + 1);
- ans += (i - next + 1);
- }
- cout << ans << endl;
- }
- }
No comments