#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 6e5;
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);
vector<int> comp;
for(int i=1; i<=A+B+1; i++) comp.push_back(i);
comp.push_back(C);
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].lb+=A; P[i].rb+=A;
P[i].ra++; P[i].rb++;
P[i].chk=0;
comp.push_back(P[i].la);
comp.push_back(P[i].la+1);
comp.push_back(P[i].ra);
comp.push_back(P[i].lb);
comp.push_back(P[i].lb+1);
comp.push_back(P[i].rb);
}
sort(comp.begin(), comp.end());
comp.erase(unique(comp.begin(), comp.end()), comp.end());
for(int i=1; i<=N; i++)
{
P[i].la=lower_bound(comp.begin(), comp.end(), P[i].la)-comp.begin()+1;
P[i].lb=lower_bound(comp.begin(), comp.end(), P[i].lb)-comp.begin()+1;
P[i].ra=lower_bound(comp.begin(), comp.end(), P[i].ra)-comp.begin()+1;
P[i].rb=lower_bound(comp.begin(), comp.end(), P[i].rb)-comp.begin()+1;
seg.update(1, 1, comp.size()-1, P[i].la, P[i].ra-1, i);
seg.update(1, 1, comp.size()-1, P[i].lb, P[i].rb-1, i);
}
for(int i=1; i<=comp.size()+1; i++) par[i]=i;
int now=lower_bound(comp.begin(), comp.end(), C)-comp.begin()+1; ll cnt=0, bef=0;
while(1)
{
ans+=bef*(comp[now]-comp[now-1]);
cnt+=comp[now]-comp[now-1];
seg.query(1, 1, comp.size()-1, 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;
bef=p.w;
//printf("%d %d %d %d %lld\n", p.la, p.ra, p.lb, p.rb, 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:108:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
108 | for(int i=1; i<=comp.size()+1; i++) par[i]=i;
| ~^~~~~~~~~~~~~~~
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:80:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
80 | scanf("%d%d%d%d%lld", &P[i].la, &P[i].ra, &P[i].lb, &P[i].rb, &P[i].w);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
56696 KB |
Output is correct |
2 |
Correct |
45 ms |
56696 KB |
Output is correct |
3 |
Correct |
41 ms |
56824 KB |
Output is correct |
4 |
Correct |
38 ms |
56704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
56832 KB |
Output is correct |
2 |
Correct |
39 ms |
56824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
57208 KB |
Output is correct |
2 |
Correct |
42 ms |
56952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
167 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
173 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
167 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
168 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
165 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
170 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
172 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |