結果
問題 |
No.2161 Black Market
|
ユーザー |
![]() |
提出日時 | 2024-02-11 10:25:38 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 278 ms / 7,000 ms |
コード長 | 4,738 bytes |
コンパイル時間 | 2,016 ms |
コンパイル使用メモリ | 152,016 KB |
最終ジャッジ日時 | 2025-02-19 04:53:02 |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 37 |
ソースコード
#include<iostream> #include<string> #include<vector> #include<algorithm> #include<numeric> #include<cmath> #include<utility> #include<tuple> #include<cstdint> #include<cstdio> #include<iomanip> #include<map> #include<queue> #include<set> #include<stack> #include<deque> #include<unordered_map> #include<unordered_set> #include<bitset> #include<cctype> #include<chrono> #include<random> #include<cassert> #include<cstddef> #include<iterator> #include<string_view> #include<type_traits> #ifdef LOCAL # include "debug_print.hpp" # define debug(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__) #else # define debug(...) (static_cast<void>(0)) #endif using namespace std; #define rep(i,n) for(int i=0; i<(n); i++) #define rrep(i,n) for(int i=(n)-1; i>=0; i--) #define FOR(i,a,b) for(int i=(a); i<(b); i++) #define RFOR(i,a,b) for(int i=(b-1); i>=(a); i--) #define ALL(v) v.begin(), v.end() #define RALL(v) v.rbegin(), v.rend() #define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() ); #define pb push_back using ll = long long; using D = double; using LD = long double; using P = pair<int, int>; template<typename T> using PQ = priority_queue<T,vector<T>>; template<typename T> using minPQ = priority_queue<T, vector<T>, greater<T>>; template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } void yesno(bool flag) {cout << (flag?"Yes":"No") << "\n";} template<typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << p.first << " " << p.second; return os; } template<typename T, typename U> istream &operator>>(istream &is, pair<T, U> &p) { is >> p.first >> p.second; return is; } template<typename T> ostream &operator<<(ostream &os, const vector<T> &v) { int s = (int)v.size(); for (int i = 0; i < s; i++) os << (i ? " " : "") << v[i]; return os; } template<typename T> istream &operator>>(istream &is, vector<T> &v) { for (auto &x : v) is >> x; return is; } void in() {} template<typename T, class... U> void in(T &t, U &...u) { cin >> t; in(u...); } void out() { cout << "\n"; } template<typename T, class... U, char sep = ' '> void out(const T &t, const U &...u) { cout << t; if (sizeof...(u)) cout << sep; out(u...); } void outr() {} template<typename T, class... U, char sep = ' '> void outr(const T &t, const U &...u) { cout << t; outr(u...); } #include "atcoder/segtree.hpp" ll op(ll a, ll b) {return a+b;} ll e() {return 0;} int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int n,k; ll l,p; in(n,k,l,p); vector<ll> a(n),b(n); rep(i,n) in(a[i],b[i]); int half_n = n/2; int half_f = (1 << half_n); int half_s = (1 << (n-half_n)); vector<ll> cost_fst(half_f), val_fst(half_f); vector<ll> cost_snd(half_s), val_snd(half_s); vector<ll> cost_bin, val_bin; rep(bit, half_f){ rep(i,half_n){ if((bit >> i) & 1) { cost_fst[bit] += a[i]; val_fst[bit] += b[i]; } } } rep(i,half_f) { cost_bin.emplace_back(cost_fst[i]); val_bin.emplace_back(val_fst[i]); } sort(ALL(cost_bin)); UNIQUE(cost_bin); sort(ALL(val_bin)); UNIQUE(val_bin); int sz_cost = cost_bin.size(); int sz_val = val_bin.size(); vector<tuple<ll,ll,int>> pair_f, pair_s; rep(i,half_f){ pair_f.emplace_back(cost_fst[i], val_fst[i], __builtin_popcount(i)); } rep(bit, half_s){ FOR(i,half_n,n){ if((bit >> (i-half_n)) & 1) { cost_snd[bit] += a[i]; val_snd[bit] += b[i]; } } } rep(i,half_s){ pair_s.emplace_back(cost_snd[i], val_snd[i], __builtin_popcount(i)); } sort(ALL(pair_f)); sort(RALL(pair_s)); vector<atcoder::segtree<ll,op,e>> st(k+1,atcoder::segtree<ll,op,e>(sz_val)); int pos = 0; ll ans = 0; rep(i,half_s){ while(pos < half_f and get<0>(pair_f[pos]) + get<0>(pair_s[i]) <= l){ if(get<2> (pair_f[pos]) > k) { pos++; continue; } int val_idx = lower_bound(ALL(val_bin), get<1>(pair_f[pos])) - val_bin.begin(); st[get<2>(pair_f[pos])].set(val_idx, st[get<2>(pair_f[pos])].get(val_idx)+1); pos++; } ll val_min = p-get<1>(pair_s[i]); if(val_min > val_bin.back()) continue; ll num_max = k-get<2>(pair_s[i]); rep(j,num_max+1){ int val_idx = lower_bound(ALL(val_bin), val_min) - val_bin.begin(); ans += st[j].prod(val_idx, sz_val); } } out(ans); }