Sreekar_0125

Untitled

Sep 22nd, 2024
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.58 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4. using namespace std;
  5. using namespace __gnu_pbds;
  6. template <class T>
  7. using pbds =
  8.     tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  9. // #define cerr   \
  10. //     if (false) \
  11. //     cerr
  12. #define int long long
  13. #define pb push_back
  14. #define F first
  15. #define S second
  16. #define yes cout << "Yes\n"
  17. #define no cout << "No\n"
  18. #define yn(x) x ? yes : no
  19. #define f(i, s, e) for (int i = s; i < e; i++)
  20. #define vi vector<int>
  21. #define vb vector<bool>
  22. #define pii pair<int, int>
  23. #define vpi vector<pii>
  24. #define all(x) x.begin(), x.end()
  25. #define minele(x) *min_element(all(x))
  26. #define maxele(x) *max_element(all(x))
  27. #define endl '\n'
  28. #define in(v, x) binary_search(all(v), x)
  29.  
  30. const int N = 1e4;
  31. const int MOD = 1e9 + 7;
  32. const int inf = LLONG_MAX;
  33. const int minf = LLONG_MIN;
  34.  
  35. #ifndef ONLINE_JUDGE
  36. #define debug(x)            \
  37.     cerr << (#x) << " is "; \
  38.     _print(x)
  39. #define dbg(x...)           \
  40.     cerr << (#x) << " is "; \
  41.     _print(x)
  42. #else
  43. #define debug(x)
  44. #define dbg(x)
  45. #define dbg(x...)
  46. #endif
  47.  
  48. template <typename T>
  49. void _print(T a)
  50. {
  51.     cerr << a;
  52. }
  53. template <typename T1, typename... T2>
  54. void _print(T1 t1, T2... t2)
  55. {
  56.     cerr << t1 << ", ";
  57.     _print(t2...);
  58.     cerr << endl;
  59. }
  60. template <typename T>
  61. void print(T a)
  62. {
  63.     cout << a << ' ';
  64. }
  65. template <typename T>
  66. void println(T a)
  67. {
  68.     cout << a << endl;
  69. }
  70. template <class T>
  71. istream &operator>>(istream &is, vector<T> &a)
  72. {
  73.     for (auto &x : a)
  74.         is >> x;
  75.     return is;
  76. }
  77. template <class T>
  78. ostream &operator<<(ostream &os, const vector<T> &a)
  79. {
  80.     for (const auto &x : a)
  81.         os << x << ' ';
  82.     return os;
  83. }
  84.  
  85. template <class T, class V>
  86. void _print(pair<T, V> p);
  87. template <class T>
  88. void _print(vector<T> v);
  89. template <class T>
  90. void _print(set<T> v);
  91. template <class T, class V>
  92. void _print(map<T, V> v);
  93. template <class T>
  94. void _print(multiset<T> v);
  95. template <class T, class V>
  96. void _print(pair<T, V> p)
  97. {
  98.     cerr << "{";
  99.     _print(p.F);
  100.     cerr << ",";
  101.     _print(p.S);
  102.     cerr << "} ";
  103. }
  104. template <class T>
  105. void _print(vector<T> v)
  106. {
  107.     cerr << "[ ";
  108.     for (T i : v)
  109.     {
  110.         _print(i);
  111.         cerr << " ";
  112.     }
  113.     cerr << "]";
  114.     cerr << endl;
  115. }
  116. template <class T>
  117. void _print(set<T> v)
  118. {
  119.     cerr << "[ ";
  120.     for (T i : v)
  121.     {
  122.         _print(i);
  123.         cerr << " ";
  124.     }
  125.     cerr << "]";
  126.     cerr << endl;
  127. }
  128. template <class T>
  129. void _print(multiset<T> v)
  130. {
  131.     cerr << "[ ";
  132.     for (T i : v)
  133.     {
  134.         _print(i);
  135.         cerr << " ";
  136.     }
  137.     cerr << "]";
  138.     cerr << endl;
  139. }
  140. template <class T, class V>
  141. void _print(map<T, V> v)
  142. {
  143.     cerr << "[ ";
  144.     for (auto i : v)
  145.     {
  146.         _print(i);
  147.         cerr << " ";
  148.     }
  149.     cerr << "]";
  150.     cerr << endl;
  151. }
  152. template <class T, class V>
  153. void _print(unordered_map<T, V> v)
  154. {
  155.     cerr << "[ ";
  156.     for (auto i : v)
  157.     {
  158.         _print(i);
  159.         cerr << " ";
  160.     }
  161.     cerr << "]";
  162.     cerr << endl;
  163. }
  164.  
  165. void fast()
  166. {
  167.     ios_base::sync_with_stdio(false);
  168.     cin.tie(NULL);
  169.     cout.tie(NULL);
  170. }
  171.  
  172. void init_code()
  173. {
  174. #ifndef ONLINE_JUDGE
  175.     freopen("input.txt", "r", stdin);
  176.     freopen("output.txt", "w", stdout);
  177.     freopen("error.txt", "w", stderr);
  178. #endif // ONLINE_JUDGE
  179. }
  180. bool cmp(pair<pii, int> &a, pair<pii, int> &b)
  181. {
  182.     if (a.S == b.S)
  183.     {
  184.         return a.F.F <= b.F.F;
  185.     }
  186.     return a.S > b.S;
  187. }
  188.  
  189. int search(vector<pair<pii, int>> &v, int x)
  190. {
  191.     int l = 0, r = v.size() - 1;
  192.     int ans = -1;
  193.     while (l <= r)
  194.     {
  195.         int m = (l + r) >> 1;
  196.         if (v[m].S > x)
  197.         {
  198.             ans = m;
  199.             l = m + 1;
  200.         }
  201.         else
  202.         {
  203.             r = m - 1;
  204.         }
  205.     }
  206.     return ans;
  207. }
  208. void solve()
  209. {
  210.     int n, q;
  211.     cin >> n >> q;
  212.  
  213.     vector<pair<pii, int>> v;
  214.     f(i, 0, n)
  215.     {
  216.         int c, m, t;
  217.         cin >> c >> m >> t;
  218.         v.pb({{c, m}, t});
  219.     }
  220.     sort(all(v), cmp);
  221.  
  222.     debug(v);
  223.     map<int, int> end;
  224.     map<int, vpi> queries;
  225.  
  226.     vi ans(q, -1);
  227.     f(i, 0, q)
  228.     {
  229.         int x, y;
  230.         cin >> x >> y;
  231.         if(n == 0) continue;
  232.        
  233.         // end[x] = v[search(v, x)].S;
  234.         int xx = search(v, x);
  235.         if (xx == -1)
  236.         {
  237.             ans[i] = 0;
  238.             continue;
  239.         }
  240.         end[x] = v[search(v, x)].S;
  241.         queries[x].pb({y, i});
  242.     }
  243.     if(n == 0) {
  244.         f(i, 0, q) {
  245.             cout << 0 << endl;
  246.         }
  247.         return;
  248.     }
  249.     debug(end);
  250.     if(queries.empty()) {
  251.         for(auto i : ans) cout << i << endl;
  252.         return;
  253.     }
  254.     auto it = queries.rbegin();
  255.  
  256.     vi ndp(N + 1), pdp(N + 1);
  257.  
  258.     for (int i = 0; i < n; i++)
  259.     {
  260.         for (int j = 0; j <= 1e4; j++)
  261.         {
  262.             ndp[j] = max(ndp[j], pdp[j]);
  263.              
  264.             if (j >= v[i].F.S)
  265.                 ndp[j] = max(ndp[j], pdp[j - v[i].F.S] + v[i].F.F);
  266.         }
  267.         if (end[it->F] == v[i].S)
  268.         {
  269.             for (auto k : it->S)
  270.             {
  271.                 ans[k.S] = ndp[k.F];
  272.             }
  273.             it++;
  274.             if (it == queries.rend())
  275.                 break;
  276.         }
  277.         debug(ndp);
  278.         pdp = ndp;
  279.     }
  280.     if(it != queries.rend()) {
  281.         assert(1 == 0);
  282.     }
  283.  
  284.     f(i, 0, q)
  285.     {
  286.         cout << ans[i] << endl;
  287.     }
  288. }
  289.  
  290. signed main()
  291. {
  292.     init_code();
  293.     fast();
  294.     int t = 1;
  295.     // cin >> t;
  296.     while (t--)
  297.     {
  298.         solve();
  299.     }
  300.     return 0;
  301. }
  302.  
Advertisement
Add Comment
Please, Sign In to add comment