QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#428852 | #7520. Monster Generator | pandapythoner | WA | 1ms | 3868kb | C++20 | 5.7kb | 2024-06-01 22:29:31 | 2024-06-01 22:29:32 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll __int128_t
#define flt double
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
const ll inf = 1e18;
mt19937 rnd(234);
const ll mod = 1e9 + 7;
struct line {
ll k, b;
ll operator()(ll x) const {
return k * x + b;
}
ll sign(ll x) {
ll val = k * x + b;
if (val > 0) {
return 1;
}
if (val < 0) {
return -1;
}
if (k > 0) {
return 1;
}
return -1;
}
};
line operator-(const line& a) {
return line{ -a.k, -a.b };
}
line operator+(const line& a, const line& b) {
return line{ a.k + b.k, a.b + b.b };
}
line operator-(const line& a, const line& b) {
return line{ a.k - b.k, a.b - b.b };
}
ll intersect(const line& a, const line& b) {
assert(a.k != b.k);
ll p = (b.b - a.b);
ll q = (a.k - b.k);
if (q < 0) {
p *= -1;
q *= -1;
}
ll md = (p % q + q) % q;
return (p - md) / q + (md == 0 ? 0 : 1);
}
istream& operator>>(istream& in, ll& x) {
unsigned long long f;
in >> f;
x = f;
return in;
}
ostream& operator<<(ostream& out, ll y) {
unsigned long long f = y;
out << f;
return out;
}
int n;
ll m;
vector<ll> a, da, b, db;
vector<line> lna, lnb, sm;
unsigned long long solve() {
unsigned long long rs = 0;
vector<ll> aboba = { 0, m + 1 };
lna.resize(n);
lnb.resize(n);
sm.resize(n);
for (int i = 0; i < n; i += 1) {
lna[i] = line{ da[i], a[i] };
lnb[i] = line{ db[i], b[i] };
sm[i] = -lna[i] + lnb[i];
}
for (int i = 0; i < n; i += 1) {
if (da[i] != db[i]) {
aboba.push_back(intersect(lna[i], lnb[i]));
}
for (int j = i + 1; j < n; j += 1) {
if (lna[i].k != lna[j].k) {
aboba.push_back(intersect(lna[i], lna[j]));
}
if (lnb[i].k != lnb[j].k) {
aboba.push_back(intersect(lnb[i], lnb[j]));
}
if (lnb[i].k != lna[j].k) {
aboba.push_back(intersect(lnb[i], lna[j]));
}
if (lna[i].k != lnb[j].k) {
aboba.push_back(intersect(lna[i], lnb[j]));
}
}
}
sort(all(aboba));
for (int i = 0; i < (int)aboba.size() - 1; i += 1) {
ll l = aboba[i];
ll r = aboba[i + 1];
if (l < 0 or r > m + 1 or l >= r) {
continue;
}
vector<int> p(n);
for (int i = 0; i < n; i += 1) {
p[i] = i;
}
sort(all(p), [&](int x, int y) {
if (sm[x].sign(l) != sm[y].sign(l)) {
return sm[x].sign(l) > sm[y].sign(l);
}
if (sm[x].sign(l) == 1) {
return make_pair(lna[x](l), lna[x].k) < make_pair(lnb[y](l), lnb[y].k);
}
return make_pair(lnb[x](l), lnb[x].k) > make_pair(lnb[y](l), lnb[y].k);
});
line acc{ 0, 0 };
vector<line> lines;
lines.reserve(n + 1);
lines.push_back(line{ 0, 0 });
for (auto i : p) {
acc = acc + lna[i];
lines.push_back(acc);
acc = acc - lnb[i];
}
sort(all(lines), [&](const line& a, const line& b) {
return a.k < b.k;
});
vector<pair<ll, line>> stck;
for (auto ln : lines) {
if (stck.empty()) {
stck.push_back(make_pair(l, ln));
continue;
}
if (stck.back().second.k == ln.k and stck.back().second.b >= ln.b) {
continue;
}
while (!stck.empty()) {
if (stck.back().second(stck.back().first) > ln(stck.back().first)) {
break;
}
stck.pop_back();
}
if (stck.empty()) {
stck.push_back(make_pair(l, ln));
} else {
ll bg = intersect(ln, stck.back().second);
if(bg >= r){
continue;
}
stck.push_back(make_pair(bg, ln));
}
}
for (int j = 0; j < (int)stck.size(); j += 1) {
ll tl = max(l, stck[j].first);
ll tr = r;
if (j + 1 < (int)stck.size()) {
tr = min(tr, stck[j + 1].first);
}
if (tl >= tr) {
assert(0);
continue;
}
auto ln = stck[j].second;
ll p = (tr - tl);
ll q = (ln(tl) + ln(tr - 1));
assert(ln(tl) > 0 and ln(tr - 1) > 0);
if (p % 2 == 0) {
p /= 2;
} else if (q % 2 == 0) {
q /= 2;
} else {
assert(0);
}
rs += ((unsigned long long)p) * ((unsigned long long)q);
}
}
return rs;
}
int32_t main() {
if (1) {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
cin >> n >> m;
a.resize(n);
da.resize(n);
b.resize(n);
db.resize(n);
for (int i = 0; i < n; i += 1) {
cin >> a[i] >> da[i] >> b[i] >> db[i];
}
auto rs = solve();
cout << rs << "\n";
return 0;
}
/*
7 1000000000000000000
776874380544333 197 471391764744275 33
159838820333814 107 677112662750393 41
962335658276824 48 255593531071176 11
127404116579775 209 268525254990127 34
647620110614714 76 897947476313307 13
146196843402516 221 772928712898807 39
637929916804442 2 716937021892338 15
*/
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3672kb
input:
3 5 3 1 5 2 4 2 1 3 1 9 100 1
output:
113
result:
ok single line: '113'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3580kb
input:
3 100000000 3 1 5 2 4 2 1 3 1 9 100 1
output:
35000000549999998
result:
ok single line: '35000000549999998'
Test #3:
score: -100
Wrong Answer
time: 1ms
memory: 3868kb
input:
10 1000000000000000000 776874380544333 197 471391764744275 33 159838820333814 107 677112662750393 41 962335658276824 48 255593531071176 11 127404116579775 209 268525254990127 34 647620110614714 76 897947476313307 13 146196843402516 221 772928712898807 39 637929916804442 2 716937021892338 15 64200226...
output:
8070427944931518906
result:
wrong answer 1st lines differ - expected: '17883317185357051350', found: '8070427944931518906'