답안 #396576

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
396576 2021-04-30T10:25:56 Z MarcoMeijer Flood (IOI07_flood) C++14
91 / 100
447 ms 43044 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][2];
bitset<MX> used;
set<int> oneWaterSide, bothWaterSide;
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][isVertical[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] = 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][1].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][0].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();

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

    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; swap(Mem, oneWaterSide);
        FOR(i,Mem) {
            setWaterSide(i,0);
            setWaterSide(i,1);
        }
    }

    OUTL(answer.size());
    FOR(i,answer) OUTL(i+1);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 16460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 16436 KB Output is correct
2 Correct 12 ms 16460 KB Output is correct
3 Correct 11 ms 16460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 16460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 16460 KB Output is correct
2 Correct 12 ms 16492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 16460 KB Output is correct
2 Correct 11 ms 16480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 16460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 16460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 16460 KB Output is correct
2 Correct 12 ms 16512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 16552 KB Output is correct
2 Correct 12 ms 16460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 18732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 192 ms 27400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 198 ms 23884 KB Output is correct
2 Correct 423 ms 28752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 404 ms 27256 KB Output is correct
2 Correct 361 ms 29180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 447 ms 27924 KB Output is correct
2 Runtime error 316 ms 43044 KB Memory limit exceeded