#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
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 |
1 |
Correct |
14 ms |
19072 KB |
Output is correct |
2 |
Correct |
16 ms |
19200 KB |
Output is correct |
3 |
Correct |
14 ms |
19200 KB |
Output is correct |
4 |
Correct |
14 ms |
19200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
19200 KB |
Output is correct |
2 |
Correct |
14 ms |
19200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
19584 KB |
Output is correct |
2 |
Correct |
16 ms |
19456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
42 ms |
38776 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
40 ms |
38780 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
119 ms |
49016 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
128 ms |
49656 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
128 ms |
49016 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
123 ms |
49272 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
118 ms |
59240 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |