This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 2e5;
struct Data
{
int la, ra, lb, rb; ll w; int chk;
bool operator < (const Data &p) const
{
if(w!=p.w) return w<p.w;
if(chk!=p.chk) return chk>p.chk;
if(chk==1) return la>p.la;
else return lb>p.lb;
}
};
int N, A, B, C;
Data P[MAXN+10];
bool visA[MAXN+10], visB[MAXN+10];
priority_queue<Data> PQ;
struct SEG
{
vector<int> tree[MAXN*4+10];
void update(int node, int tl, int tr, int l, int r, int p)
{
if(r<tl || tr<l) return;
if(l<=tl && tr<=r)
{
tree[node].push_back(p);
return;
}
int mid=tl+tr>>1;
update(node*2, tl, mid, l, r, p);
update(node*2+1, mid+1, tr, l, r, p);
}
void query(int node, int tl, int tr, int p)
{
for(auto it : tree[node])
{
if(P[it].chk!=0) continue;
P[it].chk=1;
PQ.push(P[it]);
//printf("!%d\n", it);
}
vector<int>().swap(tree[node]);
if(tl==tr) return;
int mid=tl+tr>>1;
if(p<=mid) query(node*2, tl, mid, p);
else query(node*2+1, mid+1, tr, p);
}
}seg;
int par[MAXN+10];
int Find(int x)
{
if(x==par[x]) return x;
return par[x]=Find(par[x]);
}
ll ans=0;
int main()
{
scanf("%d%d%d%d", &A, &B, &C, &N);
for(int i=1; i<=N; i++)
{
scanf("%d%d%d%d%lld", &P[i].la, &P[i].ra, &P[i].lb, &P[i].rb, &P[i].w);
P[i].chk=0;
}
for(int i=1; i<=N; i++)
{
P[i].lb+=A; P[i].rb+=A;
seg.update(1, 1, A+B, P[i].la, P[i].ra, i);
seg.update(1, 1, A+B, P[i].lb, P[i].rb, i);
}
for(int i=1; i<=A+B+1; i++) par[i]=i;
int now=C, cnt=0;
while(1)
{
cnt++;
//printf("%d\n", now);
seg.query(1, 1, A+B, now);
par[now]=now+1;
Data p; bool t=false;
while(!PQ.empty())
{
p=PQ.top();
if(p.chk==2)
{
if(Find(p.lb)>p.rb) { PQ.pop(); continue; }
t=true; break;
}
if(p.chk==1)
{
if(Find(p.la)>p.ra)
{
p.chk=2;
PQ.pop();
PQ.push(p);
continue;
}
t=true; break;
}
}
if(!t) break;
//printf("%d %d %d %d %lld\n", p.la, p.ra, p.lb, p.rb, p.w);
ans+=p.w;
if(p.chk==1)
{
now=Find(p.la);
}
else
{
now=Find(p.lb);
}
}
if(cnt==A+B) printf("%lld\n", ans);
else printf("-1\n");
}
Compilation message (stderr)
invitation.cpp: In member function 'void SEG::update(int, int, int, int, int, int)':
invitation.cpp:40:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
40 | int mid=tl+tr>>1;
| ~~^~~
invitation.cpp: In member function 'void SEG::query(int, int, int, int)':
invitation.cpp:56:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
56 | int mid=tl+tr>>1;
| ~~^~~
invitation.cpp: In function 'int main()':
invitation.cpp:73:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
73 | scanf("%d%d%d%d", &A, &B, &C, &N);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
invitation.cpp:76:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
76 | scanf("%d%d%d%d%lld", &P[i].la, &P[i].ra, &P[i].lb, &P[i].rb, &P[i].w);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |