Submission #70523

# Submission time Handle Problem Language Result Execution time Memory
70523 2018-08-23T05:06:47 Z funcsr Land of the Rainbow Gold (APIO17_rainbow) C++17
35 / 100
3000 ms 63360 KB
#include "rainbow.h"
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <bitset>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007
const int DX[4] = {-1,0,1,0}, DY[4]={0,-1,0,1};

struct UF {
  vector<int> U, R;
  int C;
  UF(int N) {
    rep(i, N) U.pb(i), R.pb(1);
    C = N;
  }
  int find(int x) {
    if (U[x]==x)return x;
    return U[x]=find(U[x]);
  }
  void unite(int x, int y) {
    x=find(x), y=find(y);
    if (x==y)return;
    if (R[x]<R[y])swap(x,y);
    U[y]=x;
    R[x]+=R[y];
    C--;
  }
};
int H, W;
bool bad[50][50];
int id[50][50];
vector<int> both[4], both2[4];
bool yo[4][200000];
int SX, SY;
string T;

void init(int HH, int WW, int sr, int sc, int M, char *S) {
  SX=sc-1,SY=sr-1;T=string(S, S+M);
  H = HH, W = WW;
  if (H == 2) {
    map<P, bool> bad;
    int y = sr-1, x = sc-1;
    bad[P(x, y)] = true;
    rep(i, M) {
      if (S[i] == 'N') y--;
      if (S[i] == 'S') y++;
      if (S[i] == 'W') x--;
      if (S[i] == 'E') x++;
      bad[P(x, y)] = true;
    }
    rep(S, 1<<H) {
      rep(x, W) {
        bool all = true;
        rep(y, H) if ((S>>y)&1 && !bad[P(x,y)]) all = false;
        yo[S][x] = all;
      }
      rep(x, W) if (yo[S][x]) both[S].pb(x);
      rep(x, W-1) if (yo[S][x]&&yo[S][x+1]) both2[S].pb(x);
    }
  }
  else if (W <= 50 && H <= 50) {
    int y = sr-1, x = sc-1;
    bad[x][y] = true;
    rep(i, M) {
      if (S[i] == 'N') y--;
      if (S[i] == 'S') y++;
      if (S[i] == 'W') x--;
      if (S[i] == 'E') x++;
      bad[x][y] = true;
    }
  }
}

int colour(int ar, int ac, int br, int bc) {
  ar--, ac--, br--, bc--;
  if(false){
  //if (H == 2) {
    int S = 0;
    for (int y=ar; y<=br; y++) S |= 1<<y;
    int l = ac, r = bc;
    int s = index(both[S], r+1)-index(both[S], l);
    if (s==0)return 1;
    s -= index(both2[S], r)-index(both2[S], l);
    s++;
    if (yo[S][l]) s--;
    if (yo[S][r]) s--;
    return s;
  }
  else if (false) {
  //else if (W <= 50 && H <= 50) {
    int V = 0;
    for (int x=ac; x<=bc; x++) {
      for (int y=ar; y<=br; y++) {
        if (!bad[x][y]) id[x][y] = V++;
        else id[x][y] = -1;
      }
    }
    UF uf(V);
    for (int x=ac; x<=bc; x++) {
      for (int y=ar; y<=br; y++) {
        if (x+1<=bc && !bad[x][y] && !bad[x+1][y]) uf.unite(id[x][y], id[x+1][y]);
        if (y+1<=br && !bad[x][y] && !bad[x][y+1]) uf.unite(id[x][y], id[x][y+1]);
      }
    }
    return uf.C;
  }
  else {
    br++,bc++;
    //cout<<"Q=1\n";
    // Q=1
    map<P, bool> blocks;
    int x = SX, y = SY;
    blocks[P(x,y)] = true;
    vector<P> inner;
    vector<P> possible_tate, possible_yoko;
    if (ac<=x&&x<=bc&&ar<=y&&y<=br-1) possible_tate.pb(P(x, y));
    if (ac<=x+1&&x+1<=bc&&ar<=y&&y<=br-1) possible_tate.pb(P(x+1, y));
    if (ac<=x&&x<=bc-1&&ar<=y&&y<=br) possible_yoko.pb(P(x, y));
    if (ac<=x&&x<=bc-1&&ar<=y+1&&y+1<=br) possible_yoko.pb(P(x, y+1));
    if (ac<=x&&x<=bc-1&&ar<=y&&y<=br-1) inner.pb(P(x, y));
    for (char c:T) {
      if (c == 'N') y--;
      if (c == 'S') y++;
      if (c == 'W') x--;
      if (c == 'E') x++;
      if (ac<=x&&x<=bc&&ar<=y&&y<=br-1) possible_tate.pb(P(x, y));
      if (ac<=x+1&&x+1<=bc&&ar<=y&&y<=br-1) possible_tate.pb(P(x+1, y));
      if (ac<=x&&x<=bc-1&&ar<=y&&y<=br) possible_yoko.pb(P(x, y));
      if (ac<=x&&x<=bc-1&&ar<=y+1&&y+1<=br) possible_yoko.pb(P(x, y+1));
      if (ac<=x&&x<=bc-1&&ar<=y&&y<=br-1) inner.pb(P(x, y));
      blocks[P(x,y)] = true;
    }
    vector<P> tate, yoko;
    for (P p : possible_tate) if (!blocks[P(p._1-1,p._2)]||!blocks[P(p._1,p._2)]) tate.pb(p);
    for (P p : possible_yoko) if (!blocks[P(p._1,p._2-1)]||!blocks[P(p._1,p._2)]) yoko.pb(p);
    for (int y=ar; y<=br-1; y++) tate.pb(P(ac, y)), tate.pb(P(bc, y));
    for (int x=ac; x<=bc-1; x++) yoko.pb(P(x, ar)), yoko.pb(P(x, br));
    sort(all(tate)); uniq(tate);
    sort(all(yoko)); uniq(yoko);
    vector<P> pts;
    for (P p : tate) pts.pb(p), pts.pb(P(p._1, p._2+1));
    for (P p : yoko) pts.pb(p), pts.pb(P(p._1+1, p._2));
    sort(all(pts)); uniq(pts);
    UF uf(pts.size());
    for (P p : tate) {
      int u = index(pts, p), v = index(pts, P(p._1, p._2+1));
      uf.unite(u, v);
    }
    for (P p : yoko) {
      int u = index(pts, p), v = index(pts, P(p._1+1, p._2));
      uf.unite(u, v);
    }
    sort(all(inner)); uniq(inner);
    UF uf2(inner.size());
    //cout<<"inner={";for(P x:inner)cout<<"("<<x._1<<","<<x._2<<"),";cout<<"}\n";
    rep(i, inner.size()) {
      P p = inner[i];
      rep(k, 4) {
        int x = p._1+DX[k], y=p._2+DY[k];
        if (!binary_search(all(inner), P(x,y))) continue;
        int t = index(inner, P(x,y));
        uf2.unite(i,t);
      }
    }
    int e = tate.size()+yoko.size();
    int v = pts.size();
    int c = uf.C;
    //cout<<"e="<<e<<", v="<<v<<", c="<<c<<" -= "<<uf2.C<<"\n";
    return e-v+c-uf2.C;
  }
}

Compilation message

rainbow.cpp: In function 'int colour(int, int, int, int)':
rainbow.cpp:18:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define rep(i, n) for (int i=0; i<(n); i++)
                                  ^
