Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define eps 0.00000000001
- using namespace std;
- #define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
- void err(istream_iterator<string> it) {}
- template<typename T, typename... Args>
- void err(istream_iterator<string> it, T a, Args... args) {
- cerr << *it << " = " << a << endl;
- err(++it, args...);
- }
- class CHT{
- vector<long long int>m,b;
- int slopetype, querytype;
- public:
- CHT(int _s = 0, int _q = 0){
- slopetype = _s;
- querytype = _q;
- }
- bool isbad(int f1, int f2, int f3){
- double ll = (double)(b[f3]-b[f1])*(m[f1]-m[f2]);
- double rr = (double)(b[f2]-b[f1])*(m[f1]-m[f3]);
- if((slopetype^querytype))return (ll>rr)||(fabs(ll-rr)<eps);
- else return (ll<rr)||(fabs(ll-rr)<eps);
- }
- void addline(int _m, int _b){
- m.push_back(_m);
- b.push_back(_b);
- int sz = m.size();
- while(sz>=3 && isbad(sz-3,sz-2,sz-1)){
- swap(m[sz-2],m[sz-1]); swap(b[sz-2],b[sz-1]);
- m.pop_back();
- b.pop_back();
- sz--;
- }
- }
- long long int f(long long int x, int idx){
- return m[idx]*x+b[idx];
- }
- long long int query(long long int x){
- int lo = 0;
- int hi = m.size()-1;
- while((lo+3)<hi){
- error(lo,hi);
- int m1 = lo+(hi-lo)/3;
- int m2 = hi-(hi-lo)/3;
- long long int y1 = f(x,m1);
- long long int y2 = f(x,m2);
- if(querytype){
- //finding maximum here
- if(y1>=y2){
- hi = m2;
- }
- else{
- lo = m1;
- }
- }
- else{
- if(y1<=y2){
- hi = m2;
- }
- else{
- lo = m1;
- }
- }
- }
- long long int kk = 0; long long int ll = 1000000000000000000;
- for(int i = lo;i<=hi;++i){
- kk = max(kk,f(x,i));
- ll = min(ll,f(x,i));
- }
- if(querytype){
- return kk;
- }
- else{
- return ll;
- }
- }
- long long int brutequery(long long int x){
- long long int ans = -1; int ff = 0;
- for(int i = 0 ; i<m.size();++i){
- if(ff==0){
- ans = f(x,i);
- ff = 1;
- }
- else{
- if(querytype)ans = max(ans,f(x,i));
- else ans = min(ans,f(x,i));
- }
- }
- return ans;
- }
- long long querymax(long long int x) {
- if(m.size() == 0) return -1000000000000000000;
- int l = -1, r = m.size() - 1;
- while(r - l > 1) {
- int mid = (l + r) / 2;
- if(f(x,mid) <= f(x,mid+1)) l = mid;
- else r = mid;
- }
- return max(f(x,l),f(x,r));
- }
- long long querymin(long long int x) {
- if(m.size() == 0) return 1000000000000000000;
- int l = -1, r = m.size() - 1;
- while(r - l > 1) {
- int mid = (l + r) / 2;
- if(f(x,mid) >= f(x,mid+1)) l = mid;
- else r = mid;
- }
- return min(f(x,l),f(x,r));
- }
- long long int qquery(long long int x){
- if(querytype)return querymax(x);
- else return querymin(x);
- }
- };
- int mtype[4] = {0,0,1,1};
- int qtype[4] = {0,1,0,1};
- int main(){
- int q,a;
- scanf("%d %d",&q,&a);
- CHT mycht(mtype[a-1],qtype[a-1]);
- for(int i = 1; i<=q; ++i){
- int type;
- scanf("%d",&type);
- if(type==1){
- int m,b;
- scanf("%d %d",&m,&b);
- mycht.addline(m,b);
- }
- else{
- int x;
- scanf("%d",&x);
- printf("%lld\n",mycht.qquery(x));
- //printf("%lld\n",mycht.brutequery(x));
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment