QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#211025 | #2342. Directing Rainfall | HIR180 | TL | 4885ms | 210840kb | C++20 | 6.3kb | 2023-10-12 00:03:55 | 2023-10-12 00:03:55 |
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){
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: 4ms
memory: 17300kb
Test #2:
score: 0
Accepted
time: 4ms
memory: 18868kb
Test #3:
score: 0
Accepted
time: 0ms
memory: 18832kb
Test #4:
score: 0
Accepted
time: 3ms
memory: 17936kb
Test #5:
score: 0
Accepted
time: 2ms
memory: 19368kb
Test #6:
score: 0
Accepted
time: 0ms
memory: 19392kb
Test #7:
score: 0
Accepted
time: 0ms
memory: 19376kb
Test #8:
score: 0
Accepted
time: 2ms
memory: 18376kb
Test #9:
score: 0
Accepted
time: 0ms
memory: 18812kb
Test #10:
score: 0
Accepted
time: 2ms
memory: 19184kb
Test #11:
score: 0
Accepted
time: 0ms
memory: 19276kb
Test #12:
score: 0
Accepted
time: 2655ms
memory: 210748kb
Test #13:
score: 0
Accepted
time: 0ms
memory: 17644kb
Test #14:
score: 0
Accepted
time: 0ms
memory: 19032kb
Test #15:
score: 0
Accepted
time: 3ms
memory: 18732kb
Test #16:
score: 0
Accepted
time: 3ms
memory: 18288kb
Test #17:
score: 0
Accepted
time: 2ms
memory: 17616kb
Test #18:
score: 0
Accepted
time: 2ms
memory: 17660kb
Test #19:
score: 0
Accepted
time: 0ms
memory: 17688kb
Test #20:
score: 0
Accepted
time: 0ms
memory: 17460kb
Test #21:
score: 0
Accepted
time: 2ms
memory: 18284kb
Test #22:
score: 0
Accepted
time: 0ms
memory: 18524kb
Test #23:
score: 0
Accepted
time: 3ms
memory: 17644kb
Test #24:
score: 0
Accepted
time: 0ms
memory: 18460kb
Test #25:
score: 0
Accepted
time: 3ms
memory: 18888kb
Test #26:
score: 0
Accepted
time: 0ms
memory: 18152kb
Test #27:
score: 0
Accepted
time: 3ms
memory: 17484kb
Test #28:
score: 0
Accepted
time: 0ms
memory: 18424kb
Test #29:
score: 0
Accepted
time: 2ms
memory: 18108kb
Test #30:
score: 0
Accepted
time: 2ms
memory: 17916kb
Test #31:
score: 0
Accepted
time: 0ms
memory: 18840kb
Test #32:
score: 0
Accepted
time: 3ms
memory: 19280kb
Test #33:
score: 0
Accepted
time: 0ms
memory: 18568kb
Test #34:
score: 0
Accepted
time: 0ms
memory: 18304kb
Test #35:
score: 0
Accepted
time: 3068ms
memory: 210840kb
Test #36:
score: 0
Accepted
time: 3279ms
memory: 206812kb
Test #37:
score: 0
Accepted
time: 3367ms
memory: 210512kb
Test #38:
score: 0
Accepted
time: 2631ms
memory: 210800kb
Test #39:
score: 0
Accepted
time: 2718ms
memory: 148180kb
Test #40:
score: 0
Accepted
time: 1673ms
memory: 173404kb
Test #41:
score: 0
Accepted
time: 4859ms
memory: 124940kb
Test #42:
score: 0
Accepted
time: 4885ms
memory: 124456kb
Test #43:
score: 0
Accepted
time: 4ms
memory: 18216kb
Test #44:
score: 0
Accepted
time: 3ms
memory: 17944kb
Test #45:
score: 0
Accepted
time: 0ms
memory: 18152kb
Test #46:
score: 0
Accepted
time: 0ms
memory: 18336kb
Test #47:
score: 0
Accepted
time: 4ms
memory: 19296kb
Test #48:
score: 0
Accepted
time: 0ms
memory: 17380kb
Test #49:
score: 0
Accepted
time: 3ms
memory: 17808kb
Test #50:
score: 0
Accepted
time: 2ms
memory: 17572kb
Test #51:
score: 0
Accepted
time: 0ms
memory: 18640kb
Test #52:
score: 0
Accepted
time: 3ms
memory: 17904kb
Test #53:
score: 0
Accepted
time: 0ms
memory: 18840kb
Test #54:
score: 0
Accepted
time: 0ms
memory: 18068kb
Test #55:
score: 0
Accepted
time: 3ms
memory: 19424kb
Test #56:
score: 0
Accepted
time: 0ms
memory: 18544kb
Test #57:
score: 0
Accepted
time: 3ms
memory: 17676kb
Test #58:
score: 0
Accepted
time: 0ms
memory: 19176kb
Test #59:
score: 0
Accepted
time: 3ms
memory: 17472kb
Test #60:
score: 0
Accepted
time: 0ms
memory: 18576kb
Test #61:
score: 0
Accepted
time: 4ms
memory: 17720kb
Test #62:
score: 0
Accepted
time: 0ms
memory: 17556kb
Test #63:
score: 0
Accepted
time: 3ms
memory: 18232kb
Test #64:
score: 0
Accepted
time: 7ms
memory: 17992kb
Test #65:
score: 0
Accepted
time: 3ms
memory: 17988kb
Test #66:
score: 0
Accepted
time: 4ms
memory: 18632kb
Test #67:
score: 0
Accepted
time: 4ms
memory: 18564kb
Test #68:
score: 0
Accepted
time: 46ms
memory: 19864kb
Test #69:
score: 0
Accepted
time: 51ms
memory: 19848kb
Test #70:
score: 0
Accepted
time: 51ms
memory: 19960kb
Test #71:
score: 0
Accepted
time: 48ms
memory: 20900kb
Test #72:
score: 0
Accepted
time: 694ms
memory: 37944kb
Test #73:
score: 0
Accepted
time: 699ms
memory: 39620kb
Test #74:
score: 0
Accepted
time: 659ms
memory: 38928kb
Test #75:
score: 0
Accepted
time: 671ms
memory: 39672kb
Test #76:
score: 0
Accepted
time: 689ms
memory: 38096kb
Test #77:
score: 0
Accepted
time: 4487ms
memory: 133016kb
Test #78:
score: 0
Accepted
time: 4399ms
memory: 120420kb
Test #79:
score: 0
Accepted
time: 4280ms
memory: 130692kb
Test #80:
score: 0
Accepted
time: 4334ms
memory: 122972kb
Test #81:
score: 0
Accepted
time: 4408ms
memory: 121288kb
Test #82:
score: 0
Accepted
time: 4343ms
memory: 123300kb
Test #83:
score: 0
Accepted
time: 3927ms
memory: 123012kb
Test #84:
score: 0
Accepted
time: 4337ms
memory: 141328kb
Test #85:
score: 0
Accepted
time: 4502ms
memory: 122384kb
Test #86:
score: 0
Accepted
time: 4413ms
memory: 120548kb
Test #87:
score: 0
Accepted
time: 2973ms
memory: 141856kb
Test #88:
score: 0
Accepted
time: 3086ms
memory: 137264kb
Test #89:
score: 0
Accepted
time: 4041ms
memory: 130192kb
Test #90:
score: -100
Time Limit Exceeded