Advertisement
t_naveen_2308

Untitled

Feb 8th, 2025
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.20 KB | None | 0 0
  1. // Author : Naveen
  2. // Program Start
  3. // Libraries and Namespace Start
  4. #include <bits/stdc++.h>
  5. #include <ext/pb_ds/assoc_container.hpp>
  6. #include <ext/pb_ds/tree_policy.hpp>
  7. using namespace std;
  8. using namespace __gnu_pbds;
  9. // Libraries and Namespace End
  10.  
  11. //------------------------------------------------------------------------------------------
  12.  
  13. // Important Shortcuts Start
  14. // Macros Start
  15. #define F first
  16. #define S second
  17. #define pb push_back
  18. #define yes cout << "yes\n"
  19. #define no cout << "no\n"
  20. #define Yes cout << "Yes\n"
  21. #define No cout << "No\n"
  22. #define YES cout << "YES\n"
  23. #define NO cout << "NO\n"
  24. #define fri(i, s, e) for (ll i = s; i < e; ++i)
  25. #define frd(i, e, s) for(ll i = e; i > s; --i)
  26. #define all(v) v.begin(), v.end()
  27. // Macros End
  28.  
  29. // Custom Hash Function Start
  30. struct custom_hash {
  31.     static uint64_t splitMix64(uint64_t x) {
  32.         x += 0x9e3779b97f4a7c15;
  33.         x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
  34.         x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
  35.         return x ^ (x >> 31);
  36.     }
  37.     size_t operator()(uint64_t x) const {
  38.         static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
  39.         return splitMix64(x + FIXED_RANDOM);
  40.     }
  41. };
  42. // Custom Hash Function End
  43.  
  44. // Declarations Start
  45. typedef bool bl;
  46. typedef long long int ll;
  47. typedef unsigned long long int ull;
  48. typedef long double ld;
  49. typedef string str;
  50. typedef pair<ll, ll> prll;
  51. typedef tuple<ll, ll, ll> tupll;
  52. typedef stack<ll> stkll;
  53. typedef queue<ll> quell;
  54. typedef vector<bl> vecbl;
  55. typedef vector<ll> vecll;
  56. typedef vector<prll> vecprll;
  57. typedef vector<tupll> vectupll;
  58. typedef vector<ld> vecld;
  59. typedef set<ll> setll;
  60. typedef multiset<ll> mltsetll;
  61. typedef map<ll, ll> mapll;
  62. typedef multimap<ll, ll> mltmapll;
  63. // Declarations End
  64.  
  65. // Using Declarations Start
  66. template <typename T>
  67. using unord_set = unordered_set<T, custom_hash>;
  68. typedef unord_set<ll> unsetll;
  69.  
  70. template <typename T>
  71. using unord_multiset = unordered_multiset<T, custom_hash>;
  72. typedef unord_multiset<ll> unmltsetll;
  73.  
  74. template <typename T1, typename T2>
  75. using unord_map = unordered_map<T1, T2, custom_hash>;
  76. typedef unord_map<ll, ll> unmapll;
  77.  
  78. template <typename T1, typename T2>
  79. using unord_multimap = unordered_multimap<T1, T2, custom_hash>;
  80. typedef unord_multimap<ll, ll> unmltmapll;
  81.  
  82. template <typename T, typename Compare = greater<T>>
  83. using heap = priority_queue<T, vector<T>, Compare>;
  84. typedef heap<ll> minheapll;
  85. typedef heap<prll> minheapprll;
  86. typedef heap<ll, less<ll>> maxheapll;
  87. typedef heap<prll, less<prll>> maxheapprll;
  88.  
  89. template <typename T, typename Compare = less<T>>
  90. using pbds_tree = tree<T, null_type, Compare, rb_tree_tag, tree_order_statistics_node_update>;
  91. typedef pbds_tree<ll> pbdssetll;
  92. typedef pbds_tree<ll, less_equal<ll>> pbdsmltsetll;
  93. typedef pbds_tree<prll> pbdsmapll;
  94. typedef pbds_tree<prll, less_equal<prll>> pbdsmltmapll;
  95. // Using Declarations End
  96. // Important Shortcuts End
  97.  
  98. //------------------------------------------------------------------------------------------
  99.  
  100. // Important Functions Start
  101. // Constants Start
  102. const ll inf = 9223372036854775807ll;
  103. const ll neg_inf = -9223372036854775807ll - 1ll;
  104. const ll mod1 = 1000000007ll;
  105. const ll mod2 = 998244353ll;
  106. const ll max_if = 1000000ll;
  107. const ll one = 1ll;
  108. const ll zero = 0ll;
  109. const str spc = " ";
  110. const str emp = "";
  111. const str newl = "\n";
  112. // Constants End
  113.  
  114. // Input and Output Functions Start
  115. template <class U, class T, class V>
  116. U& operator>>(U& is, pair<T, V>& p) {
  117.     is >> p.first >> p.second;
  118.     return is;
  119. }
  120. template <class U, class T>
  121. U& operator>>(U& is, vector<T>& a) {
  122.     for (T& x : a) {
  123.         is >> x;
  124.     }
  125.     return is;
  126. }
  127.  
  128. template <class U, class T>
  129. U& operator<<(U& os, vector<T>& a) {
  130.     for (ll i = 0;i < a.size();++i) {
  131.         os << a[i] << (i != a.size() - 1 ? spc : emp);
  132.     }
  133.     return os;
  134. }
  135.  
  136. inline void yn(const bool con) {
  137.     cout << (con ? "yes" : "no") << '\n';
  138. }
  139. inline void Yn(const bool con) {
  140.     cout << (con ? "Yes" : "No") << '\n';
  141. }
  142. inline void YN(const bool con) {
  143.     cout << (con ? "YES" : "NO") << '\n';
  144. }
  145. // Input and Output Functions End
  146.  
  147. // Debug Functions Start
  148. #if LOCAL
  149. #include <debug.h>
  150. #else
  151. #define debug(...) 60
  152. #endif
  153. // Debug Functions End
  154. // Important Functions End
  155.  
  156. //------------------------------------------------------------------------------------------
  157.  
  158. // Basic Functions Start
  159. // String Operator Overload Start
  160. template <class T>
  161. str operator*(const str& s, const T& n) {
  162.     str res;
  163.     res.reserve(s.size() * n);
  164.     for (ll i = 0; i < n; ++i) res += s;
  165.     return res;
  166. }
  167. // String Operator Overload End
  168.  
  169. //Binary Search Start
  170. template <typename T>
  171. inline ll bin_search(const vector<T>& v, T x) {
  172.     auto it = lower_bound(v.begin(), v.end(), x);
  173.     if (it != v.end() && *it == x) return distance(v.begin(), it);
  174.     return -1;
  175. }
  176. // Binary Search End
  177.  
  178. // Min Function Start
  179. template <class T>
  180. inline T min(const vector<T>& vec) {
  181.     assert(vec.size() >= 1 && "Cannot find minimum of an empty vector.");
  182.     return *min_element(vec.begin(), vec.end());
  183. }
  184. // Min Function End
  185.  
  186. // Max Function Start
  187. template <class T>
  188. inline T max(const vector<T>& vec) {
  189.     assert(vec.size() >= 1 && "Cannot find minimum of an empty vector.");
  190.     return *max_element(vec.begin(), vec.end());
  191. }
  192. // Max Function End
  193.  
  194. // Sum Function Start
  195. template <class T>
  196. inline T sum(T v) {
  197.     return v;
  198. }
  199. template <class T>
  200. inline T sum(T v1, T v2) {
  201.     return v1 + v2;
  202. }
  203. template <class T>
  204. T sum(const initializer_list<T>& ilist) {
  205.     T sumi = T();
  206.     for (const T& val : ilist) sumi += val;
  207.     return sumi;
  208. }
  209. template <class T>
  210. T sum(const vector<T>& vec) {
  211.     T sumi = T();
  212.     for (auto i : vec) sumi += sum(i);
  213.     return sumi;
  214. }
  215. template <class T>
  216. T sum(const set<T>& seti) {
  217.     T sumi = T();
  218.     for (auto i : seti) sumi += sum(i);
  219.     return sumi;
  220. }
  221. template <class T>
  222. T sum(const multiset<T>& seti) {
  223.     T sumi = T();
  224.     for (auto i : seti) sumi += sum(i);
  225.     return sumi;
  226. }
  227. template <typename T, typename Compare = less<T>>
  228. T sum(const pbds_tree<T, Compare>& seti) {
  229.     T sumi = T();
  230.     for (auto i : seti) sumi += sum(i);
  231.     return sumi;
  232. }
  233. // Sum Function End
  234.  
  235. // Binary Start
  236. template <class T>
  237. str bin(const T& dn) {
  238.     str bin_str = bitset<64>(dn).to_string();
  239.     ll firstOne = bin_str.find('1');
  240.     if (firstOne != str::npos) {
  241.         bin_str = bin_str.substr(firstOne);
  242.         return bin_str;
  243.     }
  244.     return "0";
  245. }
  246.  
  247. ll int2(const str& bin_str) {
  248.     bitset<64> bits(bin_str);
  249.     ll dn = static_cast<ll>(bits.to_ulong());
  250.     return dn;
  251. }
  252. // Binary End
  253.  
  254. // Square Root Start
  255. inline ll sqrt_int(ll n) {
  256.     assert(n >= 0 && "The number can't be negative.");
  257.     return static_cast<ll>(sqrt(n));
  258. }
  259.  
  260. ll sqrt_int_u(ll n) {
  261.     assert(n >= 0 && "The number can't be negative.");
  262.     ll k = sqrt(n);
  263.     return k * k == n ? k : k + 1;
  264. }
  265. // Square Root End
  266.  
  267. // Map Computation Start
  268. template <class T>
  269. void map_com(map<T, ll>& ma, const vector<T>& vec) {
  270.     for (T i : vec) ma[i]++;
  271. }
  272. // Map Computation End
  273. // Basic Functions End
  274.  
  275. //------------------------------------------------------------------------------------------
  276.  
  277. // Algorithms Start
  278. // Power Function Start
  279. ll power(ll a, ll b, const ll mod = mod1) {
  280.     assert(b >= 0 && "Power cannot be negative.");
  281.     ll ans = 1;
  282.     a %= mod;
  283.     while (b) {
  284.         if (b & 1) ans = (ans * a) % mod;
  285.         a = (a * a) % mod;
  286.         b >>= 1;
  287.     }
  288.     return ans;
  289. }
  290. // Power Function End
  291.  
  292. // Modular Inverse Start
  293. inline ll mod_inv(ll a, const ll mod = mod1) {
  294.     assert(a >= 0 && "The number must be non-negative.");
  295.     return power(a, mod - 2, mod);
  296. }
  297. // Modular Inverse End
  298.  
  299. // Fast Fib Start
  300. // Fast Fib End
  301.  
  302. // KMP Algorithm Start
  303. // KMP Algorithm End
  304. // Algorithms End
  305.  
  306. //------------------------------------------------------------------------------------------
  307.  
  308. // Classes Start
  309. // Big Int Start
  310. // Big Int end
  311.  
  312. // Modular Start
  313. // Modular End
  314.  
  315. // PNC Start
  316. class PNC;
  317. // PNC End
  318.  
  319. // Prime Start
  320. class Prime;
  321. // Prime End
  322.  
  323. // Polynomial Hashing Start
  324. // Polynomial Hashing End
  325.  
  326. // Disjoint Set Union Start
  327. // Disjoint Set Union End
  328.  
  329. // Unweighted Graphs Start
  330. // Unweighted Graphs End
  331.  
  332. // Weighted Graphs Start
  333. // Weighted Graphs End
  334.  
  335. // Segment Tree Start
  336. // Segment Tree End
  337.  
  338. // Trie Start
  339. // Trie End
  340.  
  341. // Monotonic Stack Start
  342. // Monotonic Stack End
  343. // Classes End
  344.  
  345. //------------------------------------------------------------------------------------------
  346.  
  347. // Global Variables Start
  348. PNC* pnc;
  349. Prime* prime;
  350.  
  351. auto func = []() {
  352.     ios_base::sync_with_stdio(false);
  353.     cin.tie(nullptr);
  354.     cout.tie(nullptr);
  355.     // pnc = new PNC();
  356.     // prime = new Prime();
  357.     return 0;
  358.     }();
  359. // Global Variables End
  360.  
  361. // /* // Solution Class Start
  362. class Solution {
  363. public:
  364.  
  365.     void solve(ll index) {
  366.         //----------------------------------------------------------------------------------
  367.  
  368.         // debug("Case #", index, ": ", newl);
  369.         ll n, i, j, k, r;
  370.         cin >> n >> r;
  371.         if (r == 0) {
  372.             cout << 1 << newl;
  373.             vecll temp(n, 1);
  374.             cout << temp << newl;
  375.             return;
  376.         }
  377.         vecprll pairs(r);
  378.         cin >> pairs;
  379.         sort(all(pairs));
  380.         ll ans = pairs[0].S - pairs[0].F + 1;
  381.         ll prev = pairs[0].S;
  382.         fri(i, 1, pairs.size()) {
  383.             if (pairs[i].F <= prev && pairs[i].S > prev) {
  384.                 ll cou = prev - pairs[i].F + 1;
  385.                 ll val = ans - cou;
  386.                 ll diff = pairs[i].S - prev;
  387.                 if (diff > val) {
  388.                     ans += diff - val;
  389.                 }
  390.             }
  391.         }
  392.         vecll ansvec(n, -1);
  393.         vecll stuff;
  394.         for (int i = 1;i <= ans;++i) {
  395.             stuff.pb(i);
  396.         }
  397.         ll inx = 0;
  398.         fri(i, pairs[0].F, pairs[0].S + 1) {
  399.             ansvec[inx] = stuff[inx];
  400.             inx = (inx + 1) % ans;
  401.         }
  402.         fri(i, 1, pairs.size()) {
  403.             if (pairs[i].F <= prev && pairs[i].S > prev) {
  404.                 fri(j, prev, pairs[i].S + 1) {
  405.                     if(ansvec[inx]==-1) {
  406.                         ansvec[inx] = stuff[inx];
  407.                         inx = (inx + 1) % ans;
  408.                     }
  409.                 }
  410.             }
  411.         }
  412.         fri(i, 0, ansvec.size()) {
  413.             if (ansvec[i] == -1) {
  414.                 ansvec[i] = 1;
  415.             }
  416.         }
  417.         cout << ans << newl;
  418.         cout << ansvec << newl;
  419.  
  420.         // cout<<"Case #"<<index<<": "<<ans<<newl;
  421.  
  422.         //----------------------------------------------------------------------------------
  423.     }
  424.  
  425.     bool test_cases = true;
  426. };
  427. // Solution Class End
  428.  
  429. //------------------------------------------------------------------------------------------
  430.  
  431. // Main Function Start
  432. int main() {
  433.     Solution sol;
  434.     ll test_cases = 1;
  435.     if (sol.test_cases) cin >> test_cases;
  436.     for (ll test_case = 1; test_case <= test_cases; ++test_case) sol.solve(test_case);
  437.     return 0;
  438. }
  439. // Main Function End
  440. // */ // Program End
  441. //------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement