BotByte

Untitled

May 8th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define eps 0.00000000001
  3. using namespace std;
  4.  
  5. #define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
  6.  
  7. void err(istream_iterator<string> it) {}
  8. template<typename T, typename... Args>
  9. void err(istream_iterator<string> it, T a, Args... args) {
  10. cerr << *it << " = " << a << endl;
  11. err(++it, args...);
  12. }
  13.  
  14. class CHT{
  15. vector<long long int>m,b;
  16. int slopetype, querytype;
  17. public:
  18. CHT(int _s = 0, int _q = 0){
  19. slopetype = _s;
  20. querytype = _q;
  21. }
  22. bool isbad(int f1, int f2, int f3){
  23. double ll = (double)(b[f3]-b[f1])*(m[f1]-m[f2]);
  24. double rr = (double)(b[f2]-b[f1])*(m[f1]-m[f3]);
  25. if((slopetype^querytype))return (ll>rr)||(fabs(ll-rr)<eps);
  26. else return (ll<rr)||(fabs(ll-rr)<eps);
  27. }
  28. void addline(int _m, int _b){
  29. m.push_back(_m);
  30. b.push_back(_b);
  31.  
  32. int sz = m.size();
  33.  
  34. while(sz>=3 && isbad(sz-3,sz-2,sz-1)){
  35. swap(m[sz-2],m[sz-1]); swap(b[sz-2],b[sz-1]);
  36. m.pop_back();
  37. b.pop_back();
  38. sz--;
  39. }
  40. }
  41. long long int f(long long int x, int idx){
  42. return m[idx]*x+b[idx];
  43. }
  44. long long int query(long long int x){
  45. int lo = 0;
  46. int hi = m.size()-1;
  47. while((lo+3)<hi){
  48. error(lo,hi);
  49. int m1 = lo+(hi-lo)/3;
  50. int m2 = hi-(hi-lo)/3;
  51. long long int y1 = f(x,m1);
  52. long long int y2 = f(x,m2);
  53. if(querytype){
  54. //finding maximum here
  55. if(y1>=y2){
  56. hi = m2;
  57. }
  58. else{
  59. lo = m1;
  60. }
  61. }
  62. else{
  63. if(y1<=y2){
  64. hi = m2;
  65. }
  66. else{
  67. lo = m1;
  68. }
  69. }
  70. }
  71. long long int kk = 0; long long int ll = 1000000000000000000;
  72. for(int i = lo;i<=hi;++i){
  73. kk = max(kk,f(x,i));
  74. ll = min(ll,f(x,i));
  75. }
  76. if(querytype){
  77. return kk;
  78. }
  79. else{
  80. return ll;
  81. }
  82.  
  83. }
  84. long long int brutequery(long long int x){
  85. long long int ans = -1; int ff = 0;
  86. for(int i = 0 ; i<m.size();++i){
  87. if(ff==0){
  88. ans = f(x,i);
  89. ff = 1;
  90. }
  91. else{
  92. if(querytype)ans = max(ans,f(x,i));
  93. else ans = min(ans,f(x,i));
  94. }
  95. }
  96. return ans;
  97. }
  98. long long querymax(long long int x) {
  99. if(m.size() == 0) return -1000000000000000000;
  100. int l = -1, r = m.size() - 1;
  101. while(r - l > 1) {
  102. int mid = (l + r) / 2;
  103. if(f(x,mid) <= f(x,mid+1)) l = mid;
  104. else r = mid;
  105. }
  106. return max(f(x,l),f(x,r));
  107. }
  108. long long querymin(long long int x) {
  109. if(m.size() == 0) return 1000000000000000000;
  110. int l = -1, r = m.size() - 1;
  111. while(r - l > 1) {
  112. int mid = (l + r) / 2;
  113. if(f(x,mid) >= f(x,mid+1)) l = mid;
  114. else r = mid;
  115. }
  116. return min(f(x,l),f(x,r));
  117. }
  118. long long int qquery(long long int x){
  119. if(querytype)return querymax(x);
  120. else return querymin(x);
  121. }
  122. };
  123.  
  124. int mtype[4] = {0,0,1,1};
  125. int qtype[4] = {0,1,0,1};
  126.  
  127. int main(){
  128. int q,a;
  129. scanf("%d %d",&q,&a);
  130. CHT mycht(mtype[a-1],qtype[a-1]);
  131. for(int i = 1; i<=q; ++i){
  132. int type;
  133. scanf("%d",&type);
  134. if(type==1){
  135. int m,b;
  136. scanf("%d %d",&m,&b);
  137. mycht.addline(m,b);
  138. }
  139. else{
  140. int x;
  141. scanf("%d",&x);
  142. printf("%lld\n",mycht.qquery(x));
  143. //printf("%lld\n",mycht.brutequery(x));
  144. }
  145. }
  146. return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment