Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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;
- template <class T>
- using pbds =
- tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
- // #define cerr \
- // if (false) \
- // cerr
- #define int long long
- #define pb push_back
- #define F first
- #define S second
- #define yes cout << "Yes\n"
- #define no cout << "No\n"
- #define yn(x) x ? yes : no
- #define f(i, s, e) for (int i = s; i < e; i++)
- #define vi vector<int>
- #define vb vector<bool>
- #define pii pair<int, int>
- #define vpi vector<pii>
- #define all(x) x.begin(), x.end()
- #define minele(x) *min_element(all(x))
- #define maxele(x) *max_element(all(x))
- #define endl '\n'
- #define in(v, x) binary_search(all(v), x)
- const int N = 1e4;
- const int MOD = 1e9 + 7;
- const int inf = LLONG_MAX;
- const int minf = LLONG_MIN;
- #ifndef ONLINE_JUDGE
- #define debug(x) \
- cerr << (#x) << " is "; \
- _print(x)
- #define dbg(x...) \
- cerr << (#x) << " is "; \
- _print(x)
- #else
- #define debug(x)
- #define dbg(x)
- #define dbg(x...)
- #endif
- template <typename T>
- void _print(T a)
- {
- cerr << a;
- }
- template <typename T1, typename... T2>
- void _print(T1 t1, T2... t2)
- {
- cerr << t1 << ", ";
- _print(t2...);
- cerr << endl;
- }
- template <typename T>
- void print(T a)
- {
- cout << a << ' ';
- }
- template <typename T>
- void println(T a)
- {
- cout << a << endl;
- }
- template <class T>
- istream &operator>>(istream &is, vector<T> &a)
- {
- for (auto &x : a)
- is >> x;
- return is;
- }
- template <class T>
- ostream &operator<<(ostream &os, const vector<T> &a)
- {
- for (const auto &x : a)
- os << x << ' ';
- return os;
- }
- template <class T, class V>
- void _print(pair<T, V> p);
- template <class T>
- void _print(vector<T> v);
- template <class T>
- void _print(set<T> v);
- template <class T, class V>
- void _print(map<T, V> v);
- template <class T>
- void _print(multiset<T> v);
- template <class T, class V>
- void _print(pair<T, V> p)
- {
- cerr << "{";
- _print(p.F);
- cerr << ",";
- _print(p.S);
- cerr << "} ";
- }
- template <class T>
- void _print(vector<T> v)
- {
- cerr << "[ ";
- for (T i : v)
- {
- _print(i);
- cerr << " ";
- }
- cerr << "]";
- cerr << endl;
- }
- template <class T>
- void _print(set<T> v)
- {
- cerr << "[ ";
- for (T i : v)
- {
- _print(i);
- cerr << " ";
- }
- cerr << "]";
- cerr << endl;
- }
- template <class T>
- void _print(multiset<T> v)
- {
- cerr << "[ ";
- for (T i : v)
- {
- _print(i);
- cerr << " ";
- }
- cerr << "]";
- cerr << endl;
- }
- template <class T, class V>
- void _print(map<T, V> v)
- {
- cerr << "[ ";
- for (auto i : v)
- {
- _print(i);
- cerr << " ";
- }
- cerr << "]";
- cerr << endl;
- }
- template <class T, class V>
- void _print(unordered_map<T, V> v)
- {
- cerr << "[ ";
- for (auto i : v)
- {
- _print(i);
- cerr << " ";
- }
- cerr << "]";
- cerr << endl;
- }
- void fast()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- cout.tie(NULL);
- }
- void init_code()
- {
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- freopen("error.txt", "w", stderr);
- #endif // ONLINE_JUDGE
- }
- bool cmp(pair<pii, int> &a, pair<pii, int> &b)
- {
- if (a.S == b.S)
- {
- return a.F.F <= b.F.F;
- }
- return a.S > b.S;
- }
- int search(vector<pair<pii, int>> &v, int x)
- {
- int l = 0, r = v.size() - 1;
- int ans = -1;
- while (l <= r)
- {
- int m = (l + r) >> 1;
- if (v[m].S > x)
- {
- ans = m;
- l = m + 1;
- }
- else
- {
- r = m - 1;
- }
- }
- return ans;
- }
- void solve()
- {
- int n, q;
- cin >> n >> q;
- vector<pair<pii, int>> v;
- f(i, 0, n)
- {
- int c, m, t;
- cin >> c >> m >> t;
- v.pb({{c, m}, t});
- }
- sort(all(v), cmp);
- debug(v);
- map<int, int> end;
- map<int, vpi> queries;
- vi ans(q, -1);
- f(i, 0, q)
- {
- int x, y;
- cin >> x >> y;
- if(n == 0) continue;
- // end[x] = v[search(v, x)].S;
- int xx = search(v, x);
- if (xx == -1)
- {
- ans[i] = 0;
- continue;
- }
- end[x] = v[search(v, x)].S;
- queries[x].pb({y, i});
- }
- if(n == 0) {
- f(i, 0, q) {
- cout << 0 << endl;
- }
- return;
- }
- debug(end);
- if(queries.empty()) {
- for(auto i : ans) cout << i << endl;
- return;
- }
- auto it = queries.rbegin();
- vi ndp(N + 1), pdp(N + 1);
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j <= 1e4; j++)
- {
- ndp[j] = max(ndp[j], pdp[j]);
- if (j >= v[i].F.S)
- ndp[j] = max(ndp[j], pdp[j - v[i].F.S] + v[i].F.F);
- }
- if (end[it->F] == v[i].S)
- {
- for (auto k : it->S)
- {
- ans[k.S] = ndp[k.F];
- }
- it++;
- if (it == queries.rend())
- break;
- }
- debug(ndp);
- pdp = ndp;
- }
- if(it != queries.rend()) {
- assert(1 == 0);
- }
- f(i, 0, q)
- {
- cout << ans[i] << endl;
- }
- }
- signed main()
- {
- init_code();
- fast();
- int t = 1;
- // cin >> t;
- while (t--)
- {
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment