Submission #70526

# Submission time Handle Problem Language Result Execution time Memory
70526 2018-08-23T05:10:24 Z funcsr Land of the Rainbow Gold (APIO17_rainbow) C++17
47 / 100
3000 ms 61096 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;
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);
    }
  }
}

int colour(int ar, int ac, int br, int bc) {
  ar--, ac--, br--, bc--;
  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 {
    // O((N+M)Q)
    br++,bc++;
    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:143:5: note: in expansion of macro 'rep'
     rep(i, inner.size()) {
     ^~~
# Verdict Execution time Memory Grader output
1 Correct 106 ms 376 KB Output is correct
2 Correct 434 ms 684 KB Output is correct
3 Correct 128 ms 832 KB Output is correct
4 Correct 184 ms 832 KB Output is correct
5 Correct 635 ms 980 KB Output is correct
6 Correct 2 ms 980 KB Output is correct
7 Correct 2 ms 980 KB Output is correct
8 Correct 4 ms 980 KB Output is correct
9 Correct 2 ms 980 KB Output is correct
10 Correct 2 ms 980 KB Output is correct
11 Correct 393 ms 980 KB Output is correct
12 Correct 680 ms 980 KB Output is correct
13 Correct 917 ms 1200 KB Output is correct
14 Correct 1858 ms 1372 KB Output is correct
15 Correct 2 ms 1372 KB Output is correct
16 Correct 2 ms 1372 KB Output is correct
17 Correct 3 ms 1372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1372 KB Output is correct
2 Correct 3 ms 1372 KB Output is correct
3 Correct 365 ms 29584 KB Output is correct
4 Correct 401 ms 29928 KB Output is correct
5 Correct 387 ms 30016 KB Output is correct
6 Correct 341 ms 30076 KB Output is correct
7 Correct 356 ms 30076 KB Output is correct
8 Correct 349 ms 30076 KB Output is correct
9 Correct 359 ms 30076 KB Output is correct
10 Correct 437 ms 30128 KB Output is correct
11 Correct 355 ms 30196 KB Output is correct
12 Correct 312 ms 30196 KB Output is correct
13 Correct 356 ms 30196 KB Output is correct
14 Correct 334 ms 30196 KB Output is correct
15 Correct 318 ms 30196 KB Output is correct
16 Correct 352 ms 30196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1372 KB Output is correct
2 Correct 297 ms 30196 KB Output is correct
3 Correct 285 ms 30196 KB Output is correct
4 Correct 418 ms 33552 KB Output is correct
5 Correct 181 ms 33552 KB Output is correct
6 Correct 198 ms 33552 KB Output is correct
7 Correct 445 ms 34908 KB Output is correct
8 Correct 86 ms 34908 KB Output is correct
9 Correct 54 ms 34908 KB Output is correct
10 Correct 32 ms 34908 KB Output is correct
11 Correct 43 ms 34908 KB Output is correct
12 Correct 78 ms 34908 KB Output is correct
13 Correct 89 ms 34908 KB Output is correct
14 Correct 80 ms 34908 KB Output is correct
15 Correct 72 ms 34908 KB Output is correct
16 Correct 192 ms 34908 KB Output is correct
17 Correct 179 ms 34908 KB Output is correct
18 Correct 258 ms 34908 KB Output is correct
19 Correct 342 ms 34908 KB Output is correct
20 Correct 427 ms 41216 KB Output is correct
21 Correct 305 ms 41216 KB Output is correct
22 Correct 283 ms 41216 KB Output is correct
23 Correct 160 ms 41216 KB Output is correct
24 Correct 320 ms 41216 KB Output is correct
25 Correct 689 ms 60504 KB Output is correct
26 Correct 739 ms 61048 KB Output is correct
27 Correct 667 ms 61048 KB Output is correct
28 Correct 532 ms 61048 KB Output is correct
29 Correct 443 ms 61048 KB Output is correct
30 Correct 455 ms 61048 KB Output is correct
31 Correct 547 ms 61048 KB Output is correct
32 Correct 672 ms 61096 KB Output is correct
33 Correct 628 ms 61096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 106 ms 376 KB Output is correct
2 Correct 434 ms 684 KB Output is correct
3 Correct 128 ms 832 KB Output is correct
4 Correct 184 ms 832 KB Output is correct
5 Correct 635 ms 980 KB Output is correct
6 Correct 2 ms 980 KB Output is correct
7 Correct 2 ms 980 KB Output is correct
8 Correct 4 ms 980 KB Output is correct
9 Correct 2 ms 980 KB Output is correct
10 Correct 2 ms 980 KB Output is correct
11 Correct 393 ms 980 KB Output is correct
12 Correct 680 ms 980 KB Output is correct
13 Correct 917 ms 1200 KB Output is correct
14 Correct 1858 ms 1372 KB Output is correct
15 Correct 2 ms 1372 KB Output is correct
16 Correct 2 ms 1372 KB Output is correct
17 Correct 3 ms 1372 KB Output is correct
18 Execution timed out 3034 ms 61096 KB Time limit exceeded
19 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 106 ms 376 KB Output is correct
2 Correct 434 ms 684 KB Output is correct
3 Correct 128 ms 832 KB Output is correct
4 Correct 184 ms 832 KB Output is correct
5 Correct 635 ms 980 KB Output is correct
6 Correct 2 ms 980 KB Output is correct
7 Correct 2 ms 980 KB Output is correct
8 Correct 4 ms 980 KB Output is correct
9 Correct 2 ms 980 KB Output is correct
10 Correct 2 ms 980 KB Output is correct
11 Correct 393 ms 980 KB Output is correct
12 Correct 680 ms 980 KB Output is correct
13 Correct 917 ms 1200 KB Output is correct
14 Correct 1858 ms 1372 KB Output is correct
15 Correct 2 ms 1372 KB Output is correct
16 Correct 2 ms 1372 KB Output is correct
17 Correct 3 ms 1372 KB Output is correct
18 Execution timed out 3034 ms 61096 KB Time limit exceeded
19 Halted 0 ms 0 KB -