#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);
vector<int> comp;
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:107:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
107 | 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:79:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
79 | 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 |
13 ms |
19072 KB |
Output is correct |
2 |
Correct |
14 ms |
19200 KB |
Output is correct |
3 |
Incorrect |
13 ms |
19200 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
14 ms |
19200 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
19584 KB |
Output is correct |
2 |
Correct |
16 ms |
19456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
23 ms |
19968 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
23 ms |
19968 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
189 ms |
50012 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
191 ms |
49884 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
179 ms |
50012 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
180 ms |
50012 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
132 ms |
32860 KB |
Output is correct |
2 |
Runtime error |
186 ms |
53980 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |