Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 KB | None | 0 0
  1. #pragma region
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. // Common DS shorteners
  5. using ll = long long; using ull = unsigned long long;
  6. using pii = pair<int, int>; using pll = pair<ll, ll>;
  7. template <typename T> using vec = vector<T>;
  8. template <typename K, typename V> using um = unordered_map<K, V>; template<typename K, typename V> using om = map<K, V>;
  9. template <typename K> using us = unordered_set<K>; template <typename K> using os = set<K>;
  10. using vi = vec<int>; using vl = vec<ll>; using vpi = vec<pii>; using vpl = vec<pll>;
  11. // Shorthand Macros
  12. #define INF 0x3f3f3f3f
  13. #define LLINF 0x3f3f3f3f3f3f3f3f
  14. #define mpr make_pair
  15. #define pb push_back
  16. // Shorthand Function Macros
  17. #define Fori(a, b) for (int i = a; i < b; i++)
  18. #define Forj(a, b) for (int j = a; j < b; j++)
  19. #define Fork(a, b) for (int k = a; k < b; k++)
  20. #define Forinci(a, b) for (int i = a; i <= b; i++)
  21. #define Forincj(a, b) for (int j = a; j <= b; j++)
  22. #define Forinck(a, b) for (int k = a; k <= b; k++)
  23. #define Cmplt(type) bool operator<(const type &o) const
  24. #define Cmpgt(type) bool operator>(const type &o) const
  25. #define Cmpfn(type) bool operator()(const type &a, const type &b)
  26. // Shorthand Function Macros Part 2
  27. #define Pow2(x) (1LL << (x))
  28. // Shorthand Functions
  29. template<typename T> inline void maxa(T& st, T v) { st = max(st, v); }
  30. template<typename T> inline void mina(T& st, T v) { st = min(st, v); }
  31. inline void setprec(ostream& out, int prec) { out << setprecision(prec) << fixed; }
  32. // Out operators and printing for arrays and vectors
  33. template <typename T> ostream& operator<<(ostream& out,vector<T> iter){out<<"[";for(auto t:iter){out<<t<<", ";}out<<"]";return out;}
  34. template <typename T> void debugArray(T *arr,int sz){cout<<"[";for(int i=0;i<sz;i++){cout<<arr[i]<<", "; } cout<<"]\n";}
  35. template <typename T> void printArray(T *arr,int sz){for(int i=0;i<sz;i++){cout<<arr[i]<<" "; } cout<<"\n";}
  36. #define OUT_OPERATOR(type, propa, propb) ostream& operator<<(ostream& out,type obj){out<<"("<<#propa<<"="<<obj. propa<<", "<<#propb<<"="<<obj. propb<<")";return out;}
  37. // I/O Operations
  38. inline void scan(){}
  39. template<typename F, typename... R> inline void scan(F &f,R&... r){cin>>f;scan(r...);}
  40. template <typename F> inline void println(F t){cout<<t<<'\n';}
  41. template<typename F, typename... R> inline void println(F f,R... r){cout<<f<<" ";println(r...);}
  42. inline void print(){}
  43. template<typename F, typename... R> inline void print(F f,R... r){cout<<f;print(r...);}
  44. // Debugging
  45. int di_; string dnms_, casttostr_ = ",";
  46. void debug_(){cout<<endl;}
  47. template<typename T> string debug_tostring_(T val) { ostringstream os; os << val; return os.str(); }
  48. string debug_name_(string s, string str_val) {
  49. string ret = ""; if (s[0] == ' ') { ret += ' '; s = s.substr(1); } if (s[0] == '"' && s[s.length() - 1] == '"') { ret += string("[") + str_val + "]"; }
  50. else if (s.length() == (size_t)3 && s[0] == '\'' && s[2] == '\'') { ret += '(' + str_val + ')'; } else { ret += s + ": " + str_val; } return ret;
  51. } template<typename F, typename... R> void debug_(F f, R... r) {
  52. int bc = 0; string pr = "";
  53. while (bc != 0 || dnms_[di_] != ',') { if (dnms_[di_] == '(') { bc++; } else if (dnms_[di_] == ')') { bc--; } pr += dnms_[di_++]; }
  54. di_++; cout << debug_name_(pr, debug_tostring_(f)) << ","; debug_(r...);
  55. }
  56. #define debug(...) do{dnms_=#__VA_ARGS__+casttostr_,di_=0,debug_(__VA_ARGS__);}while(0)
  57. #pragma endregion
  58.  
  59. struct p{
  60. ll a,b;
  61. Cmplt(p){return a<o.a;}
  62. Cmpgt(p){return a>o.a;}
  63. };
  64. int n,A[21],B[21];
  65. ll h;
  66.  
  67. const int MN=2e5+10;
  68. int bit[MN];
  69. int qu(int x){
  70. int s=0;
  71. for(;x;x-=x&-x)s+=bit[x];
  72. return s;
  73. }
  74. void inc(int x){
  75. for(;x<MN;x+=x&-x)bit[x]++;
  76. }
  77.  
  78. vl ranks;
  79. int rk(ll x){
  80. return lower_bound(ranks.begin(), ranks.end(),x)-ranks.begin()+1;
  81. }
  82.  
  83. vec<p> solve(int l, int r) {
  84. int sz = r-l+1;
  85. vec<p> ret;
  86.  
  87. int sel[21];
  88. function<void(int)> rec = [&](int cur) {
  89. if (cur == sz) {
  90. ll ta=0,tb=0;
  91. Fori(0,sz){
  92. if (sel[i]&1)
  93. ta+=A[l+i];
  94. if (sel[i]&2)
  95. tb+=B[l+i];
  96. }
  97. ret.pb({ta,tb});
  98. return;
  99. }
  100.  
  101. Forinci(1, 3) {
  102. sel[cur] = i;
  103. rec(cur+1);
  104. }
  105. };
  106. // ret.pb({0,0});
  107.  
  108. rec(0);
  109. return ret;
  110. }
  111.  
  112. ll solve(){
  113. int mid=n/2;
  114. auto aa=solve(0,mid),bb=solve(mid+1,n-1);
  115. // debug(mid);
  116.  
  117. sort(aa.begin(), aa.end());
  118. // sort(bb.begin(), bb.end());
  119. // bb.resize(unique(bb.begin(), bb.end()) - bb.begin());
  120. sort(bb.begin(), bb.end(),greater<p>());
  121. ll tot=0;
  122. int ptr=0,bsz=bb.size();
  123.  
  124. for(auto x:aa){ranks.pb(x.b);ranks.pb(h-x.b);}
  125. for(auto x:bb){ranks.pb(x.b);}
  126. sort(ranks.begin(), ranks.end());
  127.  
  128. // for(auto x:aa)debug("aa",x.a,x.b);
  129. // for(auto x:bb)debug("bb",x.a,x.b);
  130.  
  131. // vl bs;
  132. for (auto x : aa) {
  133. while(ptr<bsz&&x.a+bb[ptr].a>=h){
  134. // bs.pb(bb[ptr].b);
  135. // int rkk=rk(bb[ptr].b);debug('+',bb[ptr].b,rkk);
  136. inc(rk(bb[ptr].b));
  137. ptr++;
  138. }
  139. tot+=qu(MN-1)-qu(rk(h-x.b)-1);
  140. }
  141.  
  142. return tot;
  143. }
  144.  
  145. int main(){
  146. ios_base::sync_with_stdio(false);
  147. cin.tie(NULL);
  148. int T;scan(T);int cno=1;
  149. while (T--) {
  150. scan(n,h);
  151. Fori(0,n)scan(A[i]);
  152. Fori(0,n)scan(B[i]);
  153. ranks.clear();
  154. memset(bit,0,sizeof bit);
  155. cout<<"Case #"<<cno<<": "<<solve()<<"\n";cno++;
  156. }
  157. return 0;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement