QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#210998 | #2342. Directing Rainfall | HIR180 | RE | 4826ms | 210960kb | C++14 | 6.2kb | 2023-10-11 23:09:11 | 2023-10-11 23:09:12 |
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 += 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());
if(si(za) != 2*n+2) return;
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 {
add(i,i,1000000);
}
}
int loop=0,loop1=0;
for(auto id:ord){
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;
add(nxtpos+1, nxt2pos, dp-nxtdp);
loop++;
}
}
}
end:;
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;
}
add(nxt2pos, nxtpos, dp-nxtdp);
loop1++;
}
}
}
end2:;
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<<'\n';
}
signed main(){
cin.tie(0);
ios::sync_with_stdio(0);
cout<<fixed<<setprecision(20);
solve();
}
Details
Test #1:
score: 100
Accepted
time: 0ms
memory: 17624kb
Test #2:
score: 0
Accepted
time: 2ms
memory: 19052kb
Test #3:
score: 0
Accepted
time: 3ms
memory: 19016kb
Test #4:
score: 0
Accepted
time: 0ms
memory: 17644kb
Test #5:
score: 0
Accepted
time: 3ms
memory: 17728kb
Test #6:
score: 0
Accepted
time: 4ms
memory: 19512kb
Test #7:
score: 0
Accepted
time: 3ms
memory: 18548kb
Test #8:
score: 0
Accepted
time: 0ms
memory: 17856kb
Test #9:
score: 0
Accepted
time: 0ms
memory: 18380kb
Test #10:
score: 0
Accepted
time: 3ms
memory: 17644kb
Test #11:
score: 0
Accepted
time: 0ms
memory: 19080kb
Test #12:
score: 0
Accepted
time: 2597ms
memory: 210500kb
Test #13:
score: 0
Accepted
time: 0ms
memory: 18896kb
Test #14:
score: 0
Accepted
time: 0ms
memory: 17736kb
Test #15:
score: 0
Accepted
time: 0ms
memory: 18556kb
Test #16:
score: 0
Accepted
time: 3ms
memory: 17720kb
Test #17:
score: 0
Accepted
time: 0ms
memory: 18644kb
Test #18:
score: 0
Accepted
time: 3ms
memory: 18604kb
Test #19:
score: 0
Accepted
time: 0ms
memory: 18552kb
Test #20:
score: 0
Accepted
time: 2ms
memory: 18304kb
Test #21:
score: 0
Accepted
time: 3ms
memory: 17588kb
Test #22:
score: 0
Accepted
time: 2ms
memory: 18604kb
Test #23:
score: 0
Accepted
time: 0ms
memory: 19264kb
Test #24:
score: 0
Accepted
time: 2ms
memory: 18724kb
Test #25:
score: 0
Accepted
time: 0ms
memory: 18176kb
Test #26:
score: 0
Accepted
time: 0ms
memory: 19344kb
Test #27:
score: 0
Accepted
time: 0ms
memory: 18280kb
Test #28:
score: 0
Accepted
time: 4ms
memory: 19212kb
Test #29:
score: 0
Accepted
time: 0ms
memory: 17832kb
Test #30:
score: 0
Accepted
time: 0ms
memory: 19328kb
Test #31:
score: 0
Accepted
time: 4ms
memory: 19168kb
Test #32:
score: 0
Accepted
time: 0ms
memory: 18492kb
Test #33:
score: 0
Accepted
time: 3ms
memory: 18448kb
Test #34:
score: 0
Accepted
time: 0ms
memory: 19008kb
Test #35:
score: 0
Accepted
time: 3099ms
memory: 210832kb
Test #36:
score: 0
Accepted
time: 3234ms
memory: 206684kb
Test #37:
score: 0
Accepted
time: 3307ms
memory: 210572kb
Test #38:
score: 0
Accepted
time: 2548ms
memory: 210960kb
Test #39:
score: 0
Accepted
time: 2674ms
memory: 148188kb
Test #40:
score: 0
Accepted
time: 1676ms
memory: 173960kb
Test #41:
score: 0
Accepted
time: 4767ms
memory: 124556kb
Test #42:
score: 0
Accepted
time: 4826ms
memory: 124556kb
Test #43:
score: 0
Accepted
time: 0ms
memory: 17792kb
Test #44:
score: 0
Accepted
time: 3ms
memory: 18136kb
Test #45:
score: 0
Accepted
time: 3ms
memory: 19244kb
Test #46:
score: 0
Accepted
time: 3ms
memory: 17408kb
Test #47:
score: 0
Accepted
time: 0ms
memory: 18464kb
Test #48:
score: 0
Accepted
time: 0ms
memory: 18056kb
Test #49:
score: 0
Accepted
time: 0ms
memory: 17924kb
Test #50:
score: 0
Accepted
time: 2ms
memory: 18180kb
Test #51:
score: 0
Accepted
time: 0ms
memory: 18916kb
Test #52:
score: 0
Accepted
time: 3ms
memory: 19584kb
Test #53:
score: 0
Accepted
time: 3ms
memory: 18836kb
Test #54:
score: -100
Runtime Error