Submission #396562

#TimeUsernameProblemLanguageResultExecution timeMemory
396562MarcoMeijerFlood (IOI07_flood)C++14
36 / 100
304 ms40104 KiB
#include <bits/stdc++.h> using namespace std; // macros typedef long long ll; typedef long double ld; typedef pair<int, int> ii; typedef pair<ll, ll> lll; typedef tuple<int, int, int> iii; typedef vector<int> vi; typedef vector<ii> vii; typedef vector<iii> viii; typedef vector<ll> vll; typedef vector<lll> vlll; #define REP(a,b,c) for(int a=int(b); a<int(c); a++) #define RE(a,c) REP(a,0,c) #define RE1(a,c) REP(a,1,c+1) #define REI(a,b,c) REP(a,b,c+1) #define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--) #define FOR(a,b) for(auto& a : b) #define all(a) a.begin(), a.end() #define INF 1e18 #define EPS 1e-9 #define pb push_back #define popb pop_back #define fi first #define se second mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); // input template<class T> void IN(T& x) {cin >> x;} template<class H, class... T> void IN(H& h, T&... t) {IN(h); IN(t...); } // output template<class T1, class T2> void OUT(const pair<T1,T2>& x); template<class T> void OUT(const vector<T>& x); template<class T> void OUT(const T& x) {cout << x;} template<class H, class... T> void OUT(const H& h, const T&... t) {OUT(h); OUT(t...); } template<class T1, class T2> void OUT(const pair<T1,T2>& x) {OUT(x.fi,' ',x.se);} template<class T> void OUT(const vector<T>& x) {RE(i,x.size()) OUT(i==0?"":" ",x[i]);} template<class... T> void OUTL(const T&... t) {OUT(t..., "\n"); } template<class H> void OUTLS(const H& h) {OUTL(h); } template<class H, class... T> void OUTLS(const H& h, const T&... t) {OUT(h,' '); OUTLS(t...); } // dp template<class T> bool ckmin(T&a, T&b) { bool bl = a > b; a = min(a,b); return bl;} template<class T> bool ckmax(T&a, T&b) { bool bl = a < b; a = max(a,b); return bl;} void program(); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); program(); } //===================// // begin program // //===================// const int MX = 5e5; const int N = (1<<20); int n, w; int x[MX], y[MX], a[MX], b[MX]; bitset<MX> isVertical; int wallDir[MX][4]; // 0=up 1=right 2=down 3=left int cwDir[4] = {1,1,0,0}; int ccwDir[4] = {0,0,1,1}; int waterSide[MX][2]; vi wallAdj[MX]; bitset<MX> used; set<int> oneWaterSide, bothWaterSide; vi answer; void setWaterSide(int x, int v); void goClockWise(int point, int dir) { while(true) { dir++; if(dir == 4) dir = 0; if(wallDir[point][dir] != -1) { setWaterSide(wallDir[point][dir], ccwDir[dir]); return; } } } void goCounterClockWise(int point, int dir) { while(true) { dir--; if(dir == -1) dir = 3; if(wallDir[point][dir] != -1) { setWaterSide(wallDir[point][dir], cwDir[dir]); return; } } } void setWaterSide(int i, int v) { if(waterSide[i][v]) return; waterSide[i][v] = 1; if(isVertical[i] && v) FOR(u,wallAdj[i]) setWaterSide(u,0); int cnt = waterSide[i][0] + waterSide[i][1]; if(!used[i]) { if(cnt == 1) oneWaterSide.insert(i); if(cnt == 2) oneWaterSide.erase(i), bothWaterSide.insert(i); } if(isVertical[i]) { if(v) { goClockWise (b[i],0); goCounterClockWise(a[i],2); } else { goClockWise (a[i],2); goCounterClockWise(b[i],0); } } else { if(v) { goClockWise (a[i],1); goCounterClockWise(b[i],3); } else { goClockWise (b[i],3); goCounterClockWise(a[i],1); } } } // wall adj int SEG[N*2], LAZ[N*2]; void setSeg(int i, int j, int v, int lazy=-1, int l=0, int r=N-1, int p=1) { SEG[p] = lazy == -1 ? SEG[p] : lazy; LAZ[p] = lazy == -1 ? LAZ[p] : lazy; if(j < l || i > r) return; if(i <= l && j >= r) { SEG[p] = v; LAZ[p] = v; return; } int m=(l+r)/2; setSeg(i,j,v,LAZ[p],l ,m,p*2 ); setSeg(i,j,v,LAZ[p],m+1,r,p*2+1); LAZ[p] = -1; SEG[p] = max(SEG[p*2], SEG[p*2+1]); } int getSeg(int i, int j, int lazy=-1, int l=0, int r=N-1, int p=1) { SEG[p] = lazy == -1 ? SEG[p] : lazy; LAZ[p] = lazy == -1 ? LAZ[p] : lazy; if(j < l || i > r) return -1; if(i <= l && j >= r) return SEG[p]; int m=(l+r)/2; int a=getSeg(i,j,LAZ[p],l ,m,p*2 ); int b=getSeg(i,j,LAZ[p],m+1,r,p*2+1); LAZ[p] = -1; return max(a,b); } void fillWallAdj() { RE(i,N*2) SEG[i] = LAZ[i] = -1; vii verticalWalls; RE(i,w) if(isVertical[i]) verticalWalls.pb({x[a[i]], i}); sort(all(verticalWalls)); FOR(p,verticalWalls) { int wall = p.se; int y1 = y[a[wall]]; int y2 = y[b[wall]] - 1; int prev = getSeg(y1,y2); if(prev == -1) { // this wall is connected to the outside setWaterSide(wall,0); } else { // if the right side of the prev wall is filled with water, this wall will also be filled with water wallAdj[prev].pb(wall); } setSeg(y1,y2,p.se); } } void program() { IN(n); RE(i,n) IN(x[i],y[i]); IN(w); RE(i,w) IN(a[i],b[i]), a[i]--, b[i]--; // coördinate compression vi difX, difY; RE(i,n) difX.pb(x[i]); RE(i,n) difY.pb(y[i]); sort(all(difX)); sort(all(difY)); RE(i,n) x[i] = lower_bound(all(difX), x[i]) - difX.begin(); RE(i,n) y[i] = lower_bound(all(difY), y[i]) - difY.begin(); // make the walls only go in increasing x or y RE(i,w) if(x[a[i]] > x[b[i]] || y[a[i]] > y[b[i]]) swap(a[i], b[i]); // fill points direction // 0=up 1=right 2=down 3=left RE(i,w) if(x[a[i]] == x[b[i]]) isVertical[i] = 1; RE(i,n) RE(j,4) wallDir[i][j] = -1; RE(i,w) { if(isVertical[i]) { wallDir[a[i]][2] = i; wallDir[b[i]][0] = i; } else { wallDir[a[i]][1] = i; wallDir[b[i]][3] = i; } } fillWallAdj(); while(!oneWaterSide.empty() || !bothWaterSide.empty()) { FOR(i,bothWaterSide) used[i] = 1; FOR(i,oneWaterSide) used[i] = 1; FOR(i,bothWaterSide) answer.pb(i); bothWaterSide.clear(); set<int> Mem = oneWaterSide; oneWaterSide.clear(); FOR(i,Mem) { setWaterSide(i,0); setWaterSide(i,1); } } OUTL(answer.size()); FOR(i,answer) OUTL(i+1); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...