Submission #35053588


Source Code Expand

#include <bits/stdc++.h>
typedef long long ll;
#define rep(i,a,n) for (int i=a;i<(int)n;i++)
ll read(){ll r;scanf("%lld",&r);return r;}

using Point = std::pair<ll, ll>;
const ll INF = 0x3f3f3f3f3f3f3f3f;
Point sub(Point a,Point b){ // return a-b
  return {a.first - b.first,a.second-b.second};
}

ll cross(Point a, Point b, Point c){ // (a-b) x (c-b)
  auto [x0,y0] = sub(a,b);
  auto [x1,y1] = sub(c,b);
  return x1*y0 -x0*y1;
}

struct ConvexHull{
  std::vector<Point> lower;
  std::vector<Point> upper;
  ConvexHull(Point p): lower{p}, upper{p}{}
  ConvexHull(const ConvexHull& a, const ConvexHull& b){ // 两个凸包合并
    {
      std::vector<Point> v; // 逆时针弧
      std::merge(a.lower.begin(), a.lower.end(), b.lower.begin(), b.lower.end(), back_inserter(v)); // 本身a,b下标有序, merge 会按pair的比较的排序
      for(Point p : v){
        while(lower.size() >= 2 && cross(lower.rbegin()[1], lower.back(), p) <= 0) lower.pop_back();
        lower.push_back(p);
      }
    }
    {
      std::vector<Point> v; // 顺时针弧
      std::merge(a.upper.begin(), a.upper.end(), b.upper.begin(), b.upper.end(), back_inserter(v)); // 本身a,b下标有序, merge 会按pair的比较的排序
      for(Point p : v){
        while(upper.size() >= 2 && cross(upper.rbegin()[1], upper.back(), p) >= 0) upper.pop_back();
        upper.push_back(p);
      }
    }
  }
  ll get(ll A, ll B) const { // 凸包中 求 Ax+By 的最大值,  B > 0, max(a/bx+y) 顺时针 上凸, B < 0 min(a/bx+y) 逆时针下凸
    auto& s = B < 0 ? lower : upper;
    auto f = [&](ll i){ return A * s[i].first + B * s[i].second; };
    ll l = 0;
    ll r = s.size() - 1;
    while(r-l >= 3){ // 3分
      ll x1 = (2*l+r)/3;
      ll x2 = (l+2*r)/3;
      ll f1 = f(x1);
      ll f2 = f(x2);
      if(f1 < f2) l = x1;
      else r = x2;
    }
    ll ans = -INF;
    rep(i,l,r+1) ans = std::max(ans, f(i));
    return ans;
  }
};
int main(){
  ll Q = read(); // 2e5
  std::vector<ConvexHull> S;
  rep(i,1,Q+1){
    ll X = read();
    ll Y = read(); // + (X,Y)
    ll A = read(); //
    ll B = read(); // 查询(A,B)
    S.emplace_back(std::pair{X, Y});
    // 最低位 2的幂次 次处理, 其实就是树状数组的想法, 每个位置表示 二进制最低位对应的一断内容,现在直接收集起来
    for(int j = i; j%2==0; j/=2){
      S.rbegin()[1] = ConvexHull(S.rbegin()[1], S.back());
      S.pop_back();
    }
    ll ans = -INF;
    for(auto& s : S) ans = std::max(ans, s.get(A, B));
    printf("%lld\n",ans);
  }
}

// r/3 + 2l/3 
// 2r/3 + l/3

Submission Info

Submission Time
Task Ex - Linear Maximization
User cromarmot
Language C++ (GCC 9.2.1)
Score 600
Code Size 2565 Byte
Status AC
Exec Time 380 ms
Memory 9568 KiB

Compile Error

./Main.cpp: In function ‘ll read()’:
./Main.cpp:4:21: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
    4 | ll read(){ll r;scanf("%lld",&r);return r;}
      |                ~~~~~^~~~~~~~~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 2
AC × 29
Set Name Test Cases
Sample sample_01.txt, sample_02.txt
All circle_01.txt, circle_02.txt, circle_03.txt, circle_04.txt, dence_01.txt, dence_02.txt, large_01.txt, large_02.txt, line2_01.txt, line2_02.txt, line2_03.txt, line_01.txt, line_02.txt, line_03.txt, prec_01.txt, prec_02.txt, sample_01.txt, sample_02.txt, small_01.txt, small_02.txt, small_03.txt, small_04.txt, small_05.txt, small_06.txt, small_07.txt, small_08.txt, small_09.txt, small_10.txt, zero_01.txt
Case Name Status Exec Time Memory
circle_01.txt AC 378 ms 8456 KiB
circle_02.txt AC 380 ms 8808 KiB
circle_03.txt AC 374 ms 9356 KiB
circle_04.txt AC 379 ms 9568 KiB
dence_01.txt AC 183 ms 3764 KiB
dence_02.txt AC 183 ms 3724 KiB
large_01.txt AC 223 ms 4300 KiB
large_02.txt AC 218 ms 4300 KiB
line2_01.txt AC 147 ms 3724 KiB
line2_02.txt AC 145 ms 3672 KiB
line2_03.txt AC 157 ms 3708 KiB
line_01.txt AC 161 ms 4264 KiB
line_02.txt AC 160 ms 4136 KiB
line_03.txt AC 170 ms 4224 KiB
prec_01.txt AC 217 ms 4292 KiB
prec_02.txt AC 212 ms 4244 KiB
sample_01.txt AC 6 ms 3756 KiB
sample_02.txt AC 3 ms 3644 KiB
small_01.txt AC 2 ms 3748 KiB
small_02.txt AC 2 ms 3640 KiB
small_03.txt AC 2 ms 3756 KiB
small_04.txt AC 2 ms 3712 KiB
small_05.txt AC 2 ms 3620 KiB
small_06.txt AC 2 ms 3584 KiB
small_07.txt AC 2 ms 3640 KiB
small_08.txt AC 3 ms 3708 KiB
small_09.txt AC 5 ms 3668 KiB
small_10.txt AC 9 ms 3648 KiB
zero_01.txt AC 180 ms 3568 KiB