QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#658563 | #5144. Set of Intervals | etherealUnicorn | WA | 0ms | 3572kb | C++14 | 2.4kb | 2024-10-19 17:03:11 | 2024-10-19 17:03:12 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
const int N = 1e5 + 10;
const ll INF = 1e9;
pll lr[N];
ll calc(ll l1, ll r1, ll l2, ll r2)
{
if (r1 < l2)
{
return 1ll * (r1 - l1 + 1) * (r2 - l2 + 1);
}
else
{
r1 = min(r1, r2);
l2 = max(l2, l1);
return 1ll * (l2 - l1) * (r2 - l2 + 1) + 1ll * (r1 - l2 + 1) * (r2 - l2 + r2 - r1) / 2;
}
}
int main()
{
ios::sync_with_stdio(false);
int t, pos1, pos2;
ll minl, maxr;
bool samepos;
cin >> t;
while (t--)
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> lr[i].first >> lr[i].second;
if (n == 1)
{
cout << 1 << endl;
continue;
}
samepos = false;
minl = INF, maxr = -INF;
pos1 = pos2 = 0;
for (int i = 1; i <= n; i++)
{
if (lr[i].first < minl && lr[i].second > maxr)
{
pos1 = i, minl = lr[i].first;
pos2 = i, maxr = lr[i].second;
}
else
{
if (lr[i].first < minl)
{
pos1 = i;
minl = lr[i].first;
}
else if (lr[i].second > maxr)
{
pos2 = i;
maxr = lr[i].second;
}
}
}
if (pos1 == pos2)
{
samepos = true;
swap(lr[1], lr[pos1]);
}
else
{
if (pos2 == 1 && pos1 == 2)
swap(lr[1], lr[2]);
else if (pos2 == 1)
{
swap(lr[1], lr[2]);
swap(lr[pos1], lr[1]);
}
else
{
swap(lr[1], lr[pos1]);
swap(lr[2], lr[pos2]);
}
}
if (n == 2 && !samepos)
{
cout << calc(lr[1].first, lr[1].second, lr[2].first, lr[2].second) << endl;
continue;
}
ll secl = 1e9, secr = -1e9;
int st = (samepos ? 2 : 3);
for (int i = st; i <= n; i++)
{
secl = min(secl, lr[i].first);
secr = max(secr, lr[i].second);
}
ll p1 = calc(minl, maxr, secl, secr) + calc(secl, secr, minl, maxr) - calc(secl, secr, secl, secr);
if (samepos)
{
cout << p1 << endl;
}
else
{
p1 += calc(minl, secl - 1, lr[2].first, secl - 1);
p1 += calc(secr + 1, lr[1].second, secr + 1, maxr);
if (n >= 4)
p1 += calc(minl, secl - 1, secr + 1, maxr);
else
{
if (lr[1].second >= secl - 1 || lr[2].first <= secr + 1)
p1 += calc(minl, secl - 1, secr + 1, maxr);
else
p1 += calc(minl, secl - 1, lr[2].first, maxr) + calc(minl, lr[1].second, secr + 1, maxr) - calc(minl, lr[1].second, lr[2].first, maxr);
}
cout << p1 << endl;
}
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3572kb
input:
4 1 1 1000000000 2 1 1000000000 1 1000000000 4 1 2 3 4 5 6 7 8 4 1 3 2 4 5 8 6 7
output:
1 499999999500000000 10 21
result:
wrong answer 3rd numbers differ - expected: '26', found: '10'