結果
| 問題 |
No.960 マンハッタン距離3
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-12-23 23:34:00 |
| 言語 | Java (openjdk 23) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 12,317 bytes |
| コンパイル時間 | 5,248 ms |
| コンパイル使用メモリ | 90,696 KB |
| 実行使用メモリ | 88,612 KB |
| 最終ジャッジ日時 | 2024-09-19 04:16:54 |
| 合計ジャッジ時間 | 56,304 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 207 WA * 9 |
ソースコード
package adv2019;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.InputMismatchException;
public class D23_2 {
InputStream is;
PrintWriter out;
String INPUT = "";
int w, h;
int[][] co;
void solve()
{
w = ni();
h = ni();
int n = ni();
co = new int[n][];
for(int i = 0;i < n;i++){
co[i] = new int[]{ni(), ni(), -1, -1};
co[i][2] = co[i][0] + co[i][1];
co[i][3] = co[i][0] - co[i][1];
}
Arrays.sort(co, new Comparator<int[]>() {
public int compare(int[] a, int[] b) {
return a[0] - b[0];
}
});
int[][] es = new int[6*n][];
int p = 0;
for(int i = 1;i < n;i++){
int d = Math.abs(co[i][0] - co[0][0]) +
Math.abs(co[i][1] - co[0][1]);
if(d % 2 != 0){
out.println(0);
return;
}
// c
// ab/d
// ----e
// ih\f
// g
if(co[0][0] == co[i][0]){
if(co[0][1] < co[i][1]){
// a
es[p++] = new int[]{
1, co[0][1] + d/2, co[0][1] + d/2 + 1, 1, 0, i
};
es[p++] = new int[]{
w+1, co[0][1] + d/2, co[0][1] + d/2 + 1, -1, 0, i
};
}else{
// i
es[p++] = new int[]{
1, co[0][1] - d/2, co[0][1] - d/2 + 1, 1, 0, i
};
es[p++] = new int[]{
w+1, co[0][1] - d/2, co[0][1] - d/2 + 1, -1, 0, i
};
}
}else{
if(co[0][1] < co[i][1]){
if(co[0][3] > co[i][3]){
// b
es[p++] = new int[]{
1, co[0][1] + d/2, co[0][1] + d/2 + 1, 1, 0, i
};
es[p++] = new int[]{
co[0][0], co[0][1] + d/2, co[0][1] + d/2 + 1, -1, 0, i
};
es[p++] = new int[]{
co[0][0], co[0][1] + d/2, -1, 1, 2, i
};
es[p++] = new int[]{
co[i][0], co[i][1] - d/2, -1, -1, 2, i
};
es[p++] = new int[]{
co[i][0], co[i][1] - d/2, co[i][1] - d/2 + 1, 1, 0, i
};
es[p++] = new int[]{
w+1, co[i][1] - d/2, co[i][1] - d/2 + 1, -1, 0, i
};
}else if(co[0][3] == co[i][3]){
// c
es[p++] = new int[]{
1, co[i][1], h + 1, 1, 0, i
};
es[p++] = new int[]{
co[0][0]+1, co[i][1], h + 1, -1, 0, i
};
es[p++] = new int[]{
co[0][0]+1, co[i][1]-1, -1, 1, 2, i
};
es[p++] = new int[]{
co[i][0], co[0][1], -1, -1, 2, i
};
es[p++] = new int[]{
co[i][0], 1, co[0][1]+1, 1, 0, i
};
es[p++] = new int[]{
w+1, 1, co[0][1]+1, -1, 0, i
};
}else{
// d
es[p++] = new int[]{
co[i][0]-d/2, co[i][1], h + 1, 1, 0, i
};
es[p++] = new int[]{
co[i][0]-d/2+1, co[i][1], h + 1, -1, 0, i
};
es[p++] = new int[]{
co[i][0]-d/2, co[i][1], -1, 1, 2, i
};
es[p++] = new int[]{
co[0][0]+d/2, co[0][1], -1, -1, 2, i
};
es[p++] = new int[]{
co[0][0]+d/2, 1, co[0][1]+1, 1, 0, i
};
es[p++] = new int[]{
co[0][0]+d/2+1, 1, co[0][1]+1, -1, 0, i
};
}
}else if(co[0][1] == co[i][1]){
// e
es[p++] = new int[]{
co[0][0]+d/2, 1, h + 1, 1, 0, i
};
es[p++] = new int[]{
co[0][0]+d/2+1, 1, h+1, -1, 0, i
};
}else if(co[0][1] > co[i][1]){
if(co[0][2] > co[i][2]){
// h
es[p++] = new int[]{
1, co[0][1] - d/2, co[0][1] - d/2 + 1, 1, 0, i
};
es[p++] = new int[]{
co[0][0], co[0][1] - d/2, co[0][1] - d/2 + 1, -1, 0, i
};
es[p++] = new int[]{
co[0][0], co[0][1] - d/2, 1, 1, 1, i
};
es[p++] = new int[]{
co[i][0], co[i][1] + d/2, 1, -1, 1, i
};
es[p++] = new int[]{
co[i][0], co[i][1] + d/2, co[i][1] + d/2 + 1, 1, 0, i
};
es[p++] = new int[]{
w+1, co[i][1] + d/2, co[i][1] + d/2 + 1, -1, 0, i
};
}else if(co[0][2] == co[i][2]){
// g
es[p++] = new int[]{
1, 1, co[i][1]+1, 1, 0, i
};
es[p++] = new int[]{
co[0][0]+1, 1, co[i][1] + 1, -1, 0, i
};
es[p++] = new int[]{
co[0][0]+1, co[i][1]+1, 1, 1, 1, i
};
es[p++] = new int[]{
co[i][0], co[0][1], 1, -1, 1, i
};
es[p++] = new int[]{
co[i][0], co[0][1], h+1, 1, 0, i
};
es[p++] = new int[]{
w+1, co[0][1], h+1, -1, 0, i
};
}else{
// f
es[p++] = new int[]{
co[i][0]-d/2, 1, co[i][1]+1, 1, 0, i
};
es[p++] = new int[]{
co[i][0]-d/2+1, 1, co[i][1]+1, -1, 0, i
};
es[p++] = new int[]{
co[i][0]-d/2+1, co[i][1]+1, 1, 1, 1, i
};
es[p++] = new int[]{
co[0][0]+d/2, co[0][1], 1, -1, 1, i
};
es[p++] = new int[]{
co[0][0]+d/2, co[0][1], h+1, 1, 0, i
};
es[p++] = new int[]{
co[0][0]+d/2+1, co[0][1], h+1, -1, 0, i
};
}
}
}
}
es = Arrays.copyOf(es, p);
// for(int[] e : es){
// tr(e);
// }
Arrays.sort(es, new Comparator<int[]>() {
public int compare(int[] a, int[] b) {
return a[0] - b[0];
}
});
MinHeap sups = new MinHeap(n);
MinHeap infs = new MinHeap(n);
long S02 = 0, S12 = 0, S22 = 0;
long S01 = 0, S11 = 0, S21 = 0;
int ins = 0;
int x = 1;
long S = 0;
for(int[] e : es){
if(x < e[0]){
if(ins == n-1){
boolean ok = true;
if(S01 > 0){
if(S11 % S01 == 0 && (S11/S01)*(S11/S01)*S01 == S21){
}else{
ok = false;
}
}
if(S02 > 0){
if(S12 % S02 == 0 && (S12/S02)*(S12/S02)*S02 == S22){
}else{
ok = false;
}
}
if(ok){
// tr(S01, S02, sups.size());
if(S01 > 0 && S02 > 0){
long ce1 = S11/S01;
long ly1 = x+ce1;// y-x
long ry1 = e[0]+ce1-1;
long ce2 = S12/S02;
long ly2 = ce2-x;// y+x
long ry2 = ce2-e[0]+1;
if((ly2-ly1) % 2 == 0 && ly1 <= ly2 && ry1 >= ry2){
if(sups.size() == 0){
S++;
}else{
long yy = (ly1+ly2)/2;
long sup = sups.min();
long inf = -infs.min();
if(inf <= yy && yy < sup){
S++;
}
}
}
}else if(S01 == 0 && S02 > 0){
if(sups.size() == 0){
S += e[0]-x;
}else{
long ce = S12/S02;
long ly = ce-x+1;// y+x
long ry = ce-e[0]+1;
long sup = sups.min();
long inf = -infs.min();
inf = Math.max(inf, ry);
sup = Math.min(sup, ly);
S += Math.max(sup-inf, 0);
}
}else if(S02 == 0 && S01 > 0){
if(sups.size() == 0){
S += e[0]-x;
}else{
long ce = S11/S01;
long ly = x+ce;// y-x
long ry = e[0]+ce;
long sup = sups.min();
long inf = -infs.min();
inf = Math.max(inf, ly);
sup = Math.min(sup, ry);
S += Math.max(sup-inf, 0);
}
}else{
int sup = sups.min();
int inf = -infs.min();
if(inf < sup){
S += (long)(sup-inf) * (e[0] - x);
}
}
// tr(x, e[0], ins, n, S);
}
}
}
ins += e[3];
if(e[4] == 0){
// rectangle
if(e[3] == 1){
sups.add(e[5], e[2]);
infs.add(e[5], -e[1]);
}else{
sups.remove(e[5]);
infs.remove(e[5]);
}
}else if(e[4] == 1){
// /
S01 += e[3];
S11 += e[3] * (e[1] - e[0]);
S21 += (long)e[3] * (e[1] - e[0]) * (e[1] - e[0]);
}else{
// \
S02 += e[3];
S12 += e[3] * (e[1] + e[0]);
S22 += (long)e[3] * (e[1] + e[0]) * (e[1] + e[0]);
}
x = e[0];
}
out.println(S);
}
public static class MinHeap {
public int[] a;
public int[] map;
public int[] imap;
public int n;
public int pos;
public static int INF = Integer.MAX_VALUE;
public MinHeap(int m)
{
n = m+2;
a = new int[n];
map = new int[n];
imap = new int[n];
Arrays.fill(a, INF);
Arrays.fill(map, -1);
Arrays.fill(imap, -1);
pos = 1;
}
public int add(int ind, int x)
{
int ret = imap[ind];
if(imap[ind] < 0){
a[pos] = x; map[pos] = ind; imap[ind] = pos;
pos++;
up(pos-1);
}
return ret != -1 ? a[ret] : x;
}
public int update(int ind, int x)
{
int ret = imap[ind];
if(imap[ind] < 0){
a[pos] = x; map[pos] = ind; imap[ind] = pos;
pos++;
up(pos-1);
}else{
int o = a[ret];
a[ret] = x;
up(ret);
down(ret);
// if(a[ret] > o){
// up(ret);
// }else{
// down(ret);
// }
}
return x;
}
public int remove(int ind)
{
if(pos == 1)return INF;
if(imap[ind] == -1)return INF;
pos--;
int rem = imap[ind];
int ret = a[rem];
map[rem] = map[pos];
imap[map[pos]] = rem;
imap[ind] = -1;
a[rem] = a[pos];
a[pos] = INF;
map[pos] = -1;
up(rem);
down(rem);
return ret;
}
public int min() { return a[1]; }
public int argmin() { return map[1]; }
public int size() { return pos-1; }
private void up(int cur)
{
for(int c = cur, p = c>>>1;p >= 1 && a[p] > a[c];c>>>=1, p>>>=1){
int d = a[p]; a[p] = a[c]; a[c] = d;
int e = imap[map[p]]; imap[map[p]] = imap[map[c]]; imap[map[c]] = e;
e = map[p]; map[p] = map[c]; map[c] = e;
}
}
private void down(int cur)
{
for(int c = cur;2*c < pos;){
int b = a[2*c] < a[2*c+1] ? 2*c : 2*c+1;
if(a[b] < a[c]){
int d = a[c]; a[c] = a[b]; a[b] = d;
int e = imap[map[c]]; imap[map[c]] = imap[map[b]]; imap[map[b]] = e;
e = map[c]; map[c] = map[b]; map[b] = e;
c = b;
}else{
break;
}
}
}
}
void run() throws Exception
{
is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
// Thread t = new Thread(null, null, "~", Runtime.getRuntime().maxMemory()){
// @Override
// public void run() {
// long s = System.currentTimeMillis();
// solve();
// out.flush();
// if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
// }
// };
// t.start();
// t.join();
}
public static void main(String[] args) throws Exception { new D23_2().run(); }
private byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
private int readByte()
{
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
private double nd() { return Double.parseDouble(ns()); }
private char nc() { return (char)skip(); }
private String ns()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n)
{
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private int[] na(int n)
{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = ni();
return a;
}
private long[] nal(int n)
{
long[] a = new long[n];
for(int i = 0;i < n;i++)a[i] = nl();
return a;
}
private char[][] nm(int n, int m) {
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = ns(m);
return map;
}
private int[][] nmi(int n, int m) {
int[][] map = new int[n][];
for(int i = 0;i < n;i++)map[i] = na(m);
return map;
}
private int ni() { return (int)nl(); }
private long nl()
{
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); }
}