rainbow.cpp:176:5: note: in expansion of macro 'rep'
     rep(i, inner.size()) {
     ^~~
# Verdict Execution time Memory Grader output
1 Correct 97 ms 376 KB Output is correct
2 Correct 446 ms 680 KB Output is correct
3 Correct 106 ms 680 KB Output is correct
4 Correct 167 ms 680 KB Output is correct
5 Correct 651 ms 940 KB Output is correct
6 Correct 2 ms 940 KB Output is correct
7 Correct 3 ms 940 KB Output is correct
8 Correct 3 ms 940 KB Output is correct
9 Correct 3 ms 940 KB Output is correct
10 Correct 3 ms 940 KB Output is correct
11 Correct 396 ms 940 KB Output is correct
12 Correct 766 ms 976 KB Output is correct
13 Correct 857 ms 1016 KB Output is correct
14 Correct 1747 ms 1196 KB Output is correct
15 Correct 2 ms 1196 KB Output is correct
16 Correct 2 ms 1196 KB Output is correct
17 Correct 2 ms 1196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1196 KB Output is correct
2 Correct 2 ms 1196 KB Output is correct
3 Execution timed out 3011 ms 38976 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1196 KB Output is correct
2 Correct 324 ms 38976 KB Output is correct
3 Correct 310 ms 38976 KB Output is correct
4 Correct 463 ms 38976 KB Output is correct
5 Correct 209 ms 38976 KB Output is correct
6 Correct 231 ms 38976 KB Output is correct
7 Correct 444 ms 38976 KB Output is correct
8 Correct 79 ms 38976 KB Output is correct
9 Correct 49 ms 38976 KB Output is correct
10 Correct 32 ms 38976 KB Output is correct
11 Correct 46 ms 38976 KB Output is correct
12 Correct 72 ms 38976 KB Output is correct
13 Correct 69 ms 38976 KB Output is correct
14 Correct 68 ms 38976 KB Output is correct
15 Correct 62 ms 38976 KB Output is correct
16 Correct 179 ms 38976 KB Output is correct
17 Correct 168 ms 38976 KB Output is correct
18 Correct 239 ms 38976 KB Output is correct
19 Correct 324 ms 38976 KB Output is correct
20 Correct 415 ms 42040 KB Output is correct
21 Correct 280 ms 42040 KB Output is correct
22 Correct 254 ms 42040 KB Output is correct
23 Correct 157 ms 42040 KB Output is correct
24 Correct 288 ms 42040 KB Output is correct
25 Correct 637 ms 61792 KB Output is correct
26 Correct 748 ms 62672 KB Output is correct
27 Correct 651 ms 62672 KB Output is correct
28 Correct 515 ms 62672 KB Output is correct
29 Correct 409 ms 62672 KB Output is correct
30 Correct 437 ms 62672 KB Output is correct
31 Correct 513 ms 62672 KB Output is correct
32 Correct 619 ms 63244 KB Output is correct
33 Correct 662 ms 63360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 97 ms 376 KB Output is correct
2 Correct 446 ms 680 KB Output is correct
3 Correct 106 ms 680 KB Output is correct
4 Correct 167 ms 680 KB Output is correct
5 Correct 651 ms 940 KB Output is correct
6 Correct 2 ms 940 KB Output is correct
7 Correct 3 ms 940 KB Output is correct
8 Correct 3 ms 940 KB Output is correct
9 Correct 3 ms 940 KB Output is correct
10 Correct 3 ms 940 KB Output is correct
11 Correct 396 ms 940 KB Output is correct
12 Correct 766 ms 976 KB Output is correct
13 Correct 857 ms 1016 KB Output is correct
14 Correct 1747 ms 1196 KB Output is correct
15 Correct 2 ms 1196 KB Output is correct
16 Correct 2 ms 1196 KB Output is correct
17 Correct 2 ms 1196 KB Output is correct
18 Execution timed out 3027 ms 63360 KB Time limit exceeded
19 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 97 ms 376 KB Output is correct
2 Correct 446 ms 680 KB Output is correct
3 Correct 106 ms 680 KB Output is correct
4 Correct 167 ms 680 KB Output is correct
5 Correct 651 ms 940 KB Output is correct
6 Correct 2 ms 940 KB Output is correct
7 Correct 3 ms 940 KB Output is correct
8 Correct 3 ms 940 KB Output is correct
9 Correct 3 ms 940 KB Output is correct
10 Correct 3 ms 940 KB Output is correct
11 Correct 396 ms 940 KB Output is correct
12 Correct 766 ms 976 KB Output is correct
13 Correct 857 ms 1016 KB Output is correct
14 Correct 1747 ms 1196 KB Output is correct
15 Correct 2 ms 1196 KB Output is correct
16 Correct 2 ms 1196 KB Output is correct
17 Correct 2 ms 1196 KB Output is correct
18 Execution timed out 3027 ms 63360 KB Time limit exceeded
19 Halted 0 ms 0 KB -