QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#283185#6327. Count Arithmetic ProgressionMisukiWA 48ms12544kbC++205.5kb2023-12-14 00:58:142023-12-14 00:58:14

Judging History

你现在查看的是最新测评结果

  • [2023-12-14 00:58:14]
  • 评测
  • 测评结果:WA
  • 用时:48ms
  • 内存:12544kb
  • [2023-12-14 00:58:14]
  • 提交

answer

#pragma GCC optimize("O2")
#include <algorithm>
#include <array>
#include <bit>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <cfloat>
#include <chrono>
#include <cinttypes>
#include <climits>
#include <cmath>
#include <compare>
#include <complex>
#include <concepts>
#include <cstdarg>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <new>
#include <numbers>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <ranges>
#include <set>
#include <span>
#include <sstream>
#include <stack>
#include <streambuf>
#include <string>
#include <tuple>
#include <type_traits>
#include <variant>

#define int ll
#define INT128_MAX (__int128)(((unsigned __int128) 1 << ((sizeof(__int128) * __CHAR_BIT__) - 1)) - 1)
#define INT128_MIN (-INT128_MAX - 1)

namespace R = std::ranges;
namespace V = std::views;

using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
using tiii = tuple<int, int, int>;
using ldb = long double;
//#define double ldb

template<class T>
ostream& operator<<(ostream& os, const pair<T, T> pr) {
  return os << pr.first << ' ' << pr.second;
}
template<class T, size_t N>
ostream& operator<<(ostream& os, const array<T, N> &arr) {
  for(const T &X : arr)
    os << X << ' ';
  return os;
}
template<class T>
ostream& operator<<(ostream& os, const vector<T> &vec) {
  for(const T &X : vec)
    os << X << ' ';
  return os;
}

bool operator<=(array<int, 2> l, array<int, 2> r) {
  return l[0] * r[1] <= l[1] * r[0];
}
bool operator>=(array<int, 2> l, array<int, 2> r) {
  return l[0] * r[1] >= l[1] * r[0];
}


signed main() {
  ios::sync_with_stdio(false), cin.tie(NULL);

  int n; cin >> n;
  vector<int> l(n), r(n);
  for(int &x : l)
    cin >> x;
  for(int &x : r)
    cin >> x;

  auto inter = [&](array<int, 2> l1, array<int, 2> l2) { return l1[0] > l2[0] ? array<int, 2>{l2[1] - l1[1], l1[0] - l2[0]} :
                                                                                array<int, 2>{l1[1] - l2[1], l2[0] - l1[0]}; };

  vector<array<int, 2>> mnHull; //{ax + b}
  {
    vector<array<int, 2>> cand(n);
    for(int i = 0; i < n; i++)
      cand[i] = {-i, r[i] + 1};
    for(array<int, 2> L : cand) {
      while(ssize(mnHull) >= 2 and inter(end(mnHull)[-2], end(mnHull)[-1]) >= inter(end(mnHull)[-1], L))
        mnHull.pop_back();
      mnHull.emplace_back(L);
    }
  }

  vector<array<int, 2>> mxHull;
  {
    vector<array<int, 2>> cand(n);
    for(int i = 0; i < n; i++)
      cand[i] = {-i, l[i]};
    for(array<int, 2> L : cand | V::reverse) {
      while(ssize(mxHull) >= 2 and inter(end(mxHull)[-2], end(mxHull)[-1]) >= inter(end(mxHull)[-1], L))
        mxHull.pop_back();
      mxHull.emplace_back(L);
    }
  }

  auto eval = [&](array<int, 2> L, int x) { return L[0] * x + L[1]; };

  auto evalHull = [&](int _x, vector<array<int, 2>> &hull) {
    array<int, 2> x = {_x, 1};
    int l = 0, r = ssize(hull) - 1;
    while(l < r) {
      int mid = (l + r) / 2;
      if (inter(hull[mid], hull[mid + 1]) <= x)
        l = mid + 1;
      else
        r = mid;
    }
    return eval(hull[l], _x);
  };

  auto Floor = [&](int a, int b) { return a >= 0 ? a / b : (a - b) / b; };

  const int C = 1ll << 40;

  int mp;
  {
    int l = -C, r = C;
    while(l < r) {
      int mid = Floor(l + r, 2);
      if (evalHull(mid, mnHull) - evalHull(mid, mxHull) <= evalHull(mid + 1, mnHull) - evalHull(mid + 1, mxHull))
        l = mid + 1;
      else
        r = mid;
    }
    mp = l;
  }

  int lb;
  {
    int l = -C, r = mp;
    while(l < r) {
      int mid = Floor(l + r, 2);
      if (evalHull(mid, mnHull) - evalHull(mid, mxHull) <= 0)
        l = mid + 1;
      else
        r = mid;
    }
    lb = l;
  }

  int rb;
  {
    int l = mp, r = C;
    while(l < r) {
      int mid = Floor(l + r, 2);
      if (evalHull(mid, mnHull) - evalHull(mid, mxHull) > 0)
        l = mid + 1;
      else
        r = mid;
    }
    rb = l;
  }

  auto FLOOR = [](array<int, 2> x) { return x[0] >= 0 ? x[0] / x[1] : (x[0] - x[1]) / x[1]; };
  auto sum = [](int l, int r, int n) { return (l + r) % 998244353 * n / 2; };

  int ans = 0;
  R::reverse(mnHull);
  R::reverse(mxHull);
  while(lb < rb) {
    while(ssize(mnHull) >= 2 and inter(end(mnHull)[-1], end(mnHull)[-2]) <= array<int, 2>{lb, 1})
      mnHull.pop_back();
    while(ssize(mxHull) >= 2 and inter(end(mxHull)[-1], end(mxHull)[-2]) <= array<int, 2>{lb, 1})
      mxHull.pop_back();
    int nxt = rb;
    if (ssize(mnHull) >= 2)
      nxt = min(nxt, FLOOR(inter(end(mnHull)[-1], end(mnHull)[-2])) + 1);
    if (ssize(mxHull) >= 2)
      nxt = min(nxt, FLOOR(inter(end(mxHull)[-1], end(mxHull)[-2])) + 1);
    ans = (ans + sum(eval(end(mnHull)[-1], lb), eval(end(mnHull)[-1], nxt - 1), nxt - lb) - 
                 sum(eval(end(mxHull)[-1], lb), eval(end(mxHull)[-1], nxt - 1), nxt - lb)) % 998244353;
    //cout << lb << " to " << nxt << " " << ans << '\n';
    //cout << mnHull << '\n';
    //cout << mxHull << '\n';
    lb = nxt;
  }

  cout << ans << '\n';

  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3468kb

input:

3
5 5 2
7 6 7

output:

6

result:

ok 1 number(s): "6"

Test #2:

score: 0
Accepted
time: 1ms
memory: 3548kb

input:

4
2 3 1 6
5 6 4 8

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: 0
Accepted
time: 48ms
memory: 12544kb

input:

300000
121398540794 60081434790 252920491076 43666218735 95583832019 21178121624 315727133812 89837170656 138389231466 174687947367 124350262341 108583578309 289629745492 321006985392 301201031648 138149017169 255983300245 138801256482 285614769875 130583757928 261690466826 74624776159 303504239011 ...

output:

2000014

result:

ok 1 number(s): "2000014"

Test #4:

score: -100
Wrong Answer
time: 0ms
memory: 3476kb

input:

2
1 1
1000000000000 1000000000000

output:

703834535

result:

wrong answer 1st numbers differ - expected: '276262510', found: '703834535'