QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#170021#6327. Count Arithmetic Progressionucup-team1209#WA 55ms22232kbC++202.6kb2023-09-09 14:30:192023-09-09 14:31:50

Judging History

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

  • [2023-09-09 14:31:50]
  • 评测
  • 测评结果:WA
  • 用时:55ms
  • 内存:22232kb
  • [2023-09-09 14:30:19]
  • 提交

answer

#include<bits/stdc++.h>
using std::cin;
using std::cout;
using ll = long long;
const int N = 300005;
const int mod = 998244353;
const int inv2 = (mod + 1) / 2;
int n;
struct func {
	ll k, a;
	ll operator () (ll x) const {
		return x * k + a;
	}
	ll inte(ll l, ll r) {
		ll sa = a % mod * ((r - l + 1) % mod) % mod;
		ll sk = (l + r) % mod * ((r - l + 1) % mod) % mod * inv2 % mod * k % mod;
		return (sa + sk) % mod;
	}
};
ll cross(func x, func y, func z) {
	return (y.k - x.k) * (z.a - x.a) - (z.k - x.k) * (y.a - x.a);
}
struct hull {
	std::vector<func> o;
	void init() {
		sort(o.begin(), o.end(), [](func x, func y) {
			return x.k < y.k;
		});
		std::vector<func> stack;
		for(func x : o) {
			for(;stack.size() >= 2 && cross(stack.rbegin()[1], stack.back(), x) >= 0;) {
				stack.pop_back();
			}
			stack.push_back(x);
		}
		o = stack;
	}
	using pr = std::pair<ll, int>;
	pr eval(ll x) {
		int l = 0, r = o.size() - 1;
		for(;l < r;) {
			int mid = (l + r) >> 1;
			if(o[mid](x) > o[mid + 1](x)) {
				r = mid;
			} else {
				l = mid + 1;
			}
		}
		return {o[l](x), l};
	}
	ll inte(ll l, ll r, pr vl, pr vr) {
		if(vl.second == vr.second) {
			return o[vl.second].inte(l, r);
		}
		int mid = (l + r) >> 1;
		return (inte(l, mid, vl, eval(mid)) + inte(mid + 1, r, eval(mid + 1), vr)) % mod;
	}
	ll inte(ll l, ll r) {
		return inte(l, r, eval(l), eval(r));
		ll ans = 0;
		for(int i = l;i <= r;++i) ans += eval(i).first;
		assert(ans == inte(l, r, eval(l), eval(r)));
		return ans;
	}
} up, down;
ll L[N], R[N];
int main() {
	std::ios::sync_with_stdio(false), cin.tie(0);
	cin >> n;
	for(int i = 1;i <= n;++i) cin >> L[i];
	for(int i = 1;i <= n;++i) cin >> R[i];
	for(int i = 1;i <= n;++i) {
		down.o.push_back(func{-(i - 1), L[i]});
		up.o.push_back(func{(i - 1), -R[i]});
	}
	/*
	int out = 0;
	for(int x = L[1];x <= R[1];++x) {
		for(int d = -100;d <= 500;++d) {
			int ok = 1;
			for(int j = 1;j <= n;++j) {
				ll w = x + (j - 1) * d;
				ok = ok && L[j] <= w && w <= R[j];
			}
			out += ok;
		}
	}
	cout << out << '\n';
	*/
	const ll V = 1.01e12;
	ll l = -V, r = V;
	up.init(), down.init();

	auto get = [&](ll x) { return -(up.eval(x).first + down.eval(x).first); };

	for(;l + 1 < r;) {
		ll mid = (l + r) >> 1;
		if(get(mid) > get(mid + 1)) {
			r = mid;
		} else {
			l = mid + 1;
		}
	}
	if(get(l) < 0) {
		cout << 0 << '\n';
		return 0;
	}
	for(ll s = 1ll << 39;s;s >>= 1) {
		if(get(l - s) >= 0) l -= s;
		if(get(r + s) >= 0) r += s;
	}
	ll ans = -(up.inte(l, r) + down.inte(l, r));
	ans += r - l + 1;
	ans %= mod;
	ans += mod;
	ans %= mod;
	cout << ans << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 5672kb

input:

3
5 5 2
7 6 7

output:

6

result:

ok 1 number(s): "6"

Test #2:

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

input:

4
2 3 1 6
5 6 4 8

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: -100
Wrong Answer
time: 55ms
memory: 22232kb

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:

1981292

result:

wrong answer 1st numbers differ - expected: '2000014', found: '1981292'