Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Author : Naveen
- // Program Start
- // Libraries and Namespace Start
- #include <bits/stdc++.h>
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- using namespace std;
- using namespace __gnu_pbds;
- // Libraries and Namespace End
- //------------------------------------------------------------------------------------------
- // Important Shortcuts Start
- // Macros Start
- #define F first
- #define S second
- #define pb push_back
- #define yes cout << "yes\n"
- #define no cout << "no\n"
- #define Yes cout << "Yes\n"
- #define No cout << "No\n"
- #define YES cout << "YES\n"
- #define NO cout << "NO\n"
- #define fri(i, s, e) for (ll i = s; i < e; ++i)
- #define frd(i, e, s) for(ll i = e; i > s; --i)
- #define all(v) v.begin(), v.end()
- // Macros End
- // Custom Hash Function Start
- struct custom_hash {
- static uint64_t splitMix64(uint64_t x) {
- x += 0x9e3779b97f4a7c15;
- x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
- x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
- return x ^ (x >> 31);
- }
- size_t operator()(uint64_t x) const {
- static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
- return splitMix64(x + FIXED_RANDOM);
- }
- };
- // Custom Hash Function End
- // Declarations Start
- typedef bool bl;
- typedef long long int ll;
- typedef unsigned long long int ull;
- typedef long double ld;
- typedef string str;
- typedef pair<ll, ll> prll;
- typedef tuple<ll, ll, ll> tupll;
- typedef stack<ll> stkll;
- typedef queue<ll> quell;
- typedef vector<bl> vecbl;
- typedef vector<ll> vecll;
- typedef vector<prll> vecprll;
- typedef vector<tupll> vectupll;
- typedef vector<ld> vecld;
- typedef set<ll> setll;
- typedef multiset<ll> mltsetll;
- typedef map<ll, ll> mapll;
- typedef multimap<ll, ll> mltmapll;
- // Declarations End
- // Using Declarations Start
- template <typename T>
- using unord_set = unordered_set<T, custom_hash>;
- typedef unord_set<ll> unsetll;
- template <typename T>
- using unord_multiset = unordered_multiset<T, custom_hash>;
- typedef unord_multiset<ll> unmltsetll;
- template <typename T1, typename T2>
- using unord_map = unordered_map<T1, T2, custom_hash>;
- typedef unord_map<ll, ll> unmapll;
- template <typename T1, typename T2>
- using unord_multimap = unordered_multimap<T1, T2, custom_hash>;
- typedef unord_multimap<ll, ll> unmltmapll;
- template <typename T, typename Compare = greater<T>>
- using heap = priority_queue<T, vector<T>, Compare>;
- typedef heap<ll> minheapll;
- typedef heap<prll> minheapprll;
- typedef heap<ll, less<ll>> maxheapll;
- typedef heap<prll, less<prll>> maxheapprll;
- template <typename T, typename Compare = less<T>>
- using pbds_tree = tree<T, null_type, Compare, rb_tree_tag, tree_order_statistics_node_update>;
- typedef pbds_tree<ll> pbdssetll;
- typedef pbds_tree<ll, less_equal<ll>> pbdsmltsetll;
- typedef pbds_tree<prll> pbdsmapll;
- typedef pbds_tree<prll, less_equal<prll>> pbdsmltmapll;
- // Using Declarations End
- // Important Shortcuts End
- //------------------------------------------------------------------------------------------
- // Important Functions Start
- // Constants Start
- const ll inf = 9223372036854775807ll;
- const ll neg_inf = -9223372036854775807ll - 1ll;
- const ll mod1 = 1000000007ll;
- const ll mod2 = 998244353ll;
- const ll max_if = 1000000ll;
- const ll one = 1ll;
- const ll zero = 0ll;
- const str spc = " ";
- const str emp = "";
- const str newl = "\n";
- // Constants End
- // Input and Output Functions Start
- template <class U, class T, class V>
- U& operator>>(U& is, pair<T, V>& p) {
- is >> p.first >> p.second;
- return is;
- }
- template <class U, class T>
- U& operator>>(U& is, vector<T>& a) {
- for (T& x : a) {
- is >> x;
- }
- return is;
- }
- template <class U, class T>
- U& operator<<(U& os, vector<T>& a) {
- for (ll i = 0;i < a.size();++i) {
- os << a[i] << (i != a.size() - 1 ? spc : emp);
- }
- return os;
- }
- inline void yn(const bool con) {
- cout << (con ? "yes" : "no") << '\n';
- }
- inline void Yn(const bool con) {
- cout << (con ? "Yes" : "No") << '\n';
- }
- inline void YN(const bool con) {
- cout << (con ? "YES" : "NO") << '\n';
- }
- // Input and Output Functions End
- // Debug Functions Start
- #if LOCAL
- #include <debug.h>
- #else
- #define debug(...) 60
- #endif
- // Debug Functions End
- // Important Functions End
- //------------------------------------------------------------------------------------------
- // Basic Functions Start
- // String Operator Overload Start
- template <class T>
- str operator*(const str& s, const T& n) {
- str res;
- res.reserve(s.size() * n);
- for (ll i = 0; i < n; ++i) res += s;
- return res;
- }
- // String Operator Overload End
- //Binary Search Start
- template <typename T>
- inline ll bin_search(const vector<T>& v, T x) {
- auto it = lower_bound(v.begin(), v.end(), x);
- if (it != v.end() && *it == x) return distance(v.begin(), it);
- return -1;
- }
- // Binary Search End
- // Min Function Start
- template <class T>
- inline T min(const vector<T>& vec) {
- assert(vec.size() >= 1 && "Cannot find minimum of an empty vector.");
- return *min_element(vec.begin(), vec.end());
- }
- // Min Function End
- // Max Function Start
- template <class T>
- inline T max(const vector<T>& vec) {
- assert(vec.size() >= 1 && "Cannot find minimum of an empty vector.");
- return *max_element(vec.begin(), vec.end());
- }
- // Max Function End
- // Sum Function Start
- template <class T>
- inline T sum(T v) {
- return v;
- }
- template <class T>
- inline T sum(T v1, T v2) {
- return v1 + v2;
- }
- template <class T>
- T sum(const initializer_list<T>& ilist) {
- T sumi = T();
- for (const T& val : ilist) sumi += val;
- return sumi;
- }
- template <class T>
- T sum(const vector<T>& vec) {
- T sumi = T();
- for (auto i : vec) sumi += sum(i);
- return sumi;
- }
- template <class T>
- T sum(const set<T>& seti) {
- T sumi = T();
- for (auto i : seti) sumi += sum(i);
- return sumi;
- }
- template <class T>
- T sum(const multiset<T>& seti) {
- T sumi = T();
- for (auto i : seti) sumi += sum(i);
- return sumi;
- }
- template <typename T, typename Compare = less<T>>
- T sum(const pbds_tree<T, Compare>& seti) {
- T sumi = T();
- for (auto i : seti) sumi += sum(i);
- return sumi;
- }
- // Sum Function End
- // Binary Start
- template <class T>
- str bin(const T& dn) {
- str bin_str = bitset<64>(dn).to_string();
- ll firstOne = bin_str.find('1');
- if (firstOne != str::npos) {
- bin_str = bin_str.substr(firstOne);
- return bin_str;
- }
- return "0";
- }
- ll int2(const str& bin_str) {
- bitset<64> bits(bin_str);
- ll dn = static_cast<ll>(bits.to_ulong());
- return dn;
- }
- // Binary End
- // Square Root Start
- inline ll sqrt_int(ll n) {
- assert(n >= 0 && "The number can't be negative.");
- return static_cast<ll>(sqrt(n));
- }
- ll sqrt_int_u(ll n) {
- assert(n >= 0 && "The number can't be negative.");
- ll k = sqrt(n);
- return k * k == n ? k : k + 1;
- }
- // Square Root End
- // Map Computation Start
- template <class T>
- void map_com(map<T, ll>& ma, const vector<T>& vec) {
- for (T i : vec) ma[i]++;
- }
- // Map Computation End
- // Basic Functions End
- //------------------------------------------------------------------------------------------
- // Algorithms Start
- // Power Function Start
- ll power(ll a, ll b, const ll mod = mod1) {
- assert(b >= 0 && "Power cannot be negative.");
- ll ans = 1;
- a %= mod;
- while (b) {
- if (b & 1) ans = (ans * a) % mod;
- a = (a * a) % mod;
- b >>= 1;
- }
- return ans;
- }
- // Power Function End
- // Modular Inverse Start
- inline ll mod_inv(ll a, const ll mod = mod1) {
- assert(a >= 0 && "The number must be non-negative.");
- return power(a, mod - 2, mod);
- }
- // Modular Inverse End
- // Fast Fib Start
- // Fast Fib End
- // KMP Algorithm Start
- // KMP Algorithm End
- // Algorithms End
- //------------------------------------------------------------------------------------------
- // Classes Start
- // Big Int Start
- // Big Int end
- // Modular Start
- // Modular End
- // PNC Start
- class PNC;
- // PNC End
- // Prime Start
- class Prime;
- // Prime End
- // Polynomial Hashing Start
- // Polynomial Hashing End
- // Disjoint Set Union Start
- // Disjoint Set Union End
- // Unweighted Graphs Start
- // Unweighted Graphs End
- // Weighted Graphs Start
- // Weighted Graphs End
- // Segment Tree Start
- // Segment Tree End
- // Trie Start
- // Trie End
- // Monotonic Stack Start
- // Monotonic Stack End
- // Classes End
- //------------------------------------------------------------------------------------------
- // Global Variables Start
- PNC* pnc;
- Prime* prime;
- auto func = []() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- // pnc = new PNC();
- // prime = new Prime();
- return 0;
- }();
- // Global Variables End
- // /* // Solution Class Start
- class Solution {
- public:
- void solve(ll index) {
- //----------------------------------------------------------------------------------
- // debug("Case #", index, ": ", newl);
- ll n, i, j, k, r;
- cin >> n >> r;
- if (r == 0) {
- cout << 1 << newl;
- vecll temp(n, 1);
- cout << temp << newl;
- return;
- }
- vecprll pairs(r);
- cin >> pairs;
- sort(all(pairs));
- ll ans = pairs[0].S - pairs[0].F + 1;
- ll prev = pairs[0].S;
- fri(i, 1, pairs.size()) {
- if (pairs[i].F <= prev && pairs[i].S > prev) {
- ll cou = prev - pairs[i].F + 1;
- ll val = ans - cou;
- ll diff = pairs[i].S - prev;
- if (diff > val) {
- ans += diff - val;
- }
- }
- }
- vecll ansvec(n, -1);
- vecll stuff;
- for (int i = 1;i <= ans;++i) {
- stuff.pb(i);
- }
- ll inx = 0;
- fri(i, pairs[0].F, pairs[0].S + 1) {
- ansvec[inx] = stuff[inx];
- inx = (inx + 1) % ans;
- }
- fri(i, 1, pairs.size()) {
- if (pairs[i].F <= prev && pairs[i].S > prev) {
- fri(j, prev, pairs[i].S + 1) {
- if(ansvec[inx]==-1) {
- ansvec[inx] = stuff[inx];
- inx = (inx + 1) % ans;
- }
- }
- }
- }
- fri(i, 0, ansvec.size()) {
- if (ansvec[i] == -1) {
- ansvec[i] = 1;
- }
- }
- cout << ans << newl;
- cout << ansvec << newl;
- // cout<<"Case #"<<index<<": "<<ans<<newl;
- //----------------------------------------------------------------------------------
- }
- bool test_cases = true;
- };
- // Solution Class End
- //------------------------------------------------------------------------------------------
- // Main Function Start
- int main() {
- Solution sol;
- ll test_cases = 1;
- if (sol.test_cases) cin >> test_cases;
- for (ll test_case = 1; test_case <= test_cases; ++test_case) sol.solve(test_case);
- return 0;
- }
- // Main Function End
- // */ // Program End
- //------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement