QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#211917 | #2342. Directing Rainfall | HIR180 | AC ✓ | 4950ms | 210764kb | C++20 | 6.3kb | 2023-10-12 23:01:27 | 2023-10-12 23:01:27 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define rng(i,a,b) for(int i=(int)a;i<(int)b;i++)
#define rep(i,b) rng(i,0,b)
#define repn(i,b) rng(i,1,b+1)
#define pb push_back
#define eb emplace_back
#define si(x) (int)(x.size())
#define tct template<class t>
#define tctu template<class t, class u>
tctu bool chmax(t&a, u b){if(a<b){a=b;return 1;}return 0;}
tctu bool chmin(t&a, u b){if(a>b){a=b;return 1;}return 0;}
tct using vc=vector<t>;
using vi=vc<int>;
using P=pair<int,int>;
#define a first
#define b second
#define mp make_pair
pair<ll,ll>get(int a,int b,int c,int d,int e){
if(a == e) return mp(c, 1);
if(b == e) return mp(d, 1);
int up = d-c;
int dw = b-a;
pair<ll,ll>ret = mp(1LL*c*dw, dw);
ret.a += 1LL*up*(e-a);
return ret;
}
pair<ll, ll>get_mn(pair<ll,ll>x, pair<ll,ll>y){
//x.a/x.b < y.a/y.b?
__int128 le = (__int128)x.a * y.b;
__int128 ri = (__int128)y.a * x.b;
if(le < ri) return x;
return y;
}
struct seg{
//(a,c)-(b,d)
//a<b
int a,b,c,d;
seg(int A,int B,int C,int D){
a=A,b=B,c=C,d=D;
}
bool operator<(const seg &s)const{
if(a==s.a and b==s.b and c==s.c and d==s.d)return 0;
int mn = min(b, s.b);
int mx = max(a, s.a);
if(mn < mx) return false;
auto xa = get(a,b,c,d,mn);
auto xb = get(a,b,c,d,mx);
xa = get_mn(xa, xb);
auto xc = get(s.a,s.b,s.c,s.d,mn);
auto xd = get(s.a,s.b,s.c,s.d,mx);
xc = get_mn(xc, xd);
return get_mn(xa, xc) == xc;
}
};
int lb, ub, n;
vi za;
vc<tuple<int,int,int,int>>vec;
P Q[1000005];
set<seg>S;
map<int,int>M;
int in[500005];
vi edge[500005];
struct st{
vi val;
int nn;
void init(int sz){
nn=1;
while(sz+5>=nn) nn<<=1;
val.assign(nn+nn, 0);
}
int get(int vall,int k,int l,int r){
if(l == r) return val[k];
if(vall <= (l+r)/2) return val[k] + get(vall, k*2+1, l, (l+r)/2);
return val[k] + get(vall, k*2+2, (l+r)/2+1, r);
}
void upd(int le, int ri, int add, int k,int l,int r){
if(le>ri or ri<l or r<le) return;
if(le <= l and r <= ri){
val[k]+=add;
return;
}
upd(le,ri,add,k*2+1,l,(l+r)/2);
upd(le,ri,add,k*2+2,(l+r)/2+1,r);
}
int get(int val){ return get(val,0,0,nn-1); }
void upd(int le,int ri,int add){ upd(le,ri,add,0,0,nn-1); }
}s;
void solve(){
cin>>lb>>ub>>n;
za.pb(lb); za.pb(ub);
rep(i,n){
int ax,ay,bx,by;
cin>>ax>>ay>>bx>>by;
if(ax > bx){
swap(ax, bx);
swap(ay, by);
}
vec.eb(ax,bx,ay,by);
za.pb(ax);
za.pb(bx);
M[ax] = i;
M[bx] = i;
}
sort(za.begin(), za.end());
za.erase(unique(za.begin(), za.end()), za.end());
rep(i,n){
Q[lower_bound(za.begin(), za.end(), get<0>(vec[i]))-za.begin()] = mp(1, i);
Q[lower_bound(za.begin(), za.end(), get<1>(vec[i]))-za.begin()] = mp(2, i);
}
rep(i,si(za)){
{
auto [ty, id]=Q[i];
if(!ty) continue;
auto [a,b,c,d]=vec[id];
if(ty == 2){
S.erase(S.find(seg(a,b,c,d)));
}
else if(ty == 1){
auto it = S.lower_bound(seg(a,b,c,d));
if(it != S.end()){
int v = (*it).a;
edge[id].pb(M[v]);
in[M[v]] ++;
}
if(it != S.begin()){
it --;
int v = (*it).a;
edge[M[v]].pb(id);
in[id] ++;
}
S.insert(seg(a,b,c,d));
}
}
}
queue<int>que;
rep(i,n) if(!in[i]) que.push(i);
vi ord;
while(!que.empty()){
int q = que.front(); que.pop();
ord.pb(q);
for(auto to:edge[q]){
in[to]--;
if(!in[to]) que.push(to);
}
}
set<int>up,dw;
set<int>dif;
s.init(si(za));
auto add=[&](int l,int r,int v){
if(l > r) return;
if(l){
int le = s.get(l-1);
int ri = s.get(l);
if(le != ri){
dif.erase(l-1);
}
if(le < ri) up.erase(l-1);
if(le > ri) dw.erase(l-1);
if(le != ri+v){
dif.insert(l-1);
}
if(le < ri+v) up.insert(l-1);
if(le > ri+v) dw.insert(l-1);
}
if(r+1 < si(za)){
int le = s.get(r);
int ri = s.get(r+1);
if(le != ri){
dif.erase(r);
}
if(le < ri) up.erase(r);
if(le > ri) dw.erase(r);
if(le+v != ri){
dif.insert(r);
}
if(le+v < ri) up.insert(r);
if(le+v > ri) dw.insert(r);
}s.upd(l,r,v);
};
rng(i,0,si(za)){
if(lb <= za[i] and za[i] <= ub);
else {
//s.upd(i,i,1000000);
add(i,i,1000000);
}
}
for(auto id:ord){
//cout<<id<<endl;
int mn = lower_bound(za.begin(), za.end(), get<0>(vec[id])) - za.begin();
int mx = lower_bound(za.begin(), za.end(), get<1>(vec[id])) - za.begin();
int ay = get<2>(vec[id]);
int by = get<3>(vec[id]);
if(ay > by){
int cur = mn;
while(1){
auto it0 = up.lower_bound(cur);
if(it0 == up.end() or (*it0) >= mx) goto end;
cur = (*it0);
int dp = s.get(cur);
while(1){
auto it = dif.lower_bound(cur);
if(it == dif.end() or (*it) >= mx) goto end;
int nxtpos = *it;
int nxtdp = s.get(nxtpos+1);
if(dp >= nxtdp){
cur = nxtpos+1;
break;
}
else{
auto it2 = it; it2++;
int nxt2pos;
if(it2 == dif.end() or (*it2) >= mx) nxt2pos = mx;
else nxt2pos = *it2;
//s.upd(nxtpos+1, nxt2pos, dp-nxtdp);
add(nxtpos+1, nxt2pos, dp-nxtdp);
}
}
}
end:;
//s.upd(mn+1, mx-1, 1);
add(mn+1, mx-1, 1);
}
else{
int cur = mx;
while(1){
auto it0 = dw.lower_bound(cur);
if(it0 == dw.begin()) goto end2;
it0--;
if((*it0) < mn) goto end2;
cur = (*it0)+1;
int dp = s.get(cur);
while(1){
auto it = dif.lower_bound(cur);
if(it == dif.begin()) goto end2;
it --;
if((*it) < mn) goto end2;
int nxtpos = (*it);
int nxtdp = s.get(nxtpos);
if(dp >= nxtdp){
cur = nxtpos;
break;
}
else{
auto it2 = it;
int nxt2pos;
if(it2 == dif.begin()) nxt2pos = mn;
else{
it2--;
if((*it2)<mn) nxt2pos = mn;
else nxt2pos = (*it2)+1;
}
//s.upd(nxt2pos, nxtpos, dp-nxtdp);
add(nxt2pos, nxtpos, dp-nxtdp);
}
}
}
end2:;
//s.upd(mn+1, mx-1, 1);
add(mn+1, mx-1, 1);
}
}
lb = lower_bound(za.begin(), za.end(), lb)-za.begin();
ub = lower_bound(za.begin(), za.end(), ub)-za.begin();
int ans = 1000000000;
rng(i,lb,ub+1) chmin(ans,s.get(i));
cout<<ans<<endl;
}
signed main(){
cin.tie(0);
ios::sync_with_stdio(0);
cout<<fixed<<setprecision(20);
solve();
}
Details
Test #1:
score: 100
Accepted
time: 0ms
memory: 19184kb
Test #2:
score: 0
Accepted
time: 4ms
memory: 17764kb
Test #3:
score: 0
Accepted
time: 0ms
memory: 19380kb
Test #4:
score: 0
Accepted
time: 0ms
memory: 18464kb
Test #5:
score: 0
Accepted
time: 3ms
memory: 17840kb
Test #6:
score: 0
Accepted
time: 0ms
memory: 18360kb
Test #7:
score: 0
Accepted
time: 3ms
memory: 17608kb
Test #8:
score: 0
Accepted
time: 0ms
memory: 18216kb
Test #9:
score: 0
Accepted
time: 0ms
memory: 18536kb
Test #10:
score: 0
Accepted
time: 3ms
memory: 17752kb
Test #11:
score: 0
Accepted
time: 0ms
memory: 18028kb
Test #12:
score: 0
Accepted
time: 2672ms
memory: 210508kb
Test #13:
score: 0
Accepted
time: 0ms
memory: 17508kb
Test #14:
score: 0
Accepted
time: 3ms
memory: 18316kb
Test #15:
score: 0
Accepted
time: 0ms
memory: 18764kb
Test #16:
score: 0
Accepted
time: 0ms
memory: 17648kb
Test #17:
score: 0
Accepted
time: 0ms
memory: 18648kb
Test #18:
score: 0
Accepted
time: 3ms
memory: 18640kb
Test #19:
score: 0
Accepted
time: 0ms
memory: 19288kb
Test #20:
score: 0
Accepted
time: 4ms
memory: 17476kb
Test #21:
score: 0
Accepted
time: 3ms
memory: 17860kb
Test #22:
score: 0
Accepted
time: 3ms
memory: 17520kb
Test #23:
score: 0
Accepted
time: 0ms
memory: 18976kb
Test #24:
score: 0
Accepted
time: 0ms
memory: 17524kb
Test #25:
score: 0
Accepted
time: 2ms
memory: 18960kb
Test #26:
score: 0
Accepted
time: 2ms
memory: 18616kb
Test #27:
score: 0
Accepted
time: 0ms
memory: 18256kb
Test #28:
score: 0
Accepted
time: 2ms
memory: 17620kb
Test #29:
score: 0
Accepted
time: 2ms
memory: 18596kb
Test #30:
score: 0
Accepted
time: 0ms
memory: 19196kb
Test #31:
score: 0
Accepted
time: 0ms
memory: 17448kb
Test #32:
score: 0
Accepted
time: 0ms
memory: 18820kb
Test #33:
score: 0
Accepted
time: 3ms
memory: 18812kb
Test #34:
score: 0
Accepted
time: 0ms
memory: 17688kb
Test #35:
score: 0
Accepted
time: 3068ms
memory: 210468kb
Test #36:
score: 0
Accepted
time: 3300ms
memory: 206836kb
Test #37:
score: 0
Accepted
time: 3346ms
memory: 210764kb
Test #38:
score: 0
Accepted
time: 2722ms
memory: 210556kb
Test #39:
score: 0
Accepted
time: 2716ms
memory: 148480kb
Test #40:
score: 0
Accepted
time: 1179ms
memory: 106188kb
Test #41:
score: 0
Accepted
time: 4950ms
memory: 124408kb
Test #42:
score: 0
Accepted
time: 4885ms
memory: 124360kb
Test #43:
score: 0
Accepted
time: 0ms
memory: 18528kb
Test #44:
score: 0
Accepted
time: 0ms
memory: 19012kb
Test #45:
score: 0
Accepted
time: 3ms
memory: 17704kb
Test #46:
score: 0
Accepted
time: 4ms
memory: 17956kb
Test #47:
score: 0
Accepted
time: 3ms
memory: 19128kb
Test #48:
score: 0
Accepted
time: 3ms
memory: 18732kb
Test #49:
score: 0
Accepted
time: 3ms
memory: 18400kb
Test #50:
score: 0
Accepted
time: 4ms
memory: 18920kb
Test #51:
score: 0
Accepted
time: 0ms
memory: 18572kb
Test #52:
score: 0
Accepted
time: 3ms
memory: 18884kb
Test #53:
score: 0
Accepted
time: 0ms
memory: 18264kb
Test #54:
score: 0
Accepted
time: 2ms
memory: 18384kb
Test #55:
score: 0
Accepted
time: 4ms
memory: 18836kb
Test #56:
score: 0
Accepted
time: 0ms
memory: 19116kb
Test #57:
score: 0
Accepted
time: 4ms
memory: 18172kb
Test #58:
score: 0
Accepted
time: 0ms
memory: 18840kb
Test #59:
score: 0
Accepted
time: 0ms
memory: 18564kb
Test #60:
score: 0
Accepted
time: 4ms
memory: 19600kb
Test #61:
score: 0
Accepted
time: 4ms
memory: 19204kb
Test #62:
score: 0
Accepted
time: 0ms
memory: 18932kb
Test #63:
score: 0
Accepted
time: 4ms
memory: 18264kb
Test #64:
score: 0
Accepted
time: 4ms
memory: 17944kb
Test #65:
score: 0
Accepted
time: 4ms
memory: 17808kb
Test #66:
score: 0
Accepted
time: 4ms
memory: 19368kb
Test #67:
score: 0
Accepted
time: 4ms
memory: 17852kb
Test #68:
score: 0
Accepted
time: 46ms
memory: 20740kb
Test #69:
score: 0
Accepted
time: 52ms
memory: 19184kb
Test #70:
score: 0
Accepted
time: 41ms
memory: 20432kb
Test #71:
score: 0
Accepted
time: 48ms
memory: 20044kb
Test #72:
score: 0
Accepted
time: 704ms
memory: 39216kb
Test #73:
score: 0
Accepted
time: 701ms
memory: 38608kb
Test #74:
score: 0
Accepted
time: 678ms
memory: 38252kb
Test #75:
score: 0
Accepted
time: 674ms
memory: 39684kb
Test #76:
score: 0
Accepted
time: 681ms
memory: 38408kb
Test #77:
score: 0
Accepted
time: 4607ms
memory: 132956kb
Test #78:
score: 0
Accepted
time: 4503ms
memory: 120712kb
Test #79:
score: 0
Accepted
time: 4262ms
memory: 130716kb
Test #80:
score: 0
Accepted
time: 4138ms
memory: 122680kb
Test #81:
score: 0
Accepted
time: 4411ms
memory: 121332kb
Test #82:
score: 0
Accepted
time: 4338ms
memory: 123180kb
Test #83:
score: 0
Accepted
time: 3886ms
memory: 122824kb
Test #84:
score: 0
Accepted
time: 4321ms
memory: 141524kb
Test #85:
score: 0
Accepted
time: 4500ms
memory: 122404kb
Test #86:
score: 0
Accepted
time: 4448ms
memory: 120500kb
Test #87:
score: 0
Accepted
time: 2009ms
memory: 114976kb
Test #88:
score: 0
Accepted
time: 2144ms
memory: 115928kb
Test #89:
score: 0
Accepted
time: 2691ms
memory: 115092kb
Test #90:
score: 0
Accepted
time: 3120ms
memory: 118768kb
Test #91:
score: 0
Accepted
time: 4073ms
memory: 127848kb
Test #92:
score: 0
Accepted
time: 1718ms
memory: 114044kb
Test #93:
score: 0
Accepted
time: 1656ms
memory: 113580kb
Test #94:
score: 0
Accepted
time: 1692ms
memory: 113832kb
Test #95:
score: 0
Accepted
time: 1941ms
memory: 113576kb
Test #96:
score: 0
Accepted
time: 1510ms
memory: 113944kb