답안 #396582

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
396582 2021-04-30T10:30:38 Z MarcoMeijer Flood (IOI07_flood) C++14
100 / 100
343 ms 29348 KB
#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 = 3e5;
const int N = (1<<17);

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;
vi oneWaterSide;
vi answer;
vii futureWaterSide;

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(v) {
        FOR(u,wallAdj[i])
            setWaterSide(u,0);
        wallAdj[i].clear();
    }
    int cnt = waterSide[i][0] + waterSide[i][1];
    if(!used[i]) {
        if(cnt == 1) oneWaterSide.pb(i);
        if(cnt == 2) {
            answer.pb(i);
            used[i] = 1;
        }
    }
    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] = min(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 1e9;
    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 min(a,b);
}
void fillWallAdjVert() {
    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
            futureWaterSide.pb({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,wall);
    }
}
void fillWallAdjHor() {
    RE(i,N*2) SEG[i] = LAZ[i] = -1;

    vii horizontalWalls;
    RE(i,w) if(!isVertical[i]) horizontalWalls.pb({y[a[i]], i});
    sort(all(horizontalWalls));

    FOR(p,horizontalWalls) {
        int wall = p.se;
        int x1 = x[a[wall]];
        int x2 = x[b[wall]] - 1;
        int prev = getSeg(x1,x2);
        if(prev == -1) {
            // this wall is connected to the outside
            futureWaterSide.pb({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(x1,x2,wall);
    }
}

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();
    difX.clear();
    difY.clear();

    // 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;
        }
    }

    fillWallAdjVert();
    fillWallAdjHor();

    // initial state
    FOR(p,futureWaterSide) setWaterSide(p.fi,p.se);
    futureWaterSide.clear();

    while(!oneWaterSide.empty()) {
        vi next;
        FOR(i,oneWaterSide) {
            if(!used[i]) next.pb(i);
            used[i] = 1;
        }
        oneWaterSide.clear();
        FOR(i,next) {
            setWaterSide(i,0);
            setWaterSide(i,1);
        }
    }

    OUTL(answer.size());
    FOR(i,answer) OUTL(i+1);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9420 KB Output is correct
2 Correct 6 ms 9420 KB Output is correct
3 Correct 5 ms 9420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9420 KB Output is correct
2 Correct 7 ms 9420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9420 KB Output is correct
2 Correct 6 ms 9420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9420 KB Output is correct
2 Correct 9 ms 9524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9420 KB Output is correct
2 Correct 9 ms 9420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 11172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 169 ms 18192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 171 ms 16860 KB Output is correct
2 Correct 324 ms 20044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 271 ms 18552 KB Output is correct
2 Correct 343 ms 19080 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 321 ms 20092 KB Output is correct
2 Correct 236 ms 29348 KB Output is correct