id
stringlengths
22
25
content
stringlengths
327
628k
max_stars_repo_path
stringlengths
49
49
condefects-java_data_301
import java.util.*; import java.io.*; class Main{ public static void main(String[] args) { FastScanner str=new FastScanner(System.in); int n=str.nextInt(); int x=str.nextInt(); int y=str.nextInt(); int z=str.nextInt(); int[] a=new int[n]; int[] b=new int[n]; boolean[] flag=new boolean[n]; for(int i=0;i<n;i++){ a[i]=str.nextInt(); } for(int i=0;i<n;i++){ b[i]=str.nextInt(); } /*x*/ for(int i=0;i<x;i++){ int pos=-1; for(int j=0;j<n;j++){ if(!flag[j]){ if(pos==-1||a[j]>a[pos]){ pos=i; } } } flag[pos]=true; } /*y*/ for(int i=0;i<y;i++){ int pos=-1; for(int j=0;j<n;j++){ if(!flag[j]){ if(pos==-1||b[j]>b[pos]){ pos=j; } } } flag[pos]=true; } for(int i=0;i<z;i++){ int pos=-1; for(int j=0;j<n;j++){ if(!flag[j]){ if(pos==-1||a[j]+b[j]>a[pos]+b[pos]){ pos=j; } } } flag[pos]=true; } for(int i=0;i<n;i++){ if(flag[i])System.out.println(i+1); } } } class FastScanner implements Closeable { private final InputStream in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; public FastScanner(InputStream in) { this.in = in; } private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;} private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;} public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();} public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while(true){ if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next());} public void close() { try { in.close(); } catch (IOException e) { } } } import java.util.*; import java.io.*; class Main{ public static void main(String[] args) { FastScanner str=new FastScanner(System.in); int n=str.nextInt(); int x=str.nextInt(); int y=str.nextInt(); int z=str.nextInt(); int[] a=new int[n]; int[] b=new int[n]; boolean[] flag=new boolean[n]; for(int i=0;i<n;i++){ a[i]=str.nextInt(); } for(int i=0;i<n;i++){ b[i]=str.nextInt(); } /*x*/ for(int i=0;i<x;i++){ int pos=-1; for(int j=0;j<n;j++){ if(!flag[j]){ if(pos==-1||a[j]>a[pos]){ pos=j; } } } flag[pos]=true; } /*y*/ for(int i=0;i<y;i++){ int pos=-1; for(int j=0;j<n;j++){ if(!flag[j]){ if(pos==-1||b[j]>b[pos]){ pos=j; } } } flag[pos]=true; } for(int i=0;i<z;i++){ int pos=-1; for(int j=0;j<n;j++){ if(!flag[j]){ if(pos==-1||a[j]+b[j]>a[pos]+b[pos]){ pos=j; } } } flag[pos]=true; } for(int i=0;i<n;i++){ if(flag[i])System.out.println(i+1); } } } class FastScanner implements Closeable { private final InputStream in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; public FastScanner(InputStream in) { this.in = in; } private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;} private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;} public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();} public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while(true){ if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next());} public void close() { try { in.close(); } catch (IOException e) { } } }
ConDefects/ConDefects/Code/abc260_b/Java/35419851
condefects-java_data_302
import java.util.*; public class Main { static List<Integer> ar=new ArrayList<Integer>(); public static void main (String[]args){ Scanner scan=new Scanner(System.in); int N=scan.nextInt(); HashMap<Integer, Character>hashMap=new HashMap<Integer, Character>(); ArrayList<Character> l=new ArrayList<>(); char c; for(c='a';c<='z';++c){ l.add(c); } for(int i=97,z=0; i<122;z++,i++){ hashMap.put(i,l.get(z)); } System.out.println(hashMap.get(N)); } } import java.util.*; public class Main { static List<Integer> ar=new ArrayList<Integer>(); public static void main (String[]args){ Scanner scan=new Scanner(System.in); int N=scan.nextInt(); HashMap<Integer, Character>hashMap=new HashMap<Integer, Character>(); ArrayList<Character> l=new ArrayList<>(); char c; for(c='a';c<='z';++c){ l.add(c); } for(int i=97,z=0; i<=122;z++,i++){ hashMap.put(i,l.get(z)); } System.out.println(hashMap.get(N)); } }
ConDefects/ConDefects/Code/abc252_a/Java/35198991
condefects-java_data_303
import java.util.Scanner; public class Main { public static void main(String []args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); char c = (char)n; System.out.println(n); } } import java.util.Scanner; public class Main { public static void main(String []args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); char c = (char)n; System.out.println(c); } }
ConDefects/ConDefects/Code/abc252_a/Java/38176973
condefects-java_data_304
import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; import java.util.Stack; public class Main { static long MOD = 998244353l; int min = Integer.MAX_VALUE; int max = 0; char result[][]; int count = 0; int pattern = 0; public static void main(String[] args) throws Exception { // FileInputStream fis = new FileInputStream(new File("1.txt")); // var sc = new FastScanner(); // // var sc = new FastScanner(fis); // // var pw = new FastPrintStream("test_normal_result.csv"); // var pw = new FastPrintStream(); // solve(sc, pw); // sc.close(); // pw.flush(); // pw.close(); Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int a[][] = new int[2][n]; for (int i = 0; i < 2; i++) { for (int j = 2; j < n; j++) { System.out.println("? " + (i + 1) + " " + (j + 1)); a[i][j] = sc.nextInt(); System.out.flush(); } } boolean bool = true; int min = Integer.MAX_VALUE; for (int i = 2; i < n; i++) { min = Math.min(a[0][i] + a[1][i], min); if (Math.abs(a[0][i] - a[1][i]) != 1) { bool = false; } } for (int i = 0; i < 2; i++) { boolean temp = true; for (int j = 0; j < n; j++) { if (a[i][j] == 1) { temp = false; break; } } if (temp) { System.out.println("! 1"); return; } } List<Integer> list = new ArrayList<Integer>(); if (bool) { for (int i = 2; i < n; i++) { if (a[0][i] == 1) { list.add(i); break; } } for (int i = 2; i < n; i++) { if (a[1][i] == 1) { list.add(i); break; } } System.out.println("? " + (list.get(0) + 1) + " " + (list.get(1) + 1)); int sa = sc.nextInt(); if (sa!=1) { System.out.println("! 1"); return ; } } System.out.println(min); } public static void solve(FastScanner sc, FastPrintStream pw) { } public static Point checkPoint(char ch) { Point re = new Point(0, 0); switch (ch) { case 'N': { re.x--; break; } case 'S': { re.x++; break; } case 'E': { re.y++; break; } case 'W': { re.y--; break; } } return re; } public char[][] copyArray(char c[][], int n) { char re[][] = new char[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { re[i][j] = c[i][j]; } } return re; } public static void swap(int[] s, int i, int j) { int tmp = s[i]; s[i] = s[j]; s[j] = tmp; } public void permutation(int[] s, int from, int to, int a[]) { if (to <= 1) return; if (from == to) { check(s, a); } else { for (int i = from; i <= to; i++) { swap(s, i, from); permutation(s, from + 1, to, a); swap(s, from, i); } } } public void check(int[] s, int a[]) { int re = 0; int b[][] = new int[2][3]; for (int i = 0; i < 6; i++) { b[i / 3][i % 3] = a[s[i]]; } int maxx = 0; int maxy = 0; int minx = 10000; int miny = 10000; for (int i = 0; i < 3; i++) { maxx = Math.max(maxx, b[0][i]); minx = Math.min(minx, b[0][i]); maxy = Math.max(maxy, b[1][i]); miny = Math.min(miny, b[1][i]); } re = (maxx - minx) * (maxy - miny) * 2; re = re - (Math.abs(b[0][0] - b[0][1]) * Math.abs(b[1][0] - b[1][1])); re = re - (Math.abs(b[0][0] - b[0][2]) * Math.abs(b[1][0] - b[1][2])); re = re - (Math.abs(b[0][2] - b[0][1]) * Math.abs(b[1][2] - b[1][1])); max = Math.max(re, max); } public static long anothertoTen(long ano, int another) { long ten = 0; long now = 1; long temp = ano; while (temp > 0) { long i = temp % 10; ten += now * i; now *= another; temp /= 10; } return ten; } public static long tentoAnother(long ten, int another) { Stack<Long> stack = new Stack<Long>(); while (ten > 0) { stack.add(ten % another); ten /= another; } long re = 0; while (!stack.isEmpty()) { long pop = stack.pop(); re = re * 10 + pop; } return re; } // 2C5 = 5*4/(2*1) public static long XCY(long x, long y) { long temp = 1; for (int i = 0; i < x; i++) { temp = (temp * (y - i)) % MOD; } long tempx = 1; for (int i = 2; i <= x; i++) { tempx = (tempx * i) % MOD; } tempx = modpow(tempx, (long) MOD - 2); temp = (temp * tempx) % MOD; return temp; } static long modpow(long N, Long K) { return BigInteger.valueOf(N).modPow(BigInteger.valueOf(K), BigInteger.valueOf(MOD)).longValue(); } static long modpow(long N, Long K, long mod) { return BigInteger.valueOf(N).modPow(BigInteger.valueOf(K), BigInteger.valueOf(mod)).longValue(); } public static long gcd(long a, long b) { if (b == 0) { return a; } if (a < b) { return gcd(b, a); } return gcd(b, a % b); } public static int gcd(int a, int b) { if (b == 0) { return a; } if (a < b) { return gcd(b, a); } return gcd(b, a % b); } } class Node implements Comparable<Node> { int tyoten; long minDistance; public Node(int t, long m) { tyoten = t; minDistance = m; } @Override public int compareTo(Node o) { int res = -1; if (this.minDistance - o.minDistance >= 0) { res = 1; } return res; } } class Range { long x = 0; long c = 0; public Range(long x, long c) { this.x = x; this.c = c; } } class Vertex { String key; Vertex(String key) { this.key = key; } } class Edge { Vertex start; Vertex end; long key; Edge(Vertex start, Vertex end, long key) { this.start = start; this.end = end; this.key = key; } } class Target { int t; long c; boolean bool; } class Point implements Comparable { int x; int y; public Point() { } public Point(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Object p) { Point t = (Point) p; if (this.x < t.x) { return -1; } if (this.x > t.x) { return 1; } return 0; } @Override public boolean equals(Object p) { Point t = (Point) p; return this.x == t.x; } } class PointY implements Comparable { int a; int b; public int compareTo(Object p) { PointY t = (PointY) p; if (this.a > t.a) { return -1; } if (this.a < t.a) { return 1; } return 0; } } class FastPrintStream implements AutoCloseable { private static final int BUF_SIZE = 1 << 15; private final byte[] buf = new byte[BUF_SIZE]; private int ptr = 0; private final java.lang.reflect.Field strField; private final java.nio.charset.CharsetEncoder encoder; private java.io.OutputStream out; public FastPrintStream(java.io.OutputStream out) { this.out = out; java.lang.reflect.Field f; try { f = java.lang.String.class.getDeclaredField("value"); f.setAccessible(true); } catch (NoSuchFieldException | SecurityException e) { f = null; } this.strField = f; this.encoder = java.nio.charset.StandardCharsets.US_ASCII.newEncoder(); } public FastPrintStream(java.io.File file) throws java.io.IOException { this(new java.io.FileOutputStream(file)); } public FastPrintStream(java.lang.String filename) throws java.io.IOException { this(new java.io.File(filename)); } public FastPrintStream() { this(System.out); try { java.lang.reflect.Field f = java.io.PrintStream.class.getDeclaredField("autoFlush"); f.setAccessible(true); f.set(System.out, false); } catch (IllegalAccessException | IllegalArgumentException | NoSuchFieldException e) { // ignore } } public FastPrintStream println() { if (ptr == BUF_SIZE) internalFlush(); buf[ptr++] = (byte) '\n'; return this; } public FastPrintStream println(java.lang.Object o) { return print(o).println(); } public FastPrintStream println(java.lang.String s) { return print(s).println(); } public FastPrintStream println(char[] s) { return print(s).println(); } public FastPrintStream println(char c) { return print(c).println(); } public FastPrintStream println(int x) { return print(x).println(); } public FastPrintStream println(long x) { return print(x).println(); } public FastPrintStream println(double d, int precision) { return print(d, precision).println(); } private FastPrintStream print(byte[] bytes) { int n = bytes.length; if (ptr + n > BUF_SIZE) { internalFlush(); try { out.write(bytes); } catch (java.io.IOException e) { throw new RuntimeException(); } } else { System.arraycopy(bytes, 0, buf, ptr, n); ptr += n; } return this; } public FastPrintStream print(java.lang.Object o) { return print(o.toString()); } public FastPrintStream print(java.lang.String s) { if (strField == null) { return print(s.getBytes()); } else { try { return print((byte[]) strField.get(s)); } catch (IllegalAccessException e) { return print(s.getBytes()); } } } public FastPrintStream print(char[] s) { try { return print(encoder.encode(java.nio.CharBuffer.wrap(s)).array()); } catch (java.nio.charset.CharacterCodingException e) { byte[] bytes = new byte[s.length]; for (int i = 0; i < s.length; i++) { bytes[i] = (byte) s[i]; } return print(bytes); } } public FastPrintStream print(char c) { if (ptr == BUF_SIZE) internalFlush(); buf[ptr++] = (byte) c; return this; } public FastPrintStream print(int x) { if (x == 0) { if (ptr == BUF_SIZE) internalFlush(); buf[ptr++] = '0'; return this; } int d = len(x); if (ptr + d > BUF_SIZE) internalFlush(); if (x < 0) { buf[ptr++] = '-'; x = -x; d--; } int j = ptr += d; while (x > 0) { buf[--j] = (byte) ('0' + (x % 10)); x /= 10; } return this; } public FastPrintStream print(long x) { if (x == 0) { if (ptr == BUF_SIZE) internalFlush(); buf[ptr++] = '0'; return this; } int d = len(x); if (ptr + d > BUF_SIZE) internalFlush(); if (x < 0) { buf[ptr++] = '-'; x = -x; d--; } int j = ptr += d; while (x > 0) { buf[--j] = (byte) ('0' + (x % 10)); x /= 10; } return this; } public FastPrintStream print(double d, int precision) { if (d < 0) { print('-'); d = -d; } d += Math.pow(10, -d) / 2; print((long) d).print('.'); d -= (long) d; for (int i = 0; i < precision; i++) { d *= 10; print((int) d); d -= (int) d; } return this; } private void internalFlush() { try { out.write(buf, 0, ptr); ptr = 0; } catch (java.io.IOException e) { throw new RuntimeException(e); } } public void flush() { try { out.write(buf, 0, ptr); out.flush(); ptr = 0; } catch (java.io.IOException e) { throw new RuntimeException(e); } } public void close() { try { out.close(); } catch (java.io.IOException e) { throw new RuntimeException(e); } } private static int len(int x) { int d = 1; if (x >= 0) { d = 0; x = -x; } int p = -10; for (int i = 1; i < 10; i++, p *= 10) if (x > p) return i + d; return 10 + d; } private static int len(long x) { int d = 1; if (x >= 0) { d = 0; x = -x; } long p = -10; for (int i = 1; i < 19; i++, p *= 10) if (x > p) return i + d; return 19 + d; } } class FastScanner implements AutoCloseable { private final java.io.InputStream in; private final byte[] buf = new byte[2048]; private int ptr = 0; private int buflen = 0; public FastScanner(java.io.InputStream in) { this.in = in; } public FastScanner() { this(System.in); } private boolean hasNextByte() { if (ptr < buflen) return true; ptr = 0; try { buflen = in.read(buf); } catch (java.io.IOException e) { throw new RuntimeException(e); } return buflen > 0; } private int readByte() { return hasNextByte() ? buf[ptr++] : -1; } public boolean hasNext() { while (hasNextByte() && !(32 < buf[ptr] && buf[ptr] < 127)) ptr++; return hasNextByte(); } private StringBuilder nextSequence() { if (!hasNext()) throw new java.util.NoSuchElementException(); StringBuilder sb = new StringBuilder(); for (int b = readByte(); 32 < b && b < 127; b = readByte()) { sb.appendCodePoint(b); } return sb; } public String next() { return nextSequence().toString(); } public String next(int len) { return new String(nextChars(len)); } public char nextChar() { if (!hasNextByte()) throw new java.util.NoSuchElementException(); return (char) readByte(); } public char[] nextChars() { StringBuilder sb = nextSequence(); int l = sb.length(); char[] dst = new char[l]; sb.getChars(0, l, dst, 0); return dst; } public char[] nextChars(int len) { if (!hasNext()) throw new java.util.NoSuchElementException(); char[] s = new char[len]; int i = 0; int b = readByte(); while (32 < b && b < 127 && i < len) { s[i++] = (char) b; b = readByte(); } if (i != len) { throw new java.util.NoSuchElementException( String.format("Next token has smaller length than expected.", len)); } return s; } public long nextLong() { if (!hasNext()) throw new java.util.NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) throw new NumberFormatException(); while (true) { if ('0' <= b && b <= '9') { n = n * 10 + b - '0'; } else if (b == -1 || !(32 < b && b < 127)) { return minus ? -n : n; } else throw new NumberFormatException(); b = readByte(); } } public int nextInt() { return Math.toIntExact(nextLong()); } public double nextDouble() { return Double.parseDouble(next()); } public void close() { try { in.close(); } catch (java.io.IOException e) { throw new RuntimeException(e); } } } /** * @verified https://atcoder.jp/contests/practice2/tasks/practice2_j */ class SegTree<S> { final int MAX; final int N; final java.util.function.BinaryOperator<S> op; final S E; final S[] data; @SuppressWarnings("unchecked") public SegTree(int n, java.util.function.BinaryOperator<S> op, S e) { this.MAX = n; int k = 1; while (k < n) k <<= 1; this.N = k; this.E = e; this.op = op; this.data = (S[]) new Object[N << 1]; java.util.Arrays.fill(data, E); } public SegTree(S[] dat, java.util.function.BinaryOperator<S> op, S e) { this(dat.length, op, e); build(dat); } private void build(S[] dat) { int l = dat.length; System.arraycopy(dat, 0, data, N, l); for (int i = N - 1; i > 0; i--) { data[i] = op.apply(data[i << 1 | 0], data[i << 1 | 1]); } } public void set(int p, S x) { exclusiveRangeCheck(p); data[p += N] = x; p >>= 1; while (p > 0) { data[p] = op.apply(data[p << 1 | 0], data[p << 1 | 1]); p >>= 1; } } public S get(int p) { exclusiveRangeCheck(p); return data[p + N]; } public S prod(int l, int r) { if (l > r) { throw new IllegalArgumentException(String.format("Invalid range: [%d, %d)", l, r)); } inclusiveRangeCheck(l); inclusiveRangeCheck(r); S sumLeft = E; S sumRight = E; l += N; r += N; while (l < r) { if ((l & 1) == 1) sumLeft = op.apply(sumLeft, data[l++]); if ((r & 1) == 1) sumRight = op.apply(data[--r], sumRight); l >>= 1; r >>= 1; } return op.apply(sumLeft, sumRight); } public S allProd() { return data[1]; } public int maxRight(int l, java.util.function.Predicate<S> f) { inclusiveRangeCheck(l); if (!f.test(E)) { throw new IllegalArgumentException("Identity element must satisfy the condition."); } if (l == MAX) return MAX; l += N; S sum = E; do { l >>= Long.numberOfTrailingZeros(l); if (!f.test(op.apply(sum, data[l]))) { while (l < N) { l = l << 1; if (f.test(op.apply(sum, data[l]))) { sum = op.apply(sum, data[l]); l++; } } return l - N; } sum = op.apply(sum, data[l]); l++; } while ((l & -l) != l); return MAX; } public int minLeft(int r, java.util.function.Predicate<S> f) { inclusiveRangeCheck(r); if (!f.test(E)) { throw new IllegalArgumentException("Identity element must satisfy the condition."); } if (r == 0) return 0; r += N; S sum = E; do { r--; while (r > 1 && (r & 1) == 1) r >>= 1; if (!f.test(op.apply(data[r], sum))) { while (r < N) { r = r << 1 | 1; if (f.test(op.apply(data[r], sum))) { sum = op.apply(data[r], sum); r--; } } return r + 1 - N; } sum = op.apply(data[r], sum); } while ((r & -r) != r); return 0; } private void exclusiveRangeCheck(int p) { if (p < 0 || p >= MAX) { throw new IndexOutOfBoundsException( String.format("Index %d out of bounds for the range [%d, %d).", p, 0, MAX)); } } private void inclusiveRangeCheck(int p) { if (p < 0 || p > MAX) { throw new IndexOutOfBoundsException( String.format("Index %d out of bounds for the range [%d, %d].", p, 0, MAX)); } } // **************** DEBUG **************** // private int indent = 6; public void setIndent(int newIndent) { this.indent = newIndent; } @Override public String toString() { return toSimpleString(); } public String toDetailedString() { return toDetailedString(1, 0); } private String toDetailedString(int k, int sp) { if (k >= N) return indent(sp) + data[k]; String s = ""; s += toDetailedString(k << 1 | 1, sp + indent); s += "\n"; s += indent(sp) + data[k]; s += "\n"; s += toDetailedString(k << 1 | 0, sp + indent); return s; } private static String indent(int n) { StringBuilder sb = new StringBuilder(); while (n-- > 0) sb.append(' '); return sb.toString(); } public String toSimpleString() { StringBuilder sb = new StringBuilder(); sb.append('['); for (int i = 0; i < N; i++) { sb.append(data[i + N]); if (i < N - 1) sb.append(',').append(' '); } sb.append(']'); return sb.toString(); } } class DSU { private int n; private int[] parentOrSize; public DSU(int n) { this.n = n; this.parentOrSize = new int[n]; Arrays.fill(parentOrSize, -1); } int merge(int a, int b) { if (!(0 <= a && a < n) || !(0 <= b && b < n)) { return -1; } int x = leader(a); int y = leader(b); if (x == y) return x; if (-parentOrSize[x] < -parentOrSize[y]) { int tmp = x; x = y; y = tmp; } parentOrSize[x] += parentOrSize[y]; parentOrSize[y] = x; return x; } boolean same(int a, int b) { if (!(0 <= a && a < n) || !(0 <= b && b < n)) { return false; } return leader(a) == leader(b); } int leader(int a) { if (parentOrSize[a] < 0) { return a; } else { parentOrSize[a] = leader(parentOrSize[a]); return parentOrSize[a]; } } int size(int a) { if (!(0 <= a && a < n)) { return -1; } return -parentOrSize[leader(a)]; } ArrayList<ArrayList<Integer>> groups() { int[] leaderBuf = new int[n]; int[] groupSize = new int[n]; for (int i = 0; i < n; i++) { leaderBuf[i] = leader(i); groupSize[leaderBuf[i]]++; } ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); for (int i = 0; i < n; i++) { result.add(new ArrayList<>()); } for (int i = 0; i < n; i++) { result.get(leaderBuf[i]).add(i); } return result; } } import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; import java.util.Stack; public class Main { static long MOD = 998244353l; int min = Integer.MAX_VALUE; int max = 0; char result[][]; int count = 0; int pattern = 0; public static void main(String[] args) throws Exception { // FileInputStream fis = new FileInputStream(new File("1.txt")); // var sc = new FastScanner(); // // var sc = new FastScanner(fis); // // var pw = new FastPrintStream("test_normal_result.csv"); // var pw = new FastPrintStream(); // solve(sc, pw); // sc.close(); // pw.flush(); // pw.close(); Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int a[][] = new int[2][n]; for (int i = 0; i < 2; i++) { for (int j = 2; j < n; j++) { System.out.println("? " + (i + 1) + " " + (j + 1)); a[i][j] = sc.nextInt(); System.out.flush(); } } boolean bool = true; int min = Integer.MAX_VALUE; for (int i = 2; i < n; i++) { min = Math.min(a[0][i] + a[1][i], min); if (Math.abs(a[0][i] - a[1][i]) != 1) { bool = false; } } for (int i = 0; i < 2; i++) { boolean temp = true; for (int j = 0; j < n; j++) { if (a[i][j] == 1) { temp = false; break; } } if (temp) { System.out.println("! 1"); return; } } List<Integer> list = new ArrayList<Integer>(); if (bool) { for (int i = 2; i < n; i++) { if (a[0][i] == 1) { list.add(i); break; } } for (int i = 2; i < n; i++) { if (a[1][i] == 1) { list.add(i); break; } } System.out.println("? " + (list.get(0) + 1) + " " + (list.get(1) + 1)); int sa = sc.nextInt(); if (sa!=1) { System.out.println("! 1"); return ; } } System.out.println("! "+min); } public static void solve(FastScanner sc, FastPrintStream pw) { } public static Point checkPoint(char ch) { Point re = new Point(0, 0); switch (ch) { case 'N': { re.x--; break; } case 'S': { re.x++; break; } case 'E': { re.y++; break; } case 'W': { re.y--; break; } } return re; } public char[][] copyArray(char c[][], int n) { char re[][] = new char[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { re[i][j] = c[i][j]; } } return re; } public static void swap(int[] s, int i, int j) { int tmp = s[i]; s[i] = s[j]; s[j] = tmp; } public void permutation(int[] s, int from, int to, int a[]) { if (to <= 1) return; if (from == to) { check(s, a); } else { for (int i = from; i <= to; i++) { swap(s, i, from); permutation(s, from + 1, to, a); swap(s, from, i); } } } public void check(int[] s, int a[]) { int re = 0; int b[][] = new int[2][3]; for (int i = 0; i < 6; i++) { b[i / 3][i % 3] = a[s[i]]; } int maxx = 0; int maxy = 0; int minx = 10000; int miny = 10000; for (int i = 0; i < 3; i++) { maxx = Math.max(maxx, b[0][i]); minx = Math.min(minx, b[0][i]); maxy = Math.max(maxy, b[1][i]); miny = Math.min(miny, b[1][i]); } re = (maxx - minx) * (maxy - miny) * 2; re = re - (Math.abs(b[0][0] - b[0][1]) * Math.abs(b[1][0] - b[1][1])); re = re - (Math.abs(b[0][0] - b[0][2]) * Math.abs(b[1][0] - b[1][2])); re = re - (Math.abs(b[0][2] - b[0][1]) * Math.abs(b[1][2] - b[1][1])); max = Math.max(re, max); } public static long anothertoTen(long ano, int another) { long ten = 0; long now = 1; long temp = ano; while (temp > 0) { long i = temp % 10; ten += now * i; now *= another; temp /= 10; } return ten; } public static long tentoAnother(long ten, int another) { Stack<Long> stack = new Stack<Long>(); while (ten > 0) { stack.add(ten % another); ten /= another; } long re = 0; while (!stack.isEmpty()) { long pop = stack.pop(); re = re * 10 + pop; } return re; } // 2C5 = 5*4/(2*1) public static long XCY(long x, long y) { long temp = 1; for (int i = 0; i < x; i++) { temp = (temp * (y - i)) % MOD; } long tempx = 1; for (int i = 2; i <= x; i++) { tempx = (tempx * i) % MOD; } tempx = modpow(tempx, (long) MOD - 2); temp = (temp * tempx) % MOD; return temp; } static long modpow(long N, Long K) { return BigInteger.valueOf(N).modPow(BigInteger.valueOf(K), BigInteger.valueOf(MOD)).longValue(); } static long modpow(long N, Long K, long mod) { return BigInteger.valueOf(N).modPow(BigInteger.valueOf(K), BigInteger.valueOf(mod)).longValue(); } public static long gcd(long a, long b) { if (b == 0) { return a; } if (a < b) { return gcd(b, a); } return gcd(b, a % b); } public static int gcd(int a, int b) { if (b == 0) { return a; } if (a < b) { return gcd(b, a); } return gcd(b, a % b); } } class Node implements Comparable<Node> { int tyoten; long minDistance; public Node(int t, long m) { tyoten = t; minDistance = m; } @Override public int compareTo(Node o) { int res = -1; if (this.minDistance - o.minDistance >= 0) { res = 1; } return res; } } class Range { long x = 0; long c = 0; public Range(long x, long c) { this.x = x; this.c = c; } } class Vertex { String key; Vertex(String key) { this.key = key; } } class Edge { Vertex start; Vertex end; long key; Edge(Vertex start, Vertex end, long key) { this.start = start; this.end = end; this.key = key; } } class Target { int t; long c; boolean bool; } class Point implements Comparable { int x; int y; public Point() { } public Point(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Object p) { Point t = (Point) p; if (this.x < t.x) { return -1; } if (this.x > t.x) { return 1; } return 0; } @Override public boolean equals(Object p) { Point t = (Point) p; return this.x == t.x; } } class PointY implements Comparable { int a; int b; public int compareTo(Object p) { PointY t = (PointY) p; if (this.a > t.a) { return -1; } if (this.a < t.a) { return 1; } return 0; } } class FastPrintStream implements AutoCloseable { private static final int BUF_SIZE = 1 << 15; private final byte[] buf = new byte[BUF_SIZE]; private int ptr = 0; private final java.lang.reflect.Field strField; private final java.nio.charset.CharsetEncoder encoder; private java.io.OutputStream out; public FastPrintStream(java.io.OutputStream out) { this.out = out; java.lang.reflect.Field f; try { f = java.lang.String.class.getDeclaredField("value"); f.setAccessible(true); } catch (NoSuchFieldException | SecurityException e) { f = null; } this.strField = f; this.encoder = java.nio.charset.StandardCharsets.US_ASCII.newEncoder(); } public FastPrintStream(java.io.File file) throws java.io.IOException { this(new java.io.FileOutputStream(file)); } public FastPrintStream(java.lang.String filename) throws java.io.IOException { this(new java.io.File(filename)); } public FastPrintStream() { this(System.out); try { java.lang.reflect.Field f = java.io.PrintStream.class.getDeclaredField("autoFlush"); f.setAccessible(true); f.set(System.out, false); } catch (IllegalAccessException | IllegalArgumentException | NoSuchFieldException e) { // ignore } } public FastPrintStream println() { if (ptr == BUF_SIZE) internalFlush(); buf[ptr++] = (byte) '\n'; return this; } public FastPrintStream println(java.lang.Object o) { return print(o).println(); } public FastPrintStream println(java.lang.String s) { return print(s).println(); } public FastPrintStream println(char[] s) { return print(s).println(); } public FastPrintStream println(char c) { return print(c).println(); } public FastPrintStream println(int x) { return print(x).println(); } public FastPrintStream println(long x) { return print(x).println(); } public FastPrintStream println(double d, int precision) { return print(d, precision).println(); } private FastPrintStream print(byte[] bytes) { int n = bytes.length; if (ptr + n > BUF_SIZE) { internalFlush(); try { out.write(bytes); } catch (java.io.IOException e) { throw new RuntimeException(); } } else { System.arraycopy(bytes, 0, buf, ptr, n); ptr += n; } return this; } public FastPrintStream print(java.lang.Object o) { return print(o.toString()); } public FastPrintStream print(java.lang.String s) { if (strField == null) { return print(s.getBytes()); } else { try { return print((byte[]) strField.get(s)); } catch (IllegalAccessException e) { return print(s.getBytes()); } } } public FastPrintStream print(char[] s) { try { return print(encoder.encode(java.nio.CharBuffer.wrap(s)).array()); } catch (java.nio.charset.CharacterCodingException e) { byte[] bytes = new byte[s.length]; for (int i = 0; i < s.length; i++) { bytes[i] = (byte) s[i]; } return print(bytes); } } public FastPrintStream print(char c) { if (ptr == BUF_SIZE) internalFlush(); buf[ptr++] = (byte) c; return this; } public FastPrintStream print(int x) { if (x == 0) { if (ptr == BUF_SIZE) internalFlush(); buf[ptr++] = '0'; return this; } int d = len(x); if (ptr + d > BUF_SIZE) internalFlush(); if (x < 0) { buf[ptr++] = '-'; x = -x; d--; } int j = ptr += d; while (x > 0) { buf[--j] = (byte) ('0' + (x % 10)); x /= 10; } return this; } public FastPrintStream print(long x) { if (x == 0) { if (ptr == BUF_SIZE) internalFlush(); buf[ptr++] = '0'; return this; } int d = len(x); if (ptr + d > BUF_SIZE) internalFlush(); if (x < 0) { buf[ptr++] = '-'; x = -x; d--; } int j = ptr += d; while (x > 0) { buf[--j] = (byte) ('0' + (x % 10)); x /= 10; } return this; } public FastPrintStream print(double d, int precision) { if (d < 0) { print('-'); d = -d; } d += Math.pow(10, -d) / 2; print((long) d).print('.'); d -= (long) d; for (int i = 0; i < precision; i++) { d *= 10; print((int) d); d -= (int) d; } return this; } private void internalFlush() { try { out.write(buf, 0, ptr); ptr = 0; } catch (java.io.IOException e) { throw new RuntimeException(e); } } public void flush() { try { out.write(buf, 0, ptr); out.flush(); ptr = 0; } catch (java.io.IOException e) { throw new RuntimeException(e); } } public void close() { try { out.close(); } catch (java.io.IOException e) { throw new RuntimeException(e); } } private static int len(int x) { int d = 1; if (x >= 0) { d = 0; x = -x; } int p = -10; for (int i = 1; i < 10; i++, p *= 10) if (x > p) return i + d; return 10 + d; } private static int len(long x) { int d = 1; if (x >= 0) { d = 0; x = -x; } long p = -10; for (int i = 1; i < 19; i++, p *= 10) if (x > p) return i + d; return 19 + d; } } class FastScanner implements AutoCloseable { private final java.io.InputStream in; private final byte[] buf = new byte[2048]; private int ptr = 0; private int buflen = 0; public FastScanner(java.io.InputStream in) { this.in = in; } public FastScanner() { this(System.in); } private boolean hasNextByte() { if (ptr < buflen) return true; ptr = 0; try { buflen = in.read(buf); } catch (java.io.IOException e) { throw new RuntimeException(e); } return buflen > 0; } private int readByte() { return hasNextByte() ? buf[ptr++] : -1; } public boolean hasNext() { while (hasNextByte() && !(32 < buf[ptr] && buf[ptr] < 127)) ptr++; return hasNextByte(); } private StringBuilder nextSequence() { if (!hasNext()) throw new java.util.NoSuchElementException(); StringBuilder sb = new StringBuilder(); for (int b = readByte(); 32 < b && b < 127; b = readByte()) { sb.appendCodePoint(b); } return sb; } public String next() { return nextSequence().toString(); } public String next(int len) { return new String(nextChars(len)); } public char nextChar() { if (!hasNextByte()) throw new java.util.NoSuchElementException(); return (char) readByte(); } public char[] nextChars() { StringBuilder sb = nextSequence(); int l = sb.length(); char[] dst = new char[l]; sb.getChars(0, l, dst, 0); return dst; } public char[] nextChars(int len) { if (!hasNext()) throw new java.util.NoSuchElementException(); char[] s = new char[len]; int i = 0; int b = readByte(); while (32 < b && b < 127 && i < len) { s[i++] = (char) b; b = readByte(); } if (i != len) { throw new java.util.NoSuchElementException( String.format("Next token has smaller length than expected.", len)); } return s; } public long nextLong() { if (!hasNext()) throw new java.util.NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) throw new NumberFormatException(); while (true) { if ('0' <= b && b <= '9') { n = n * 10 + b - '0'; } else if (b == -1 || !(32 < b && b < 127)) { return minus ? -n : n; } else throw new NumberFormatException(); b = readByte(); } } public int nextInt() { return Math.toIntExact(nextLong()); } public double nextDouble() { return Double.parseDouble(next()); } public void close() { try { in.close(); } catch (java.io.IOException e) { throw new RuntimeException(e); } } } /** * @verified https://atcoder.jp/contests/practice2/tasks/practice2_j */ class SegTree<S> { final int MAX; final int N; final java.util.function.BinaryOperator<S> op; final S E; final S[] data; @SuppressWarnings("unchecked") public SegTree(int n, java.util.function.BinaryOperator<S> op, S e) { this.MAX = n; int k = 1; while (k < n) k <<= 1; this.N = k; this.E = e; this.op = op; this.data = (S[]) new Object[N << 1]; java.util.Arrays.fill(data, E); } public SegTree(S[] dat, java.util.function.BinaryOperator<S> op, S e) { this(dat.length, op, e); build(dat); } private void build(S[] dat) { int l = dat.length; System.arraycopy(dat, 0, data, N, l); for (int i = N - 1; i > 0; i--) { data[i] = op.apply(data[i << 1 | 0], data[i << 1 | 1]); } } public void set(int p, S x) { exclusiveRangeCheck(p); data[p += N] = x; p >>= 1; while (p > 0) { data[p] = op.apply(data[p << 1 | 0], data[p << 1 | 1]); p >>= 1; } } public S get(int p) { exclusiveRangeCheck(p); return data[p + N]; } public S prod(int l, int r) { if (l > r) { throw new IllegalArgumentException(String.format("Invalid range: [%d, %d)", l, r)); } inclusiveRangeCheck(l); inclusiveRangeCheck(r); S sumLeft = E; S sumRight = E; l += N; r += N; while (l < r) { if ((l & 1) == 1) sumLeft = op.apply(sumLeft, data[l++]); if ((r & 1) == 1) sumRight = op.apply(data[--r], sumRight); l >>= 1; r >>= 1; } return op.apply(sumLeft, sumRight); } public S allProd() { return data[1]; } public int maxRight(int l, java.util.function.Predicate<S> f) { inclusiveRangeCheck(l); if (!f.test(E)) { throw new IllegalArgumentException("Identity element must satisfy the condition."); } if (l == MAX) return MAX; l += N; S sum = E; do { l >>= Long.numberOfTrailingZeros(l); if (!f.test(op.apply(sum, data[l]))) { while (l < N) { l = l << 1; if (f.test(op.apply(sum, data[l]))) { sum = op.apply(sum, data[l]); l++; } } return l - N; } sum = op.apply(sum, data[l]); l++; } while ((l & -l) != l); return MAX; } public int minLeft(int r, java.util.function.Predicate<S> f) { inclusiveRangeCheck(r); if (!f.test(E)) { throw new IllegalArgumentException("Identity element must satisfy the condition."); } if (r == 0) return 0; r += N; S sum = E; do { r--; while (r > 1 && (r & 1) == 1) r >>= 1; if (!f.test(op.apply(data[r], sum))) { while (r < N) { r = r << 1 | 1; if (f.test(op.apply(data[r], sum))) { sum = op.apply(data[r], sum); r--; } } return r + 1 - N; } sum = op.apply(data[r], sum); } while ((r & -r) != r); return 0; } private void exclusiveRangeCheck(int p) { if (p < 0 || p >= MAX) { throw new IndexOutOfBoundsException( String.format("Index %d out of bounds for the range [%d, %d).", p, 0, MAX)); } } private void inclusiveRangeCheck(int p) { if (p < 0 || p > MAX) { throw new IndexOutOfBoundsException( String.format("Index %d out of bounds for the range [%d, %d].", p, 0, MAX)); } } // **************** DEBUG **************** // private int indent = 6; public void setIndent(int newIndent) { this.indent = newIndent; } @Override public String toString() { return toSimpleString(); } public String toDetailedString() { return toDetailedString(1, 0); } private String toDetailedString(int k, int sp) { if (k >= N) return indent(sp) + data[k]; String s = ""; s += toDetailedString(k << 1 | 1, sp + indent); s += "\n"; s += indent(sp) + data[k]; s += "\n"; s += toDetailedString(k << 1 | 0, sp + indent); return s; } private static String indent(int n) { StringBuilder sb = new StringBuilder(); while (n-- > 0) sb.append(' '); return sb.toString(); } public String toSimpleString() { StringBuilder sb = new StringBuilder(); sb.append('['); for (int i = 0; i < N; i++) { sb.append(data[i + N]); if (i < N - 1) sb.append(',').append(' '); } sb.append(']'); return sb.toString(); } } class DSU { private int n; private int[] parentOrSize; public DSU(int n) { this.n = n; this.parentOrSize = new int[n]; Arrays.fill(parentOrSize, -1); } int merge(int a, int b) { if (!(0 <= a && a < n) || !(0 <= b && b < n)) { return -1; } int x = leader(a); int y = leader(b); if (x == y) return x; if (-parentOrSize[x] < -parentOrSize[y]) { int tmp = x; x = y; y = tmp; } parentOrSize[x] += parentOrSize[y]; parentOrSize[y] = x; return x; } boolean same(int a, int b) { if (!(0 <= a && a < n) || !(0 <= b && b < n)) { return false; } return leader(a) == leader(b); } int leader(int a) { if (parentOrSize[a] < 0) { return a; } else { parentOrSize[a] = leader(parentOrSize[a]); return parentOrSize[a]; } } int size(int a) { if (!(0 <= a && a < n)) { return -1; } return -parentOrSize[leader(a)]; } ArrayList<ArrayList<Integer>> groups() { int[] leaderBuf = new int[n]; int[] groupSize = new int[n]; for (int i = 0; i < n; i++) { leaderBuf[i] = leader(i); groupSize[leaderBuf[i]]++; } ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); for (int i = 0; i < n; i++) { result.add(new ArrayList<>()); } for (int i = 0; i < n; i++) { result.get(leaderBuf[i]).add(i); } return result; } }
ConDefects/ConDefects/Code/arc142_c/Java/32607087
condefects-java_data_305
import java.util.*; import java.lang.*; import java.io.*; public class Main { public static FastScanner sc = new FastScanner(System.in); public static void main(String[] args){ // 入力 int N = ini(); int ans = 0; // 最短距離、距離3リストを作成 int minD = Integer.MAX_VALUE; List<Integer> dist3List = new ArrayList<>(); boolean failFlg = false; for(int i = 3; i <= N; i++){ int dist1 = question(1,i); if(dist1 == -1){ failFlg = true; break; } int dist2 = question(2,i); if(dist1 == -1){ failFlg = true; break; } int dist = dist1 + dist2; minD = Math.min(minD, dist); if(dist == 3){ dist3List.add(i); } } if(!failFlg){ if(minD != 3){ ans = minD; }else{ if(dist3List.size() != 2){ ans = 1; }else{ int u = dist3List.get(0); int v = dist3List.get(0); int dist = question(u,v); if(dist == 1){ ans = 3; } else if(dist > 1){ ans = 1; } } } } // 出力 System.out.println("! " + ans); } // 質問する関数 public static int question(int u, int v){ System.out.println("? " + u + " " + v); int res = ini(); return res; } // 関数 public static void name(){ } // インプット関数 public static int ini(){ return sc.nextInt(); } public static long inl(){ return sc.nextLong(); } public static double ind(){ return sc.nextDouble(); } public static String ins(){ return sc.next(); } public static int[] inia(int N){ int[] A = new int[N]; for(int i = 0; i < N; i++){ A[i] = ini(); } return A; } public static void inia(int[] A, int[] B){ int n = A.length; for(int i = 0; i < n; i++){ A[i] = ini(); B[i] = ini(); } } public static void inia(int[] A, int[] B, int[] C){ int n = A.length; for(int i = 0; i < n; i++){ A[i] = ini(); B[i] = ini(); C[i] = ini(); } } public static long[] inla(int N){ long[] A = new long[N]; for(int i = 0; i < N; i++){ A[i] = inl(); } return A; } public static void inla(long[] A, long[] B){ int n = A.length; for(int i = 0; i < n; i++){ A[i] = inl(); B[i] = inl(); } } public static void inla(long[] A, long[] B, long[] C){ int n = A.length; for(int i = 0; i < n; i++){ A[i] = inl(); B[i] = inl(); C[i] = inl(); } } public static double[] inda(int N){ double[] A = new double[N]; for(int i = 0; i < N; i++){ A[i] = ind(); } return A; } public static void inda(double[] A, double[] B){ int n = A.length; for(int i = 0; i < n; i++){ A[i] = ind(); B[i] = ind(); } } public static void inda(double[] A, double[] B, double[] C){ int n = A.length; for(int i = 0; i < n; i++){ A[i] = ind(); B[i] = ind(); C[i] = ind(); } } public static String[] insa(int N){ String[] A = new String[N]; for(int i = 0; i < N; i++){ A[i] = ins(); } return A; } public static void insa(String[] A, String[] B){ int n = A.length; for(int i = 0; i < n; i++){ A[i] = ins(); B[i] = ins(); } } public static void insa(String[] A, String[] B, String[] C){ int n = A.length; for(int i = 0; i < n; i++){ A[i] = ins(); B[i] = ins(); C[i] = ins(); } } public static char[] inca(){ return ins().toCharArray(); } // 高速スキャナー static class FastScanner { private BufferedReader reader = null; private StringTokenizer tokenizer = null; public FastScanner(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); tokenizer = null; } public String next() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public String nextLine() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken("\n"); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } } } import java.util.*; import java.lang.*; import java.io.*; public class Main { public static FastScanner sc = new FastScanner(System.in); public static void main(String[] args){ // 入力 int N = ini(); int ans = 0; // 最短距離、距離3リストを作成 int minD = Integer.MAX_VALUE; List<Integer> dist3List = new ArrayList<>(); boolean failFlg = false; for(int i = 3; i <= N; i++){ int dist1 = question(1,i); if(dist1 == -1){ failFlg = true; break; } int dist2 = question(2,i); if(dist1 == -1){ failFlg = true; break; } int dist = dist1 + dist2; minD = Math.min(minD, dist); if(dist == 3){ dist3List.add(i); } } if(!failFlg){ if(minD != 3){ ans = minD; }else{ if(dist3List.size() != 2){ ans = 1; }else{ int u = dist3List.get(0); int v = dist3List.get(1); int dist = question(u,v); if(dist == 1){ ans = 3; } else if(dist > 1){ ans = 1; } } } } // 出力 System.out.println("! " + ans); } // 質問する関数 public static int question(int u, int v){ System.out.println("? " + u + " " + v); int res = ini(); return res; } // 関数 public static void name(){ } // インプット関数 public static int ini(){ return sc.nextInt(); } public static long inl(){ return sc.nextLong(); } public static double ind(){ return sc.nextDouble(); } public static String ins(){ return sc.next(); } public static int[] inia(int N){ int[] A = new int[N]; for(int i = 0; i < N; i++){ A[i] = ini(); } return A; } public static void inia(int[] A, int[] B){ int n = A.length; for(int i = 0; i < n; i++){ A[i] = ini(); B[i] = ini(); } } public static void inia(int[] A, int[] B, int[] C){ int n = A.length; for(int i = 0; i < n; i++){ A[i] = ini(); B[i] = ini(); C[i] = ini(); } } public static long[] inla(int N){ long[] A = new long[N]; for(int i = 0; i < N; i++){ A[i] = inl(); } return A; } public static void inla(long[] A, long[] B){ int n = A.length; for(int i = 0; i < n; i++){ A[i] = inl(); B[i] = inl(); } } public static void inla(long[] A, long[] B, long[] C){ int n = A.length; for(int i = 0; i < n; i++){ A[i] = inl(); B[i] = inl(); C[i] = inl(); } } public static double[] inda(int N){ double[] A = new double[N]; for(int i = 0; i < N; i++){ A[i] = ind(); } return A; } public static void inda(double[] A, double[] B){ int n = A.length; for(int i = 0; i < n; i++){ A[i] = ind(); B[i] = ind(); } } public static void inda(double[] A, double[] B, double[] C){ int n = A.length; for(int i = 0; i < n; i++){ A[i] = ind(); B[i] = ind(); C[i] = ind(); } } public static String[] insa(int N){ String[] A = new String[N]; for(int i = 0; i < N; i++){ A[i] = ins(); } return A; } public static void insa(String[] A, String[] B){ int n = A.length; for(int i = 0; i < n; i++){ A[i] = ins(); B[i] = ins(); } } public static void insa(String[] A, String[] B, String[] C){ int n = A.length; for(int i = 0; i < n; i++){ A[i] = ins(); B[i] = ins(); C[i] = ins(); } } public static char[] inca(){ return ins().toCharArray(); } // 高速スキャナー static class FastScanner { private BufferedReader reader = null; private StringTokenizer tokenizer = null; public FastScanner(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); tokenizer = null; } public String next() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public String nextLine() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken("\n"); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } } }
ConDefects/ConDefects/Code/arc142_c/Java/32619864
condefects-java_data_306
import java.io.*; import java.util.*; public class Main { static Scanner sc; static PrintWriter out; public static void main(String[] args) { sc = new Scanner(System.in); out = new PrintWriter(System.out); new Main().solve(); out.flush(); } public void solve() { int n = sc.nextInt(); int[][] d = new int[n][2]; boolean notd1 = false; for(int i=2; i<n; i++) { for(int j=0; j<2; j++) { out.printf("? %d %d\n", j + 1, i + 1); out.flush(); d[i][j] = sc.nextInt(); } if(Math.abs(d[i][0]-d[i][1]) != 1) notd1 = true; } if(!notd1) { if(n == 4) { out.println("? 3 4"); out.flush(); int d34 = sc.nextInt(); if(d34 == 1) { out.println("! 3"); return; } } out.println("! 1"); return; } int res = n-1; for(int i=2; i<n; i++) { res = Math.min(res, d[i][0]+d[i][1]); } out.println("! " + res); } } import java.io.*; import java.util.*; public class Main { static Scanner sc; static PrintWriter out; public static void main(String[] args) { sc = new Scanner(System.in); out = new PrintWriter(System.out); new Main().solve(); out.flush(); } public void solve() { int n = sc.nextInt(); int[][] d = new int[n][2]; boolean notd1 = false; for(int i=2; i<n; i++) { for(int j=0; j<2; j++) { out.printf("? %d %d\n", j + 1, i + 1); out.flush(); d[i][j] = sc.nextInt(); } if(Math.abs(d[i][0]-d[i][1]) != 1) notd1 = true; } if(!notd1) { if(n == 4) { out.println("? 3 4"); out.flush(); int d34 = sc.nextInt(); if(d34 == 1 && d[2][0]+d[2][1] == d[3][0]+d[3][1]) { out.println("! 3"); return; } } out.println("! 1"); return; } int res = n-1; for(int i=2; i<n; i++) { res = Math.min(res, d[i][0]+d[i][1]); } out.println("! " + res); } }
ConDefects/ConDefects/Code/arc142_c/Java/32611677
condefects-java_data_307
import java.util.*; import java.io.*; public class Main { static ContestScanner sc = new ContestScanner(System.in); static PrintWriter pw = new PrintWriter(System.out); static StringBuilder sb = new StringBuilder(); static long mod = (long) 1e9 + 7; public static void main(String[] args) throws Exception { solve(); pw.flush(); } public static void solve() { int N = sc.nextInt(); int min = Integer.MAX_VALUE; ArrayList<Integer> arr = new ArrayList<>(); for(int i = 3; i <= N; i++){ int v1 = quest(1,i); int v2 = quest(2,i); if(min > v1+v2){ min = v1+v2; arr.clear(); arr.add(i); }else{ arr.add(i); } } if(min != 3){ answer(min); }else{ if(arr.size() != 2){ answer(1); }else{ int l = quest(arr.get(0),arr.get(1)); if(l == 1){ answer(3); }else{ answer(1); } } } } public static int quest(int v1, int v2){ System.out.println("? " + v1 + " " + v2); return sc.nextInt(); } public static void answer(int v){ System.out.println("! " + v); } static class GeekInteger { public static void save_sort(int[] array) { shuffle(array); Arrays.sort(array); } public static int[] shuffle(int[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); int randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } public static void save_sort(long[] array) { shuffle(array); Arrays.sort(array); } public static long[] shuffle(long[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); long randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } } } /** * refercence : https://github.com/NASU41/AtCoderLibraryForJava/blob/master/ContestIO/ContestScanner.java */ class ContestScanner { private final java.io.InputStream in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private static final long LONG_MAX_TENTHS = 922337203685477580L; private static final int LONG_MAX_LAST_DIGIT = 7; private static final int LONG_MIN_LAST_DIGIT = 8; public ContestScanner(java.io.InputStream in){ this.in = in; } public ContestScanner(java.io.File file) throws java.io.FileNotFoundException { this(new java.io.BufferedInputStream(new java.io.FileInputStream(file))); } public ContestScanner(){ this(System.in); } private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (java.io.IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } public String next() { if (!hasNext()) throw new java.util.NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new java.util.NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { int digit = b - '0'; if (n >= LONG_MAX_TENTHS) { if (n == LONG_MAX_TENTHS) { if (minus) { if (digit <= LONG_MIN_LAST_DIGIT) { n = -n * 10 - digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString(b)) ); } } } else { if (digit <= LONG_MAX_LAST_DIGIT) { n = n * 10 + digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString(b)) ); } } } } throw new ArithmeticException( String.format("%s%d%d... overflows long.", minus ? "-" : "", n, digit) ); } n = n * 10 + digit; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } public long[] nextLongArray(int length){ long[] array = new long[length]; for(int i=0; i<length; i++) array[i] = this.nextLong(); return array; } public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map){ long[] array = new long[length]; for(int i=0; i<length; i++) array[i] = map.applyAsLong(this.nextLong()); return array; } public int[] nextIntArray(int length){ int[] array = new int[length]; for(int i=0; i<length; i++) array[i] = this.nextInt(); return array; } public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map){ int[] array = new int[length]; for(int i=0; i<length; i++) array[i] = map.applyAsInt(this.nextInt()); return array; } public double[] nextDoubleArray(int length){ double[] array = new double[length]; for(int i=0; i<length; i++) array[i] = this.nextDouble(); return array; } public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map){ double[] array = new double[length]; for(int i=0; i<length; i++) array[i] = map.applyAsDouble(this.nextDouble()); return array; } public long[][] nextLongMatrix(int height, int width){ long[][] mat = new long[height][width]; for(int h=0; h<height; h++) for(int w=0; w<width; w++){ mat[h][w] = this.nextLong(); } return mat; } public int[][] nextIntMatrix(int height, int width){ int[][] mat = new int[height][width]; for(int h=0; h<height; h++) for(int w=0; w<width; w++){ mat[h][w] = this.nextInt(); } return mat; } public double[][] nextDoubleMatrix(int height, int width){ double[][] mat = new double[height][width]; for(int h=0; h<height; h++) for(int w=0; w<width; w++){ mat[h][w] = this.nextDouble(); } return mat; } public char[][] nextCharMatrix(int height, int width){ char[][] mat = new char[height][width]; for(int h=0; h<height; h++){ String s = this.next(); for(int w=0; w<width; w++){ mat[h][w] = s.charAt(w); } } return mat; } } import java.util.*; import java.io.*; public class Main { static ContestScanner sc = new ContestScanner(System.in); static PrintWriter pw = new PrintWriter(System.out); static StringBuilder sb = new StringBuilder(); static long mod = (long) 1e9 + 7; public static void main(String[] args) throws Exception { solve(); pw.flush(); } public static void solve() { int N = sc.nextInt(); int min = Integer.MAX_VALUE; ArrayList<Integer> arr = new ArrayList<>(); for(int i = 3; i <= N; i++){ int v1 = quest(1,i); int v2 = quest(2,i); if(min > v1+v2){ min = v1+v2; arr.clear(); arr.add(i); }else if(min == v1+v2){ arr.add(i); } } if(min != 3){ answer(min); }else{ if(arr.size() != 2){ answer(1); }else{ int l = quest(arr.get(0),arr.get(1)); if(l == 1){ answer(3); }else{ answer(1); } } } } public static int quest(int v1, int v2){ System.out.println("? " + v1 + " " + v2); return sc.nextInt(); } public static void answer(int v){ System.out.println("! " + v); } static class GeekInteger { public static void save_sort(int[] array) { shuffle(array); Arrays.sort(array); } public static int[] shuffle(int[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); int randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } public static void save_sort(long[] array) { shuffle(array); Arrays.sort(array); } public static long[] shuffle(long[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); long randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } } } /** * refercence : https://github.com/NASU41/AtCoderLibraryForJava/blob/master/ContestIO/ContestScanner.java */ class ContestScanner { private final java.io.InputStream in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private static final long LONG_MAX_TENTHS = 922337203685477580L; private static final int LONG_MAX_LAST_DIGIT = 7; private static final int LONG_MIN_LAST_DIGIT = 8; public ContestScanner(java.io.InputStream in){ this.in = in; } public ContestScanner(java.io.File file) throws java.io.FileNotFoundException { this(new java.io.BufferedInputStream(new java.io.FileInputStream(file))); } public ContestScanner(){ this(System.in); } private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (java.io.IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } public String next() { if (!hasNext()) throw new java.util.NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new java.util.NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { int digit = b - '0'; if (n >= LONG_MAX_TENTHS) { if (n == LONG_MAX_TENTHS) { if (minus) { if (digit <= LONG_MIN_LAST_DIGIT) { n = -n * 10 - digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString(b)) ); } } } else { if (digit <= LONG_MAX_LAST_DIGIT) { n = n * 10 + digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString(b)) ); } } } } throw new ArithmeticException( String.format("%s%d%d... overflows long.", minus ? "-" : "", n, digit) ); } n = n * 10 + digit; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } public long[] nextLongArray(int length){ long[] array = new long[length]; for(int i=0; i<length; i++) array[i] = this.nextLong(); return array; } public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map){ long[] array = new long[length]; for(int i=0; i<length; i++) array[i] = map.applyAsLong(this.nextLong()); return array; } public int[] nextIntArray(int length){ int[] array = new int[length]; for(int i=0; i<length; i++) array[i] = this.nextInt(); return array; } public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map){ int[] array = new int[length]; for(int i=0; i<length; i++) array[i] = map.applyAsInt(this.nextInt()); return array; } public double[] nextDoubleArray(int length){ double[] array = new double[length]; for(int i=0; i<length; i++) array[i] = this.nextDouble(); return array; } public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map){ double[] array = new double[length]; for(int i=0; i<length; i++) array[i] = map.applyAsDouble(this.nextDouble()); return array; } public long[][] nextLongMatrix(int height, int width){ long[][] mat = new long[height][width]; for(int h=0; h<height; h++) for(int w=0; w<width; w++){ mat[h][w] = this.nextLong(); } return mat; } public int[][] nextIntMatrix(int height, int width){ int[][] mat = new int[height][width]; for(int h=0; h<height; h++) for(int w=0; w<width; w++){ mat[h][w] = this.nextInt(); } return mat; } public double[][] nextDoubleMatrix(int height, int width){ double[][] mat = new double[height][width]; for(int h=0; h<height; h++) for(int w=0; w<width; w++){ mat[h][w] = this.nextDouble(); } return mat; } public char[][] nextCharMatrix(int height, int width){ char[][] mat = new char[height][width]; for(int h=0; h<height; h++){ String s = this.next(); for(int w=0; w<width; w++){ mat[h][w] = s.charAt(w); } } return mat; } }
ConDefects/ConDefects/Code/arc142_c/Java/32637633
condefects-java_data_308
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws java.lang.Exception { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Solution solver = new Solution(); boolean isTest = false; int tC = isTest ? Integer.parseInt(in.next()) : 1; for (int i = 1; i <= tC; i++) solver.solve(in, out, i); out.close(); } /* ............................................................. */ static class Solution { InputReader in; PrintWriter out; int ask(int u,int v) { out.println("? "+u+" "+v); out.flush(); return ni(); } public void solve(InputReader in, PrintWriter out, int test) { this.in = in; this.out = out; int n=ni(); int root[]=new int[n]; int ver[]=new int[n]; for(int i=2;i<n;i++) { root[i]=ask(1,i+1); ver[i]=ask(2, i+1); } Set<Integer> set=new HashSet<>(); int q=-1; int min=1000000000; for(int i=2;i<n;i++) { if(ver[i]==1) { set.add(root[i]); q=i+1; min=Math.min(min, root[i]); } } //out.print("! "); if(set.size()==0) { out.print("! "); pn(0); } else if(set.size()==1){ boolean flag=false; for(int i=2;i<n;i++) { if(root[i]==1) { int x=ask(q, i+1); if(x>ver[i]) { out.print("! "); pn(min-1); } else { out.print("! "); pn(min+1); } flag=true; break; } } if(!flag) { out.print("! "); pn(min-1); } } else { out.print("! "); pn(min+1); } out.flush(); } class Pair { int x; int y; long w; Pair(int x, int y, long w) { this.x = x; this.y = y; this.w = w; } } char[] n() { return in.next().toCharArray(); } int ni() { return in.nextInt(); } long nl() { return in.nextLong(); } long[] nal(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } void pn(long zx) { out.println(zx); } void pn(String sz) { out.println(sz); } void pn(double dx) { out.println(dx); } void pn(long ar[]) { for (int i = 0; i < ar.length; i++) out.print(ar[i] + " "); out.println(); } void pn(String ar[]) { for (int i = 0; i < ar.length; i++) out.println(ar[i]); } } /* ......................Just Input............................. */ static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } /* ......................Just Input............................. */ } import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws java.lang.Exception { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Solution solver = new Solution(); boolean isTest = false; int tC = isTest ? Integer.parseInt(in.next()) : 1; for (int i = 1; i <= tC; i++) solver.solve(in, out, i); out.close(); } /* ............................................................. */ static class Solution { InputReader in; PrintWriter out; int ask(int u,int v) { out.println("? "+u+" "+v); out.flush(); return ni(); } public void solve(InputReader in, PrintWriter out, int test) { this.in = in; this.out = out; int n=ni(); int root[]=new int[n]; int ver[]=new int[n]; for(int i=2;i<n;i++) { root[i]=ask(1,i+1); ver[i]=ask(2, i+1); } Set<Integer> set=new HashSet<>(); int q=-1; int min=1000000000; for(int i=2;i<n;i++) { if(ver[i]==1) { set.add(root[i]); q=i+1; min=Math.min(min, root[i]); } } //out.print("! "); if(set.size()==0) { out.print("! "); pn(1); } else if(set.size()==1){ boolean flag=false; for(int i=2;i<n;i++) { if(root[i]==1) { int x=ask(q, i+1); if(x>ver[i]) { out.print("! "); pn(min-1); } else { out.print("! "); pn(min+1); } flag=true; break; } } if(!flag) { out.print("! "); pn(min-1); } } else { out.print("! "); pn(min+1); } out.flush(); } class Pair { int x; int y; long w; Pair(int x, int y, long w) { this.x = x; this.y = y; this.w = w; } } char[] n() { return in.next().toCharArray(); } int ni() { return in.nextInt(); } long nl() { return in.nextLong(); } long[] nal(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } void pn(long zx) { out.println(zx); } void pn(String sz) { out.println(sz); } void pn(double dx) { out.println(dx); } void pn(long ar[]) { for (int i = 0; i < ar.length; i++) out.print(ar[i] + " "); out.println(); } void pn(String ar[]) { for (int i = 0; i < ar.length; i++) out.println(ar[i]); } } /* ......................Just Input............................. */ static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } /* ......................Just Input............................. */ }
ConDefects/ConDefects/Code/arc142_c/Java/32607028
condefects-java_data_309
import java.util.*; import java.time.*; import java.io.*; import java.math.*; public class Main { public static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); public static BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out)); public static long ret; public static int reti,rd; public static boolean neg; public static final int mod=998244353; public static final int mod2=1_000_000_007; public static Useful us=new Useful(mod2); public static ArrayList<ArrayList<Pii>> r=new ArrayList<>(); public static ArrayList<ArrayList<Integer>> to = new ArrayList<>(); public static void main(String[] args) throws Exception{ final int n=readint(); int[][] dist=new int[n+5][3]; us.arr(dist,1<<28); for(int i=1;i<=2;i++) { for(int j=3;j<=n;j++) { System.out.println("? "+i+" "+j); System.out.flush(); dist[j][i]=readint(); } } int ans=(1<<30); ArrayList<Integer> c1=new ArrayList<>(); ArrayList<Integer> c2=new ArrayList<>(); int de=0; for(int i=3;i<=n;i++) { ans=Math.min(dist[i][1]+dist[i][2],ans); if(dist[i][1]>dist[i][2]) { c2.add(i); } if(dist[i][1]<dist[i][2]) { c1.add(i); } } if((c1.isEmpty()||c2.isEmpty())&&n!=3) { int mux=1; for(int i=3;i<=n;i++) { mux=Math.max(mux,dist[3][1]-dist[3][2]); } ans=Math.min(mux,ans); } else if(c1.size()+c2.size()==n-2) { int l=c1.get(0); int r=c2.get(0); int y; System.out.println("? "+l+" "+r); System.out.flush(); y=readint(); if(y<dist[l][1]+dist[r][2]) { ans=Math.min(Math.abs(y+dist[l][1]+dist[r][2]),ans); } else { ans=Math.min(Math.max(y-dist[l][1]-dist[r][2],dist[l][2]+dist[r][1]-y),ans); } } //System.out.println(ans); System.out.println("! "+ans); } public static int readint() throws Exception{ reti=0; neg=false; while(rd<48||rd>57) { rd=br.read(); if(rd=='-') { neg=true; } } while(rd>47&&rd<58) { reti*=10; reti+=(rd&15); rd=br.read(); } if(neg)reti*=-1; return reti; } public static long readlong() throws Exception{ ret=0; neg=false; while(rd<48||rd>57) { rd=br.read(); if(rd=='-') { neg=true; } } while(rd>47&&rd<58) { ret*=10; ret+=(rd&15); rd=br.read(); } if(neg)ret*=-1; return ret; } } /* */ /* */ class Pii{ int x; long y; Pii(int a,long b){ x=a; y=b; } } class Pll{ long x,y; Pll(long a,long b){ x=a; y=b; } } class Useful{ long mod; Useful(long m){mod=m;} void al(ArrayList<ArrayList<Integer>> a,int n){for(int i=0;i<n;i++) {a.add(new ArrayList<Integer>());}} void arr(int[] a,int init) {for(int i=0;i<a.length;i++) {a[i]=init;}} void arr(long[] a,long init) {for(int i=0;i<a.length;i++) {a[i]=init;}} void arr(int[][] a,int init) {for(int i=0;i<a.length;i++) {for(int j=0;j<a[i].length;j++) {a[i][j]=init;}}} void arr(long[][] a,long init) {for(int i=0;i<a.length;i++) {for(int j=0;j<a[i].length;j++) {a[i][j]=init;}}} void arr(int[][][] a,int init) {for(int i=0;i<a.length;i++) {for(int j=0;j<a[i].length;j++) {for(int k=0;k<a[i][j].length;k++) {a[i][j][k]=init;}}}} void arr(long[][][] a,long init) {for(int i=0;i<a.length;i++) {for(int j=0;j<a[i].length;j++) {for(int k=0;k<a[i][j].length;k++) {a[i][j][k]=init;}}}} void arr(int[][][][] a,int init) {for(int i=0;i<a.length;i++) {for(int j=0;j<a[i].length;j++) {for(int k=0;k<a[i][j].length;k++) {Arrays.fill(a[i][j][k],init);}}}} void arr(long[][][][] a,long init) {for(int i=0;i<a.length;i++) {for(int j=0;j<a[i].length;j++) {for(int k=0;k<a[i][j].length;k++) {Arrays.fill(a[i][j][k],init);}}}} long fast(long x,long pw) { if(pw<=0)return 1; if(pw==1)return x; long h=fast(x,pw>>1); if((pw&1)==0) { return h*h%mod; } return h*h%mod*x%mod; } long[][] mul(long[][] a,long[][] b){ long[][] c=new long[a.length][b[0].length]; for(int i=0;i<a.length;i++) { for(int j=0;j<b[0].length;j++) { for(int k=0;k<a[0].length;k++) { c[i][j]+=a[i][k]*b[k][j]; c[i][j]%=mod; } } } return c; } long[][] fast(long[][] x,int pw){ if(pw==1)return x; long[][] h=fast(x,pw>>1); if((pw&1)==0) { return mul(h,h); } else { return mul(mul(h,h),x); } } int gcd(int a,int b) { if(a==0)return b; if(b==0)return a; return gcd(b,a%b); } long gcd(long a,long b) { if(a==0)return b; if(b==0)return a; return gcd(b,a%b); } long lcm(long a, long b){ return a*(b/gcd(a,b)); } double log2(int x) { return (Math.log(x)/Math.log(2)); } double log2(long x) { return (Math.log(x)/Math.log(2)); } } import java.util.*; import java.time.*; import java.io.*; import java.math.*; public class Main { public static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); public static BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out)); public static long ret; public static int reti,rd; public static boolean neg; public static final int mod=998244353; public static final int mod2=1_000_000_007; public static Useful us=new Useful(mod2); public static ArrayList<ArrayList<Pii>> r=new ArrayList<>(); public static ArrayList<ArrayList<Integer>> to = new ArrayList<>(); public static void main(String[] args) throws Exception{ final int n=readint(); int[][] dist=new int[n+5][3]; us.arr(dist,1<<28); for(int i=1;i<=2;i++) { for(int j=3;j<=n;j++) { System.out.println("? "+i+" "+j); System.out.flush(); dist[j][i]=readint(); } } int ans=(1<<30); ArrayList<Integer> c1=new ArrayList<>(); ArrayList<Integer> c2=new ArrayList<>(); int de=0; for(int i=3;i<=n;i++) { ans=Math.min(dist[i][1]+dist[i][2],ans); if(dist[i][1]>dist[i][2]) { c2.add(i); } if(dist[i][1]<dist[i][2]) { c1.add(i); } } if((c1.isEmpty()||c2.isEmpty())&&n!=3) { int mux=1; for(int i=3;i<=n;i++) { mux=Math.max(mux,dist[i][1]-dist[i][2]); } ans=Math.min(mux,ans); } else if(c1.size()+c2.size()==n-2) { int l=c1.get(0); int r=c2.get(0); int y; System.out.println("? "+l+" "+r); System.out.flush(); y=readint(); if(y<dist[l][1]+dist[r][2]) { ans=Math.min(Math.abs(y+dist[l][1]+dist[r][2]),ans); } else { ans=Math.min(Math.max(y-dist[l][1]-dist[r][2],dist[l][2]+dist[r][1]-y),ans); } } //System.out.println(ans); System.out.println("! "+ans); } public static int readint() throws Exception{ reti=0; neg=false; while(rd<48||rd>57) { rd=br.read(); if(rd=='-') { neg=true; } } while(rd>47&&rd<58) { reti*=10; reti+=(rd&15); rd=br.read(); } if(neg)reti*=-1; return reti; } public static long readlong() throws Exception{ ret=0; neg=false; while(rd<48||rd>57) { rd=br.read(); if(rd=='-') { neg=true; } } while(rd>47&&rd<58) { ret*=10; ret+=(rd&15); rd=br.read(); } if(neg)ret*=-1; return ret; } } /* */ /* */ class Pii{ int x; long y; Pii(int a,long b){ x=a; y=b; } } class Pll{ long x,y; Pll(long a,long b){ x=a; y=b; } } class Useful{ long mod; Useful(long m){mod=m;} void al(ArrayList<ArrayList<Integer>> a,int n){for(int i=0;i<n;i++) {a.add(new ArrayList<Integer>());}} void arr(int[] a,int init) {for(int i=0;i<a.length;i++) {a[i]=init;}} void arr(long[] a,long init) {for(int i=0;i<a.length;i++) {a[i]=init;}} void arr(int[][] a,int init) {for(int i=0;i<a.length;i++) {for(int j=0;j<a[i].length;j++) {a[i][j]=init;}}} void arr(long[][] a,long init) {for(int i=0;i<a.length;i++) {for(int j=0;j<a[i].length;j++) {a[i][j]=init;}}} void arr(int[][][] a,int init) {for(int i=0;i<a.length;i++) {for(int j=0;j<a[i].length;j++) {for(int k=0;k<a[i][j].length;k++) {a[i][j][k]=init;}}}} void arr(long[][][] a,long init) {for(int i=0;i<a.length;i++) {for(int j=0;j<a[i].length;j++) {for(int k=0;k<a[i][j].length;k++) {a[i][j][k]=init;}}}} void arr(int[][][][] a,int init) {for(int i=0;i<a.length;i++) {for(int j=0;j<a[i].length;j++) {for(int k=0;k<a[i][j].length;k++) {Arrays.fill(a[i][j][k],init);}}}} void arr(long[][][][] a,long init) {for(int i=0;i<a.length;i++) {for(int j=0;j<a[i].length;j++) {for(int k=0;k<a[i][j].length;k++) {Arrays.fill(a[i][j][k],init);}}}} long fast(long x,long pw) { if(pw<=0)return 1; if(pw==1)return x; long h=fast(x,pw>>1); if((pw&1)==0) { return h*h%mod; } return h*h%mod*x%mod; } long[][] mul(long[][] a,long[][] b){ long[][] c=new long[a.length][b[0].length]; for(int i=0;i<a.length;i++) { for(int j=0;j<b[0].length;j++) { for(int k=0;k<a[0].length;k++) { c[i][j]+=a[i][k]*b[k][j]; c[i][j]%=mod; } } } return c; } long[][] fast(long[][] x,int pw){ if(pw==1)return x; long[][] h=fast(x,pw>>1); if((pw&1)==0) { return mul(h,h); } else { return mul(mul(h,h),x); } } int gcd(int a,int b) { if(a==0)return b; if(b==0)return a; return gcd(b,a%b); } long gcd(long a,long b) { if(a==0)return b; if(b==0)return a; return gcd(b,a%b); } long lcm(long a, long b){ return a*(b/gcd(a,b)); } double log2(int x) { return (Math.log(x)/Math.log(2)); } double log2(long x) { return (Math.log(x)/Math.log(2)); } }
ConDefects/ConDefects/Code/arc142_c/Java/32652103
condefects-java_data_310
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int a=Integer.MAX_VALUE; int b=Integer.MIN_VALUE; int c=Integer.MAX_VALUE; int d=Integer.MIN_VALUE; for(int i=0;i<10;i++){ String s=scanner.next(); for(int j=0;j<10;j++){ if(s.charAt(i)=='#'){ a=Math.min(a,i+1); b=Math.max(b,i+1); c=Math.min(c,j+1); d=Math.max(d,j+1); } } } System.out.println(a+" "+b); System.out.println(c+" "+d); } } import java.util.Scanner; class Main { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int a=Integer.MAX_VALUE; int b=Integer.MIN_VALUE; int c=Integer.MAX_VALUE; int d=Integer.MIN_VALUE; for(int i=0;i<10;i++){ String s=scanner.next(); for(int j=0;j<10;j++){ if(s.charAt(j)=='#'){ a=Math.min(a,i+1); b=Math.max(b,i+1); c=Math.min(c,j+1); d=Math.max(d,j+1); } } } System.out.println(a+" "+b); System.out.println(c+" "+d); } }
ConDefects/ConDefects/Code/abc269_b/Java/41619931
condefects-java_data_311
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); String s=scanner.next(); if(s.equals("Monday")){ System.out.println(5); } else if(s.equals("Tuesday")){ System.out.println(4); } else if(s.equals("Wednesday")){ System.out.println(3); } else if(s.equals("Thursday")){ System.out.println(2); } else{ System.out.println("Friday"); } } } import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); String s=scanner.next(); if(s.equals("Monday")){ System.out.println(5); } else if(s.equals("Tuesday")){ System.out.println(4); } else if(s.equals("Wednesday")){ System.out.println(3); } else if(s.equals("Thursday")){ System.out.println(2); } else{ System.out.println(1); } } }
ConDefects/ConDefects/Code/abc267_a/Java/41474445
condefects-java_data_312
import java.util.*; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); String s = sc.next(); if(s.equals("Monday")){ System.out.println(5); } else if(s.equals("Tuesday")){ System.out.println(4); } else if(s.equals("Wednesday")){ System.out.println(3); } else if(s.equals("Thursday")){ System.out.println(2); } else if(s.equals("Fruday")){ System.out.println(1); } sc.close(); } } import java.util.*; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); String s = sc.next(); if(s.equals("Monday")){ System.out.println(5); } else if(s.equals("Tuesday")){ System.out.println(4); } else if(s.equals("Wednesday")){ System.out.println(3); } else if(s.equals("Thursday")){ System.out.println(2); } else if(s.equals("Friday")){ System.out.println(1); } sc.close(); } }
ConDefects/ConDefects/Code/abc267_a/Java/39597595
condefects-java_data_313
import java.util.*; import java.io.*; public class Main{ public static void main(String[] args) { ContestScanner sc = new ContestScanner(); ContestPrinter ou = new ContestPrinter(); //ここから String S = sc.next(); if(S.charAt(0)=='M') { System.out.println(5); }else if(S.charAt(0)=='T') { System.out.println(4); }else if(S.charAt(0)=='W') { System.out.println(3); }else if(S.charAt(1)=='h') { System.out.println(2); }else if(S.charAt(0)=='F') { System.out.println(1); } //ここ ou.flush(); } } class ContestScanner { private final java.io.InputStream in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private static final long LONG_MAX_TENTHS = 922337203685477580L; private static final int LONG_MAX_LAST_DIGIT = 7; private static final int LONG_MIN_LAST_DIGIT = 8; public ContestScanner(java.io.InputStream in) { this.in = in; } public ContestScanner(java.io.File file) throws java.io.FileNotFoundException { this(new java.io.BufferedInputStream(new java.io.FileInputStream(file))); } public ContestScanner() { this(System.in); } private boolean hasNextByte() { if (ptr < buflen) { return true; } else { ptr = 0; try { buflen = in.read(buffer); } catch (java.io.IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } public String next() { if (!hasNext()) throw new java.util.NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new java.util.NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { int digit = b - '0'; if (n >= LONG_MAX_TENTHS) { if (n == LONG_MAX_TENTHS) { if (minus) { if (digit <= LONG_MIN_LAST_DIGIT) { n = -n * 10 - digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString(b))); } } } else { if (digit <= LONG_MAX_LAST_DIGIT) { n = n * 10 + digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString(b))); } } } } throw new ArithmeticException( String.format("%s%d%d... overflows long.", minus ? "-" : "", n, digit)); } n = n * 10 + digit; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } public long[] nextLongArray(int length) { long[] array = new long[length]; for (int i = 0; i < length; i++) array[i] = this.nextLong(); return array; } public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map) { long[] array = new long[length]; for (int i = 0; i < length; i++) array[i] = map.applyAsLong(this.nextLong()); return array; } public int[] nextIntArray(int length) { int[] array = new int[length]; for (int i = 0; i < length; i++) array[i] = this.nextInt(); return array; } public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map) { int[] array = new int[length]; for (int i = 0; i < length; i++) array[i] = map.applyAsInt(this.nextInt()); return array; } public double[] nextDoubleArray(int length) { double[] array = new double[length]; for (int i = 0; i < length; i++) array[i] = this.nextDouble(); return array; } public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map) { double[] array = new double[length]; for (int i = 0; i < length; i++) array[i] = map.applyAsDouble(this.nextDouble()); return array; } public long[][] nextLongMatrix(int height, int width) { long[][] mat = new long[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) { mat[h][w] = this.nextLong(); } return mat; } public int[][] nextIntMatrix(int height, int width) { int[][] mat = new int[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) { mat[h][w] = this.nextInt(); } return mat; } public double[][] nextDoubleMatrix(int height, int width) { double[][] mat = new double[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) { mat[h][w] = this.nextDouble(); } return mat; } public char[][] nextCharMatrix(int height, int width) { char[][] mat = new char[height][width]; for (int h = 0; h < height; h++) { String s = this.next(); for (int w = 0; w < width; w++) { mat[h][w] = s.charAt(w); } } return mat; } } class ContestPrinter extends java.io.PrintWriter { public ContestPrinter(java.io.PrintStream stream) { super(stream); } public ContestPrinter(java.io.File file) throws java.io.FileNotFoundException { super(new java.io.PrintStream(file)); } public ContestPrinter() { super(System.out); } private static String dtos(double x, int n) { StringBuilder sb = new StringBuilder(); if (x < 0) { sb.append('-'); x = -x; } x += Math.pow(10, -n) / 2; sb.append((long) x); sb.append("."); x -= (long) x; for (int i = 0; i < n; i++) { x *= 10; sb.append((int) x); x -= (int) x; } return sb.toString(); } @Override public void print(float f) { super.print(dtos(f, 20)); } @Override public void println(float f) { super.println(dtos(f, 20)); } @Override public void print(double d) { super.print(dtos(d, 20)); } @Override public void println(double d) { super.println(dtos(d, 20)); } public void printArray(int[] array, String separator) { int n = array.length; if (n == 0) { super.println(); return; } for (int i = 0; i < n - 1; i++) { super.print(array[i]); super.print(separator); } super.println(array[n - 1]); } public void printArray(int[] array) { this.printArray(array, " "); } public void printArray(int[] array, String separator, java.util.function.IntUnaryOperator map) { int n = array.length; if (n == 0) { super.println(); return; } for (int i = 0; i < n - 1; i++) { super.print(map.applyAsInt(array[i])); super.print(separator); } super.println(map.applyAsInt(array[n - 1])); } public void printArray(int[] array, java.util.function.IntUnaryOperator map) { this.printArray(array, " ", map); } public void printArray(long[] array, String separator) { int n = array.length; if (n == 0) { super.println(); return; } for (int i = 0; i < n - 1; i++) { super.print(array[i]); super.print(separator); } super.println(array[n - 1]); } public void printArray(long[] array) { this.printArray(array, " "); } public void printArray(long[] array, String separator, java.util.function.LongUnaryOperator map) { int n = array.length; if (n == 0) { super.println(); return; } for (int i = 0; i < n - 1; i++) { super.print(map.applyAsLong(array[i])); super.print(separator); } super.println(map.applyAsLong(array[n - 1])); } public void printArray(long[] array, java.util.function.LongUnaryOperator map) { this.printArray(array, " ", map); } public <T> void printArray(T[] array, String separator) { int n = array.length; if (n == 0) { super.println(); return; } for (int i = 0; i < n - 1; i++) { super.print(array[i]); super.print(separator); } super.println(array[n - 1]); } public <T> void printArray(T[] array) { this.printArray(array, " "); } public <T> void printArray(T[] array, String separator, java.util.function.UnaryOperator<T> map) { int n = array.length; if (n == 0) { super.println(); return; } for (int i = 0; i < n - 1; i++) { super.print(map.apply(array[i])); super.print(separator); } super.println(map.apply(array[n - 1])); } public <T> void printArray(T[] array, java.util.function.UnaryOperator<T> map) { this.printArray(array, " ", map); } } import java.util.*; import java.io.*; public class Main{ public static void main(String[] args) { ContestScanner sc = new ContestScanner(); ContestPrinter ou = new ContestPrinter(); //ここから String S = sc.next(); if(S.charAt(0)=='M') { System.out.println(5); }else if(S.charAt(1)=='u') { System.out.println(4); }else if(S.charAt(0)=='W') { System.out.println(3); }else if(S.charAt(1)=='h') { System.out.println(2); }else if(S.charAt(0)=='F') { System.out.println(1); } //ここ ou.flush(); } } class ContestScanner { private final java.io.InputStream in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private static final long LONG_MAX_TENTHS = 922337203685477580L; private static final int LONG_MAX_LAST_DIGIT = 7; private static final int LONG_MIN_LAST_DIGIT = 8; public ContestScanner(java.io.InputStream in) { this.in = in; } public ContestScanner(java.io.File file) throws java.io.FileNotFoundException { this(new java.io.BufferedInputStream(new java.io.FileInputStream(file))); } public ContestScanner() { this(System.in); } private boolean hasNextByte() { if (ptr < buflen) { return true; } else { ptr = 0; try { buflen = in.read(buffer); } catch (java.io.IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } public String next() { if (!hasNext()) throw new java.util.NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new java.util.NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { int digit = b - '0'; if (n >= LONG_MAX_TENTHS) { if (n == LONG_MAX_TENTHS) { if (minus) { if (digit <= LONG_MIN_LAST_DIGIT) { n = -n * 10 - digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString(b))); } } } else { if (digit <= LONG_MAX_LAST_DIGIT) { n = n * 10 + digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString(b))); } } } } throw new ArithmeticException( String.format("%s%d%d... overflows long.", minus ? "-" : "", n, digit)); } n = n * 10 + digit; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } public long[] nextLongArray(int length) { long[] array = new long[length]; for (int i = 0; i < length; i++) array[i] = this.nextLong(); return array; } public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map) { long[] array = new long[length]; for (int i = 0; i < length; i++) array[i] = map.applyAsLong(this.nextLong()); return array; } public int[] nextIntArray(int length) { int[] array = new int[length]; for (int i = 0; i < length; i++) array[i] = this.nextInt(); return array; } public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map) { int[] array = new int[length]; for (int i = 0; i < length; i++) array[i] = map.applyAsInt(this.nextInt()); return array; } public double[] nextDoubleArray(int length) { double[] array = new double[length]; for (int i = 0; i < length; i++) array[i] = this.nextDouble(); return array; } public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map) { double[] array = new double[length]; for (int i = 0; i < length; i++) array[i] = map.applyAsDouble(this.nextDouble()); return array; } public long[][] nextLongMatrix(int height, int width) { long[][] mat = new long[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) { mat[h][w] = this.nextLong(); } return mat; } public int[][] nextIntMatrix(int height, int width) { int[][] mat = new int[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) { mat[h][w] = this.nextInt(); } return mat; } public double[][] nextDoubleMatrix(int height, int width) { double[][] mat = new double[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) { mat[h][w] = this.nextDouble(); } return mat; } public char[][] nextCharMatrix(int height, int width) { char[][] mat = new char[height][width]; for (int h = 0; h < height; h++) { String s = this.next(); for (int w = 0; w < width; w++) { mat[h][w] = s.charAt(w); } } return mat; } } class ContestPrinter extends java.io.PrintWriter { public ContestPrinter(java.io.PrintStream stream) { super(stream); } public ContestPrinter(java.io.File file) throws java.io.FileNotFoundException { super(new java.io.PrintStream(file)); } public ContestPrinter() { super(System.out); } private static String dtos(double x, int n) { StringBuilder sb = new StringBuilder(); if (x < 0) { sb.append('-'); x = -x; } x += Math.pow(10, -n) / 2; sb.append((long) x); sb.append("."); x -= (long) x; for (int i = 0; i < n; i++) { x *= 10; sb.append((int) x); x -= (int) x; } return sb.toString(); } @Override public void print(float f) { super.print(dtos(f, 20)); } @Override public void println(float f) { super.println(dtos(f, 20)); } @Override public void print(double d) { super.print(dtos(d, 20)); } @Override public void println(double d) { super.println(dtos(d, 20)); } public void printArray(int[] array, String separator) { int n = array.length; if (n == 0) { super.println(); return; } for (int i = 0; i < n - 1; i++) { super.print(array[i]); super.print(separator); } super.println(array[n - 1]); } public void printArray(int[] array) { this.printArray(array, " "); } public void printArray(int[] array, String separator, java.util.function.IntUnaryOperator map) { int n = array.length; if (n == 0) { super.println(); return; } for (int i = 0; i < n - 1; i++) { super.print(map.applyAsInt(array[i])); super.print(separator); } super.println(map.applyAsInt(array[n - 1])); } public void printArray(int[] array, java.util.function.IntUnaryOperator map) { this.printArray(array, " ", map); } public void printArray(long[] array, String separator) { int n = array.length; if (n == 0) { super.println(); return; } for (int i = 0; i < n - 1; i++) { super.print(array[i]); super.print(separator); } super.println(array[n - 1]); } public void printArray(long[] array) { this.printArray(array, " "); } public void printArray(long[] array, String separator, java.util.function.LongUnaryOperator map) { int n = array.length; if (n == 0) { super.println(); return; } for (int i = 0; i < n - 1; i++) { super.print(map.applyAsLong(array[i])); super.print(separator); } super.println(map.applyAsLong(array[n - 1])); } public void printArray(long[] array, java.util.function.LongUnaryOperator map) { this.printArray(array, " ", map); } public <T> void printArray(T[] array, String separator) { int n = array.length; if (n == 0) { super.println(); return; } for (int i = 0; i < n - 1; i++) { super.print(array[i]); super.print(separator); } super.println(array[n - 1]); } public <T> void printArray(T[] array) { this.printArray(array, " "); } public <T> void printArray(T[] array, String separator, java.util.function.UnaryOperator<T> map) { int n = array.length; if (n == 0) { super.println(); return; } for (int i = 0; i < n - 1; i++) { super.print(map.apply(array[i])); super.print(separator); } super.println(map.apply(array[n - 1])); } public <T> void printArray(T[] array, java.util.function.UnaryOperator<T> map) { this.printArray(array, " ", map); } }
ConDefects/ConDefects/Code/abc267_a/Java/37548438
condefects-java_data_314
import java.util.Scanner; public class Main { public static void main(final String[] args) { try (final Scanner sc = new Scanner(System.in)) { switch (sc.next()) { case "Monday": System.out.println("5"); break; case "Tuesday": System.out.println("4"); break; case "Wednesday": System.out.println("3"); break; case "Thursday": System.out.println("2"); break; case "Friday ": System.out.println("1"); break; } } } } import java.util.Scanner; public class Main { public static void main(final String[] args) { try (final Scanner sc = new Scanner(System.in)) { switch (sc.next()) { case "Monday": System.out.println("5"); break; case "Tuesday": System.out.println("4"); break; case "Wednesday": System.out.println("3"); break; case "Thursday": System.out.println("2"); break; case "Friday": System.out.println("1"); break; } } } }
ConDefects/ConDefects/Code/abc267_a/Java/45058678
condefects-java_data_315
import java.util.*; class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String S = scan.next(); Map<String, Integer> map = new HashMap<>(); map.put("Monday", 5); map.put("Tuesday", 4); map.put("Wednesday", 3); map.put("Thursdayday", 2); map.put("Friday", 1); System.out.println(map.get(S)); } } import java.util.*; class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String S = scan.next(); Map<String, Integer> map = new HashMap<>(); map.put("Monday", 5); map.put("Tuesday", 4); map.put("Wednesday", 3); map.put("Thursday", 2); map.put("Friday", 1); System.out.println(map.get(S)); } }
ConDefects/ConDefects/Code/abc267_a/Java/44186643
condefects-java_data_316
import java.math.BigInteger; import java.util.*; @SuppressWarnings("unused") public class Main { private static void solve() { int n = ni(); int a = ni(); int b = ni(); int[] d = na(n); int sd = Arrays.stream(d).sum(); int tmpA = a + b + sd; int tmpB = a - b + sd; a = tmpA; b = tmpB; for (int i = 0; i < n; i ++) { d[i] *= 2; } var dp = new BigInteger[n + 1]; dp[0] = BigInteger.ONE; for (int i = 0; i < n; i ++) { dp[i + 1] = dp[i].or(dp[i].shiftLeft(d[i])); } if (!dp[n].testBit(a) || !dp[n].testBit(b)) { System.out.println("No"); return; } int[] ret = new int[n]; for (int i = n - 1; i >= 0; i --) { if (!dp[i].testBit(a)) { ret[i] |= 1 << 0; a -= d[i]; } if (!dp[i].testBit(b)) { ret[i] |= 1 << 1; b -= d[i]; } } char[] s = {'L', 'U', 'D', 'R'}; out.println("Yes"); for (int v : ret) { out.print(s[v]); } out.println(); } public static void main(String[] args) { new Thread(null, new Runnable() { @Override public void run() { long start = System.currentTimeMillis(); String debug = args.length > 0 ? args[0] : null; if (debug != null) { try { is = java.nio.file.Files.newInputStream(java.nio.file.Paths.get(debug)); } catch (Exception e) { throw new RuntimeException(e); } } reader = new java.io.BufferedReader(new java.io.InputStreamReader(is), 32768); solve(); out.flush(); tr((System.currentTimeMillis() - start) + "ms"); } }, "", 64000000).start(); } private static java.io.InputStream is = System.in; private static java.io.PrintWriter out = new java.io.PrintWriter(System.out); private static java.util.StringTokenizer tokenizer = null; private static java.io.BufferedReader reader; public static String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new java.util.StringTokenizer(reader.readLine()); } catch (Exception e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } private static double nd() { return Double.parseDouble(next()); } private static long nl() { return Long.parseLong(next()); } private static int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private static char[] ns() { return next().toCharArray(); } private static long[] nal(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } private static int[][] ntable(int n, int m) { int[][] table = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { table[i][j] = ni(); } } return table; } private static int[][] nlist(int n, int m) { int[][] table = new int[m][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { table[j][i] = ni(); } } return table; } private static int ni() { return Integer.parseInt(next()); } private static void tr(Object... o) { if (is != System.in) System.out.println(java.util.Arrays.deepToString(o)); } } import java.math.BigInteger; import java.util.*; @SuppressWarnings("unused") public class Main { private static void solve() { int n = ni(); int a = ni(); int b = ni(); int[] d = na(n); int sd = Arrays.stream(d).sum(); int tmpA = a + b + sd; int tmpB = a - b + sd; a = tmpA; b = tmpB; for (int i = 0; i < n; i ++) { d[i] *= 2; } var dp = new BigInteger[n + 1]; dp[0] = BigInteger.ONE; for (int i = 0; i < n; i ++) { dp[i + 1] = dp[i].or(dp[i].shiftLeft(d[i])); } if (a < 0 || b < 0 || !dp[n].testBit(a) || !dp[n].testBit(b)) { System.out.println("No"); return; } int[] ret = new int[n]; for (int i = n - 1; i >= 0; i --) { if (!dp[i].testBit(a)) { ret[i] |= 1 << 0; a -= d[i]; } if (!dp[i].testBit(b)) { ret[i] |= 1 << 1; b -= d[i]; } } char[] s = {'L', 'U', 'D', 'R'}; out.println("Yes"); for (int v : ret) { out.print(s[v]); } out.println(); } public static void main(String[] args) { new Thread(null, new Runnable() { @Override public void run() { long start = System.currentTimeMillis(); String debug = args.length > 0 ? args[0] : null; if (debug != null) { try { is = java.nio.file.Files.newInputStream(java.nio.file.Paths.get(debug)); } catch (Exception e) { throw new RuntimeException(e); } } reader = new java.io.BufferedReader(new java.io.InputStreamReader(is), 32768); solve(); out.flush(); tr((System.currentTimeMillis() - start) + "ms"); } }, "", 64000000).start(); } private static java.io.InputStream is = System.in; private static java.io.PrintWriter out = new java.io.PrintWriter(System.out); private static java.util.StringTokenizer tokenizer = null; private static java.io.BufferedReader reader; public static String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new java.util.StringTokenizer(reader.readLine()); } catch (Exception e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } private static double nd() { return Double.parseDouble(next()); } private static long nl() { return Long.parseLong(next()); } private static int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private static char[] ns() { return next().toCharArray(); } private static long[] nal(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } private static int[][] ntable(int n, int m) { int[][] table = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { table[i][j] = ni(); } } return table; } private static int[][] nlist(int n, int m) { int[][] table = new int[m][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { table[j][i] = ni(); } } return table; } private static int ni() { return Integer.parseInt(next()); } private static void tr(Object... o) { if (is != System.in) System.out.println(java.util.Arrays.deepToString(o)); } }
ConDefects/ConDefects/Code/abc221_g/Java/26322629
condefects-java_data_317
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // 参加者数 int n = sc.nextInt(); // 進出数 int k = sc.nextInt(); // 希望 String s = sc.next(); // 進出者のカウント int c = 0; for (int i=0; i<n; i++) { if (c<3 && s.charAt(i) == 'o') { System.out.print('o'); c++; } else { System.out.print('x'); } } System.out.println(); } } import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // 参加者数 int n = sc.nextInt(); // 進出数 int k = sc.nextInt(); // 希望 String s = sc.next(); // 進出者のカウント int c = 0; for (int i=0; i<n; i++) { if (c<k && s.charAt(i) == 'o') { System.out.print('o'); c++; } else { System.out.print('x'); } } System.out.println(); } }
ConDefects/ConDefects/Code/abc290_b/Java/45537188
condefects-java_data_318
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int K = sc.nextInt(); String S = sc.next(); String[] num =S.split(""); int hit = 0; for(int i = 0; i < N; i++) { if(num[i].equals("o")) { if(hit<K) { hit++; System.out.print("○"); } else { System.out.print("x"); } } else { System.out.print("x"); } } System.out.println(); } } import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int K = sc.nextInt(); String S = sc.next(); String[] num =S.split(""); int hit = 0; for(int i = 0; i < N; i++) { if(num[i].equals("o")) { if(hit<K) { hit++; System.out.print("o"); } else { System.out.print("x"); } } else { System.out.print("x"); } } System.out.println(); } }
ConDefects/ConDefects/Code/abc290_b/Java/45284204
condefects-java_data_319
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner scan=new Scanner(System.in); int N= scan.nextInt(); int K=scan.nextInt(); String S = scan.next(); char[] pori= S.toCharArray(); for(int i=0;i<N;i++){ if(pori[i]=='o'&&K>0){ System.out.print("o"); K--; }else{ System.out.println("x"); } } } } import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner scan=new Scanner(System.in); int N= scan.nextInt(); int K=scan.nextInt(); String S = scan.next(); char[] pori= S.toCharArray(); for(int i=0;i<N;i++){ if(pori[i]=='o'&&K>0){ System.out.print("o"); K--; }else{ System.out.print("x"); } } } }
ConDefects/ConDefects/Code/abc290_b/Java/43201823
condefects-java_data_320
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int tuzuki = Integer.parseInt(sc.next()); int kati = Integer.parseInt(sc.next()); String takaoka = sc.next(); int nakajima = 0; char[] takaoka1 = takaoka.toCharArray(); for (int i=0;i<tuzuki;++i){ if (nakajima <= kati)++nakajima; if (takaoka1[i] == 'o' && nakajima > kati){ takaoka1[i] = 'x'; } } takaoka = String.valueOf(takaoka1); System.out.println(takaoka); sc.close(); } } import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int tuzuki = Integer.parseInt(sc.next()); int kati = Integer.parseInt(sc.next()); String takaoka = sc.next(); int nakajima = 0; char[] takaoka1 = takaoka.toCharArray(); for (int i=0;i<tuzuki;++i){ if (nakajima <= kati && takaoka1[i] == 'o')++nakajima; if (takaoka1[i] == 'o' && nakajima > kati){ takaoka1[i] = 'x'; } } takaoka = String.valueOf(takaoka1); System.out.println(takaoka); sc.close(); } }
ConDefects/ConDefects/Code/abc290_b/Java/41859133
condefects-java_data_321
import java.util.*; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); String s[] = sc.next().split(""); int count = 0; for(int i = 0; i < n; i++){ if(s[i].equals("o")){ count += 1; if(count > 3){ s[i] = "x"; } } System.out.print(s[i]); } } } import java.util.*; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); String s[] = sc.next().split(""); int count = 0; for(int i = 0; i < n; i++){ if(s[i].equals("o")){ count += 1; if(count > k){ s[i] = "x"; } } System.out.print(s[i]); } } }
ConDefects/ConDefects/Code/abc290_b/Java/43277673
condefects-java_data_322
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); String s = sc.next(); String[] win = s.split(""); int hit = 0; for(int i = 0 ; i < win.length ; i ++) { if(win[i].equals("o")) { hit++; } if(hit > k) { win[i] = "x"; } } for(int i = 0 ; i < win.length ; i ++) { System.out.println(win[i]); } } } import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); String s = sc.next(); String[] win = s.split(""); int hit = 0; for(int i = 0 ; i < win.length ; i ++) { if(win[i].equals("o")) { hit++; } if(hit > k) { win[i] = "x"; } } for(int i = 0 ; i < win.length ; i ++) { System.out.print(win[i]); } } }
ConDefects/ConDefects/Code/abc290_b/Java/45284108
condefects-java_data_323
import java.util.*; public class Main{ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int k = scanner.nextInt(); String s = scanner.next(); int number = 0; for(int i = 0; i < n; i ++) { if(s.charAt(i) == 'o' && number < 3) { System.out.print("o"); number ++; }else { System.out.print("x"); } } } } import java.util.*; public class Main{ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int k = scanner.nextInt(); String s = scanner.next(); int number = 0; for(int i = 0; i < n; i ++) { if(s.charAt(i) == 'o' && number < k) { System.out.print("o"); number ++; }else { System.out.print("x"); } } } }
ConDefects/ConDefects/Code/abc290_b/Java/39940175
condefects-java_data_324
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int A = sc.nextInt(); int B = sc.nextInt(); int C = sc.nextInt(); int D = sc.nextInt(); long dx = C - A; long dy = D - B; long area = 0; //左下基準 //y偶数のときx:2101のループ //y奇数のときx:1210のループ int[] even = {2,1,0,1}; //合計4 int[] odd = {1,2,1,0}; //合計4 //4*4エリアの面積*2は16 //左下から4*4の枚数を数えて余った右辺の面積を求める area += (dx / 4) * 4 * dy; //左側4nまで面積 //rightからDまで計算してない int right = (int) (C - dx % 4); //x座標 for (int i = right; i < C; i++) { if((i + 1000000000) % 4 == 0 || (i + 1000000000) % 4 == 1){ area += dy / 4 * 6; }else{ area += dy / 4 * 2; } //y座標 for(long j = (dy + 1000000000) % 4; j < D; j++){ if(j % 2 == 0){ area += even[(i + 1000000000) % 4]; }else{ area += odd[(i + 1000000000) % 4]; } } } System.out.println(area); } } import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int A = sc.nextInt(); int B = sc.nextInt(); int C = sc.nextInt(); int D = sc.nextInt(); long dx = C - A; long dy = D - B; long area = 0; //左下基準 //y偶数のときx:2101のループ //y奇数のときx:1210のループ int[] even = {2,1,0,1}; //合計4 int[] odd = {1,2,1,0}; //合計4 //4*4エリアの面積*2は16 //左下から4*4の枚数を数えて余った右辺の面積を求める area += (dx / 4) * 4 * dy; //左側4nまで面積 //rightからDまで計算してない int right = (int) (C - dx % 4); //x座標 for (int i = right; i < C; i++) { if((i + 1000000000) % 4 == 0 || (i + 1000000000) % 4 == 1){ area += dy / 4 * 6; }else{ area += dy / 4 * 2; } //y座標 for(long j =D- (dy + 1000000000) % 4; j < D; j++){ if(j % 2 == 0){ area += even[(i + 1000000000) % 4]; }else{ area += odd[(i + 1000000000) % 4]; } } } System.out.println(area); } }
ConDefects/ConDefects/Code/abc354_d/Java/53634809
condefects-java_data_325
import java.util.Scanner; public class Main { public static void main(String[] args) { var sc = new Scanner(System.in); long left = Long.parseLong(sc.next()); long bottom = Long.parseLong(sc.next()); long right = Long.parseLong(sc.next()); long top = Long.parseLong(sc.next()); long ans = 0; int l = (int) (left % 4 + 4) % 4; int b = (int) (bottom % 2 + 2) % 2; right -= left; left = 0; top -= bottom; bottom = 0; switch(l){ case 0 -> { if(b == 0){ ans += (right / 2) * ((top + 1) / 2); ans += ((right + 1) / 2) * (top / 2); ans += ((right + 3) / 4) * ((top + 1) / 2) * 2; ans += ((right + 2) / 4) * (top / 2) * 2; }else{ ans += ((right + 1) / 2) * ((top + 1) / 2); ans += (right / 2) * (top / 2); ans += ((right + 2) / 4) * ((top + 1) / 2) * 2; ans += ((right + 3) / 4) * (top / 2) * 2; } } case 1 -> { if(b == 0){ ans += ((right + 1) / 2) * ((top + 1) / 2); ans += (right / 2) * (top / 2); ans += (right / 4) * ((top + 1) / 2) * 2; ans += ((right + 3) / 4) * (top / 2) * 2; }else{ ans += (right / 2) * ((top + 1) / 2); ans += ((right + 1) / 2) * (top / 2); ans += ((right + 3) / 4) * ((top + 1) / 2) * 2; ans += (right / 4) * (top / 2) * 2; } } case 2 -> { if(b == 0){ ans += (right / 2) * ((top + 1) / 2); ans += ((right + 1) / 2) * (top / 2); ans += ((right * 1) / 4) * ((top + 1) / 2) * 2; ans += (right / 4) * (top / 2) * 2; }else{ ans += ((right + 1) / 2) * ((top + 1) / 2); ans += (right / 2) * (top / 2); ans += (right / 4) * ((top + 1) / 2) * 2; ans += ((right + 1) / 4) * (top / 2) * 2; } } case 3 -> { if(b == 0){ ans += ((right + 1) / 2) * ((top + 1) / 2); ans += (right / 2) * (top / 2); ans += ((right + 2) / 4) * ((top + 1) / 2) * 2; ans += ((right + 1) / 4) * (top / 2) * 2; }else{ ans += (right / 2) * ((top + 1) / 2); ans += ((right + 1) / 2) * (top / 2); ans += ((right + 1) / 4) * ((top + 1) / 2) * 2; ans += ((right + 2) / 4) * (top / 2) * 2; } } } System.out.println(ans); } } import java.util.Scanner; public class Main { public static void main(String[] args) { var sc = new Scanner(System.in); long left = Long.parseLong(sc.next()); long bottom = Long.parseLong(sc.next()); long right = Long.parseLong(sc.next()); long top = Long.parseLong(sc.next()); long ans = 0; int l = (int) (left % 4 + 4) % 4; int b = (int) (bottom % 2 + 2) % 2; right -= left; left = 0; top -= bottom; bottom = 0; switch(l){ case 0 -> { if(b == 0){ ans += (right / 2) * ((top + 1) / 2); ans += ((right + 1) / 2) * (top / 2); ans += ((right + 3) / 4) * ((top + 1) / 2) * 2; ans += ((right + 2) / 4) * (top / 2) * 2; }else{ ans += ((right + 1) / 2) * ((top + 1) / 2); ans += (right / 2) * (top / 2); ans += ((right + 2) / 4) * ((top + 1) / 2) * 2; ans += ((right + 3) / 4) * (top / 2) * 2; } } case 1 -> { if(b == 0){ ans += ((right + 1) / 2) * ((top + 1) / 2); ans += (right / 2) * (top / 2); ans += (right / 4) * ((top + 1) / 2) * 2; ans += ((right + 3) / 4) * (top / 2) * 2; }else{ ans += (right / 2) * ((top + 1) / 2); ans += ((right + 1) / 2) * (top / 2); ans += ((right + 3) / 4) * ((top + 1) / 2) * 2; ans += (right / 4) * (top / 2) * 2; } } case 2 -> { if(b == 0){ ans += (right / 2) * ((top + 1) / 2); ans += ((right + 1) / 2) * (top / 2); ans += ((right + 1) / 4) * ((top + 1) / 2) * 2; ans += (right / 4) * (top / 2) * 2; }else{ ans += ((right + 1) / 2) * ((top + 1) / 2); ans += (right / 2) * (top / 2); ans += (right / 4) * ((top + 1) / 2) * 2; ans += ((right + 1) / 4) * (top / 2) * 2; } } case 3 -> { if(b == 0){ ans += ((right + 1) / 2) * ((top + 1) / 2); ans += (right / 2) * (top / 2); ans += ((right + 2) / 4) * ((top + 1) / 2) * 2; ans += ((right + 1) / 4) * (top / 2) * 2; }else{ ans += (right / 2) * ((top + 1) / 2); ans += ((right + 1) / 2) * (top / 2); ans += ((right + 1) / 4) * ((top + 1) / 2) * 2; ans += ((right + 2) / 4) * (top / 2) * 2; } } } System.out.println(ans); } }
ConDefects/ConDefects/Code/abc354_d/Java/53624831
condefects-java_data_326
import java.io.*; import java.util.StringTokenizer; public class Main { static int offset = (int) 1e9 - 2; static int[][] val = { {0, 1}, {1, 0}, {2, 1}, {1, 2} }; public static long calArea(long x, long y) { int col = val.length, row = val[0].length; long ans = (x / col) * (y / row) * 8; for (int i = 0; i < x % col; i++) { long sum = 0; for (int j = 0; j < row; j++) { sum += val[i][j]; } ans += sum * (y / row); } for (int i = 0; i < y % row; i++) { long sum = 0; for (int j = 0; j < col; j++) { sum += val[j][i]; } ans += sum * (x / col); } for (int i = 0; i < x % col; i++) { for (int j = 0; j < y % row; j++) { ans += val[i][j]; } } return ans; } public static void solve() throws IOException{ long a = in.nextInt(); long b = in.nextInt(); long c = in.nextInt(); long d = in.nextInt(); a += offset; b += offset; c += offset; d += offset; out.println(calArea(a, b) + calArea(c, d) - calArea(a, d) - calArea(c, b)); } static boolean MULTI_CASE = false; public static void main(String[] args) throws IOException { if (MULTI_CASE) { int T = in.nextInt(); for (int i = 0; i < T; ++i) { solve(); } } else { solve(); } out.close(); } static InputReader in = new InputReader(); static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); static class InputReader { private StringTokenizer st; private BufferedReader bf; public InputReader() { bf = new BufferedReader(new InputStreamReader(System.in)); st = null; } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(bf.readLine()); } return st.nextToken(); } public String nextLine() throws IOException { return bf.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } } } /* */ import java.io.*; import java.util.StringTokenizer; public class Main { static int offset = (int) 1e9 + 2; static int[][] val = { {0, 1}, {1, 0}, {2, 1}, {1, 2} }; public static long calArea(long x, long y) { int col = val.length, row = val[0].length; long ans = (x / col) * (y / row) * 8; for (int i = 0; i < x % col; i++) { long sum = 0; for (int j = 0; j < row; j++) { sum += val[i][j]; } ans += sum * (y / row); } for (int i = 0; i < y % row; i++) { long sum = 0; for (int j = 0; j < col; j++) { sum += val[j][i]; } ans += sum * (x / col); } for (int i = 0; i < x % col; i++) { for (int j = 0; j < y % row; j++) { ans += val[i][j]; } } return ans; } public static void solve() throws IOException{ long a = in.nextInt(); long b = in.nextInt(); long c = in.nextInt(); long d = in.nextInt(); a += offset; b += offset; c += offset; d += offset; out.println(calArea(a, b) + calArea(c, d) - calArea(a, d) - calArea(c, b)); } static boolean MULTI_CASE = false; public static void main(String[] args) throws IOException { if (MULTI_CASE) { int T = in.nextInt(); for (int i = 0; i < T; ++i) { solve(); } } else { solve(); } out.close(); } static InputReader in = new InputReader(); static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); static class InputReader { private StringTokenizer st; private BufferedReader bf; public InputReader() { bf = new BufferedReader(new InputStreamReader(System.in)); st = null; } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(bf.readLine()); } return st.nextToken(); } public String nextLine() throws IOException { return bf.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } } } /* */
ConDefects/ConDefects/Code/abc354_d/Java/53695079
condefects-java_data_327
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); String s = sc.next(); int pos_r = 0, pos_s = 0; for (int i = 0; i < 3; i++) { if (s.charAt(i) == 'R') { pos_r = i; } if (s.charAt(i) == 'S') { pos_s = i; } } String ans = "No"; if (pos_r < pos_s) { ans = "Yes"; } System.out.println(ans); sc.close(); } } import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); String s = sc.next(); int pos_r = 0, pos_s = 0; for (int i = 0; i < 3; i++) { if (s.charAt(i) == 'R') { pos_r = i; } if (s.charAt(i) == 'M') { pos_s = i; } } String ans = "No"; if (pos_r < pos_s) { ans = "Yes"; } System.out.println(ans); sc.close(); } }
ConDefects/ConDefects/Code/abc360_a/Java/55129900
condefects-java_data_328
import java.util.*; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print(sc.next().matches("R.*M") ? "Yes" : "No"); } } import java.util.*; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print(sc.next().matches(".*R.*M.*") ? "Yes" : "No"); } }
ConDefects/ConDefects/Code/abc360_a/Java/55115299
condefects-java_data_329
// temprate import java.util.*; import static java.lang.Math.*; public class Main{ public static void main(String[] args){ var in = new Scanner(System.in); // ~~~ long x = in.nextLong(); long ans = x / 10; if ( x < 0 ){ ans = ans - 1; } System.out.println( ans ); } } // temprate import java.util.*; import static java.lang.Math.*; public class Main{ public static void main(String[] args){ var in = new Scanner(System.in); // ~~~ long x = in.nextLong(); long ans = x / 10; if ( x < 0 & x % 10 != 0 ){ ans = ans - 1; } System.out.println( ans ); } }
ConDefects/ConDefects/Code/abc239_b/Java/39746427
condefects-java_data_330
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ Scanner sc = new Scanner(System.in); long x = sc.nextLong(); long ans = 0; if(x>=0 || x %10 != 0) { ans = x/10; }else if(x<0 && x%10 != 0) { ans = x/10-1; } System.out.println(ans); } } import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ Scanner sc = new Scanner(System.in); long x = sc.nextLong(); long ans = 0; if(x>=0 || x %10 == 0) { ans = x/10; }else if(x<0 && x%10 != 0) { ans = x/10-1; } System.out.println(ans); } }
ConDefects/ConDefects/Code/abc239_b/Java/32510201
condefects-java_data_331
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); long x = sc.nextLong(); System.out.println((x-9)/10); } } import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); long x = sc.nextLong(); System.out.println(x<0?(x-9)/10:x/10); } }
ConDefects/ConDefects/Code/abc239_b/Java/37104826
condefects-java_data_332
//Har Har Mahadev import java.io.*; public class Main { public static void main(String[]args)throws IOException { BufferedReader buf=new BufferedReader(new InputStreamReader(System.in)); String inp=buf.readLine(); long r1=Long.parseLong(inp); if(r1>=-9&&r1<0) { System.out.println("-1"); return; } else if(r1<=9) { System.out.println(0); return; } if(inp.charAt(inp.length()-1)=='0'||inp.charAt(0)!='-') { System.out.println(inp.substring(0,inp.length()-1)); return; } long r=Long.parseLong(inp); r/=10; r--; System.out.println(r); } } //Har Har Mahadev import java.io.*; public class Main { public static void main(String[]args)throws IOException { BufferedReader buf=new BufferedReader(new InputStreamReader(System.in)); String inp=buf.readLine(); long r1=Long.parseLong(inp); if(r1>=-9&&r1<0) { System.out.println("-1"); return; } else if(r1<=9&&r1>=0) { System.out.println(0); return; } if(inp.charAt(inp.length()-1)=='0'||inp.charAt(0)!='-') { System.out.println(inp.substring(0,inp.length()-1)); return; } long r=Long.parseLong(inp); r/=10; r--; System.out.println(r); } }
ConDefects/ConDefects/Code/abc239_b/Java/35783392
condefects-java_data_333
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); long X = sc.nextLong(); long rsm = X / 10; if(rsm % 10 !=0 && X < 0){ rsm -= 1; } System.out.println(rsm); } } import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); long X = sc.nextLong(); long rsm = X / 10; if(X % 10 !=0 && X < 0){ rsm -= 1; } System.out.println(rsm); } }
ConDefects/ConDefects/Code/abc239_b/Java/31985000
condefects-java_data_334
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); long X = sc.nextLong(); long rsm = X / 10; if(X % 10 != 0 && rsm < 0){ rsm -= 1; System.out.println(rsm); } else{ System.out.println(rsm); } } } import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); long X = sc.nextLong(); long rsm = X / 10; if(X % 10 != 0 && X < 0){ rsm -= 1; System.out.println(rsm); } else{ System.out.println(rsm); } } }
ConDefects/ConDefects/Code/abc239_b/Java/32100030
condefects-java_data_335
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.NoSuchElementException; class Main implements Runnable { public static void main(String[] args) { new Main().run(); } final long p=998244353; long[] pow2=new long[(int)2e5+10]; long[] pow10=new long[(int)2e5+10]; { pow2[0]=1; pow10[0]=1; for (int i=1;i<pow2.length;++i) { pow2[i]=2*pow2[i-1]%p; pow10[i]=10*pow10[i-1]%p; } } public void run() { FastScanner sc = new FastScanner(); PrintWriter pw=new PrintWriter(System.out); char[] s=sc.next().toCharArray(); int n=s.length; long ans=0; long cum=1; for (int i=n-1;i>=0;--i) { int v=(int)(s[i]-'0'); ans+=v*pow2[i]%p*cum%p; ans%=p; cum=2*cum-pow10[n-1-i]+pow10[n-i]; cum%=p; } pw.println(ans); pw.close(); } void tr(Object...objects) { System.out.println(Arrays.deepToString(objects)); } } class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;} private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;} public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();} public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while(true){ if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next());} } import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.NoSuchElementException; class Main implements Runnable { public static void main(String[] args) { new Main().run(); } final long p=998244353; long[] pow2=new long[(int)2e5+10]; long[] pow10=new long[(int)2e5+10]; { pow2[0]=1; pow10[0]=1; for (int i=1;i<pow2.length;++i) { pow2[i]=2*pow2[i-1]%p; pow10[i]=10*pow10[i-1]%p; } } public void run() { FastScanner sc = new FastScanner(); PrintWriter pw=new PrintWriter(System.out); char[] s=sc.next().toCharArray(); int n=s.length; long ans=0; long cum=1; for (int i=n-1;i>=0;--i) { int v=(int)(s[i]-'0'); ans+=v*pow2[i]%p*cum%p; ans%=p; cum=2*cum-pow10[n-1-i]+pow10[n-i]; cum%=p; } pw.println((ans%p+p)%p); pw.close(); } void tr(Object...objects) { System.out.println(Arrays.deepToString(objects)); } } class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;} private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;} public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();} public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while(true){ if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next());} }
ConDefects/ConDefects/Code/abc224_f/Java/26780084
condefects-java_data_336
import java.util.Scanner; public class Main { public static void main(String[] args) { // 入力を受け取るためのオブジェクト Scanner sc = new Scanner(System.in); String line1 = sc.nextLine(); String[] line_1 = line1.split(" "); int L = Integer.parseInt(line_1[0]); int R = Integer.parseInt(line_1[1]); System.out.println(L); System.out.println(R); String S = sc.nextLine(); StringBuilder new_S = new StringBuilder(S); for(int i = 0; i<S.length();i++){ if ((i>=L)&(i<=R)){ //char t = S.charAt(i); new_S.setCharAt(i-1,S.charAt(R-i+L-1)); new_S.setCharAt(R-i+L-1,S.charAt(i-1)); } } System.out.println(new_S); } } import java.util.Scanner; public class Main { public static void main(String[] args) { // 入力を受け取るためのオブジェクト Scanner sc = new Scanner(System.in); String line1 = sc.nextLine(); String[] line_1 = line1.split(" "); int L = Integer.parseInt(line_1[0]); int R = Integer.parseInt(line_1[1]); String S = sc.nextLine(); StringBuilder new_S = new StringBuilder(S); for(int i = 0; i<S.length();i++){ if ((i>=L)&(i<=R)){ //char t = S.charAt(i); new_S.setCharAt(i-1,S.charAt(R-i+L-1)); new_S.setCharAt(R-i+L-1,S.charAt(i-1)); } } System.out.println(new_S); } }
ConDefects/ConDefects/Code/abc233_b/Java/44203241
condefects-java_data_337
import java.util.Scanner; public class Main { public static void main(String[] args) { @SuppressWarnings("resource") Scanner sc = new Scanner(System.in); double a = sc.nextDouble(); double b = sc.nextDouble(); double low = 1; double high = 1e18; for (int i = 0 ; i < 5000 ; i++) { double c1 = (low * 2 + high) / 3; double c2 = (low + high * 2) / 3; if (f(a,b,c1) > f(a,b,c2)) low = c1; else high = c2; } System.out.println(f(a,b,Math.round(low))); } static double f (double a, double b, double x) { return b * (x - 1) + a / Math.sqrt(x); } } import java.util.Scanner; public class Main { public static void main(String[] args) { @SuppressWarnings("resource") Scanner sc = new Scanner(System.in); double a = sc.nextDouble(); double b = sc.nextDouble(); double low = 1; double high = 1e18; for (int i = 0 ; i < 5000 ; i++) { double c1 = (low * 2 + high) / 3; double c2 = (low + high * 2) / 3; if (f(a,b,c1) > f(a,b,c2)) low = c1; else high = c2; } System.out.println(Math.min(f(a,b,Math.floor(low)), f(a,b,Math.ceil(low)))); } static double f (double a, double b, double x) { return b * (x - 1) + a / Math.sqrt(x); } }
ConDefects/ConDefects/Code/abc279_d/Java/37131801
condefects-java_data_338
import sun.reflect.generics.tree.Tree; import java.awt.image.ImageProducer; import java.io.*; import java.util.*; import java.math.*; import java.util.Set; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { int n= in.nextInt(); A[]a=new A[n]; int x=0; int min=Integer.MAX_VALUE; long sum=0; int cnt=0; for (int i = 0; i < n; i++) { a[i]=new A(in.nextInt(), in.nextInt()); if (a[i].a>a[i].b){ sum=sum+a[i].a; min=Math.min(a[i].a-a[i].b,min); cnt++; } else { sum=sum+a[i].b; min=Math.min(a[i].b-a[i].a,min); } } if (cnt%2==1||((n-cnt)%2==0&&cnt!=0)) System.out.println(sum-min); else System.out.println(sum); } class A { int a, b; public A(int a, int b) { this.a = a; this.b = b; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; A a1 = (A) o; return a == a1.a && b == a1.b; } @Override public int hashCode() { return Objects.hash(a, b); } } public static long gcd(long a, long b) { return (a == 0 ? b : gcd(b % a, a)); } static long mod = (long) 1e9+7; public static long qpow(long a, int n) { long x = 1; while (n > 0) { if ((n & 1) == 1) { x = x % mod * a % mod; } a = a * a % mod; n >>= 1; } return x; } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } boolean hasNext() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (Exception e) { return false; // TODO: handle exception } } return true; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public String nextLine() { String str = null; try { str = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } public BigInteger nextBigInteger() { return new BigInteger(next()); } public BigDecimal nextBigDecimal() { return new BigDecimal(next()); } } } import sun.reflect.generics.tree.Tree; import java.awt.image.ImageProducer; import java.io.*; import java.util.*; import java.math.*; import java.util.Set; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { int n= in.nextInt(); A[]a=new A[n]; int x=0; int min=Integer.MAX_VALUE; long sum=0; int cnt=0; for (int i = 0; i < n; i++) { a[i]=new A(in.nextInt(), in.nextInt()); if (a[i].a>a[i].b){ sum=sum+a[i].a; min=Math.min(a[i].a-a[i].b,min); cnt++; } else { sum=sum+a[i].b; min=Math.min(a[i].b-a[i].a,min); } } if (cnt%2==1) System.out.println(sum-min); else System.out.println(sum); } class A { int a, b; public A(int a, int b) { this.a = a; this.b = b; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; A a1 = (A) o; return a == a1.a && b == a1.b; } @Override public int hashCode() { return Objects.hash(a, b); } } public static long gcd(long a, long b) { return (a == 0 ? b : gcd(b % a, a)); } static long mod = (long) 1e9+7; public static long qpow(long a, int n) { long x = 1; while (n > 0) { if ((n & 1) == 1) { x = x % mod * a % mod; } a = a * a % mod; n >>= 1; } return x; } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } boolean hasNext() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (Exception e) { return false; // TODO: handle exception } } return true; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public String nextLine() { String str = null; try { str = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } public BigInteger nextBigInteger() { return new BigInteger(next()); } public BigDecimal nextBigDecimal() { return new BigDecimal(next()); } } }
ConDefects/ConDefects/Code/arc164_c/Java/43430329
condefects-java_data_339
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); new Main().main(sc); } public void main(Scanner sc) { int n = sc.nextInt(); Card[] cards = new Card[n]; int ura = 0; long ans = 0; for (int i = 0; i < n; i++) { cards[i] = new Card(sc.nextLong(), sc.nextLong()); if (cards[i].a < cards[i].b) { ans += cards[i].b; ura++; } else { ans += cards[i].a; } } if (ura % 2 == 1 && ura * 2 < n) { System.out.println(ans); } else { Arrays.sort(cards, (c1, c2) -> Long.compare(c1.a, c2.a)); long ans2 = 0; for (int i = 0; i < n; i++) { long tmp = ans - Math.abs(cards[i].a - cards[i].b); ans2 = Math.max(tmp, ans2); } System.out.println(ans2); } } private static class Card { long a, b; public Card(long a, long b) { this.a = a; this.b = b; } } } import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); new Main().main(sc); } public void main(Scanner sc) { int n = sc.nextInt(); Card[] cards = new Card[n]; int ura = 0; long ans = 0; for (int i = 0; i < n; i++) { cards[i] = new Card(sc.nextLong(), sc.nextLong()); if (cards[i].a < cards[i].b) { ans += cards[i].b; ura++; } else { ans += cards[i].a; } } if ((ura % 2 == 0 && n % 2 == 0) || (ura % 2 == 1 && n % 2 == 1)) { System.out.println(ans); } else { Arrays.sort(cards, (c1, c2) -> Long.compare(c1.a, c2.a)); long ans2 = 0; for (int i = 0; i < n; i++) { long tmp = ans - Math.abs(cards[i].a - cards[i].b); ans2 = Math.max(tmp, ans2); } System.out.println(ans2); } } private static class Card { long a, b; public Card(long a, long b) { this.a = a; this.b = b; } } }
ConDefects/ConDefects/Code/arc164_c/Java/43433106
condefects-java_data_340
import java.util.Arrays; import java.util.Scanner; class Main { static long mod = 998244353; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long [] a = new long[n]; for(int i=0; i<n; i++) { a[i] = sc.nextLong(); } sc.close(); long X = (long)Math.sqrt(n); long [] dp = new long[n]; long [][] lazy = new long[n][(int)X+1]; // Second index just means the speed dp[0] = 1; for(int i=0; i<n; i++) { for(int j=0; j<X; j++) { dp[i] = (dp[i] + lazy[i][j])%mod; } for(int j=0; j<=X; j++) { if(i+j<n) { lazy[i+j][j] = (lazy[i+j][j] + lazy[i][j])%mod; } } if(a[i]>X) { for(int j=i+(int)a[i]; j<n; j+=a[i]) { dp[j] = (dp[j] + dp[i])%mod; } } else{ if(i+(int)a[i]<n) { lazy[i+(int)a[i]][(int)a[i]] = (dp[i] + lazy[i+(int)a[i]][(int)a[i]])%mod; } } } long ans = 0; for(int i=0; i<n; i++) ans= (ans + dp[i])%mod; System.out.println(ans); } public static String arrayToString(long[][] array) { StringBuilder result = new StringBuilder(); for (long[] row : array) { result.append(Arrays.toString(row)).append("\n"); } return result.toString(); } } import java.util.Arrays; import java.util.Scanner; class Main { static long mod = 998244353; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long [] a = new long[n]; for(int i=0; i<n; i++) { a[i] = sc.nextLong(); } sc.close(); long X = (long)Math.sqrt(n); long [] dp = new long[n]; long [][] lazy = new long[n][(int)X+1]; // Second index just means the speed dp[0] = 1; for(int i=0; i<n; i++) { for(int j=0; j<=X; j++) { dp[i] = (dp[i] + lazy[i][j])%mod; } for(int j=0; j<=X; j++) { if(i+j<n) { lazy[i+j][j] = (lazy[i+j][j] + lazy[i][j])%mod; } } if(a[i]>X) { for(int j=i+(int)a[i]; j<n; j+=a[i]) { dp[j] = (dp[j] + dp[i])%mod; } } else{ if(i+(int)a[i]<n) { lazy[i+(int)a[i]][(int)a[i]] = (dp[i] + lazy[i+(int)a[i]][(int)a[i]])%mod; } } } long ans = 0; for(int i=0; i<n; i++) ans= (ans + dp[i])%mod; System.out.println(ans); } public static String arrayToString(long[][] array) { StringBuilder result = new StringBuilder(); for (long[] row : array) { result.append(Arrays.toString(row)).append("\n"); } return result.toString(); } }
ConDefects/ConDefects/Code/abc335_f/Java/49261849
condefects-java_data_341
import java.math.BigInteger; import java.util.*; import java.io.*; import java.text.*; public class Main{ //SOLUTION BEGIN static final long MOD = 998244355; void pre() throws Exception{} void solve(int TC) throws Exception { int N = ni(); int[] A = new int[N]; for(int i = 0; i< N; i++)A[i] = ni(); int SMALL = 1000; long[][] sum = new long[1+SMALL][SMALL]; long[] ways = new long[N]; for(int i = N-1; i>= 0; i--) { ways[i]++; if(A[i] <= SMALL)ways[i] += sum[A[i]][i%A[i]]; else { for(int j = i+A[i]; j< N; j+= A[i]){ ways[i] += ways[j]; if(ways[i] >= MOD)ways[i] -= MOD; } } if(ways[i] >= MOD)ways[i] -= MOD; for(int v = 1; v <= SMALL; v++) { sum[v][i % v] += ways[i]; if (sum[v][i % v] >= MOD) sum[v][i % v] -= MOD; } } pn(ways[0]); } //SOLUTION END void hold(boolean b)throws Exception{if(!b)throw new Exception("Hold right there, Sparky!");} static void dbg(Object... o){System.err.println(Arrays.deepToString(o));} void exit(boolean b){if(!b)System.exit(0);} final long IINF = Long.MAX_VALUE/2; final int INF = Integer.MAX_VALUE/2; DecimalFormat df = new DecimalFormat("0.000000000000"); double PI = 3.141592653589793238462643383279502884197169399, eps = 1e-10; static boolean multipleTC = false, memory = true, fileIO = false; FastReader in;PrintWriter out; void run() throws Exception{ long ct = System.currentTimeMillis(); if (fileIO) { in = new FastReader(""); out = new PrintWriter(""); } else { in = new FastReader(); out = new PrintWriter(System.out); } //Solution Credits: Taranpreet Singh int T = multipleTC? ni():1; pre(); for (int t = 1; t <= T; t++) solve(t); out.flush(); out.close(); System.err.println("Runtime: " + (System.currentTimeMillis() - ct)); } public static void main(String[] args) throws Exception{ if(memory)new Thread(null, new Runnable() {public void run(){try{new Main().run();}catch(Exception e){e.printStackTrace();System.exit(1);}}}, "1", 1 << 26).start(); else new Main().run(); } int[][] make(int n, int e, int[] from, int[] to, boolean f){ int[][] g = new int[n][];int[]cnt = new int[n]; for(int i = 0; i< e; i++){ cnt[from[i]]++; if(f)cnt[to[i]]++; } for(int i = 0; i< n; i++)g[i] = new int[cnt[i]]; for(int i = 0; i< e; i++){ g[from[i]][--cnt[from[i]]] = to[i]; if(f)g[to[i]][--cnt[to[i]]] = from[i]; } return g; } int[][][] makeS(int n, int e, int[] from, int[] to, boolean f){ int[][][] g = new int[n][][];int[]cnt = new int[n]; for(int i = 0; i< e; i++){ cnt[from[i]]++; if(f)cnt[to[i]]++; } for(int i = 0; i< n; i++)g[i] = new int[cnt[i]][]; for(int i = 0; i< e; i++){ g[from[i]][--cnt[from[i]]] = new int[]{to[i], i, 0}; if(f)g[to[i]][--cnt[to[i]]] = new int[]{from[i], i, 1}; } return g; } int[][] make(int n, int[] par, boolean f){ int[][] g = new int[n][]; int[] cnt = new int[n]; for(int x:par)cnt[x]++; if(f)for(int i = 1; i< n; i++)cnt[i]++; for(int i = 0; i< n; i++)g[i] = new int[cnt[i]]; for(int i = 1; i< n-1; i++){ g[par[i]][--cnt[par[i]]] = i; if(f)g[i][--cnt[i]] = par[i]; } return g; } int find(int[] set, int u){return set[u] = (set[u] == u?u:find(set, set[u]));} int digit(long s){int brute = 0;while(s>0){s/=10;brute++;}return brute;} long gcd(long a, long b){return (b==0)?a:gcd(b,a%b);} int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);} int bit(long n){return (n==0)?0:(1+bit(n&(n-1)));} void p(Object... o){for(Object oo:o)out.print(oo+" ");} void pn(Object... o){ if(o.length == 0)out.println(""); for(int i = 0; i< o.length; i++){ out.print(o[i]); out.print((i+1 == o.length?"\n":" ")); } } void pni(Object... o){for(Object oo:o)out.print(oo+" ");out.println();out.flush();} String n()throws Exception{return in.next();} String nln()throws Exception{return in.nextLine();} int ni()throws Exception{return Integer.parseInt(in.next());} long nl()throws Exception{return Long.parseLong(in.next());} double nd()throws Exception{return Double.parseDouble(in.next());} class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws Exception{ br = new BufferedReader(new FileReader(s)); } String next() throws Exception{ while (st == null || !st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); }catch (IOException e){ throw new Exception(e.toString()); } } return st.nextToken(); } String nextLine() throws Exception{ String str; try{ str = br.readLine(); }catch (IOException e){ throw new Exception(e.toString()); } return str; } } } import java.math.BigInteger; import java.util.*; import java.io.*; import java.text.*; public class Main{ //SOLUTION BEGIN static final long MOD = 998244353; void pre() throws Exception{} void solve(int TC) throws Exception { int N = ni(); int[] A = new int[N]; for(int i = 0; i< N; i++)A[i] = ni(); int SMALL = 1000; long[][] sum = new long[1+SMALL][SMALL]; long[] ways = new long[N]; for(int i = N-1; i>= 0; i--) { ways[i]++; if(A[i] <= SMALL)ways[i] += sum[A[i]][i%A[i]]; else { for(int j = i+A[i]; j< N; j+= A[i]){ ways[i] += ways[j]; if(ways[i] >= MOD)ways[i] -= MOD; } } if(ways[i] >= MOD)ways[i] -= MOD; for(int v = 1; v <= SMALL; v++) { sum[v][i % v] += ways[i]; if (sum[v][i % v] >= MOD) sum[v][i % v] -= MOD; } } pn(ways[0]); } //SOLUTION END void hold(boolean b)throws Exception{if(!b)throw new Exception("Hold right there, Sparky!");} static void dbg(Object... o){System.err.println(Arrays.deepToString(o));} void exit(boolean b){if(!b)System.exit(0);} final long IINF = Long.MAX_VALUE/2; final int INF = Integer.MAX_VALUE/2; DecimalFormat df = new DecimalFormat("0.000000000000"); double PI = 3.141592653589793238462643383279502884197169399, eps = 1e-10; static boolean multipleTC = false, memory = true, fileIO = false; FastReader in;PrintWriter out; void run() throws Exception{ long ct = System.currentTimeMillis(); if (fileIO) { in = new FastReader(""); out = new PrintWriter(""); } else { in = new FastReader(); out = new PrintWriter(System.out); } //Solution Credits: Taranpreet Singh int T = multipleTC? ni():1; pre(); for (int t = 1; t <= T; t++) solve(t); out.flush(); out.close(); System.err.println("Runtime: " + (System.currentTimeMillis() - ct)); } public static void main(String[] args) throws Exception{ if(memory)new Thread(null, new Runnable() {public void run(){try{new Main().run();}catch(Exception e){e.printStackTrace();System.exit(1);}}}, "1", 1 << 26).start(); else new Main().run(); } int[][] make(int n, int e, int[] from, int[] to, boolean f){ int[][] g = new int[n][];int[]cnt = new int[n]; for(int i = 0; i< e; i++){ cnt[from[i]]++; if(f)cnt[to[i]]++; } for(int i = 0; i< n; i++)g[i] = new int[cnt[i]]; for(int i = 0; i< e; i++){ g[from[i]][--cnt[from[i]]] = to[i]; if(f)g[to[i]][--cnt[to[i]]] = from[i]; } return g; } int[][][] makeS(int n, int e, int[] from, int[] to, boolean f){ int[][][] g = new int[n][][];int[]cnt = new int[n]; for(int i = 0; i< e; i++){ cnt[from[i]]++; if(f)cnt[to[i]]++; } for(int i = 0; i< n; i++)g[i] = new int[cnt[i]][]; for(int i = 0; i< e; i++){ g[from[i]][--cnt[from[i]]] = new int[]{to[i], i, 0}; if(f)g[to[i]][--cnt[to[i]]] = new int[]{from[i], i, 1}; } return g; } int[][] make(int n, int[] par, boolean f){ int[][] g = new int[n][]; int[] cnt = new int[n]; for(int x:par)cnt[x]++; if(f)for(int i = 1; i< n; i++)cnt[i]++; for(int i = 0; i< n; i++)g[i] = new int[cnt[i]]; for(int i = 1; i< n-1; i++){ g[par[i]][--cnt[par[i]]] = i; if(f)g[i][--cnt[i]] = par[i]; } return g; } int find(int[] set, int u){return set[u] = (set[u] == u?u:find(set, set[u]));} int digit(long s){int brute = 0;while(s>0){s/=10;brute++;}return brute;} long gcd(long a, long b){return (b==0)?a:gcd(b,a%b);} int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);} int bit(long n){return (n==0)?0:(1+bit(n&(n-1)));} void p(Object... o){for(Object oo:o)out.print(oo+" ");} void pn(Object... o){ if(o.length == 0)out.println(""); for(int i = 0; i< o.length; i++){ out.print(o[i]); out.print((i+1 == o.length?"\n":" ")); } } void pni(Object... o){for(Object oo:o)out.print(oo+" ");out.println();out.flush();} String n()throws Exception{return in.next();} String nln()throws Exception{return in.nextLine();} int ni()throws Exception{return Integer.parseInt(in.next());} long nl()throws Exception{return Long.parseLong(in.next());} double nd()throws Exception{return Double.parseDouble(in.next());} class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws Exception{ br = new BufferedReader(new FileReader(s)); } String next() throws Exception{ while (st == null || !st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); }catch (IOException e){ throw new Exception(e.toString()); } } return st.nextToken(); } String nextLine() throws Exception{ String str; try{ str = br.readLine(); }catch (IOException e){ throw new Exception(e.toString()); } return str; } } }
ConDefects/ConDefects/Code/abc335_f/Java/49101562
condefects-java_data_342
import java.util.Scanner; class Main{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); int a = scan.nextInt(); int b = scan.nextInt(); int c = scan.nextInt(); int d = scan.nextInt(); String name; if( a > c ){ name = "Aoki"; } else if ( b > d ){ name = "Aoki"; } else { name = "Takahashi"; } System.out.println(name); } } import java.util.Scanner; class Main{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); int a = scan.nextInt(); int b = scan.nextInt(); int c = scan.nextInt(); int d = scan.nextInt(); String name; if( a > c ){ name = "Aoki"; } else if ( a == c && b > d ){ name = "Aoki"; } else { name = "Takahashi"; } System.out.println(name); } }
ConDefects/ConDefects/Code/abc245_a/Java/40507837
condefects-java_data_343
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int a=scanner.nextInt(); int b=scanner.nextInt(); int c=scanner.nextInt(); int d=scanner.nextInt(); if(a<c){ System.out.println("Takahashi"); } else if(a>c){ System.out.println("Aoki"); } else if(a==c){ if(b>d){ System.out.println("Aoki"); } else if(b<d){ System.out.println("Takahashi"); } } } } import java.util.Scanner; class Main { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int a=scanner.nextInt(); int b=scanner.nextInt(); int c=scanner.nextInt(); int d=scanner.nextInt(); if(a<c){ System.out.println("Takahashi"); } else if(a>c){ System.out.println("Aoki"); } else if(a==c){ if(b>d){ System.out.println("Aoki"); } else if(b<=d){ System.out.println("Takahashi"); } } } }
ConDefects/ConDefects/Code/abc245_a/Java/41489461
condefects-java_data_344
import java.io.*; import java.util.*; import javax.swing.plaf.TextUI; public class Main { public static void main(String[] args) { solve(); } public static void solve() { FastScanner sc = new FastScanner(); // 入力を受け取る int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt(), d = sc.nextInt(); int takahashiMin = a * 60 + b; int aokiMin = c * 60 + d + 1; if (takahashiMin <= aokiMin) { System.out.println("Takahashi"); } else { System.out.println("Aoki"); } } } // Scanner class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) { return true; } else { ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } } import java.io.*; import java.util.*; import javax.swing.plaf.TextUI; public class Main { public static void main(String[] args) { solve(); } public static void solve() { FastScanner sc = new FastScanner(); // 入力を受け取る int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt(), d = sc.nextInt(); int takahashiMin = a * 60 + b; int aokiMin = c * 60 + d; if (takahashiMin <= aokiMin) { System.out.println("Takahashi"); } else { System.out.println("Aoki"); } } } // Scanner class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) { return true; } else { ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } }
ConDefects/ConDefects/Code/abc245_a/Java/39138213
condefects-java_data_345
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); int d = sc.nextInt(); if(a*60 +b >= c*60+d){ System.out.print("Takahashi"); }else{ System.out.print("Aoki"); } } } import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); int d = sc.nextInt(); if(a*60 +b <= c*60+d){ System.out.print("Takahashi"); }else{ System.out.print("Aoki"); } } }
ConDefects/ConDefects/Code/abc245_a/Java/36518471
condefects-java_data_346
import java.util.Scanner; public class Main { public static void main(String[] args) { // 어느 날, 다카하시가 정확히 A시 B분(24시간제)에 일어났고, 아오키는 정확히 C시 D분 1초에 일어났다. // 다카하시가 아오키보다 일찍 일어나면 Takahashi를 인쇄하고, 그렇지 않으면 Aoki를 인쇄하라. Scanner scan = new Scanner(System.in); String tmp = scan.nextLine(); String[] arr = tmp.split(" "); int A = Integer.parseInt(arr[0]); int B = Integer.parseInt(arr[1]); int C = Integer.parseInt(arr[2]); int D = Integer.parseInt(arr[3]); String answer = (3600* A + 60*B) > (3600 * C + 60 * D + 1) ? "Takahashi" : "Aoki"; System.out.println(answer); } } import java.util.Scanner; public class Main { public static void main(String[] args) { // 어느 날, 다카하시가 정확히 A시 B분(24시간제)에 일어났고, 아오키는 정확히 C시 D분 1초에 일어났다. // 다카하시가 아오키보다 일찍 일어나면 Takahashi를 인쇄하고, 그렇지 않으면 Aoki를 인쇄하라. Scanner scan = new Scanner(System.in); String tmp = scan.nextLine(); String[] arr = tmp.split(" "); int A = Integer.parseInt(arr[0]); int B = Integer.parseInt(arr[1]); int C = Integer.parseInt(arr[2]); int D = Integer.parseInt(arr[3]); String answer = (3600* A + 60*B) < (3600 * C + 60 * D + 1) ? "Takahashi" : "Aoki"; System.out.println(answer); } }
ConDefects/ConDefects/Code/abc245_a/Java/35814777
condefects-java_data_347
import java.awt.Dimension; import java.awt.Point; import java.io.BufferedWriter; import java.io.Closeable; import java.io.FilterOutputStream; import java.io.Flushable; import java.io.InputStream; import java.io.InterruptedIOException; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintStream; import java.io.PrintWriter; import java.io.Writer; import java.math.BigInteger; import java.nio.charset.Charset; import java.nio.charset.IllegalCharsetNameException; import java.nio.charset.UnsupportedCharsetException; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.Formatter; import java.util.List; import java.util.Locale; import java.util.Random; import java.util.function.DoubleUnaryOperator; import java.util.function.IntBinaryOperator; import java.util.function.IntUnaryOperator; import java.util.function.LongBinaryOperator; import java.util.function.LongUnaryOperator; //Maybe use import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.ListIterator; import java.util.Map.Entry; import java.util.PriorityQueue; import java.util.TreeMap; import java.util.TreeSet; final class Main { private static final boolean autoFlush = false; private static final SimpleScanner sc = new SimpleScanner( System.in ); private static final SimpleWriter out = new SimpleWriter( System.out, autoFlush ); public static void main ( String[] args ) { int N = sc.nextInt(); long X = sc.nextLong(); long Y = sc.nextLong(); long[] A = sc.nextLong(N); HashMap<Long,Long> mapY1 = new HashMap<>(); mapY1.put(0L,0L); for(int i=0;i<N;i+=4){ HashMap<Long,Long> nextY = new HashMap<>(mapY1.size()<<1); for(Entry<Long,Long> subY:mapY1.entrySet()){ long key = subY.getKey(); long val = subY.getValue(); nextY.putIfAbsent(key+A[i],(val<<2)|1); nextY.putIfAbsent(key-A[i],val<<2); } mapY1 = nextY; } HashMap<Long,Long> mapX1 = new HashMap<>(); mapX1.put(0L,0L); for(int i=1;i<N;i+=4){ HashMap<Long,Long> nextX = new HashMap<>(mapX1.size()<<1); for(Entry<Long,Long> subX:mapX1.entrySet()){ long key = subX.getKey(); long val = subX.getValue(); nextX.putIfAbsent(key+A[i],(val<<2)|1); nextX.putIfAbsent(key-A[i],val<<2); } mapX1 = nextX; } HashMap<Long,Long> mapY2 = new HashMap<>(); mapY2.put(0L,0L); for(int i=2;i<N;i+=4){ HashMap<Long,Long> nextY = new HashMap<>(mapY2.size()<<1); for(Entry<Long,Long> subY:mapY2.entrySet()){ long key = subY.getKey(); long val = subY.getValue(); nextY.putIfAbsent(key+A[i],(val<<2)|1); nextY.putIfAbsent(key-A[i],val<<2); } mapY2 = nextY; } HashMap<Long,Long> mapX2 = new HashMap<>(); mapX2.put(0L,0L); for(int i=3;i<N;i+=4){ HashMap<Long,Long> nextX = new HashMap<>(mapX2.size()<<1); for(Entry<Long,Long> subX:mapX2.entrySet()){ long key = subX.getKey(); long val = subX.getValue(); nextX.putIfAbsent(key+A[i],(val<<2)|1); nextX.putIfAbsent(key-A[i],val<<2); } mapX2 = nextX; } String SX = null; for(long num:mapX1.keySet()){ if(mapX2.containsKey(X-num)){ StringBuilder sb = new StringBuilder(); long subX = (N>>1)%2==0? (mapX1.get(num)<<1)|mapX2.get(X-num): mapX1.get(num)|(mapX2.get(X-num)<<1); for(int i=1;i<N;i+=2){ sb.append((subX&1)>0?'U':'D'); subX >>>= 1; } SX = sb.reverse().toString(); break; } } if(SX==null){ System.out.println("No"); return; } String SY = null; for(long num:mapY1.keySet()){ if(mapY2.containsKey(Y-num)){ StringBuilder sb = new StringBuilder(); long subY = (N>>1)%2==0? (mapY1.get(num)<<1)|mapY2.get(Y-num): mapY1.get(num)|(mapY2.get(Y-num)<<1); for(int i=0;i<N;i+=2){ sb.append((subY&1)>0?'U':'D'); subY >>>= 1; } SY = sb.reverse().toString(); break; } } if(SY==null){ System.out.println("No"); return; } StringBuilder ans = new StringBuilder(); char bef = 'U'; for(int i=0;i<N;i++){ if(i%2==1){ ans.append(SX.charAt(i>>1)!=bef?'L':'R'); bef = SX.charAt(i>>1); } else{ ans.append(SY.charAt(i>>1)==bef?'L':'R'); bef = SY.charAt(i>>1); } } out.println("Yes"); out.println(ans.toString()); out.close(); } } /*                            ,.へ   ___                        ム  i 「ヒ_i〉                      ゝ 〈  ト ノ                      iニ()  i {            ____         | ヽ  i i          /_, ,‐-\         i  }  |  i       /(●) ( ● )\      {、  λ  ト-┤.     /   (__人__)  \    ,ノ ̄,!  i  ゝ、_    |    ´ ̄`     | ,. '´ハ ,!  ヽ、   ``、,__\           /" \ ヽ/    \ノ ノ   ハ ̄ r/::r―--―/:7 ノ    /      ヽ.     ヽ:<:'::'|:/  /   ,."         `ー 、   \ヽ:::|/   r'"      / ̄二二二二二二二二二二二二二二二二二ヽ      | 答 | A C │|       \_二二二二二二二二二二二二二二二二二ノ */ /*//////////////////////////////////////////////// * My Library * @author viral *///////////////////////////////////////////////// final class Factorial { //階乗とその逆元 private final long[] fact, inFact; private final long mod; /** * 1~Nの階乗とその逆元をmodで割ったあまりを事前に計算します。 * * @param N 計算範囲 * @param mod 法 */ public Factorial ( final int N, final long mod ) { fact = new long[N + 1]; fact[0] = fact[1] = 1; for ( int i = 2; i <= N; ++i ) { fact[i] = fact[i - 1] * i % mod; } inFact = new long[N + 1]; inFact[N] = MathFunction.modPow( fact[N], mod - 2, mod ); for ( int i = N; i > 0; --i ) { inFact[i - 1] = inFact[i] * i % mod; } inFact[0] = 1; this.mod = mod; } /** * num!をmodで割ったあまりを返します。 * * @param num * * @return num! */ public long getFact ( final int num ) { return fact[num]; } /** * num!^-1をmodで割ったあまりを返します。 * * @param num * * @return num! */ public long getInFact ( final int num ) { return inFact[num]; } /** * modを法とした時のnum^-1を返します。 * * @param num * * @return num! */ public long getInverse ( final int num ) { return fact[num - 1] * inFact[num] % mod; } /** * aCbをmodで割ったあまりを返します。 * * @param a * @param b * * @return aCb */ public long getCombi ( final int a, final int b ) { if ( a < b || a < 0 || b < 0 ) { return 0; } return ( fact[a] * inFact[a - b] % mod ) * inFact[b] % mod; } } final class ArrayFunction { /** * カウントソートによるソートです。 * 各要素が0以上であり最大値が十分小さい時はこちらの使用を推奨します。 * * @param array ソート対象のint型配列 * @param maximumLimit array内の最大要素 */ public static void countSort ( final int[] array, final int maximumLimit ) { final int[] list = new int[maximumLimit + 1]; for ( final int num: array ) { ++list[num]; } int temp = 0; for ( int i = 0; i < list.length; ++i ) { while ( list[i]-- > 0 ) { array[temp++] = i; } } } /** * 配列を右周りに90度回転させたものを返します。 * 長方形でない配列に関しての動作は保証していません。 * * @param array 回転させる行列 * * @return 回転させた配列 */ public static int[][] rotateR ( final int[][] array ) { final int[][] ans = new int[array[0].length][array.length]; for ( int i = 0; i < ans.length; ++i ) { for ( int j = 0; j < ans[i].length; ++j ) { ans[i][j] = array[ans[i].length - j - 1][i]; } } return ans; } /** * 配列を右周りに90度回転させたものを返します。 * 長方形でない配列に関しての動作は保証していません。 * * @param array 回転させる行列 * * @return 回転させた配列 */ public static long[][] rotateR ( final long[][] array ) { final long[][] ans = new long[array[0].length][array.length]; for ( int i = 0; i < ans.length; ++i ) { for ( int j = 0; j < ans[i].length; ++j ) { ans[i][j] = array[ans[i].length - j - 1][i]; } } return ans; } /** * 配列を右周りに90度回転させたものを返します。 * 長方形でない配列に関しての動作は保証していません。 * * @param array 回転させる行列 * * @return 回転させた配列 */ public static char[][] rotateR ( final char[][] array ) { final char[][] ans = new char[array[0].length][array.length]; for ( int i = 0; i < ans.length; ++i ) { for ( int j = 0; j < ans[i].length; ++j ) { ans[i][j] = array[ans[i].length - j - 1][i]; } } return ans; } /** * 配列を右周りに90度回転させたものを返します。 * 長方形でない配列に関しての動作は保証していません。 * * @param array 回転させる行列 * * @return 回転させた配列 */ public static double[][] rotateR ( final double[][] array ) { final double[][] ans = new double[array[0].length][array.length]; for ( int i = 0; i < ans.length; ++i ) { for ( int j = 0; j < ans[i].length; ++j ) { ans[i][j] = array[ans[i].length - j - 1][i]; } } return ans; } /** * 配列を右周りに90度回転させたものを返します。 * 長方形でない配列に関しての動作は保証していません。 * * @param array 回転させる行列 * * @return 回転させた配列 */ public static boolean[][] rotateR ( final boolean[][] array ) { final boolean[][] ans = new boolean[array[0].length][array.length]; for ( int i = 0; i < ans.length; ++i ) { for ( int j = 0; j < ans[i].length; ++j ) { ans[i][j] = array[ans[i].length - j - 1][i]; } } return ans; } /** * 配列を右周りに90度回転させたものを返します。 * 長方形でない配列に関しての動作は保証していません。 * * @param array 回転させる行列 * * @return 回転させた配列 */ public static <E> E[][] rotateR ( final E[][] array, final E[][] ans ) { for ( int i = 0; i < ans.length; ++i ) { for ( int j = 0; j < ans[i].length; ++j ) { ans[i][j] = array[ans[i].length - j - 1][i]; } } return ans; } /** * 配列を左周りに90度回転させたものを返します。 * 長方形でない配列に関しての動作は保証していません。 * * @param array 回転させる行列 * * @return 回転させた配列 */ public static int[][] rotateL ( final int[][] array ) { final int[][] ans = new int[array[0].length][array.length]; for ( int i = 0; i < ans.length; ++i ) { final int index = i; Arrays.setAll( ans[i], k -> array[k][ans.length - index - 1] ); } return ans; } /** * 配列を左周りに90度回転させたものを返します。 * 長方形でない配列に関しての動作は保証していません。 * * @param array 回転させる行列 * * @return 回転させた配列 */ public static long[][] rotateL ( final long[][] array ) { final long[][] ans = new long[array[0].length][array.length]; for ( int i = 0; i < ans.length; ++i ) { final int index = i; Arrays.setAll( ans[i], k -> array[k][ans.length - index - 1] ); } return ans; } /** * 配列を左周りに90度回転させたものを返します。 * 長方形でない配列に関しての動作は保証していません。 * * @param array 回転させる行列 * * @return 回転させた配列 */ public static char[][] rotateL ( final char[][] array ) { final char[][] ans = new char[array[0].length][array.length]; for ( int i = 0; i < ans.length; ++i ) { for ( int j = 0; j < ans[i].length; ++j ) { ans[i][j] = array[j][ans.length - i - 1]; } } return ans; } /** * 配列を左周りに90度回転させたものを返します。 * 長方形でない配列に関しての動作は保証していません。 * * @param array 回転させる行列 * * @return 回転させた配列 */ public static double[][] rotateL ( final double[][] array ) { final double[][] ans = new double[array[0].length][array.length]; for ( int i = 0; i < ans.length; ++i ) { for ( int j = 0; j < ans[i].length; ++j ) { ans[i][j] = array[j][ans.length - i - 1]; } } return ans; } /** * 配列を左周りに90度回転させたものを返します。 * 長方形でない配列に関しての動作は保証していません。 * * @param array 回転させる行列 * * @return 回転させた配列 */ public static boolean[][] rotateL ( final boolean[][] array ) { final boolean[][] ans = new boolean[array[0].length][array.length]; for ( int i = 0; i < ans.length; ++i ) { for ( int j = 0; j < ans[i].length; ++j ) { ans[i][j] = array[j][ans.length - i - 1]; } } return ans; } /** * 配列を左周りに90度回転させたものを返します。 * 長方形でない配列に関しての動作は保証していません。 * * @param array 回転させる行列 * * @return 回転させた配列 */ public static <E> E[][] rotateL ( final E[][] array, final E[][] ans ) { for ( int i = 0; i < ans.length; ++i ) { for ( int j = 0; j < ans[i].length; ++j ) { ans[i][j] = array[j][ans.length - i - 1]; } } return ans; } /** * 引数の配列の最長狭義増加部分列の長さを返します。 * * @param array 最長狭義増加部分列の長さを求める配列 * * @return 最長狭義増加部分列の長さ */ public static int lis ( int[] array ) { return lis( array, false ); } /** * 引数の配列指定されたインデックスの最長狭義増加部分列の長さを返します。 * * @param arrays 最長狭義増加部分列の長さを求める配列 * @param p 探索する配列のインデックス * * @return arrays[i][p](0 < = p < = arrays.length)の最長狭義増加部分列の長さ */ public static int lis ( int[][] arrays, int p ) { return lis( arrays, p, false ); } /** * 引数の配列の最長狭義増加部分列の長さを返します。 * * @param array 最長狭義増加部分列の長さを求める配列 * * @return 最長狭義増加部分列の長さ */ public static int lis ( long[] array ) { return lis( array, false ); } /** * 引数の配列指定されたインデックスの最長狭義増加部分列の長さを返します。 * * @param arrays 最長狭義増加部分列の長さを求める配列 * @param p 探索する配列のインデックス * * @return arrays[i][p](0 < = p < = arrays.length)の最長狭義増加部分列の長さ */ public static int lis ( long[][] arrays, int p ) { return lis( arrays, p, false ); } /** * 引数の配列の最長増加部分列の長さを返します。 * * @param array 最長増加部分列の長さを求める配列 * @param include 広義増加列ならtrue、狭義増加列ならfalse * * @return 最長狭義増加部分列の長さ */ public static int lis ( int[] array, boolean include ) { int[] list = new int[array.length]; Arrays.fill( list, Integer.MAX_VALUE ); for ( int num: array ) { int index = include ? Searcher.overSearch( list, num ) : Searcher.upSearch( list, num ); list[index] = Math.min( list[index], num ); } int answer = Searcher.underSearch( list, Integer.MAX_VALUE ); return answer + 1; } /** * 引数の配列指定されたインデックスの最長狭義増加部分列の長さを返します。 * * @param arrays 最長狭義増加部分列の長さを求める配列 * @param p 探索する配列のインデックス * @param include 広義増加列ならtrue、狭義増加列ならfalse * * @return arrays[i][p](0 < = p < = arrays.length)の最長狭義増加部分列の長さ */ public static int lis ( int[][] arrays, int p, boolean include ) { int[] list = new int[arrays.length]; Arrays.fill( list, Integer.MAX_VALUE ); for ( int[] array: arrays ) { int index = include ? Searcher.overSearch( list, array[p] ) : Searcher.upSearch( list, array[p] ); list[index] = Math.min( list[index], array[p] ); } int answer = Searcher.underSearch( list, Integer.MAX_VALUE ); return answer + 1; } /** * 引数の配列の最長増加部分列の長さを返します。 * * @param array 最長増加部分列の長さを求める配列 * @param include 広義増加列ならtrue、狭義増加列ならfalse * * @return 最長狭義増加部分列の長さ */ public static int lis ( long[] array, boolean include ) { long[] list = new long[array.length]; Arrays.fill( list, Long.MAX_VALUE ); for ( long num: array ) { int index = include ? Searcher.overSearch( list, num ) : Searcher.upSearch( list, num ); list[index] = Math.min( list[index], num ); } int answer = Searcher.underSearch( list, Long.MAX_VALUE ); return answer + 1; } /** * 引数の配列指定されたインデックスの最長狭義増加部分列の長さを返します。 * * @param arrays 最長狭義増加部分列の長さを求める配列 * @param p 探索する配列のインデックス * @param include 広義増加列ならtrue、狭義増加列ならfalse * * @return arrays[i][p](0 < = p < = arrays.length)の最長狭義増加部分列の長さ */ public static int lis ( long[][] arrays, int p, boolean include ) { long[] list = new long[arrays.length]; Arrays.fill( list, Long.MAX_VALUE ); for ( long[] array: arrays ) { int index = include ? Searcher.overSearch( list, array[p] ) : Searcher.upSearch( list, array[p] ); list[index] = Math.min( list[index], array[p] ); } int answer = Searcher.underSearch( list, Long.MAX_VALUE ); return answer + 1; } /** * 引数の情報から求められる有向辺に対してトポロジカルソートを行ないます。 * 戻り値は辺を表すint型二次元配列です。 * * @param route 有向グラフの隣接リスト * * @return トポロジカルソート済み有向グラフ */ public static int[][] topologicalSort ( final ArrayList<ArrayList<Integer>> route ) { final int[] count = new int[route.size()]; int pathCount = 0; for ( final ArrayList<Integer> path: route ) { for ( final int point: path ) { ++pathCount; ++count[point]; } } final ArrayDeque<Integer> deq = new ArrayDeque<>(); for ( int i = 1; i < count.length; ++i ) { if ( count[i] == 0 ) { deq.add( i ); } } final int[][] ans = new int[pathCount][2]; int index = 0; while ( deq.size() > 0 ) { int nowP = deq.pollFirst(); for ( final int nextP: route.get( nowP ) ) { ans[index][0] = nowP; ans[index++][1] = nextP; if ( --count[nextP] == 0 ) { deq.add( nextP ); } } } return ans; } /** * 引数の情報から求められる有向辺に対してトポロジカルソートを行ないます。 * 戻り値は辺を表すint型二次元配列です。 * * @param route 有向グラフの隣接リスト * * @return トポロジカルソート済み有向グラフ */ public static int[][] topologicalSort ( final int[][] route ) { final int[] count = new int[route.length]; int pathCount = 0; for ( final int[] path: route ) { for ( final int point: path ) { ++pathCount; ++count[point]; } } final ArrayDeque<Integer> deq = new ArrayDeque<>(); for ( int i = 1; i < count.length; ++i ) { if ( count[i] == 0 ) { deq.add( i ); } } final int[][] ans = new int[pathCount][2]; int index = 0; while ( deq.size() > 0 ) { int nowP = deq.pollFirst(); for ( final int nextP: route[nowP] ) { ans[index][0] = nowP; ans[index++][1] = nextP; if ( --count[nextP] == 0 ) { deq.add( nextP ); } } } return ans; } /** * 引数の位置の要素を交換します。 * * @param array 交換する要素の含まれる配列 * @param a 交換元 * @param b 交換先 */ public static void swap ( final int[] array, final int a, final int b ) { final int temp = array[a]; array[a] = array[b]; array[b] = temp; } /** * 引数の位置の要素を交換します。 * * @param array 交換する要素の含まれる配列 * @param a 交換元 * @param b 交換先 */ public static void swap ( final long[] array, final int a, final int b ) { final long temp = array[a]; array[a] = array[b]; array[b] = temp; } /** * 引数の位置の要素を交換します。 * * @param array 交換する要素の含まれる配列 * @param a 交換元 * @param b 交換先 */ public static void swap ( final double[] array, final int a, final int b ) { final double temp = array[a]; array[a] = array[b]; array[b] = temp; } /** * 引数の位置の要素を交換します。 * * @param array 交換する要素の含まれる配列 * @param a 交換元 * @param b 交換先 */ public static void swap ( final char[] array, final int a, final int b ) { final char temp = array[a]; array[a] = array[b]; array[b] = temp; } /** * 引数の位置の要素を交換します。 * * @param array 交換する要素の含まれる配列 * @param a 交換元 * @param b 交換先 */ public static void swap ( final boolean[] array, final int a, final int b ) { final boolean temp = array[a]; array[a] = array[b]; array[b] = temp; } /** * 引数の位置の要素を交換します。 * * @param array 交換する要素の含まれる配列 * @param a 交換元 * @param b 交換先 */ public static <E> void swap ( final E[] array, final int a, final int b ) { final E temp = array[a]; array[a] = array[b]; array[b] = temp; } /** * 引数の位置の要素を交換します。 * * @param array 交換する要素の含まれる配列 * @param a 交換元の第1インデックス * @param b 交換元の第2インデックス * @param c 交換先の第1インデックス * @param d 交換先の第2インデックス */ public static void swap ( final int[][] array, final int a, final int b, final int c, final int d ) { final int temp = array[a][b]; array[a][b] = array[c][d]; array[c][d] = temp; } /** * 引数の位置の要素を交換します。 * * @param array 交換する要素の含まれる配列 * @param a 交換元の第1インデックス * @param b 交換元の第2インデックス * @param c 交換先の第1インデックス * @param d 交換先の第2インデックス */ public static void swap ( final long[][] array, final int a, final int b, final int c, final int d ) { final long temp = array[a][b]; array[a][b] = array[c][d]; array[c][d] = temp; } /** * 引数の位置の要素を交換します。 * * @param array 交換する要素の含まれる配列 * @param a 交換元の第1インデックス * @param b 交換元の第2インデックス * @param c 交換先の第1インデックス * @param d 交換先の第2インデックス */ public static void swap ( final double[][] array, final int a, final int b, final int c, final int d ) { final double temp = array[a][b]; array[a][b] = array[c][d]; array[c][d] = temp; } /** * 引数の位置の要素を交換します。 * * @param array 交換する要素の含まれる配列 * @param a 交換元の第1インデックス * @param b 交換元の第2インデックス * @param c 交換先の第1インデックス * @param d 交換先の第2インデックス */ public static void swap ( final char[][] array, final int a, final int b, final int c, final int d ) { final char temp = array[a][b]; array[a][b] = array[c][d]; array[c][d] = temp; } /** * 引数の位置の要素を交換します。 * * @param array 交換する要素の含まれる配列 * @param a 交換元の第1インデックス * @param b 交換元の第2インデックス * @param c 交換先の第1インデックス * @param d 交換先の第2インデックス */ public static void swap ( final boolean[][] array, final int a, final int b, final int c, final int d ) { final boolean temp = array[a][b]; array[a][b] = array[c][d]; array[c][d] = temp; } /** * 引数の位置の要素を交換します。 * * @param array 交換する要素の含まれる配列 * @param a 交換元の第1インデックス * @param b 交換元の第2インデックス * @param c 交換先の第1インデックス * @param d 交換先の第2インデックス */ public static <E> void swap ( final E[][] array, final int a, final int b, final int c, final int d ) { final E temp = array[a][b]; array[a][b] = array[c][d]; array[c][d] = temp; } /** * 引数の配列の要素を並び替えた時の、辞書順で引数の次に当たる順番に並び替えます。 * 原則として、要素に重複が無いものとして処理します。重複がある場合の戻り値は保証されていません。 * * @param array 並び替え対象の配列 * * @return 辞書順で次に当たる配列がある場合はtrue、arrayが降順に並んでいるならfalse */ public static boolean nextPermutation ( final int[] array ) { int index1 = 0; for ( int i = 1; i < array.length; ++i ) { if ( array[i - 1] < array[i] ) { index1 = i; } } if ( --index1 < 0 ) { return false; } int index2 = 0; int min = Integer.MAX_VALUE; for ( int i = index1 + 1; i < array.length; ++i ) { if ( MathFunction.rangeCheckOpen( array[i], array[index1], min ) ) { min = array[i]; index2 = i; } } swap( array, index1, index2 ); Arrays.sort( array, index1 + 1, array.length ); return true; } /** * 引数の配列の要素を並び替えた時の、辞書順で引数の次に当たる順番に並び替えます。 * 原則として、要素に重複が無いものとして処理します。重複がある場合の戻り値は保証されていません。 * * @param array 並び替え対象の配列 * * @return 辞書順で次に当たる配列がある場合はtrue、arrayが降順に並んでいるならfalse */ public static boolean nextPermutation ( final long[] array ) { int index1 = 0; for ( int i = 1; i < array.length; ++i ) { if ( array[i - 1] < array[i] ) { index1 = i; } } if ( --index1 < 0 ) { return false; } int index2 = 0; long min = Long.MAX_VALUE; for ( int i = index1 + 1; i < array.length; ++i ) { if ( MathFunction.rangeCheckOpen( array[i], array[index1], min ) ) { min = array[i]; index2 = i; } } swap( array, index1, index2 ); Arrays.sort( array, index1 + 1, array.length ); return true; } /** * 引数の配列の要素を並び替えた時の、辞書順で引数の次に当たる順番に並び替えます。 * 原則として、要素に重複が無いものとして処理します。重複がある場合の戻り値は保証されていません。 * * @param array 並び替え対象の配列 * * @return 辞書順で次に当たる配列がある場合はtrue、arrayが降順に並んでいるならfalse */ public static boolean nextPermutation ( final char[] array ) { int index1 = 0; for ( int i = 1; i < array.length; ++i ) { if ( array[i - 1] < array[i] ) { index1 = i; } } if ( --index1 < 0 ) { return false; } int index2 = 0; int min = Integer.MAX_VALUE; for ( int i = index1 + 1; i < array.length; ++i ) { if ( MathFunction.rangeCheckOpen( array[i], array[index1], min ) ) { min = array[i]; index2 = i; } } swap( array, index1, index2 ); Arrays.sort( array, index1 + 1, array.length ); return true; } /** * 引数の配列の要素を並び替えた時の、辞書順で引数の次に当たる順番に並び替えます。 * 原則として、要素に重複が無いものとして処理します。重複がある場合の戻り値は保証されていません。 * * @param array 並び替え対象の配列 * * @return 辞書順で次に当たる配列がある場合はtrue、arrayが降順に並んでいるならfalse */ public static <E extends Comparable<E>> boolean nextPermutation ( final E[] array ) { int index1 = 0; for ( int i = 1; i < array.length; ++i ) { if ( array[i - 1].compareTo( array[i] ) < 0 ) { index1 = i; } } if ( --index1 < 0 ) { return false; } int index2 = -1; E min = MathFunction.max( array ); int subIndex = -1; E max = array[index1]; for ( int i = index1 + 1; i < array.length; ++i ) { if ( MathFunction.rangeCheckOpen( array[i], array[index1], min ) ) { min = array[i]; index2 = i; } if ( max.compareTo( array[i] ) < 0 ) { subIndex = i; max = array[i]; } } if ( index2 == -1 ) { swap( array, index1, subIndex ); } else { swap( array, index1, index2 ); } Arrays.sort( array, index1 + 1, array.length ); return true; } /** * 引数の配列と関数を元に配列を再構築します。 * * @param array * @param func */ public static int[] reBuild ( final int[] array, final IntUnaryOperator func ) { final int[] ans = new int[array.length]; for ( int i = 0; i < array.length; ++i ) { ans[i] = func.applyAsInt( array[i] ); } return ans; } /** * 引数の配列と関数を元に配列を再構築します。 * * @param array * @param func */ public static int[] reBuild ( final int[] array, final IntBinaryOperator func ) { final int[] ans = new int[array.length]; for ( int i = 0; i < array.length; ++i ) { ans[i] = func.applyAsInt( i, array[i] ); } return ans; } /** * 引数の配列と関数を元に配列を再構築します。 * * @param array * @param func */ public static long[] reBuild ( final long[] array, final LongUnaryOperator func ) { final long[] ans = new long[array.length]; for ( int i = 0; i < array.length; ++i ) { ans[i] = func.applyAsLong( array[i] ); } return ans; } /** * 引数の配列と関数を元に配列を再構築します。 * * @param array * @param func */ public static long[] reBuild ( final long[] array, final LongBinaryOperator func ) { final long[] ans = new long[array.length]; for ( int i = 0; i < array.length; ++i ) { ans[i] = func.applyAsLong( i, array[i] ); } return ans; } } final class Converter { /** * 渡された文字列を逆順にした文字列を返します。 * * @param str 元の文字列 * * @return strを逆順にした文字列 */ public static String reverse ( final String str ) { final StringBuilder sb = new StringBuilder(); for ( int i = str.length() - 1; i >= 0; --i ) { sb.append( str.charAt( i ) ); } return sb.toString(); } /** * 渡されたラッパークラスのListをプリミティヴ型の配列に変換します。 * * @param list 元のList * * @return listの要素をプリミティヴ型に変換した配列 */ public static int[] toIntArray ( final List<Integer> list ) { final int[] ans = new int[list.size()]; int index = 0; for ( int num : list ) { ans[index++] = num; } return ans; } /** * 渡されたラッパークラスのListをプリミティヴ型の配列に変換します。 * * @param list 元のList * * @return listの要素をプリミティヴ型に変換した配列 */ public static long[] toLongArray ( final List<Long> list ) { final long[] ans = new long[list.size()]; int index = 0; for ( long num : list ) { ans[index++] = num; } return ans; } /** * 渡されたラッパークラスのListをプリミティヴ型の配列に変換します。 * * @param list 元のList * * @return listの要素をプリミティヴ型に変換した配列 */ public static char[] toCharArray ( final List<Character> list ) { final char[] ans = new char[list.size()]; int index = 0; for ( char num : list ) { ans[index++] = num; } return ans; } /** * 渡されたラッパークラスのListをプリミティヴ型の配列に変換します。 * * @param list 元のList * * @return listの要素をプリミティヴ型に変換した配列 */ public static double[] toDoubleArray ( final List<Double> list ) { final double[] ans = new double[list.size()]; int index = 0; for ( double num : list ) { ans[index++] = num; } return ans; } /** * 渡されたラッパークラスのListをプリミティヴ型の配列に変換します。 * * @param list 元のList * * @return listの要素をプリミティヴ型に変換した配列 */ public static boolean[] toBooleanArray ( final List<Boolean> list ) { final boolean[] ans = new boolean[list.size()]; int index = 0; for ( boolean num : list ) { ans[index++] = num; } return ans; } /** * 渡されたラッパークラスのListをプリミティヴ型の配列に変換します。 * * @param list 元のList * * @return listの要素をプリミティヴ型に変換した配列 */ public static byte[] toByteArray ( final List<Byte> list ) { final byte[] ans = new byte[list.size()]; int index = 0; for ( byte num : list ) { ans[index++] = num; } return ans; } /** * 渡されたラッパークラスのListをプリミティヴ型の配列に変換します。 * * @param list 元のList * * @return listの要素をプリミティヴ型に変換した配列 */ public static short[] toShortArray ( final List<Short> list ) { final short[] ans = new short[list.size()]; int index = 0; for ( short num : list ) { ans[index++] = num; } return ans; } /** * 渡されたラッパークラスのListをプリミティヴ型の配列に変換します。 * * @param list 元のList * * @return listの要素をプリミティヴ型に変換した配列 */ public static float[] toFloatArray ( final List<Float> list ) { final float[] ans = new float[list.size()]; int index = 0; for ( float num : list ) { ans[index++] = num; } return ans; } } final class MathFunction { private static final int[] numberForIntPrime = {2, 7, 61}; private static final long[] numberForLongPrime = {2, 7, 61, 325, 9375, 28178, 450775, 9780504, 1795265022}; /** * aとbの最大公約数を求めます。戻り値は0以上であることが保証されます。 * * @param a 公約数を求める整数 * @param b 公約数を求める整数 * * @return aとbの最大公約数 */ public static long gcd ( long a, long b ) { a = Math.abs( a ); b = Math.abs( b ); if ( b == 0 ) { return a; } long temp; while ( ( temp = a % b ) != 0 ) { a = b; b = temp; } return b; } /** * aとbの最小公倍数を求めます。 * オーバーフロー検知は出来ません。 * * @param a 公倍数を求める整数 * @param b 公倍数を求める整数 * * @return aとbの最小公倍数 */ public static long lcm ( final long a, final long b ) { return a / gcd( a, b ) * b; } /** * 引数が素数か判定します。 * * @param n 検査対象 * * @return nが素数であるならtrue、素数でないならfalse */ public static boolean isPrime ( long n ) { n = Math.abs( n ); if ( n == 2L ) { return true; } if ( n == 1L || ( n & 1L ) == 0L ) { return false; } if ( n <= 4_759_123_141L ) { return isPrimeForInt( n ); } if ( n <= Long.MAX_VALUE / n ) { return isPrimeForLong( n ); } return isPrimeForBigInteger( n ); } private static boolean isPrimeForInt ( long n ) { long d = n - 1; while ( ( d & 1 ) == 0L ) { d >>= 1; } for ( final long a: numberForIntPrime ) { if ( a >= n ) { return true; } long t = d; long y = MathFunction.modPow( a, t, n ); while ( t < n - 1L && y != 1 && y != n - 1 ) { y = y * y % n; t <<= 1; } if ( y != n - 1 && ( t & 1L ) == 0 ) { return false; } } return true; } private static boolean isPrimeForLong ( long n ) { long d = n - 1L; while ( ( d & 1 ) == 0L ) { d >>= 1; } for ( final long a: numberForLongPrime ) { if ( a >= n ) { return true; } long t = d; long y = MathFunction.modPow( a, t, n ); while ( t < n - 1L && y != 1 && y != n - 1 ) { y = y * y % n; t <<= 1; } if ( y != n - 1 && ( t & 1L ) == 0 ) { return false; } } return true; } private static boolean isPrimeForBigInteger ( long n ) { long d = n - 1L; while ( ( d & 1 ) == 0L ) { d >>= 1; } final BigInteger bigN = BigInteger.valueOf( n ); final BigInteger bigNSubOne = bigN.subtract( BigInteger.ONE ); final BigInteger bigD = BigInteger.valueOf( d ); for ( final long a: numberForLongPrime ) { if ( a >= n ) { return true; } BigInteger t = bigD; BigInteger y = BigInteger.valueOf( a ).modPow( t, bigN ); while ( t.compareTo( bigNSubOne ) == -1 && !y.equals( BigInteger.ONE ) && !y.equals( bigNSubOne ) ) { y = y.multiply( y ).mod( bigN ); t = t.shiftLeft( 1 ); } if ( !y.equals( bigNSubOne ) && (t.intValue()&1) == 0 ) { return false; } } return true; } /** * num以下の素数を列挙します。 * * @param num 素数を探す上限値 * * @return num以下の素数のint型配列 */ public static int[] primes ( final int num ) { if ( num < 2 ) { return new int[0]; } final BitSet numbers = new BitSet( num + 1 ); numbers.set( 2, num + 1 ); final int limit = ( int )Math.sqrt( num ); for ( int i = 2; i <= limit; ++i ) { if ( numbers.get( i ) ) { for ( int j = i * i; j <= num; j += i ) { if ( numbers.get(j) ) { numbers.clear( j ); } } } } final int[] answer = new int[numbers.cardinality()]; int i = 2, index = 0; do { i = numbers.nextSetBit( i ); answer[index++] = i++; } while ( index != answer.length ); return answer; } /** * a**bを計算します。 * * @param a 被累乗数 * @param b 指数 * * @return a**b */ public static long pow ( long a, long b ) { long ans = 1; while ( b > 0 ) { if ( ( b & 1 ) == 1 ) { ans *= a; } a *= a; b >>= 1; } return ans; } /** * a**bをmodで割ったあまりを計算します。 * * @param a 被累乗数 * @param b 指数 * @param mod 法とする整数 * * @return a**bをmodで割ったあまり */ public static long modPow ( long a, long b, final long mod ) { long ans = 1; a %= mod; while ( b > 0 ) { if ( ( b & 1 ) == 1 ) { ans *= a; } ans %= mod; a *= a; a %= mod; b >>= 1; } return ans; } /** * N!を計算します。 * * @param N 階乗を求めるのに用いる値 * * @return N! */ public static long fact ( final int N ) { long ans = 1; for ( int i = 2; i <= N; ++i ) { ans *= i; } return ans; } /** * N!をmodで割ったあまりを計算します。 * * @param N 階乗を求めるのに用いる値 * @param mod 法とする整数 * * @return N!をmodで割ったあまり */ public static long modFact ( final int N, final long mod ) { long ans = 1; for ( int i = 2; i <= N; ++i ) { ans *= i; ans %= mod; } return ans; } /** * nCrを計算します。 * * @param n 二項係数を求めるのに用いる値 * @param r 二項係数を求めるのに用いる値 * * @return nCr */ public static long combi ( final long n, long r ) { if ( r < 0 || n < r ) { return 0; } long ans = 1; r = Math.min( n - r, r ); for ( int i = 0; i < r; ++i ) { ans *= n - i; ans /= i + 1; } return ans; } /** * nCrをmodで割ったあまりを計算します。 * * @param n 二項係数を求めるのに用いる値 * @param r 二項係数を求めるのに用いる値 * @param mod 法とする整数 * * @return nCrをmodで割ったあまり */ public static long modCombi ( final long n, long r, final long mod ) { if ( r < 0 || n < r ) { return 0; } long ans = 1; r = Math.min( n - r, r ); for ( int i = 0; i < r; ++i ) { ans *= ( n - i ) % mod; ans %= mod; ans *= modPow( i + 1, mod - 2, mod ); ans %= mod; } return ans; } /** * 引数の前半二点、後半二点で構成される二線分が交差しているか返します。 * * @param x1 点1のx座標 * @param y1 点1のy座標 * @param x2 点2のx座標 * @param y2 点2のy座標 * @param x3 点3のx座標 * @param y3 点3のy座標 * @param x4 点4のx座標 * @param y4 点4のy座標 * * @return 交差している(線分の端が他方の線分上に存在する場合も含む)場合は1、同一線分直線上なら0、それ以外は-1 */ public static int isCrossed ( final int x1, final int y1, final int x2, final int y2, final int x3, final int y3, final int x4, final int y4 ) { final long s1 = ( long )( x1 - x2 ) * ( y3 - y1 ) - ( long )( y1 - y2 ) * ( x3 - x1 ); final long t1 = ( long )( x1 - x2 ) * ( y4 - y1 ) - ( long )( y1 - y2 ) * ( x4 - x1 ); final long s2 = ( long )( x3 - x4 ) * ( y1 - y3 ) - ( long )( y3 - y4 ) * ( x1 - x3 ); final long t2 = ( long )( x3 - x4 ) * ( y2 - y3 ) - ( long )( y3 - y4 ) * ( x2 - x3 ); final long temp1 = s1 * t1; final long temp2 = s2 * t2; if ( temp1 > 0 || temp2 > 0 ) { return -1; } if ( temp1 == 0 && temp2 == 0 ) { return 0; } return 1; } /** * 引数の前半二点、後半二点で構成される二線分が交差しているか返します。 * * @param p1 点1 * @param p2 点2 * @param p3 点3 * @param p4 点4 * * @return 交差している(線分の端が他方の線分上に存在する場合も含む)場合は1、同一線分直線上なら0、それ以外は-1 */ public static int isCrossed ( final Point p1, final Point p2, final Point p3, final Point p4 ) { return isCrossed( p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, p4.x, p4.y ); } /** * 指定された頂点を順に結んで出来上がる多角形が凸多角形か判定します。 * * @param points 多角形を構成する点 * * @return 多角形が凸多角形ならtrue */ public static boolean isConvex ( final Point... points ) { final int n = points.length; if ( n < 3 ) { return false; } if ( n == 3 ) { return true; } boolean conv = true; for ( int i = 0; i < n; ++i ) { int result = isCrossed( points[i], points[( i + 2 ) % n], points[( i + 1 ) % n], points[( i + 1 + n / 2 ) % n] ); conv &= result >= 0; } return conv; } /** * numをmodで割ったあまりを返します。 * 戻り値は0以上mod未満であることが保証されます。 * * @param num 被除算数 * @param mod 法とする値 * * @return numをmodで割ったあまり */ public static long remainder ( long num, final long mod ) { num %= mod; if ( num < 0 ) { num += mod; } return num; } /** * numが何桁かを返します。 * 0は1桁として捉えます。 * * @param num 調べる整数 * * @return numの桁数 */ public static int digit ( final long num ) { if ( num < 10L ) { return 1; } if ( num < 100L ) { return 2; } if ( num < 1000L ) { return 3; } if ( num < 10000L ) { return 4; } if ( num < 100000L ) { return 5; } if ( num < 1000000L ) { return 6; } if ( num < 10000000L ) { return 7; } if ( num < 100000000L ) { return 8; } if ( num < 1000000000L ) { return 9; } if ( num < 10000000000L ) { return 10; } if ( num < 100000000000L ) { return 11; } if ( num < 1000000000000L ) { return 12; } if ( num < 10000000000000L ) { return 13; } if ( num < 100000000000000L ) { return 14; } if ( num < 1000000000000000L ) { return 15; } if ( num < 10000000000000000L ) { return 16; } if ( num < 100000000000000000L ) { return 17; } if ( num < 1000000000000000000L ) { return 18; } return 19; } public static int max ( final int... nums ) { int ans = Integer.MIN_VALUE; for ( int num: nums ) { ans = Math.max( ans, num ); } return ans; } public static long max ( final long... nums ) { long ans = Long.MIN_VALUE; for ( long num: nums ) { ans = Math.max( ans, num ); } return ans; } public static double max ( final double... nums ) { double ans = -Double.MIN_VALUE; for ( double num: nums ) { ans = Math.max( ans, num ); } return ans; } public static <E extends Comparable<E>> E max ( final E[] nums ) { E ans = nums[0]; for ( E value: nums ) { if ( ans.compareTo( value ) > 0 ) { ans = value; } } return ans; } public static int min ( final int... nums ) { int ans = Integer.MAX_VALUE; for ( int num: nums ) { ans = Math.min( ans, num ); } return ans; } public static long min ( final long... nums ) { long ans = Long.MAX_VALUE; for ( long num: nums ) { ans = Math.min( ans, num ); } return ans; } public static double min ( final double... nums ) { double ans = Double.MAX_VALUE; for ( double num: nums ) { ans = Math.min( ans, num ); } return ans; } public static <E extends Comparable<E>> E min ( final E[] nums ) { E ans = nums[0]; for ( E value: nums ) { if ( ans.compareTo( value ) < 0 ) { ans = value; } } return ans; } public static long sum ( final int... nums ) { long ans = 0; for ( int num: nums ) { ans += num; } return ans; } public static long sum ( final long... nums ) { long ans = 0; for ( long num: nums ) { ans += num; } return ans; } public static long modSum ( final long mod, final int... nums ) { long ans = 0; for ( int num: nums ) { ans += num; ans %= mod; } if ( ans < 0 ) { ans += mod; } return ans; } public static long modSum ( final long mod, final long... nums ) { long ans = 0; for ( long num: nums ) { ans += num; ans %= mod; } if ( ans < 0 ) { ans += mod; } return ans; } public static long sum ( final int[] nums, int from, int to ) { long ans = 0; for ( int i = from; i < to; ++i ) { ans += nums[i]; } return ans; } public static long sum ( final long[] nums, int from, int to ) { long ans = 0; for ( int i = from; i < to; ++i ) { ans += nums[i]; } return ans; } public static long modSum ( final int[] nums, int from, int to, long mod ) { long ans = 0; for ( int i = from; i < to; ++i ) { ans += nums[i]; ans %= mod; } if ( ans < 0 ) { ans += mod; } return ans; } public static long modSum ( final long[] nums, int from, int to, long mod ) { long ans = 0; for ( int i = from; i < to; ++i ) { ans += nums[i]; ans %= mod; } if ( ans < 0 ) { ans += mod; } return ans; } /** * 引数numがl以上r未満の範囲内か判定します。 * * @param num 判定する値 * @param l 下限(lを含む) * @param r 上限(rを含まない) * * @return l <= num < rを満たしていればtrue 、 満たしていなければfalse */ public static boolean rangeCheck ( final int num, final int l, final int r ) { return l <= num && num < r; } /** * 引数numがl以上r未満の範囲内か判定します。 * * @param num 判定する値 * @param l 下限(lを含む) * @param r 上限(rを含まない) * * @return l <= num < rを満たしていればtrue 、 満たしていなければfalse */ public static boolean rangeCheck ( final long num, final long l, final long r ) { return l <= num && num < r; } /** * 引数numがl以上r未満の範囲内か判定します。 * * @param num 判定する値 * @param l 下限(lを含む) * @param r 上限(rを含まない) * * @return l <= num < rを満たしていればtrue 、 満たしていなければfalse */ public static boolean rangeCheck ( final double num, final double l, final double r ) { return l <= num && num < r; } /** * 引数numがl以上r未満の範囲内か判定します。 * * @param num 判定する値 * @param l 下限(lを含む) * @param r 上限(rを含まない) * * @return l <= num < rを満たしていればtrue 、 満たしていなければfalse */ public static <E extends Comparable<E>> boolean rangeCheck ( final E num, final E l, final E r ) { return 0 <= l.compareTo( num ) && 0 < num.compareTo( r ); } /** * 引数numがlより大きく、r未満の範囲内か判定します。 * * @param num 判定する値 * @param l 下限(lを含まない) * @param r 上限(rを含まない) * * @return l < num < rを満たしていればtrue 、 満たしていなければfalse */ public static boolean rangeCheckOpen ( final int num, final int l, final int r ) { return l < num && num < r; } /** * 引数numがlより大きく、r未満の範囲内か判定します。 * * @param num 判定する値 * @param l 下限(lを含まない) * @param r 上限(rを含まない) * * @return l < num < rを満たしていればtrue 、 満たしていなければfalse */ public static boolean rangeCheckOpen ( final long num, final long l, final long r ) { return l < num && num < r; } /** * 引数numがlより大きく、r未満の範囲内か判定します。 * * @param num 判定する値 * @param l 下限(lを含まない) * @param r 上限(rを含まない) * * @return l < num < rを満たしていればtrue 、 満たしていなければfalse */ public static boolean rangeCheckOpen ( final double num, final double l, final double r ) { return l < num && num < r; } /** * 引数numがlより大きく、r未満の範囲内か判定します。 * * @param num 判定する値 * @param l 下限(lを含まない) * @param r 上限(rを含まない) * * @return l < num < rを満たしていればtrue 、 満たしていなければfalse */ public static <E extends Comparable<E>> boolean rangeCheckOpen ( final E num, final E l, final E r ) { return 0 < l.compareTo( num ) && 0 < num.compareTo( r ); } /** * 引数numがl以上r以下の範囲内か判定します。 * * @param num 判定する値 * @param l 下限(lを含む) * @param r 上限(rを含む) * * @return l <= num <= rを満たしていればtrue 、 満たしていなければfalse */ public static boolean rangeCheckClose ( final int num, final int l, final int r ) { return l <= num && num <= r; } /** * 引数numがl以上r以下の範囲内か判定します。 * * @param num 判定する値 * @param l 下限(lを含む) * @param r 上限(rを含む) * * @return l <= num <= rを満たしていればtrue 、 満たしていなければfalse */ public static boolean rangeCheckClose ( final long num, final long l, final long r ) { return l <= num && num <= r; } /** * 引数numがl以上r以下の範囲内か判定します。 * * @param num 判定する値 * @param l 下限(lを含む) * @param r 上限(rを含む) * * @return l <= num <= rを満たしていればtrue 、 満たしていなければfalse */ public static boolean rangeCheckClose ( final double num, final double l, final double r ) { return l <= num && num <= r; } /** * 引数numがl以上r以下の範囲内か判定します。 * * @param num 判定する値 * @param l 下限(lを含む) * @param r 上限(rを含む) * * @return l <= num <= rを満たしていればtrue 、 満たしていなければfalse */ public static <E extends Comparable<E>> boolean rangeCheckClose ( final E num, final E l, final E r ) { return 0 <= l.compareTo( num ) && 0 <= num.compareTo( r ); } /** * 引数の中でのmexを求めます。 * * @param num */ public static int mex ( final int... nums ) { final IntegerSet set = new IntegerSet( nums.length << 1 ); for ( final int num : nums ) { set.add( num ); } int ans = 0; while ( set.contains( ans ) ) { ++ans; } return ans; } } final class Searcher { private static final int CYCLE_COUNT = Double.MAX_EXPONENT + 53; /** * 配列内の指定された要素を探します。 * 配列内で見つかった場合はその要素と同一で最大のインデックスを返します。 * 見つからなかった場合は指定された要素未満で最大のインデックスを返します。 * もしそのような要素が存在しない場合は-1を返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 指定された要素以下で最大のインデックス */ public static int downSearch ( final int[] array, final int value ) { int a = 0, b = array.length - 1, ans = -1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] > value ) { b = c - 1; } else { a = ( ans = c ) + 1; } } return ans; } /** * 配列内の指定された要素を探します。 * 配列内で見つかった場合はその要素と同一で最大のインデックスを返します。 * 見つからなかった場合は指定された要素未満で最大のインデックスを返します。 * もしそのような要素が存在しない場合は-1を返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 指定された要素以下で最大のインデックス */ public static int downSearch ( final long[] array, final long value ) { int a = 0, b = array.length - 1, ans = -1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] > value ) { b = c - 1; } else { a = ( ans = c ) + 1; } } return ans; } /** * 配列内の指定された要素を探します。 * 配列内で見つかった場合はその要素と同一で最大のインデックスを返します。 * 見つからなかった場合は指定された要素未満で最大のインデックスを返します。 * もしそのような要素が存在しない場合は-1を返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 指定された要素以下で最大のインデックス */ public static int downSearch ( final double[] array, final double value ) { int a = 0, b = array.length - 1, ans = -1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] > value ) { b = c - 1; } else { a = ( ans = c ) + 1; } } return ans; } /** * 配列内の指定された要素を探します。 * 配列内で見つかった場合はその要素と同一で最大のインデックスを返します。 * 見つからなかった場合は指定された要素未満で最大のインデックスを返します。 * もしそのような要素が存在しない場合は-1を返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 指定された要素以下で最大のインデックス */ public static int downSearch ( final char[] array, final int value ) { int a = 0, b = array.length - 1, ans = -1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] > value ) { b = c - 1; } else { a = ( ans = c ) + 1; } } return ans; } /** * 配列内の指定された要素を探します。 * 配列内で見つかった場合はその要素と同一で最大のインデックスを返します。 * 見つからなかった場合は指定された要素未満で最大のインデックスを返します。 * もしそのような要素が存在しない場合は-1を返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 指定された要素以下で最大のインデックス */ public static <E extends Comparable<E>> int downSearch ( final E[] array, final E value ) { int a = 0, b = array.length - 1, ans = -1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c].compareTo( value ) > 0 ) { b = c - 1; } else { a = ( ans = c ) + 1; } } return ans; } /** * リスト内の指定された要素を探します。 * リスト内で見つかった場合はその要素と同一で最大のインデックスを返します。 * 見つからなかった場合は指定された要素未満で最大のインデックスを返します。 * もしそのような要素が存在しない場合は-1を返します。 * * @param list 探索対象のリスト * @param value 探索要素 * * @return 指定された要素以下で最大のインデックス */ public static <E extends Comparable<E>> int downSearch ( final List<E> list, final E value ) { int a = 0, b = list.size() - 1, ans = -1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( list.get( c ).compareTo( value ) > 0 ) { b = c - 1; } else { a = ( ans = c ) + 1; } } return ans; } /** * 広義単調増加な関数内の指定された値を探します。 * 関数内で見つかった場合はその値と同一で最大の引数を返します。 * 見つからなかった場合は指定された値未満で最大の引数を返します。 * もしそのような要素が存在しない場合は下限-1を返します。 * * @param a 探索範囲の下限 * @param b 探索範囲の上限 * @param value 探索値 * * @return 指定された値以下で最大の引数 */ public static int downSearch ( int a, int b, final int value, final IntUnaryOperator func ) { int ans = a - 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( func.applyAsInt( c ) > value ) { b = c - 1; } else { a = ( ans = c ) + 1; } } return ans; } /** * 広義単調増加な関数内の指定された値を探します。 * 関数内で見つかった場合はその値と同一で最大の引数を返します。 * 見つからなかった場合は指定された値未満で最大の引数を返します。 * もしそのような要素が存在しない場合は下限-1を返します。 * * @param a 探索範囲の下限 * @param b 探索範囲の上限 * @param value 探索値 * * @return 指定された値以下で最大の引数 */ public static long downSearch ( long a, long b, final long value, final LongUnaryOperator func ) { long ans = a - 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( func.applyAsLong( c ) > value ) { b = c - 1; } else { a = ( ans = c ) + 1; } } return ans; } /** * 広義単調増加な関数内の指定された値を探します。 * 関数内で見つかった場合はその値と同一で最大の引数を返します。 * 見つからなかった場合は指定された値未満で最大の引数を返します。 * もしそのような要素が存在しない場合は下限より小さい値を返します。 * * @param a 探索範囲の下限 * @param b 探索範囲の上限 * @param value 探索値 * * @return 指定された値以下で最大の引数 */ public static double search ( double a, double b, final double value, final DoubleUnaryOperator func ) { double ans = a - Math.abs( a ), c; for ( int $ = 0; $ < CYCLE_COUNT; ++$ ) { c = ( a + b ) / 2; if ( func.applyAsDouble( c ) > value ) { b = c; } else { a = ( ans = c ); } } return ans; } /** * 配列内の指定された要素を探します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return arrayにvalueが含まれているかを表すboolean */ public static boolean contains ( final int[] array, final int value ) { int a = 0, b = array.length - 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] > value ) { b = c - 1; } else if ( array[c] < value ) { a = c + 1; } else { return true; } } return false; } /** * 配列内の指定された要素を探します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return arrayにvalueが含まれているかを表すboolean */ public static boolean contains ( final long[] array, final long value ) { int a = 0, b = array.length - 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] > value ) { b = c - 1; } else if ( array[c] < value ) { a = c + 1; } else { return true; } } return false; } /** * 配列内の指定された要素を探します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return arrayにvalueが含まれているかを表すboolean */ public static boolean contains ( final double[] array, final double value ) { int a = 0, b = array.length - 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] > value ) { b = c - 1; } else if ( array[c] < value ) { a = c + 1; } else { return true; } } return false; } /** * 配列内の指定された要素を探します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return arrayにvalueが含まれているかを表すboolean */ public static boolean contains ( final char[] array, final char value ) { int a = 0, b = array.length - 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] > value ) { b = c - 1; } else if ( array[c] < value ) { a = c + 1; } else { return true; } } return false; } /** * 配列内の指定された要素を探します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return arrayにvalueが含まれているかを表すboolean */ public static <E extends Comparable<E>> boolean contains ( final E[] array, final E value ) { int a = 0, b = array.length - 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; int result = array[c].compareTo( value ); if ( result > 0 ) { b = c - 1; } else if ( result < 0 ) { a = c + 1; } else { return true; } } return false; } /** * リスト内の指定された要素を探します。 * * @param list 探索対象のリスト * @param value 探索要素 * * @return listにvalueが含まれているかを表すboolean */ public static <E extends Comparable<E>> boolean contains ( final List<E> list, final E value ) { int a = 0, b = list.size() - 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; int result = list.get( c ).compareTo( value ); if ( result > 0 ) { b = c - 1; } else if ( result < 0 ) { a = c + 1; } else { return true; } } return false; } /** * 広義単調増加な関数内の指定された値を探します。 * * @param a 探索範囲の下限 * @param b 探索範囲の上限 * @param value 探索値 * * @return この関数がa以上b以下で探索値を取るかを表すboolean */ public static boolean contains ( int a, int b, final int value, final IntUnaryOperator func ) { int c; while ( a - b < 1 ) { c = ( a + b ) / 2; int num = func.applyAsInt( c ); if ( num > value ) { b = c - 1; } else if ( num < value ) { a = c + 1; } else { return true; } } return false; } /** * 広義単調増加な関数内の指定された値を探します。 * * @param a 探索範囲の下限 * @param b 探索範囲の上限 * @param value 探索値 * * @return この関数がa以上b以下で探索値を取るかを表すboolean */ public static boolean contains ( long a, long b, final long value, final LongUnaryOperator func ) { long c; while ( a - b < 1 ) { c = ( a + b ) / 2; long num = func.applyAsLong( c ); if ( num > value ) { b = c - 1; } else if ( num < value ) { a = c + 1; } else { return true; } } return false; } /** * 配列内の指定された要素を探します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return arrayのvalueのインデックス(無ければ - 1) */ public static int search ( final int[] array, final int value ) { int a = 0, b = array.length - 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] > value ) { b = c - 1; } else if ( array[c] < value ) { a = c + 1; } else { return c; } } return -1; } /** * 配列内の指定された要素を探します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return arrayのvalueのインデックス(無ければ - 1) */ public static int search ( final long[] array, final long value ) { int a = 0, b = array.length - 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] > value ) { b = c - 1; } else if ( array[c] < value ) { a = c + 1; } else { return c; } } return -1; } /** * 配列内の指定された要素を探します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return arrayのvalueのインデックス(無ければ - 1) */ public static int search ( final double[] array, final double value ) { int a = 0, b = array.length - 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] > value ) { b = c - 1; } else if ( array[c] < value ) { a = c + 1; } else { return c; } } return -1; } /** * 配列内の指定された要素を探します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return arrayのvalueのインデックス(無ければ - 1) */ public static int search ( final char[] array, final char value ) { int a = 0, b = array.length - 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] > value ) { b = c - 1; } else if ( array[c] < value ) { a = c + 1; } else { return c; } } return -1; } /** * 配列内の指定された要素を探します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return arrayにvalueが含まれているかを表すboolean */ public static <E extends Comparable<E>> int search ( final E[] array, final E value ) { int a = 0, b = array.length - 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; int result = array[c].compareTo( value ); if ( result > 0 ) { b = c - 1; } else if ( result < 0 ) { a = c + 1; } else { return c; } } return -1; } /** * リスト内の指定された要素を探します。 * * @param list 探索対象のリスト * @param value 探索要素 * * @return listのvalueのインデックス(無ければ - 1) */ public static <E extends Comparable<E>> int search ( final List<E> list, final E value ) { int a = 0, b = list.size() - 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; int result = list.get( c ).compareTo( value ); if ( result > 0 ) { b = c - 1; } else if ( result < 0 ) { a = c + 1; } else { return c; } } return -1; } /** * 広義単調増加な関数内の指定された値を探します。 * * @param a 探索範囲の下限 * @param b 探索範囲の上限 * @param value 探索値 * * @return この関数がvalueを取る引数(無ければa - 1) */ public static int search ( int a, int b, final int value, final IntUnaryOperator func ) { int c; while ( a - b < 1 ) { c = ( a + b ) / 2; int num = func.applyAsInt( c ); if ( num > value ) { b = c - 1; } else if ( num < value ) { a = c + 1; } else { return c; } } return a - 1; } /** * 広義単調増加な関数内の指定された値を探します。 * * @param a 探索範囲の下限 * @param b 探索範囲の上限 * @param value 探索値 * * @return この関数がvalueを取る引数(無ければa - 1) */ public static long search ( long a, long b, final long value, final LongUnaryOperator func ) { long c; while ( a - b < 1 ) { c = ( a + b ) / 2; long num = func.applyAsLong( c ); if ( num > value ) { b = c - 1; } else if ( num < value ) { a = c + 1; } else { return c; } } return a - 1; } /** * 配列内の指定された要素を探します。 * 配列内で見つかった場合はその要素と同一で最小のインデックスを返します。 * 見つからなかった場合は指定された要素以上で最小のインデックスを返します。 * もしそのような要素が存在しない場合はarray.lengthを返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 指定された要素以上で最小のインデックス */ public static int upSearch ( final int[] array, final int value ) { int a = 0, b = array.length - 1, ans = array.length, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] >= value ) { b = ( ans = c ) - 1; } else { a = c + 1; } } return ans; } /** * 配列内の指定された要素を探します。 * 配列内で見つかった場合はその要素と同一で最小のインデックスを返します。 * 見つからなかった場合は指定された要素以上で最小のインデックスを返します。 * もしそのような要素が存在しない場合はarray.lengthを返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 指定された要素以上で最小のインデックス */ public static int upSearch ( final long[] array, final long value ) { int a = 0, b = array.length - 1, ans = array.length, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] >= value ) { b = ( ans = c ) - 1; } else { a = c + 1; } } return ans; } /** * 配列内の指定された要素を探します。 * 配列内で見つかった場合はその要素と同一で最小のインデックスを返します。 * 見つからなかった場合は指定された要素以上で最小のインデックスを返します。 * もしそのような要素が存在しない場合はarray.lengthを返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 指定された要素以上で最小のインデックス */ public static int upSearch ( final double[] array, final double value ) { int a = 0, b = array.length - 1, ans = array.length, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] >= value ) { b = ( ans = c ) - 1; } else { a = c + 1; } } return ans; } /** * 配列内の指定された要素を探します。 * 配列内で見つかった場合はその要素と同一で最小のインデックスを返します。 * 見つからなかった場合は指定された要素以上で最小のインデックスを返します。 * もしそのような要素が存在しない場合はarray.lengthを返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 指定された要素以上で最小のインデックス */ public static int upSearch ( final char[] array, final char value ) { int a = 0, b = array.length - 1, ans = array.length, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] >= value ) { b = ( ans = c ) - 1; } else { a = c + 1; } } return ans; } /** * 配列内の指定された要素を探します。 * 配列内で見つかった場合はその要素と同一で最小のインデックスを返します。 * 見つからなかった場合は指定された要素以上で最小のインデックスを返します。 * もしそのような要素が存在しない場合はarray.lengthを返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 指定された要素以上で最小のインデックス */ public static <E extends Comparable<E>> int upSearch ( final E[] array, final E value ) { int a = 0, b = array.length - 1, ans = array.length, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c].compareTo( value ) >= 0 ) { b = ( ans = c ) - 1; } else { a = c + 1; } } return ans; } /** * リスト内の指定された要素を探します。 * リスト内で見つかった場合はその要素と同一で最小のインデックスを返します。 * 見つからなかった場合は指定された要素以上で最小のインデックスを返します。 * もしそのような要素が存在しない場合はlist.size()を返します。 * * @param list 探索対象のリスト * @param value 探索要素 * * @return 指定された要素以上で最小のインデックス */ public static <E extends Comparable<E>> int upSearch ( final List<E> list, final E value ) { int a = 0, b = list.size() - 1, ans = list.size(), c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( list.get( c ).compareTo( value ) >= 0 ) { b = ( ans = c ) - 1; } else { a = c + 1; } } return ans; } /** * 広義単調増加な関数内の指定された値を探します。 * 関数内で見つかった場合はその値と同一で最小の引数を返します。 * 見つからなかった場合は指定された値以上で最大の引数を返します。 * もしそのような要素が存在しない場合は上限+1を返します。 * * @param a 探索範囲の下限 * @param b 探索範囲の上限 * @param value 探索値 * * @return 指定された値以上で最小の引数 */ public static int upSearch ( int a, int b, final int value, final IntUnaryOperator func ) { int ans = b + 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( func.applyAsInt( c ) >= value ) { b = ( ans = c ) - 1; } else { a = c + 1; } } return ans; } /** * 広義単調増加な関数内の指定された値を探します。 * 関数内で見つかった場合はその値と同一で最小の引数を返します。 * 見つからなかった場合は指定された値以上で最大の引数を返します。 * もしそのような要素が存在しない場合は上限+1を返します。 * * @param a 探索範囲の下限 * @param b 探索範囲の上限 * @param value 探索値 * * @return 指定された値以上で最小の引数 */ public static long upSearch ( long a, long b, final long value, final LongUnaryOperator func ) { long ans = b + 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( func.applyAsLong( c ) >= value ) { b = ( ans = c ) - 1; } else { a = c + 1; } } return ans; } /** * 配列内の指定された要素より小さい要素を探します。 * 配列内で見つかった場合は条件を満たす最大のインデックスを返します。 * もしそのような要素が存在しない場合は-1を返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 条件を満たす最大のインデックス */ public static int underSearch ( final int[] array, final int value ) { int a = 0, b = array.length - 1, ans = -1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] >= value ) { b = c - 1; } else { a = ( ans = c ) + 1; } } return ans; } /** * 配列内の指定された要素より小さい要素を探します。 * 配列内で見つかった場合は条件を満たす最大のインデックスを返します。 * もしそのような要素が存在しない場合は-1を返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 条件を満たす最大のインデックス */ public static int underSearch ( final long[] array, final long value ) { int a = 0, b = array.length - 1, ans = -1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] >= value ) { b = c - 1; } else { a = ( ans = c ) + 1; } } return ans; } /** * 配列内の指定された要素より小さい要素を探します。 * 配列内で見つかった場合は条件を満たす最大のインデックスを返します。 * もしそのような要素が存在しない場合は-1を返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 条件を満たす最大のインデックス */ public static int underSearch ( final double[] array, final double value ) { int a = 0, b = array.length - 1, ans = -1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] >= value ) { b = c - 1; } else { a = ( ans = c ) + 1; } } return ans; } /** * 配列内の指定された要素より小さい要素を探します。 * 配列内で見つかった場合は条件を満たす最大のインデックスを返します。 * もしそのような要素が存在しない場合は-1を返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 条件を満たす最大のインデックス */ public static int underSearch ( final char[] array, final char value ) { int a = 0, b = array.length - 1, ans = -1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] >= value ) { b = c - 1; } else { a = ( ans = c ) + 1; } } return ans; } /** * 配列内の指定された要素より小さい要素を探します。 * 配列内で見つかった場合は条件を満たす最大のインデックスを返します。 * もしそのような要素が存在しない場合は-1を返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 条件を満たす最大のインデックス */ public static <E extends Comparable<E>> int underSearch ( final E[] array, final E value ) { int a = 0, b = array.length - 1, ans = -1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c].compareTo( value ) >= 0 ) { b = c - 1; } else { a = ( ans = c ) + 1; } } return ans; } /** * リスト内の指定された要素より小さい要素を探します。 * リスト内で見つかった場合は条件を満たす最大のインデックスを返します。 * もしそのような要素が存在しない場合は-1を返します。 * * @param list 探索対象のリスト * @param value 探索要素 * * @return 条件を満たす最大のインデックス */ public static <E extends Comparable<E>> int underSearch ( final List<E> list, final E value ) { int a = 0, b = list.size() - 1, ans = -1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( list.get( c ).compareTo( value ) >= 0 ) { b = c - 1; } else { a = ( ans = c ) + 1; } } return ans; } /** * 広義単調増加な関数内の指定された値より小さい値を探します。 * 関数内で見つかった場合は条件を満たす最大の引数を返します。 * もしそのような要素が存在しない場合は下限-1を返します。 * * @param a 探索範囲の下限 * @param b 探索範囲の上限 * @param value 探索値 * * @return 条件を満たす最大の引数 */ public static int underSearch ( int a, int b, final int value, final IntUnaryOperator func ) { int ans = a - 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( func.applyAsInt( c ) >= value ) { b = c - 1; } else { a = ( ans = c ) + 1; } } return ans; } /** * 広義単調増加な関数内の指定された値より小さい値を探します。 * 関数内で見つかった場合は条件を満たす最大の引数を返します。 * もしそのような要素が存在しない場合は下限-1を返します。 * * @param a 探索範囲の下限 * @param b 探索範囲の上限 * @param value 探索値 * * @return 条件を満たす最大の引数 */ public static long underSearch ( long a, long b, final long value, final LongUnaryOperator func ) { long ans = a - 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( func.applyAsLong( c ) >= value ) { b = c - 1; } else { a = ( ans = c ) + 1; } } return ans; } /** * 配列内の指定された要素より大きい要素を探します。 * 配列内で見つかった場合は条件を満たす最小のインデックスを返します。 * もしそのような要素が存在しない場合はarray.lengthを返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 条件を満たす最小のインデックス */ public static int overSearch ( final int[] array, final int value ) { int a = 0, b = array.length - 1, ans = array.length, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] > value ) { b = ( ans = c ) - 1; } else { a = c + 1; } } return ans; } /** * 配列内の指定された要素より大きい要素を探します。 * 配列内で見つかった場合は条件を満たす最小のインデックスを返します。 * もしそのような要素が存在しない場合はarray.lengthを返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 条件を満たす最小のインデックス */ public static int overSearch ( final long[] array, final long value ) { int a = 0, b = array.length - 1, ans = array.length, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] > value ) { b = ( ans = c ) - 1; } else { a = c + 1; } } return ans; } /** * 配列内の指定された要素より大きい要素を探します。 * 配列内で見つかった場合は条件を満たす最小のインデックスを返します。 * もしそのような要素が存在しない場合はarray.lengthを返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 条件を満たす最小のインデックス */ public static int overSearch ( final double[] array, final double value ) { int a = 0, b = array.length - 1, ans = array.length, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] > value ) { b = ( ans = c ) - 1; } else { a = c + 1; } } return ans; } /** * 配列内の指定された要素より大きい要素を探します。 * 配列内で見つかった場合は条件を満たす最小のインデックスを返します。 * もしそのような要素が存在しない場合はarray.lengthを返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 条件を満たす最小のインデックス */ public static int overSearch ( final char[] array, final char value ) { int a = 0, b = array.length - 1, ans = array.length, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] > value ) { b = ( ans = c ) - 1; } else { a = c + 1; } } return ans; } /** * 配列内の指定された要素より大きい要素を探します。 * 配列内で見つかった場合は条件を満たす最小のインデックスを返します。 * もしそのような要素が存在しない場合はarray.lengthを返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 条件を満たす最小のインデックス */ public static <E extends Comparable<E>> int overSearch ( final E[] array, final E value ) { int a = 0, b = array.length - 1, ans = array.length, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c].compareTo( value ) > 0 ) { b = ( ans = c ) - 1; } else { a = c + 1; } } return ans; } /** * リスト内の指定された要素より大きい要素を探します。 * リスト内で見つかった場合は条件を満たす最小のインデックスを返します。 * もしそのような要素が存在しない場合はlist.size()を返します。 * * @param list 探索対象のリスト * @param value 探索要素 * * @return 条件を満たす最小のインデックス */ public static <E extends Comparable<E>> int overSearch ( final List<E> list, final E value ) { int a = 0, b = list.size() - 1, ans = list.size(), c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( list.get( c ).compareTo( value ) > 0 ) { b = ( ans = c ) - 1; } else { a = c + 1; } } return ans; } /** * 広義単調増加な関数内の指定された値より大きい値を探します。 * 関数内で見つかった場合は条件を満たす最小の引数を返します。 * もしそのような要素が存在しない場合は上限+1を返します。 * * @param a 探索範囲の下限 * @param b 探索範囲の上限 * @param value 探索値 * * @return 条件を満たす最小の引数 */ public static int overSearch ( int a, int b, final int value, final IntUnaryOperator func ) { int ans = b + 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( func.applyAsInt( c ) > value ) { b = ( ans = c ) - 1; } else { a = c + 1; } } return ans; } /** * 広義単調増加な関数内の指定された値より大きい値を探します。 * 関数内で見つかった場合は条件を満たす最小の引数を返します。 * もしそのような要素が存在しない場合は上限+1を返します。 * * @param a 探索範囲の下限 * @param b 探索範囲の上限 * @param value 探索値 * * @return 条件を満たす最小の引数 */ public static long overSearch ( long a, long b, final long value, final LongUnaryOperator func ) { long ans = b + 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( func.applyAsLong( c ) > value ) { b = ( ans = c ) - 1; } else { a = c + 1; } } return ans; } /** * 配列内の指定された要素を探します。 * 配列内で見つかった場合は条件を満たす最小のインデックスを返します。 * もしそのような要素が存在しない場合は-1を返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 条件を満たす最小のインデックス */ public static int linearSearch ( final int[] array, final int value ) { for ( int i = 0; i < array.length; ++i ) { if ( array[i] == value ) { return i; } } return -1; } /** * 配列内の指定された要素を探します。 * 配列内で見つかった場合は条件を満たす最小のインデックスを返します。 * もしそのような要素が存在しない場合は-1を返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 条件を満たす最小のインデックス */ public static int linearSearch ( final long[] array, final long value ) { for ( int i = 0; i < array.length; ++i ) { if ( array[i] == value ) { return i; } } return -1; } /** * 配列内の指定された要素を探します。 * 配列内で見つかった場合は条件を満たす最小のインデックスを返します。 * もしそのような要素が存在しない場合は-1を返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 条件を満たす最小のインデックス */ public static int linearSearch ( final double[] array, final double value ) { for ( int i = 0; i < array.length; ++i ) { if ( array[i] == value ) { return i; } } return -1; } /** * 配列内の指定された要素を探します。 * 配列内で見つかった場合は条件を満たす最小のインデックスを返します。 * もしそのような要素が存在しない場合は-1を返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 条件を満たす最小のインデックス */ public static int linearSearch ( final char[] array, final char value ) { for ( int i = 0; i < array.length; ++i ) { if ( array[i] == value ) { return i; } } return -1; } /** * 配列内の指定された要素を探します。 * 配列内で見つかった場合は条件を満たす最小のインデックスを返します。 * もしそのような要素が存在しない場合は-1を返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 条件を満たす最小のインデックス */ public static <E extends Comparable<E>> int linearSearch ( final E[] array, final E value ) { for ( int i = 0; i < array.length; ++i ) { if ( array[i].compareTo( value ) == 0 ) { return i; } } return -1; } /** * 配列内の指定された要素を探します。 * 配列内で見つかった場合は条件を満たす最小のインデックスを返します。 * もしそのような要素が存在しない場合は-1を返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 条件を満たす最小のインデックス */ public static <E extends Comparable<E>> int linearSearch ( final List<E> list, final E value ) { for ( int i = 0; i < list.size(); ++i ) { if ( list.get( i ).compareTo( value ) == 0 ) { return i; } } return -1; } } // Binary Indexed Tree final class BIT { final int size; final private long[] tree; public BIT ( int n ) { size = n; tree = new long[n + 1]; } public long sum ( int i ) { long sum = 0; while ( i > 0 ) { sum += tree[i]; i ^= i & ( -i ); } return sum; } public void add ( int i, long x ) { while ( i <= size ) { tree[i] += x; i += i & ( -i ); } } public void clear () { Arrays.fill( tree, 0L ); } } // Binary Indexed Tree(int) final class BITInt { final int size; final private int[] tree; public BITInt ( int n ) { size = n; tree = new int[n + 1]; } public int sum ( int i ) { int sum = 0; while ( i > 0 ) { sum += tree[i]; i ^= i & ( -i ); } return sum; } public void add ( int i, int x ) { while ( i <= size ) { tree[i] += x; i += i & ( -i ); } } public void clear () { Arrays.fill( tree, 0 ); } } final class Bitset implements Cloneable { private final long[] bit; private final int size, len; private final long MASK; public Bitset ( final int len ) { this.len = len; size = ( len + 63 ) >> 6; bit = new long[size]; MASK = ( -1L ) >>> ( ( size << 6 ) - len ); } private Bitset ( final long[] arr ) { this( arr.length ); System.arraycopy( arr, 0, bit, 0, size ); } public void set ( final int index ) { if ( index >= size << 6 ) { throw new ArrayIndexOutOfBoundsException( index + " is out of this bitset's size " + size ); } bit[index >> 6] |= ( 1L << ( index & 0b111111 ) ); } public void clear ( final int index ) { if ( index >= size << 6 ) { throw new ArrayIndexOutOfBoundsException( index + " is out of this bitset's size " + size ); } long m = ~( 1L << ( index & 0b111111 ) ); bit[index >> 6] &= m; } public boolean get ( final int index ) { if ( index >= size << 6 ) { throw new ArrayIndexOutOfBoundsException( index + " is out of this bitset's size " + size ); } return ( bit[index >> 6] & ( 1L << ( index & 0b111111 ) ) ) != 0; } public void shiftLeft ( int num ) { if ( num >= size << 6 ) { Arrays.fill( bit, 0L ); return; } final int n = num >> 6; num &= 0b111111; for ( int i = size - 1; i >= n; --i ) { bit[i] = ( bit[i - n] << num ) | ( i != n && num != 0 ? bit[i - n - 1] >>> ( 64 - num ) : 0L ); } for ( int i = 0; i < n; ++i ) { bit[i] = 0L; } bit[size - 1] &= MASK; } public void shiftRight ( int num ) { if ( num >= size << 6 ) { Arrays.fill( bit, 0L ); return; } final int n = num >> 6; num &= 0b111111; for ( int i = 0; i < size - n; ++i ) { bit[i] = ( bit[i + n] >>> num ) | ( i + n + 1 != size && num != 0 ? bit[i + n + 1] << ( 64 - num ) : 0L ); } for ( int i = size - 1; i >= size - n; --i ) { bit[i] = 0L; } } public long[] longValues () { return bit; } public long longValue () { return bit[0]; } public void and ( final Bitset b ) { final long[] bit2 = b.longValues(); final int m = Math.min( bit2.length, size ); for ( int i = 0; i < m; ++i ) { bit[i] &= bit2[i]; } for ( int i = m; i < size; ++i ) { bit[i] = 0; } bit[size - 1] &= MASK; } public void or ( final Bitset b ) { final long[] bit2 = b.longValues(); final int m = Math.min( bit2.length, size ); for ( int i = 0; i < m; ++i ) { bit[i] |= bit2[i]; } bit[size - 1] &= MASK; } public void xor ( final Bitset b ) { final long[] bit2 = b.longValues(); final int m = Math.min( bit2.length, size ); for ( int i = 0; i < m; ++i ) { bit[i] ^= bit2[i]; } bit[size - 1] &= MASK; } public Bitset clone () throws CloneNotSupportedException { super.clone(); final Bitset b = new Bitset( bit ); return b; } } final class IntegerSet { private class Node { private final int value; private Node next; private Node ( final int val ) { value = val; } private boolean add ( final int x ) { if ( value == x ) { return false; } if ( next != null ) { return next.add( x ); } ++size; next = new Node( x ); return true; } private void add ( final Node n ) { if ( value != n.value ) { if ( next != null ) { next.add( n ); } else { next = n; } } } private boolean remove ( final int x ) { if ( next == null ) { return false; } if ( next.value != x ) { return next.remove( x ); } --size; next = next.next; return true; } private boolean contains ( final int x ) { if ( value == x ) { return true; } if ( next != null ) { return next.contains( x ); } return false; } } private Node[] table; private int size; public IntegerSet () { this( 16 ); } public IntegerSet ( final int s ) { if ( s < 1 ) { throw new NegativeArraySizeException( "hash table's size must be positive" ); } table = new Node[s]; size = 0; } public boolean add ( final int x ) { sizeCheck(); final int index = hash( x ); if ( table[index] != null ) { return table[index].add( x ); } ++size; table[index] = new Node( x ); return true; } private void add ( final Node n ) { final int index = hash( n.value ); if ( table[index] != null ) { table[index].add( n ); } else { table[index] = n; } } public boolean remove ( final int x ) { final int index = hash( x ); if ( table[index] == null ) { return false; } if ( table[index].value != x ) { return table[index].remove( x ); } --size; table[index] = table[index].next; return true; } public boolean contains ( final int x ) { final int index = hash( x ); if ( table[index] == null ) { return false; } return table[index].contains( x ); } private void reHash () { final Node[] oldTable = table; table = new Node[table.length << 1]; for ( Node node: oldTable ) { while ( node != null ) { final Node next = node.next; node.next = null; add( node ); node = next; } } } private void sizeCheck () { if ( table.length * 0.6 < size ) { reHash(); } } private int hash ( final int val ) { final int h = val % table.length; return h < 0 ? h + table.length : h; } public int size () { return size; } public int[] toArray () { final int[] array = new int[size]; int index = 0; for ( Node node: table ) { while ( node != null ) { array[index++] = node.value; node = node.next; } } return array; } public String toString () { return Arrays.toString( toArray() ); } } final class Matrix { private final long[][] matrix; public Matrix ( final int H, final int W, final long def ) { matrix = new long[H][W]; if ( def != 0 ) { for ( final long[] mat: matrix ) { Arrays.fill( mat, def ); } } } public Matrix ( final int H, final int W ) { this( H, W, 0 ); } public Matrix ( final Dimension d, final long def ) { this( d.height, d.width, def ); } public Matrix ( final long[][] mat ) { matrix = new long[mat.length][]; for ( int i = 0; i < mat.length; ++i ) { matrix[i] = Arrays.copyOf( mat[i], mat[i].length ); } } public long get ( final int i, final int j ) { return matrix[i][j]; } public long set ( final int i, final int j, final long value ) { return matrix[i][j] = value; } public Matrix copy () { return new Matrix( matrix ); } public Dimension size () { return new Dimension( matrix[0].length, matrix.length ); } public Matrix add ( final Matrix m ) { if ( !size().equals( m.size() ) ) { throw new IllegalArgumentException( "matrix size is not same" ); } final Matrix ans = new Matrix( size(), 0 ); for ( int i = 0; i < matrix.length; ++i ) { for ( int j = 0; j < matrix[i].length; ++j ) { ans.set( i, j, matrix[i][j] + m.get( i, j ) ); } } return ans; } public Matrix subtract ( final Matrix m ) { if ( !size().equals( m.size() ) ) { throw new IllegalArgumentException( "matrix size is not same" ); } final Matrix ans = new Matrix( size(), 0 ); for ( int i = 0; i < matrix.length; ++i ) { for ( int j = 0; j < matrix[i].length; ++j ) { ans.set( i, j, matrix[i][j] - m.get( i, j ) ); } } return ans; } public Matrix multiply ( final Matrix m ) { if ( size().width != m.size().height ) { throw new IllegalArgumentException( "matrix length is not same" ); } final Matrix ans = new Matrix( size().height, m.size().width ); final Dimension size = ans.size(); final int len = size().width; for ( int i = 0; i < size.height; ++i ) { for ( int j = 0; j < size.width; ++j ) { long sum = 0; for ( int k = 0; k < len; ++k ) { sum += matrix[i][k] * m.get( k, j ); } ans.set( i, j, sum ); } } return ans; } public Matrix modAdd ( final Matrix m, final long mod ) { if ( !size().equals( m.size() ) ) { throw new IllegalArgumentException( "matrix size is not same" ); } final Matrix ans = new Matrix( size(), 0 ); for ( int i = 0; i < matrix.length; ++i ) { for ( int j = 0; j < matrix[i].length; ++j ) { ans.set( i, j, MathFunction.remainder( matrix[i][j] + m.get( i, j ), mod ) ); } } return ans; } public Matrix modSubtract ( final Matrix m, final long mod ) { if ( !size().equals( m.size() ) ) { throw new IllegalArgumentException( "matrix size is not same" ); } final Matrix ans = new Matrix( size(), 0 ); for ( int i = 0; i < matrix.length; ++i ) { for ( int j = 0; j < matrix[i].length; ++j ) { ans.set( i, j, MathFunction.remainder( matrix[i][j] - m.get( i, j ), mod ) ); } } return ans; } public Matrix modMultiply ( final Matrix m, final long mod ) { if ( size().width != m.size().height ) { throw new IllegalArgumentException( "matrix length is not same" ); } final Matrix ans = new Matrix( size().height, m.size().width ); final Dimension size = ans.size(); final int len = size().width; for ( int i = 0; i < size.height; ++i ) { for ( int j = 0; j < size.width; ++j ) { long sum = 0; for ( int k = 0; k < len; ++k ) { sum = MathFunction.remainder( sum + matrix[i][k] * m.get( k, j ), mod ); } ans.set( i, j, sum ); } } return ans; } public static Matrix pow ( final Matrix original, final Matrix pw, long exp ) { Matrix a = original.copy(); Matrix b = pw.copy(); while ( 0 < exp ) { if ( ( exp & 1 ) == 1 ) { a = b.multiply( a ); } b = b.multiply( b ); exp >>= 1; } return a; } public static Matrix modPow ( final Matrix original, final Matrix pw, long exp, final long mod ) { Matrix a = original.copy(); Matrix b = pw.copy(); while ( 0 < exp ) { if ( ( exp & 1 ) == 1 ) { a = b.modMultiply( a, mod ); } b = b.modMultiply( b, mod ); exp >>= 1; } return a; } public long determinant () { return determinant( matrix ); } private static long determinant ( final long[][] mat ) { if ( mat.length == 1 ) { return mat[0][0]; } final long[][] miniMat = new long[mat.length - 1][mat.length - 1]; for ( int i = 1; i < mat.length; ++i ) { System.arraycopy( mat[i], 1, miniMat[i - 1], 0, miniMat.length ); } long ans = mat[0][0] * determinant( miniMat ); for ( int i = 1; i < mat.length; ++i ) { for ( int j = 1; j < mat.length; ++j ) { miniMat[j - 1][i - 1] = mat[j][i - 1]; } final long num = mat[0][i] * determinant( miniMat ); ans += i % 2 == 0 ? num : -num; } return ans; } @Override public String toString () { final StringBuilder ans = new StringBuilder(); ans.append( Arrays.toString( matrix[0] ) ); for ( int i = 1; i < matrix.length; ++i ) { ans.append( "\n" ); ans.append( Arrays.toString( matrix[i] ) ); } return ans.toString(); } } final class RollingHash implements Comparable<RollingHash> { private static final long BASE = new Random().nextInt( 1000 ) + Character.MAX_VALUE + 1; private static final long MASK30 = ( 1L << 30 ) - 1; private static final long MASK31 = ( 1L << 31 ) - 1; private static final long MOD = ( 1L << 61 ) - 1; private static final long MASK61 = MOD; private long[] hash; private String string; public RollingHash ( final String str ) { string = str; hash = new long[str.length() + 1]; roll(); } private void roll () { final int len = string.length(); for ( int i = 1; i <= len; ++i ) { hash[i] = multiply( hash[i - 1], BASE ) + string.charAt( i - 1 ) - ' ' + 1; if ( MOD <= hash[i] ) { hash[i] -= MOD; } } } private static long multiply ( final long a, final long b ) { final long au = a >> 31; final long ad = a & MASK31; final long bu = b >> 31; final long bd = b & MASK31; final long mid = ad * bu + au * bd; final long midu = mid >> 30; final long midd = mid & MASK30; return mod( au * bu * 2 + midu + ( midd << 31 ) + ad * bd ); } private static long mod ( final long x ) { final long xu = x >> 61; final long xd = x & MASK61; long ans = xu + xd; if ( MOD <= ans ) { ans -= MOD; } return ans; } public long getHash ( final int l, final int r ) { return ( hash[r] - multiply( hash[l], modBasePow( r - l ) ) + MOD ) % MOD; } private static long modBasePow ( long b ) { long ans = 1; long a = BASE; while ( b > 0 ) { if ( ( b & 1 ) == 1 ) { ans = multiply( ans, a ); } a = multiply( a, a ); b >>= 1; } return ans; } public boolean equals ( final RollingHash rh, final int l1, final int r1, final int l2, final int r2 ) { if ( r1 - l1 != r2 - l2 ) { return false; } return getHash( l1, r1 ) == rh.getHash( l2, r2 ); } public int length () { return string.length(); } @Override public int hashCode () { return string.hashCode(); } @Override public String toString () { return string; } @Override public boolean equals ( Object o ) { if ( o instanceof final RollingHash rh ) { return equals( rh, 0, length(), 0, rh.length() ); } return false; } @Override public int compareTo ( RollingHash rh ) { return string.compareTo( rh.toString() ); } public int compareTo ( String str ) { return string.compareTo( str ); } public char charAt ( final int i ) { return string.charAt( i ); } public int compareToIgnoreCase ( final RollingHash rh ) { return string.compareToIgnoreCase( rh.toString() ); } public int compareToIgnoreCase ( final String str ) { return string.compareToIgnoreCase( str ); } public void concat ( final RollingHash rh ) { concat( rh.toString() ); } public void concat ( final String str ) { string = string.concat( str ); hash = new long[string.length() + 1]; roll(); } public boolean contains ( final RollingHash rh ) { final long hash = rh.getHash( 0, rh.length() ); final int len = length() - rh.length(); for ( int i = 0; i <= len; ++i ) { if ( hash == getHash( i, rh.length() + i ) ) { return true; } } return false; } public boolean contains ( final String str ) { return indexOf( str ) != -1; } public int indexOf ( final int ch ) { return indexOf( ch, 0 ); } public int indexOf ( final int ch, final int fromIndex ) { final int len = length(); for ( int i = fromIndex; i < len; ++i ) { if ( string.charAt( i ) == ch ) { return i; } } return -1; } public int indexOf ( final String str ) { return indexOf( str, 0 ); } public int indexOf ( final String str, final int fromIndex ) { long hash = 0; for ( final char c: str.toCharArray() ) { hash = multiply( hash, BASE ) + c - ' ' + 1; if ( MOD <= hash ) { hash -= MOD; } } final int len = length() - str.length(); for ( int i = fromIndex; i <= len; ++i ) { if ( hash == getHash( i, str.length() + i ) ) { return i; } } return -1; } public boolean isEmpty () { return length() == 0; } public int lastIndexOf ( final int ch, final int fromIndex ) { for ( int i = fromIndex; i >= 0; --i ) { if ( string.charAt( i ) == ch ) { return i; } } return -1; } public int lastIndexOf ( final int ch ) { return lastIndexOf( ch, length() - 1 ); } public static RollingHash valueOf ( final boolean b ) { return new RollingHash( b ? "true" : "false" ); } public static RollingHash valueOf ( final char c ) { return new RollingHash( "" + c ); } public static RollingHash valueOf ( final char[] c ) { return new RollingHash( String.valueOf( c, 0, c.length ) ); } public static RollingHash valueOf ( final char[] c, final int offset, final int count ) { return new RollingHash( String.valueOf( c, offset, count ) ); } public static RollingHash valueOf ( final double d ) { return new RollingHash( String.valueOf( d ) ); } public static RollingHash valueOf ( final float f ) { return new RollingHash( String.valueOf( f ) ); } public static RollingHash valueOf ( final int i ) { return new RollingHash( String.valueOf( i ) ); } public static RollingHash valueOf ( final long l ) { return new RollingHash( String.valueOf( l ) ); } public static RollingHash valueOf ( final Object obj ) { return new RollingHash( String.valueOf( obj ) ); } } @SuppressWarnings( "unchecked" ) abstract class SegmentTree<E> { final int N, size; final E def; final Object[] node; public SegmentTree ( final int n, final E def, final boolean include ) { int num = 2; while ( num < n << 1 ) { num <<= 1; } N = num; size = num >> 1 - ( include ? 1 : 0 ); node = new Object[N]; this.def = def; Arrays.fill( node, this.def ); } public SegmentTree ( final E[] arr, final E def, final boolean include ) { int num = 2; while ( num < arr.length << 1 ) { num <<= 1; } N = num; size = num >> 1 - ( include ? 1 : 0 ); node = new Object[N]; this.def = def; System.arraycopy( arr, 0, node, N >> 1, arr.length ); for ( int i = arr.length + ( N >> 1 ); i < N; i++ ) { node[i] = def; } updateAll(); } public SegmentTree ( final int n, final E def ) { this( n, def, false ); } private void updateAll () { for ( int i = ( N >> 1 ) - 1; i > 0; i-- ) { node[i] = function( ( E )node[i << 1], ( E )node[( i << 1 ) + 1] ); } } public void update ( int n, final E value ) { n += size; node[n] = value; n >>= 1; while ( n > 0 ) { node[n] = function( ( E )node[n << 1], ( E )node[( n << 1 ) + 1] ); n >>= 1; } } public E get ( final int a ) { return ( E )node[a + size]; } public E answer () { return ( E )node[1]; } public E query ( int l, int r ) { l += size; r += size; E answer = def; while ( l > 0 && r > 0 && l <= r ) { if ( l % 2 == 1 ) { answer = function( ( E )node[l++], answer ); } l >>= 1; if ( r % 2 == 0 ) { answer = function( answer, ( E )node[r--] ); } r >>= 1; } return answer; } abstract public E function ( E a, E b ); } final class UnionFind { private final int[] par, rank, size, path; private int count; public UnionFind ( final int N ) { count = N; par = new int[N]; rank = new int[N]; size = new int[N]; path = new int[N]; Arrays.fill( par, -1 ); Arrays.fill( size, 1 ); } public int root ( final int ind ) { if ( par[ind] == -1 ) { return ind; } else { return par[ind] = root( par[ind] ); } } public boolean isSame ( final int x, final int y ) { return root( x ) == root( y ); } public boolean unite ( final int x, final int y ) { int rx = root( x ); int ry = root( y ); ++path[rx]; if ( rx == ry ) { return false; } if ( rank[rx] < rank[ry] ) { int temp = rx; rx = ry; ry = temp; } par[ry] = rx; if ( rank[rx] == rank[ry] ) { ++rank[rx]; } path[rx] += path[ry]; size[rx] += size[ry]; --count; return true; } public int groupCount () { return count; } public int pathCount ( final int x ) { return path[root( x )]; } public int size ( final int x ) { return size[root( x )]; } } final class Tree<E extends Comparable<E>> { private Node<E> root; private int size, hash; public Tree () { size = 0; root = null; hash = 0; } static final private class Node<E> { E value; int height, size; Node<E> left, right, parent; public Node ( final Node<E> p, final E v ) { value = v; parent = p; height = 1; size = 1; } } public boolean add ( final E x ) { boolean bool = true; if ( root == null ) { root = new Node<>( null, x ); } else { Node<E> par; Node<E> now = root; do { par = now; final int result = x.compareTo( now.value ); if ( result < 0 ) { now = now.left; } else if ( result > 0 ) { now = now.right; } else { bool = false; break; } } while ( now != null ); if ( bool ) { final int result = x.compareTo( par.value ); if ( result < 0 ) { par.left = new Node<>( par, x ); } else { par.right = new Node<>( par, x ); } fix( par ); } } if ( bool ) { ++size; hash ^= x.hashCode(); } return bool; } public E get ( int index ) { if ( root == null || size <= index ) { throw new NullPointerException(); } Node<E> now = root; while ( true ) { assert now != null; final int ls = now.left != null ? now.left.size : 0; if ( index < ls ) { now = now.left; } else if ( ls < index ) { now = now.right; index -= ls + 1; } else { break; } } return now.value; } public boolean remove ( final E x ) { final Node<E> n = getNode( x ); if ( n == null ) { return false; } --size; hash ^= n.value.hashCode(); delete( n ); return true; } private void delete ( final Node<E> node ) { if ( node != null ) { if ( node.left == null && node.right == null ) { if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = null; } else { node.parent.right = null; } fix( node.parent ); } else { root = null; } node.parent = null; } else { if ( node.left != null && node.right != null ) { final Node<E> rep = getFirstNode( node.right ); node.value = rep.value; delete( rep ); } else { final Node<E> rep = node.left != null ? node.left : node.right; rep.parent = node.parent; if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = rep; } else { node.parent.right = rep; } fix( node.parent ); } else { root = rep; } node.parent = null; } } } } private Node<E> getNode ( final E x ) { Node<E> now = root; while ( now != null ) { final int result = x.compareTo( now.value ); if ( result < 0 ) { now = now.left; } else if ( result > 0 ) { now = now.right; } else { break; } } return now; } public E first () { if ( root == null ) { return null; } return getFirstNode( root ).value; } private Node<E> getFirstNode ( Node<E> node ) { assert node != null; Node<E> par = null; while ( node != null ) { par = node; node = par.left; } return par; } public E last () { if ( root == null ) { return null; } return getLastNode( root ).value; } private Node<E> getLastNode ( Node<E> node ) { assert node != null; Node<E> par = null; while ( node != null ) { par = node; node = par.right; } return par; } public boolean contains ( final E x ) { if ( root == null ) { return false; } return getNode( x ) != null; } public E pollFirst () { if ( root == null ) { return null; } --size; final Node<E> min = getFirstNode( root ); hash ^= min.value.hashCode(); delete( min ); return min.value; } public E pollLast () { if ( root == null ) { return null; } --size; final Node<E> max = getLastNode( root ); hash ^= max.value.hashCode(); delete( max ); return max.value; } public E ceiling ( final E x ) { return ceiling( root, x ); } private E ceiling ( Node<E> node, final E x ) { Node<E> ans = null; while ( node != null ) { final int result = x.compareTo( node.value ); if ( result > 0 ) { node = node.right; } else if ( result < 0 ) { ans = node; node = node.left; } else { return x; } } return ans != null ? ans.value : null; } public E higher ( final E x ) { return higher( root, x ); } private E higher ( Node<E> node, final E x ) { Node<E> ans = null; while ( node != null ) { final int result = x.compareTo( node.value ); if ( result >= 0 ) { node = node.right; } else { ans = node; node = node.left; } } return ans != null ? ans.value : null; } public E floor ( final E x ) { return floor( root, x ); } private E floor ( Node<E> node, final E x ) { Node<E> ans = null; while ( node != null ) { final int result = x.compareTo( node.value ); if ( result < 0 ) { node = node.left; } else if ( result > 0 ) { ans = node; node = node.right; } else { return x; } } return ans != null ? ans.value : null; } public E lower ( final E x ) { return lower( root, x ); } private E lower ( Node<E> node, final E x ) { Node<E> ans = null; while ( node != null ) { final int result = x.compareTo( node.value ); if ( result <= 0 ) { node = node.left; } else { ans = node; node = node.right; } } return ans != null ? ans.value : null; } public void clear () { root = null; size = 0; hash = 0; } public boolean isEmpty () { return size == 0; } public int size () { return size; } public ArrayList<E> toList () { final ArrayList<E> list = new ArrayList<>(); if ( root != null ) { final ArrayDeque<Node<E>> deq = new ArrayDeque<>(); deq.add( root ); while ( !deq.isEmpty() ) { final Node<E> now = deq.pollLast(); if ( list.size() == 0 ) { if ( now.left != null ) { deq.add( now ); deq.add( now.left ); } else { list.add( now.value ); if ( now.right != null ) { deq.add( now.right ); } } } else if ( now.left != null && list.get( list.size() - 1 ).compareTo( now.left.value ) < 0 ) { deq.add( now ); deq.add( now.left ); } else { list.add( now.value ); if ( now.right != null ) { deq.add( now.right ); } } } } return list; } @Override public String toString () { final ArrayList<E> list = toList(); return list.toString(); } @Override public boolean equals ( final Object o ) { if ( o instanceof final Tree<?> tree ) { if ( size != tree.size() ) { return false; } final ArrayList<E> list1 = toList(); final ArrayList<?> list2 = tree.toList(); for ( int i = 0; i < size; ++i ) { if ( !list1.get( i ).equals( list2.get( i ) ) ) { return false; } } return true; } return false; } @Override public int hashCode () { return hash; } /* * 以下、平衡用メソッド */ private void fix ( Node<E> node ) { while ( node != null ) { final int lh = node.left == null ? 0 : node.left.height; final int rh = node.right == null ? 0 : node.right.height; if ( lh - rh > 1 ) { assert node.left != null; if ( node.left.right != null && node.left.right.height == lh - 1 ) { rotateL( node.left ); } rotateR( node ); } else if ( rh - lh > 1 ) { assert node.right != null; if ( node.right.left != null && node.right.left.height == rh - 1 ) { rotateR( node.right ); } rotateL( node ); } else { setStates( node ); } node = node.parent; } } private void rotateR ( final Node<E> node ) { final Node<E> temp = node.left; node.left = temp.right; if ( node.left != null ) { node.left.parent = node; } temp.right = node; temp.parent = node.parent; if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = temp; } else { node.parent.right = temp; } } else { root = temp; } node.parent = temp; setStates( node ); } private void rotateL ( final Node<E> node ) { final Node<E> temp = node.right; node.right = temp.left; if ( node.right != null ) { node.right.parent = node; } temp.left = node; temp.parent = node.parent; if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = temp; } else { node.parent.right = temp; } } else { root = temp; } node.parent = temp; setStates( node ); } private void setStates ( final Node<E> node ) { final int lh = node.left != null ? node.left.height : 0; final int rh = node.right != null ? node.right.height : 0; node.height = Math.max( lh, rh ) + 1; final int ls = node.left != null ? node.left.size : 0; final int rs = node.right != null ? node.right.size : 0; node.size = ls + rs + 1; } } final class TreeInt { private Node root; private int size, hash; public TreeInt () { size = 0; root = null; hash = 0; } static final private class Node { int value; int height, size; Node left, right, parent; public Node ( final Node p, final int v ) { value = v; parent = p; height = 1; size = 1; } } public boolean add ( final int x ) { boolean bool = true; if ( root == null ) { root = new Node( null, x ); } else { Node par; Node now = root; do { par = now; if ( x < now.value ) { now = now.left; } else if ( x > now.value ) { now = now.right; } else { bool = false; break; } } while ( now != null ); if ( bool ) { if ( x < par.value ) { par.left = new Node( par, x ); } else { par.right = new Node( par, x ); } fix( par ); } } if ( bool ) { ++size; hash ^= x; } return bool; } public int get ( int index ) { if ( root == null || size <= index ) { throw new NullPointerException(); } Node now = root; while ( true ) { assert now != null; final int ls = now.left != null ? now.left.size : 0; if ( index < ls ) { now = now.left; } else if ( ls < index ) { now = now.right; index -= ls + 1; } else { break; } } return now.value; } public boolean remove ( final int x ) { final Node n = getNode( x ); if ( n == null ) { return false; } --size; hash ^= n.value; delete( n ); return true; } private void delete ( final Node node ) { if ( node != null ) { if ( node.left == null && node.right == null ) { if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = null; } else { node.parent.right = null; } fix( node.parent ); } else { root = null; } node.parent = null; } else { if ( node.left != null && node.right != null ) { final Node rep = getFirstNode( node.right ); node.value = rep.value; delete( rep ); } else { final Node rep = node.left != null ? node.left : node.right; rep.parent = node.parent; if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = rep; } else { node.parent.right = rep; } fix( node.parent ); } else { root = rep; } node.parent = null; } } } } private Node getNode ( final int x ) { Node now = root; while ( now != null ) { if ( x < now.value ) { now = now.left; } else if ( x > now.value ) { now = now.right; } else { break; } } return now; } public int first () { if ( root == null ) { throw new NullPointerException(); } return getFirstNode( root ).value; } private Node getFirstNode ( Node node ) { assert node != null; Node par = null; while ( node != null ) { par = node; node = par.left; } return par; } public int last () { if ( root == null ) { throw new NullPointerException(); } return getLastNode( root ).value; } private Node getLastNode ( Node node ) { assert node != null; Node par = null; while ( node != null ) { par = node; node = par.right; } return par; } public boolean contains ( final int x ) { if ( root == null ) { return false; } return getNode( x ) != null; } public int pollFirst () { if ( root == null ) { throw new NullPointerException(); } --size; final Node min = getFirstNode( root ); hash ^= min.value; delete( min ); return min.value; } public int pollLast () { if ( root == null ) { throw new NullPointerException(); } --size; final Node max = getLastNode( root ); hash ^= max.value; delete( max ); return max.value; } public int ceiling ( final int x ) { return ceiling( root, x ); } private int ceiling ( Node node, final int x ) { Node ans = new Node( null, x - 1 ); while ( node != null ) { if ( x > node.value ) { node = node.right; } else if ( x < node.value ) { ans = node; node = node.left; } else { return x; } } return ans.value; } public int higher ( final int x ) { return higher( root, x ); } private int higher ( Node node, final int x ) { Node ans = new Node( null, x - 1 ); while ( node != null ) { if ( x >= node.value ) { node = node.right; } else { ans = node; node = node.left; } } return ans.value; } public int floor ( final int x ) { return floor( root, x ); } private int floor ( Node node, final int x ) { Node ans = new Node( null, x + 1 ); while ( node != null ) { if ( x < node.value ) { node = node.left; } else if ( x > node.value ) { ans = node; node = node.right; } else { return x; } } return ans.value; } public int lower ( final int x ) { return lower( root, x ); } private int lower ( Node node, final int x ) { Node ans = new Node( null, x + 1 ); while ( node != null ) { if ( x <= node.value ) { node = node.left; } else { ans = node; node = node.right; } } return ans.value; } public void clear () { root = null; size = 0; hash = 0; } public boolean isEmpty () { return size == 0; } public int size () { return size; } public int[] toArray () { final int[] list = new int[size]; if ( root != null ) { int index = 0; final ArrayDeque<Node> deq = new ArrayDeque<>(); deq.add( root ); while ( !deq.isEmpty() ) { final Node now = deq.pollLast(); if ( index == 0 ) { if ( now.left != null ) { deq.add( now ); deq.add( now.left ); } else { list[index++] = now.value; if ( now.right != null ) { deq.add( now.right ); } } } else if ( now.left != null && list[index - 1] < now.left.value ) { deq.add( now ); deq.add( now.left ); } else { list[index++] = now.value; if ( now.right != null ) { deq.add( now.right ); } } } } return list; } @Override public String toString () { final int[] list = toArray(); return Arrays.toString( list ); } @Override public boolean equals ( final Object o ) { if ( o instanceof final TreeInt tree ) { if ( size != tree.size() ) { return false; } final int[] array1 = toArray(); final int[] array2 = tree.toArray(); for ( int i = 0; i < size; ++i ) { if ( array1[i] != array2[i] ) { return false; } } return true; } return false; } @Override public int hashCode () { return hash; } /* * 以下、平衡用メソッド */ private void fix ( Node node ) { while ( node != null ) { final int lh = node.left == null ? 0 : node.left.height; final int rh = node.right == null ? 0 : node.right.height; if ( lh - rh > 1 ) { assert node.left != null; if ( node.left.right != null && node.left.right.height == lh - 1 ) { rotateL( node.left ); } rotateR( node ); } else if ( rh - lh > 1 ) { assert node.right != null; if ( node.right.left != null && node.right.left.height == rh - 1 ) { rotateR( node.right ); } rotateL( node ); } else { setStates( node ); } node = node.parent; } } private void rotateR ( final Node node ) { final Node temp = node.left; node.left = temp.right; if ( node.left != null ) { node.left.parent = node; } temp.right = node; temp.parent = node.parent; if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = temp; } else { node.parent.right = temp; } } else { root = temp; } node.parent = temp; setStates( node ); } private void rotateL ( final Node node ) { final Node temp = node.right; node.right = temp.left; if ( node.right != null ) { node.right.parent = node; } temp.left = node; temp.parent = node.parent; if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = temp; } else { node.parent.right = temp; } } else { root = temp; } node.parent = temp; setStates( node ); } private void setStates ( final Node node ) { final int lh = node.left != null ? node.left.height : 0; final int rh = node.right != null ? node.right.height : 0; node.height = Math.max( lh, rh ) + 1; final int ls = node.left != null ? node.left.size : 0; final int rs = node.right != null ? node.right.size : 0; node.size = ls + rs + 1; } } final class TreeLong { private Node root; private int size, hash; public TreeLong () { size = 0; root = null; hash = 0; } static final private class Node { long value; int height, size; Node left, right, parent; public Node ( final Node p, final long v ) { value = v; parent = p; height = 1; size = 1; } } public boolean add ( final long x ) { boolean bool = true; if ( root == null ) { root = new Node( null, x ); } else { Node par; Node now = root; do { par = now; if ( x < now.value ) { now = now.left; } else if ( x > now.value ) { now = now.right; } else { bool = false; break; } } while ( now != null ); if ( bool ) { if ( x < par.value ) { par.left = new Node( par, x ); } else { par.right = new Node( par, x ); } fix( par ); } } if ( bool ) { ++size; hash ^= ( int )x; } return bool; } public long get ( int index ) { if ( root == null || size <= index ) { throw new NullPointerException(); } Node now = root; while ( true ) { assert now != null; final int ls = now.left != null ? now.left.size : 0; if ( index < ls ) { now = now.left; } else if ( ls < index ) { now = now.right; index -= ls + 1; } else { break; } } return now.value; } public boolean remove ( final long x ) { final Node n = getNode( x ); if ( n == null ) { return false; } --size; hash ^= ( int )x; delete( n ); return true; } private void delete ( final Node node ) { if ( node != null ) { if ( node.left == null && node.right == null ) { if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = null; } else { node.parent.right = null; } fix( node.parent ); } else { root = null; } node.parent = null; } else { if ( node.left != null && node.right != null ) { final Node rep = getFirstNode( node.right ); node.value = rep.value; delete( rep ); } else { final Node rep = node.left != null ? node.left : node.right; rep.parent = node.parent; if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = rep; } else { node.parent.right = rep; } fix( node.parent ); } else { root = rep; } node.parent = null; } } } } private Node getNode ( final long x ) { Node now = root; while ( now != null ) { if ( x < now.value ) { now = now.left; } else if ( x > now.value ) { now = now.right; } else { break; } } return now; } public long first () { if ( root == null ) { throw new NullPointerException(); } return getFirstNode( root ).value; } private Node getFirstNode ( Node node ) { assert node != null; Node par = null; while ( node != null ) { par = node; node = par.left; } return par; } public long last () { if ( root == null ) { throw new NullPointerException(); } return getLastNode( root ).value; } private Node getLastNode ( Node node ) { assert node != null; Node par = null; while ( node != null ) { par = node; node = par.right; } return par; } public boolean contains ( final long x ) { if ( root == null ) { return false; } return getNode( x ) != null; } public long pollFirst () { if ( root == null ) { throw new NullPointerException(); } --size; final Node min = getFirstNode( root ); hash ^= ( int )min.value; delete( min ); return min.value; } public long pollLast () { if ( root == null ) { throw new NullPointerException(); } --size; final Node max = getLastNode( root ); hash ^= ( int )max.value; delete( max ); return max.value; } public long ceiling ( final long x ) { return ceiling( root, x ); } private long ceiling ( Node node, final long x ) { Node ans = new Node( null, x - 1 ); while ( node != null ) { if ( x > node.value ) { node = node.right; } else if ( x < node.value ) { ans = node; node = node.left; } else { return x; } } return ans.value; } public long higher ( final long x ) { return higher( root, x ); } private long higher ( Node node, final long x ) { Node ans = new Node( null, x - 1 ); while ( node != null ) { if ( x >= node.value ) { node = node.right; } else { ans = node; node = node.left; } } return ans.value; } public long floor ( final long x ) { return floor( root, x ); } private long floor ( Node node, final long x ) { Node ans = new Node( null, x + 1 ); while ( node != null ) { if ( x < node.value ) { node = node.left; } else if ( x > node.value ) { ans = node; node = node.right; } else { return x; } } return ans.value; } public long lower ( final long x ) { return lower( root, x ); } private long lower ( Node node, final long x ) { Node ans = new Node( null, x + 1 ); while ( node != null ) { if ( x <= node.value ) { node = node.left; } else { ans = node; node = node.right; } } return ans.value; } public int size () { return size; } public long[] toArray () { final long[] list = new long[size]; if ( root != null ) { int index = 0; final ArrayDeque<Node> deq = new ArrayDeque<>(); deq.add( root ); while ( !deq.isEmpty() ) { final Node now = deq.pollLast(); if ( index == 0 ) { if ( now.left != null ) { deq.add( now ); deq.add( now.left ); } else { list[index++] = now.value; if ( now.right != null ) { deq.add( now.right ); } } } else if ( now.left != null && list[index - 1] < now.left.value ) { deq.add( now ); deq.add( now.left ); } else { list[index++] = now.value; if ( now.right != null ) { deq.add( now.right ); } } } } return list; } @Override public String toString () { final long[] list = toArray(); return Arrays.toString( list ); } @Override public boolean equals ( final Object o ) { if ( o instanceof final TreeLong tree ) { if ( size != tree.size() ) { return false; } final long[] array1 = toArray(); final long[] array2 = tree.toArray(); for ( int i = 0; i < size; ++i ) { if ( array1[i] != array2[i] ) { return false; } } return true; } return false; } @Override public int hashCode () { return hash; } /* * 以下、平衡用メソッド */ private void fix ( Node node ) { while ( node != null ) { final int lh = node.left == null ? 0 : node.left.height; final int rh = node.right == null ? 0 : node.right.height; if ( lh - rh > 1 ) { assert node.left != null; if ( node.left.right != null && node.left.right.height == lh - 1 ) { rotateL( node.left ); } rotateR( node ); } else if ( rh - lh > 1 ) { assert node.right != null; if ( node.right.left != null && node.right.left.height == rh - 1 ) { rotateR( node.right ); } rotateL( node ); } else { setStates( node ); } node = node.parent; } } private void rotateR ( final Node node ) { final Node temp = node.left; node.left = temp.right; if ( node.left != null ) { node.left.parent = node; } temp.right = node; temp.parent = node.parent; if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = temp; } else { node.parent.right = temp; } } else { root = temp; } node.parent = temp; setStates( node ); } private void rotateL ( final Node node ) { final Node temp = node.right; node.right = temp.left; if ( node.right != null ) { node.right.parent = node; } temp.left = node; temp.parent = node.parent; if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = temp; } else { node.parent.right = temp; } } else { root = temp; } node.parent = temp; setStates( node ); } private void setStates ( final Node node ) { final int lh = node.left != null ? node.left.height : 0; final int rh = node.right != null ? node.right.height : 0; node.height = Math.max( lh, rh ) + 1; final int ls = node.left != null ? node.left.size : 0; final int rs = node.right != null ? node.right.size : 0; node.size = ls + rs + 1; } } final class TreeMulti<E extends Comparable<E>> { private Node<E> root; private long size; private int uniqueSize; private int hash; public TreeMulti () { size = 0; uniqueSize = 0; root = null; hash = 0; } static final private class Node<E> { E value; long count, size; int height; Node<E> left, right, parent; public Node ( final Node<E> p, final E v, final long c ) { value = v; parent = p; count = c; height = 1; size = c; } } public void add ( final E x ) { if ( root == null ) { root = new Node<>( null, x, 1 ); ++uniqueSize; } else { Node<E> par; Node<E> now = root; boolean bool = true; do { par = now; final int result = x.compareTo( now.value ); if ( result < 0 ) { now = now.left; } else if ( result > 0 ) { now = now.right; } else { bool = false; ++now.count; break; } } while ( now != null ); if ( bool ) { ++uniqueSize; final int result = x.compareTo( par.value ); if ( result < 0 ) { par.left = new Node<>( par, x, 1 ); } else { par.right = new Node<>( par, x, 1 ); } } fix( par ); } ++size; hash ^= x.hashCode(); } public void add ( final E x, final long sum ) { if ( root == null ) { root = new Node<>( null, x, sum ); ++uniqueSize; } else { Node<E> par; Node<E> now = root; boolean bool = true; do { par = now; final int result = x.compareTo( now.value ); if ( result < 0 ) { now = now.left; } else if ( result > 0 ) { now = now.right; } else { bool = false; now.count += sum; fix( now ); break; } } while ( now != null ); if ( bool ) { ++uniqueSize; final int result = x.compareTo( par.value ); if ( result < 0 ) { par.left = new Node<>( par, x, sum ); } else { par.right = new Node<>( par, x, sum ); } fix( par ); } } size += sum; hash ^= sum % 2 == 1 ? x.hashCode() : 0; } public E get ( long index ) { if ( root == null || size <= index ) { throw new NullPointerException(); } Node<E> now = root; while ( true ) { assert now != null; final long ls = now.left != null ? now.left.size : 0; if ( index < ls ) { now = now.left; } else if ( ls + now.count <= index ) { index -= ls + now.count; now = now.right; } else { break; } } return now.value; } public boolean remove ( final E x ) { final Node<E> n = getNode( x ); if ( n == null ) { return false; } --size; hash ^= n.hashCode(); delete( n ); return true; } public long remove ( final E x, final long sum ) { final Node<E> n = getNode( x ); if ( n == null ) { return 0; } final long ans = Math.min( sum, n.count ); size -= ans; hash ^= ans % 2 == 1 ? n.hashCode() : 0; n.count -= ans - 1; delete( n ); return ans; } public long removeAll ( final E x ) { final Node<E> n = getNode( x ); if ( n == null ) { return 0; } size -= n.count; final long ans = n.count; hash ^= ans % 2 == 1 ? n.hashCode() : 0; n.count = 0; delete( n ); return ans; } private void delete ( final Node<E> node ) { if ( node != null ) { if ( node.count > 1 ) { --node.count; fix( node ); return; } if ( node.left == null && node.right == null ) { if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = null; } else { node.parent.right = null; } fix( node.parent ); } else { root = null; } node.parent = null; --uniqueSize; } else { if ( node.left != null && node.right != null ) { final Node<E> rep = getFirstNode( node.right ); node.value = rep.value; node.count = rep.count; rep.count = 0; delete( rep ); } else { final Node<E> rep = node.left != null ? node.left : node.right; rep.parent = node.parent; if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = rep; } else { node.parent.right = rep; } fix( node.parent ); } else { root = rep; } node.parent = null; --uniqueSize; } } } } private Node<E> getNode ( final E x ) { Node<E> now = root; while ( now != null ) { final int result = x.compareTo( now.value ); if ( result < 0 ) { now = now.left; } else if ( result > 0 ) { now = now.right; } else { break; } } return now; } public E first () { if ( root == null ) { return null; } return getFirstNode( root ).value; } private Node<E> getFirstNode ( Node<E> node ) { assert node != null; Node<E> par = null; while ( node != null ) { par = node; node = par.left; } return par; } public E last () { if ( root == null ) { return null; } return getLastNode( root ).value; } private Node<E> getLastNode ( Node<E> node ) { assert node != null; Node<E> par = null; while ( node != null ) { par = node; node = par.right; } return par; } public boolean contains ( final E x ) { if ( root == null ) { return false; } return getNode( x ) != null; } public long sum ( final E x ) { if ( root == null ) { return 0; } Node<E> node = getNode( x ); return node != null ? node.count : 0; } public E pollFirst () { if ( root == null ) { return null; } --size; final Node<E> min = getFirstNode( root ); hash ^= min.value.hashCode(); final E ans = min.value; delete( min ); return ans; } public E pollLast () { if ( root == null ) { return null; } --size; final Node<E> max = getLastNode( root ); hash ^= max.value.hashCode(); final E ans = max.value; delete( max ); return ans; } public E ceiling ( final E x ) { return ceiling( root, x ); } private E ceiling ( Node<E> node, final E x ) { Node<E> ans = null; while ( node != null ) { final int result = x.compareTo( node.value ); if ( result > 0 ) { node = node.right; } else if ( result < 0 ) { ans = node; node = node.left; } else { return x; } } return ans != null ? ans.value : null; } public E higher ( final E x ) { return higher( root, x ); } private E higher ( Node<E> node, final E x ) { Node<E> ans = null; while ( node != null ) { final int result = x.compareTo( node.value ); if ( result >= 0 ) { node = node.right; } else { ans = node; node = node.left; } } return ans != null ? ans.value : null; } public E floor ( final E x ) { return floor( root, x ); } private E floor ( Node<E> node, final E x ) { Node<E> ans = null; while ( node != null ) { final int result = x.compareTo( node.value ); if ( result < 0 ) { node = node.left; } else if ( result > 0 ) { ans = node; node = node.right; } else { return x; } } return ans != null ? ans.value : null; } public E lower ( final E x ) { return lower( root, x ); } private E lower ( Node<E> node, final E x ) { Node<E> ans = null; while ( node != null ) { final int result = x.compareTo( node.value ); if ( result <= 0 ) { node = node.left; } else { ans = node; node = node.right; } } return ans != null ? ans.value : null; } public int size () { return uniqueSize; } public long sumSize () { return size; } public ArrayList<E> toList () { final ArrayList<E> list = new ArrayList<>(); if ( root != null ) { final ArrayDeque<Node<E>> deq = new ArrayDeque<>(); deq.add( root ); while ( !deq.isEmpty() ) { final Node<E> now = deq.pollLast(); if ( list.size() == 0 ) { if ( now.left != null ) { deq.add( now ); deq.add( now.left ); } else { for ( int i = 0; i < now.count; ++i ) { list.add( now.value ); } if ( now.right != null ) { deq.add( now.right ); } } } else if ( now.left != null && list.get( list.size() - 1 ).compareTo( now.left.value ) < 0 ) { deq.add( now ); deq.add( now.left ); } else { for ( int i = 0; i < now.count; ++i ) { list.add( now.value ); } if ( now.right != null ) { deq.add( now.right ); } } } } return list; } @Override public String toString () { final ArrayList<E> list = toList(); return list.toString(); } @Override public boolean equals ( final Object o ) { if ( o instanceof final TreeMulti<?> tree ) { if ( size != tree.size() ) { return false; } final ArrayList<E> array1 = toList(); final ArrayList<?> array2 = tree.toList(); for ( int i = 0; i < size; ++i ) { if ( !array1.get( i ).equals( array2.get( i ) ) ) { return false; } } return true; } return false; } @Override public int hashCode () { return hash; } /* * 以下、平衡用メソッド */ private void fix ( Node<E> node ) { while ( node != null ) { final int lh = node.left == null ? 0 : node.left.height; final int rh = node.right == null ? 0 : node.right.height; if ( lh - rh > 1 ) { assert node.left != null; if ( node.left.right != null && node.left.right.height == lh - 1 ) { rotateL( node.left ); setStates( node.left ); } rotateR( node ); } else if ( rh - lh > 1 ) { assert node.right != null; if ( node.right.left != null && node.right.left.height == rh - 1 ) { rotateR( node.right ); setStates( node.right ); } rotateL( node ); } setStates( node ); node = node.parent; } } private void rotateR ( final Node<E> node ) { final Node<E> temp = node.left; node.left = temp.right; if ( node.left != null ) { node.left.parent = node; } temp.right = node; temp.parent = node.parent; if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = temp; } else { node.parent.right = temp; } } else { root = temp; } node.parent = temp; setStates( node ); } private void rotateL ( final Node<E> node ) { final Node<E> temp = node.right; node.right = temp.left; if ( node.right != null ) { node.right.parent = node; } temp.left = node; temp.parent = node.parent; if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = temp; } else { node.parent.right = temp; } } else { root = temp; } node.parent = temp; setStates( node ); } private void setStates ( final Node<E> node ) { final int lh = node.left != null ? node.left.height : 0; final int rh = node.right != null ? node.right.height : 0; node.height = Math.max( lh, rh ) + 1; final long ls = node.left != null ? node.left.size : 0; final long rs = node.right != null ? node.right.size : 0; node.size = ls + rs + node.count; } } final class TreeMultiInt { private Node root; private long size; private int uniqueSize; private int hash; public TreeMultiInt () { size = 0; uniqueSize = 0; root = null; hash = 0; } static final private class Node { int value; long count, size; int height; Node left, right, parent; public Node ( final Node p, final int v, final long c ) { value = v; parent = p; count = c; height = 1; size = c; } } public void add ( final int x ) { if ( root == null ) { root = new Node( null, x, 1 ); ++uniqueSize; } else { Node par; Node now = root; boolean bool = true; do { par = now; if ( x < now.value ) { now = now.left; } else if ( x > now.value ) { now = now.right; } else { bool = false; ++now.count; break; } } while ( now != null ); if ( bool ) { ++uniqueSize; if ( x < par.value ) { par.left = new Node( par, x, 1 ); } else { par.right = new Node( par, x, 1 ); } } fix( par ); } ++size; hash ^= x; } public void add ( final int x, final long sum ) { if ( root == null ) { root = new Node( null, x, sum ); ++uniqueSize; } else { Node par; Node now = root; boolean bool = true; do { par = now; if ( x < now.value ) { now = now.left; } else if ( x > now.value ) { now = now.right; } else { bool = false; now.count += sum; fix( now ); break; } } while ( now != null ); if ( bool ) { ++uniqueSize; if ( x < par.value ) { par.left = new Node( par, x, sum ); } else { par.right = new Node( par, x, sum ); } fix( par ); } } size += sum; if ( sum % 2 == 1 ) { hash ^= x; } } public int get ( long index ) { if ( root == null || size <= index ) { throw new NullPointerException(); } Node now = root; while ( true ) { assert now != null; final long ls = now.left != null ? now.left.size : 0; if ( index < ls ) { now = now.left; } else if ( ls + now.count <= index ) { index -= ls + now.count; now = now.right; } else { break; } } return now.value; } public boolean remove ( final int x ) { final Node n = getNode( x ); if ( n == null ) { return false; } --size; hash ^= x; delete( n ); return true; } public long remove ( final int x, final long sum ) { final Node n = getNode( x ); if ( n == null ) { return 0; } final long ans = Math.min( sum, n.count ); size -= ans; n.count -= ans - 1; if ( ans % 2 == 1 ) { hash ^= x; } delete( n ); return ans; } public long removeAll ( final int x ) { final Node n = getNode( x ); if ( n == null ) { return 0; } size -= n.count; final long ans = n.count; if ( n.count % 2 == 1 ) { hash ^= x; } n.count = 0; delete( n ); return ans; } private void delete ( final Node node ) { if ( node != null ) { if ( node.count > 1 ) { --node.count; fix( node ); return; } if ( node.left == null && node.right == null ) { if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = null; } else { node.parent.right = null; } fix( node.parent ); } else { root = null; } node.parent = null; --uniqueSize; } else { if ( node.left != null && node.right != null ) { final Node rep = getFirstNode( node.right ); node.value = rep.value; node.count = rep.count; rep.count = 0; delete( rep ); } else { final Node rep = node.left != null ? node.left : node.right; rep.parent = node.parent; if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = rep; } else { node.parent.right = rep; } fix( node.parent ); } else { root = rep; } node.parent = null; --uniqueSize; } } } } private Node getNode ( final int x ) { Node now = root; while ( now != null ) { if ( x < now.value ) { now = now.left; } else if ( x > now.value ) { now = now.right; } else { break; } } return now; } public int first () { if ( root == null ) { throw new NullPointerException(); } return getFirstNode( root ).value; } private Node getFirstNode ( Node node ) { assert node != null; Node par = null; while ( node != null ) { par = node; node = par.left; } return par; } public int last () { if ( root == null ) { throw new NullPointerException(); } return getLastNode( root ).value; } private Node getLastNode ( Node node ) { assert node != null; Node par = null; while ( node != null ) { par = node; node = par.right; } return par; } public boolean contains ( final int x ) { if ( root == null ) { return false; } return getNode( x ) != null; } public long sum ( final int x ) { if ( root == null ) { return 0; } Node node = getNode( x ); return node != null ? node.count : 0; } public int pollFirst () { if ( root == null ) { throw new NullPointerException(); } --size; final Node min = getFirstNode( root ); hash ^= min.value; final int ans = min.value; delete( min ); return ans; } public int pollLast () { if ( root == null ) { throw new NullPointerException(); } --size; final Node max = getLastNode( root ); hash ^= max.value; final int ans = max.value; delete( max ); return ans; } public int ceiling ( final int x ) { return ceiling( root, x ); } private int ceiling ( Node node, final int x ) { Node ans = new Node( null, x - 1, 0 ); while ( node != null ) { if ( x > node.value ) { node = node.right; } else if ( x < node.value ) { ans = node; node = node.left; } else { return x; } } return ans.value; } public int higher ( final int x ) { return higher( root, x ); } private int higher ( Node node, final int x ) { Node ans = new Node( null, x - 1, 0 ); while ( node != null ) { if ( x >= node.value ) { node = node.right; } else { ans = node; node = node.left; } } return ans.value; } public int floor ( final int x ) { return floor( root, x ); } private int floor ( Node node, final int x ) { Node ans = new Node( null, x + 1, 0 ); while ( node != null ) { if ( x < node.value ) { node = node.left; } else if ( x > node.value ) { ans = node; node = node.right; } else { return x; } } return ans.value; } public int lower ( final int x ) { return lower( root, x ); } private int lower ( Node node, final int x ) { Node ans = new Node( null, x + 1, 0 ); while ( node != null ) { if ( x <= node.value ) { node = node.left; } else { ans = node; node = node.right; } } return ans.value; } public int size () { return uniqueSize; } public long sumSize () { return size; } public long[][] toArray () { final long[][] list = new long[uniqueSize][2]; if ( root != null ) { int index = 0; final ArrayDeque<Node> deq = new ArrayDeque<>( root.height << 1 ); deq.add( root ); while ( !deq.isEmpty() ) { final Node now = deq.pollLast(); if ( index == 0 ) { if ( now.left != null ) { deq.add( now ); deq.add( now.left ); } else { list[index][0] = now.value; list[index++][1] = now.count; if ( now.right != null ) { deq.add( now.right ); } } } else if ( now.left != null && list[index - 1][0] < now.left.value ) { deq.add( now ); deq.add( now.left ); } else { list[index][0] = now.value; list[index++][1] = now.count; if ( now.right != null ) { deq.add( now.right ); } } } } return list; } public int[] toOneArray () { final int[] list = new int[( int )size]; if ( root != null ) { int index = 0; final ArrayDeque<Node> deq = new ArrayDeque<>(); deq.add( root ); while ( !deq.isEmpty() ) { final Node now = deq.pollLast(); if ( index == 0 ) { if ( now.left != null ) { deq.add( now ); deq.add( now.left ); } else { for ( int i = 0; i < now.count; ++i ) { list[index++] = now.value; } if ( now.right != null ) { deq.add( now.right ); } } } else if ( now.left != null && list[index - 1] < now.left.value ) { deq.add( now ); deq.add( now.left ); } else { for ( int i = 0; i < now.count; ++i ) { list[index++] = now.value; } if ( now.right != null ) { deq.add( now.right ); } } } } return list; } @Override public String toString () { final int[] list = toOneArray(); return Arrays.toString( list ); } @Override public boolean equals ( final Object o ) { if ( o instanceof final TreeMultiInt tree ) { if ( size != tree.size() ) { return false; } final long[][] array1 = toArray(); final long[][] array2 = tree.toArray(); for ( int i = 0; i < size; ++i ) { if ( array1[i][0] != array2[i][0] || array1[i][1] != array2[i][1] ) { return false; } } return true; } return false; } @Override public int hashCode () { return hash; } /* * 以下、平衡用メソッド */ private void fix ( Node node ) { while ( node != null ) { final int lh = node.left == null ? 0 : node.left.height; final int rh = node.right == null ? 0 : node.right.height; if ( lh - rh > 1 ) { assert node.left != null; if ( node.left.right != null && node.left.right.height == lh - 1 ) { rotateL( node.left ); setStates( node.left ); } rotateR( node ); } else if ( rh - lh > 1 ) { assert node.right != null; if ( node.right.left != null && node.right.left.height == rh - 1 ) { rotateR( node.right ); setStates( node.right ); } rotateL( node ); } setStates( node ); node = node.parent; } } private void rotateR ( final Node node ) { final Node temp = node.left; node.left = temp.right; if ( node.left != null ) { node.left.parent = node; } temp.right = node; temp.parent = node.parent; if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = temp; } else { node.parent.right = temp; } } else { root = temp; } node.parent = temp; setStates( node ); } private void rotateL ( final Node node ) { final Node temp = node.right; node.right = temp.left; if ( node.right != null ) { node.right.parent = node; } temp.left = node; temp.parent = node.parent; if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = temp; } else { node.parent.right = temp; } } else { root = temp; } node.parent = temp; setStates( node ); } private void setStates ( final Node node ) { final int lh = node.left != null ? node.left.height : 0; final int rh = node.right != null ? node.right.height : 0; node.height = Math.max( lh, rh ) + 1; final long ls = node.left != null ? node.left.size : 0; final long rs = node.right != null ? node.right.size : 0; node.size = ls + rs + node.count; } } final class TreeMultiLong { private Node root; private long size; private int uniqueSize; private int hash; public TreeMultiLong () { size = 0; uniqueSize = 0; root = null; hash = 0; } static final private class Node { long value; long count, size; int height; Node left, right, parent; public Node ( final Node p, final long v, final long c ) { value = v; parent = p; count = c; height = 1; size = c; } } public void add ( final long x ) { if ( root == null ) { root = new Node( null, x, 1 ); ++uniqueSize; } else { Node par; Node now = root; boolean bool = true; do { par = now; if ( x < now.value ) { now = now.left; } else if ( x > now.value ) { now = now.right; } else { bool = false; ++now.count; break; } } while ( now != null ); if ( bool ) { ++uniqueSize; if ( x < par.value ) { par.left = new Node( par, x, 1 ); } else { par.right = new Node( par, x, 1 ); } } fix( par ); } ++size; hash ^= ( int )x; } public void add ( final long x, final long sum ) { if ( root == null ) { root = new Node( null, x, sum ); ++uniqueSize; } else { Node par; Node now = root; boolean bool = true; do { par = now; if ( x < now.value ) { now = now.left; } else if ( x > now.value ) { now = now.right; } else { bool = false; now.count += sum; fix( now ); break; } } while ( now != null ); if ( bool ) { ++uniqueSize; if ( x < par.value ) { par.left = new Node( par, x, sum ); } else { par.right = new Node( par, x, sum ); } fix( par ); } } size += sum; if ( sum % 2 == 1 ) { hash ^= ( int )x; } } public long get ( long index ) { if ( root == null || size <= index ) { throw new NullPointerException(); } Node now = root; while ( true ) { assert now != null; final long ls = now.left != null ? now.left.size : 0; if ( index < ls ) { now = now.left; } else if ( ls + now.count <= index ) { index -= ls + now.count; now = now.right; } else { break; } } return now.value; } public boolean remove ( final long x ) { final Node n = getNode( x ); if ( n == null ) { return false; } --size; hash ^= x; delete( n ); return true; } public long remove ( final long x, final long sum ) { final Node n = getNode( x ); if ( n == null ) { return 0; } final long ans = Math.min( sum, n.count ); size -= ans; n.count -= ans - 1; if ( ans % 2 == 1 ) { hash ^= ( int )x; } delete( n ); return ans; } public long removeAll ( final long x ) { final Node n = getNode( x ); if ( n == null ) { return 0; } size -= n.count; final long ans = n.count; if ( n.count % 2 == 1 ) { hash ^= ( int )x; } n.count = 0; delete( n ); return ans; } private void delete ( final Node node ) { if ( node != null ) { if ( node.count > 1 ) { --node.count; fix( node ); return; } if ( node.left == null && node.right == null ) { if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = null; } else { node.parent.right = null; } fix( node.parent ); } else { root = null; } node.parent = null; --uniqueSize; } else { if ( node.left != null && node.right != null ) { final Node rep = getFirstNode( node.right ); node.value = rep.value; node.count = rep.count; rep.count = 0; delete( rep ); } else { final Node rep = node.left != null ? node.left : node.right; rep.parent = node.parent; if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = rep; } else { node.parent.right = rep; } fix( node.parent ); } else { root = rep; } node.parent = null; --uniqueSize; } } } } private Node getNode ( final long x ) { Node now = root; while ( now != null ) { if ( x < now.value ) { now = now.left; } else if ( x > now.value ) { now = now.right; } else { break; } } return now; } public long first () { if ( root == null ) { throw new NullPointerException(); } return getFirstNode( root ).value; } private Node getFirstNode ( Node node ) { assert node != null; Node par = null; while ( node != null ) { par = node; node = par.left; } return par; } public long last () { if ( root == null ) { throw new NullPointerException(); } return getLastNode( root ).value; } private Node getLastNode ( Node node ) { assert node != null; Node par = null; while ( node != null ) { par = node; node = par.right; } return par; } public boolean contains ( final long x ) { if ( root == null ) { return false; } return getNode( x ) != null; } public long sum ( final long x ) { if ( root == null ) { return 0; } final Node node = getNode( x ); return node != null ? node.count : 0; } public long pollFirst () { if ( root == null ) { throw new NullPointerException(); } --size; final Node min = getFirstNode( root ); hash ^= ( int )min.value; final long ans = min.value; delete( min ); return ans; } public long pollLast () { if ( root == null ) { throw new NullPointerException(); } --size; final Node max = getLastNode( root ); hash ^= ( int )max.value; final long ans = max.value; delete( max ); return ans; } public long ceiling ( final long x ) { return ceiling( root, x ); } private long ceiling ( Node node, final long x ) { Node ans = new Node( null, x - 1, 0 ); while ( node != null ) { if ( x > node.value ) { node = node.right; } else if ( x < node.value ) { ans = node; node = node.left; } else { return x; } } return ans.value; } public long higher ( final long x ) { return higher( root, x ); } private long higher ( Node node, final long x ) { Node ans = new Node( null, x - 1, 0 ); while ( node != null ) { if ( x >= node.value ) { node = node.right; } else { ans = node; node = node.left; } } return ans.value; } public long floor ( final long x ) { return floor( root, x ); } private long floor ( Node node, final long x ) { Node ans = new Node( null, x + 1, 0 ); while ( node != null ) { if ( x < node.value ) { node = node.left; } else if ( x > node.value ) { ans = node; node = node.right; } else { return x; } } return ans.value; } public long lower ( final long x ) { return lower( root, x ); } private long lower ( Node node, final long x ) { Node ans = new Node( null, x + 1, 0 ); while ( node != null ) { if ( x <= node.value ) { node = node.left; } else { ans = node; node = node.right; } } return ans.value; } public int size () { return uniqueSize; } public long sumSize () { return size; } public long[][] toArray () { final long[][] list = new long[uniqueSize][2]; if ( root != null ) { int index = 0; ArrayDeque<Node> deq = new ArrayDeque<>( root.height << 1 ); deq.add( root ); while ( !deq.isEmpty() ) { final Node now = deq.pollLast(); if ( index == 0 ) { if ( now.left != null ) { deq.add( now ); deq.add( now.left ); } else { list[index][0] = now.value; list[index++][1] = now.count; if ( now.right != null ) { deq.add( now.right ); } } } else if ( now.left != null && list[index - 1][0] < now.left.value ) { deq.add( now ); deq.add( now.left ); } else { list[index][0] = now.value; list[index++][1] = now.count; if ( now.right != null ) { deq.add( now.right ); } } } } return list; } public long[] toOneArray () { final long[] list = new long[( int )size]; if ( root != null ) { int index = 0; final ArrayDeque<Node> deq = new ArrayDeque<>(); deq.add( root ); while ( !deq.isEmpty() ) { final Node now = deq.pollLast(); if ( index == 0 ) { if ( now.left != null ) { deq.add( now ); deq.add( now.left ); } else { for ( int i = 0; i < now.count; ++i ) { list[index++] = now.value; } if ( now.right != null ) { deq.add( now.right ); } } } else if ( now.left != null && list[index - 1] < now.left.value ) { deq.add( now ); deq.add( now.left ); } else { for ( int i = 0; i < now.count; ++i ) { list[index++] = now.value; } if ( now.right != null ) { deq.add( now.right ); } } } } return list; } @Override public String toString () { final long[] list = toOneArray(); return Arrays.toString( list ); } @Override public boolean equals ( final Object o ) { if ( o instanceof final TreeMultiInt tree ) { if ( size != tree.size() ) { return false; } final long[][] array1 = toArray(); final long[][] array2 = tree.toArray(); for ( int i = 0; i < size; ++i ) { if ( array1[i][0] != array2[i][0] || array1[i][1] != array2[i][1] ) { return false; } } return true; } return false; } @Override public int hashCode () { return hash; } /* * 以下、平衡用メソッド */ private void fix ( Node node ) { while ( node != null ) { final int lh = node.left == null ? 0 : node.left.height; final int rh = node.right == null ? 0 : node.right.height; if ( lh - rh > 1 ) { assert node.left != null; if ( node.left.right != null && node.left.right.height == lh - 1 ) { rotateL( node.left ); setStates( node.left ); } rotateR( node ); } else if ( rh - lh > 1 ) { assert node.right != null; if ( node.right.left != null && node.right.left.height == rh - 1 ) { rotateR( node.right ); setStates( node.right ); } rotateL( node ); } setStates( node ); node = node.parent; } } private void rotateR ( final Node node ) { final Node temp = node.left; node.left = temp.right; if ( node.left != null ) { node.left.parent = node; } temp.right = node; temp.parent = node.parent; if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = temp; } else { node.parent.right = temp; } } else { root = temp; } node.parent = temp; setStates( node ); } private void rotateL ( final Node node ) { final Node temp = node.right; node.right = temp.left; if ( node.right != null ) { node.right.parent = node; } temp.left = node; temp.parent = node.parent; if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = temp; } else { node.parent.right = temp; } } else { root = temp; } node.parent = temp; setStates( node ); } private void setStates ( final Node node ) { final int lh = node.left != null ? node.left.height : 0; final int rh = node.right != null ? node.right.height : 0; node.height = Math.max( lh, rh ) + 1; final long ls = node.left != null ? node.left.size : 0; final long rs = node.right != null ? node.right.size : 0; node.size = ls + rs + node.count; } } final class SimpleScanner { private final int BUFF_SIZE = 1 << 17; private final InputStream is; private final byte[] buff; private int point, length; public SimpleScanner ( final InputStream is ) { this.is = is; buff = new byte[BUFF_SIZE]; point = length = 0; } private void reload () { do { try { length = is.read( buff, point = 0, BUFF_SIZE ); } catch ( final IOException e ) { e.printStackTrace(); System.exit( 1 ); } } while ( length == -1 ); } private byte read () { if ( point == length ) { reload(); } return buff[point++]; } public byte nextByte () { byte c = read(); while ( c <= ' ' ) { c = read(); } return c; } public int nextInt () { int ans = 0; byte c = nextByte(); final boolean negate = c == '-'; if ( !MathFunction.rangeCheckClose( c, '0', '9' ) ) { c = read(); } while ( MathFunction.rangeCheckClose( c, '0', '9' ) ) { ans = ans * 10 + c - '0'; c = read(); } return negate ? -ans : ans; } public long nextLong () { long ans = 0; byte c = nextByte(); final boolean negate = c == '-'; if ( !MathFunction.rangeCheckClose( c, '0', '9' ) ) { c = read(); } while ( MathFunction.rangeCheckClose( c, '0', '9' ) ) { ans = ans * 10L + c - '0'; c = read(); } return negate ? -ans : ans; } public char nextChar () { return ( char )nextByte(); } public String next () { final StringBuilder ans = new StringBuilder(); byte c = nextByte(); while ( c > ' ' ) { ans.append( ( char )c ); c = read(); } return ans.toString(); } public BigInteger nextBigInteger () { return new BigInteger( next() ); } public byte[] nextByte ( final int n ) { final byte[] ans = new byte[n]; for ( int i = 0; i < n; ++i ) { ans[i] = nextByte(); } return ans; } public int[] nextInt ( final int n ) { final int[] ans = new int[n]; for ( int i = 0; i < n; ++i ) { ans[i] = nextInt(); } return ans; } public long[] nextLong ( final int n ) { final long[] ans = new long[n]; for ( int i = 0; i < n; ++i ) { ans[i] = nextLong(); } return ans; } public String[] next ( final int n ) { final String[] ans = new String[n]; for ( int i = 0; i < n; ++i ) { ans[i] = next(); } return ans; } public byte[][] nextByte ( final int n, final int m ) { final byte[][] ans = new byte[n][]; for ( int i = 0; i < n; ++i ) { ans[i] = nextByte( m ); } return ans; } public int[][] nextInt ( final int n, final int m ) { final int[][] ans = new int[n][]; for ( int i = 0; i < n; ++i ) { ans[i] = nextInt( m ); } return ans; } public long[][] nextLong ( final int n, final int m ) { final long[][] ans = new long[n][]; for ( int i = 0; i < n; ++i ) { ans[i] = nextLong( m ); } return ans; } public String[][] next ( final int n, final int m ) { final String[][] ans = new String[n][]; for ( int i = 0; i < n; ++i ) { ans[i] = next( m ); } return ans; } public char[] nextCharArray () { return next().toCharArray(); } public char[][] nextCharArray ( final int n ) { final char[][] ans = new char[n][]; for ( int i = 0; i < n; ++i ) { ans[i] = nextCharArray(); } return ans; } public int[][] nextGraph ( final int N, final int M ) { if ( M == 0 ) { return new int[N + 1][0]; } final int[][] ans = new int[N + 1][]; final int[] count = new int[N + 1]; final int[][] path = nextInt( M, 2 ); for ( final int[] temp: path ) { ++count[temp[0]]; ++count[temp[1]]; } for ( int i = 1; i <= N; ++i ) { ans[i] = new int[count[i]]; } for ( final int[] temp: path ) { ans[temp[0]][--count[temp[0]]] = temp[1]; ans[temp[1]][--count[temp[1]]] = temp[0]; } ans[0] = new int[0]; return ans; } public Point nextPoint () { return new Point( nextInt(), nextInt() ); } public Point[] nextPoint ( final int n ) { final Point[] ans = new Point[n]; for ( int i = 0; i < n; ++i ) { ans[i] = nextPoint(); } return ans; } public void close () { try { is.close(); } catch ( final IOException e ) { e.printStackTrace(); System.exit( 1 ); } } } final class SimpleOutputStream extends FilterOutputStream { private final byte buf[]; private int count; public SimpleOutputStream(final OutputStream out) { this(out, 1<<17); } public SimpleOutputStream(final OutputStream out, final int size) { super(out); if (size <= 0) { throw new IllegalArgumentException("Buffer size <= 0"); } buf = new byte[size]; } private void flushBuffer() throws IOException { if (count > 0) { out.write(buf, 0, count); count = 0; } } public void write(final int b) throws IOException { if (count >= buf.length) { flushBuffer(); } buf[count++] = (byte)b; } public void write(final byte b[], final int off, final int len) throws IOException { if (len >= buf.length) { flushBuffer(); out.write(b, off, len); return; } if (len > buf.length - count) { flushBuffer(); } System.arraycopy(b, off, buf, count, len); count += len; } public void flush() throws IOException { flushBuffer(); out.flush(); } } final class SimpleWriter implements Appendable, Closeable, Flushable, AutoCloseable{ private Writer out; private final boolean autoFlush; private boolean trouble = false; private Formatter formatter; private PrintStream psOut = null; private static Charset toCharset ( final String csn ) { if ( csn == null ) { throw new NullPointerException( "charsetName" ); } try { return Charset.forName( csn ); } catch ( IllegalCharsetNameException | UnsupportedCharsetException e ) { e.printStackTrace(); System.exit( 1 ); return null; } } public SimpleWriter ( final Writer out ) { this( out, false ); } public SimpleWriter ( final Writer out, final boolean autoFlush ) { this.out = out; this.autoFlush = autoFlush; } public SimpleWriter ( final OutputStream out ) { this( out, false ); } public SimpleWriter ( final OutputStream out, final boolean autoFlush ) { this(out, autoFlush, Charset.defaultCharset()); } public SimpleWriter(final OutputStream out, final boolean autoFlush, final Charset charset) { this(new BufferedWriter(new OutputStreamWriter(new SimpleOutputStream(out), charset)), autoFlush); if (out instanceof PrintStream) { psOut = (PrintStream) out; } } private void ensureOpen () throws IOException { if ( out == null ) { throw new IOException( "Stream closed" ); } } public void flush () { try { ensureOpen(); out.flush(); } catch ( IOException x ) { trouble = true; } } public void close () { try { if ( out == null ) { return; } out.close(); out = null; } catch ( IOException x ) { trouble = true; } } public boolean checkError () { if ( out != null ) { flush(); } else if ( psOut != null ) { return psOut.checkError(); } return trouble; } private void setError () { trouble = true; } private void clearError () { trouble = false; } public void write ( final int c ) { try { ensureOpen(); out.write( c ); } catch ( InterruptedIOException x ) { Thread.currentThread().interrupt(); } catch ( IOException x ) { trouble = true; } } public void write ( final char[] buf, final int off, final int len ) { try { ensureOpen(); out.write( buf, off, len ); } catch ( InterruptedIOException x ) { Thread.currentThread().interrupt(); } catch ( IOException x ) { trouble = true; } } public void write ( final char[] buf ) { write( buf, 0, buf.length ); } public void write ( final String s, final int off, final int len ) { try { ensureOpen(); out.write( s, off, len ); } catch ( InterruptedIOException x ) { Thread.currentThread().interrupt(); } catch ( IOException x ) { trouble = true; } } public void write ( final String s ) { write( s, 0, s.length() ); } private void newLine () { try { ensureOpen(); out.write( System.lineSeparator() ); if ( autoFlush ) { out.flush(); } } catch ( InterruptedIOException x ) { Thread.currentThread().interrupt(); } catch ( IOException x ) { trouble = true; } } public void print ( final boolean b ) { write( b ? "true" : "false" ); } public void print ( final char c ) { write( c ); } public void print ( final int i ) { write( String.valueOf( i ) ); } public void print ( final long l ) { write( String.valueOf( l ) ); } public void print ( final float f ) { write( String.valueOf( f ) ); } public void print ( final double d ) { write( String.valueOf( d ) ); } public void print ( final char[] s ) { write( s ); } public void print ( final String s ) { write( s ); } public void print ( final Object obj ) { write( obj.toString() ); } public void println () { newLine(); } public void println ( final boolean x ) { print( x ); println(); } public void println ( final char x ) { print( x ); println(); } public void println ( final int x ) { print( x ); println(); } public void println ( final long x ) { print( x ); println(); } public void println ( final float x ) { print( x ); println(); } public void println ( final double x ) { print( x ); println(); } public void println ( final char[] x ) { print( x ); println(); } public void println ( final String x ) { print( x ); println(); } public void println ( final Object x ) { print( x.toString() ); println(); } public SimpleWriter printf ( final String format, final Object... args ) { return format( format, args ); } public SimpleWriter printf ( final Locale l, final String format, final Object... args ) { return format( l, format, args ); } public SimpleWriter format ( final String format, final Object... args ) { try { ensureOpen(); if ( ( formatter == null ) || ( formatter.locale() != Locale.getDefault() ) ) { formatter = new Formatter( this ); } formatter.format( Locale.getDefault(), format, args ); if ( autoFlush ) { out.flush(); } } catch ( InterruptedIOException x ) { Thread.currentThread().interrupt(); } catch ( IOException x ) { trouble = true; } return this; } public SimpleWriter format ( final Locale l, final String format, final Object... args ) { try { ensureOpen(); if ( ( formatter == null ) || ( formatter.locale() != l ) ) { formatter = new Formatter( this, l ); } formatter.format( l, format, args ); if ( autoFlush ) { out.flush(); } } catch ( InterruptedIOException x ) { Thread.currentThread().interrupt(); } catch ( IOException x ) { trouble = true; } return this; } public SimpleWriter append ( final CharSequence csq ) { write( String.valueOf( csq ) ); return this; } public SimpleWriter append ( CharSequence csq, final int start, final int end ) { if ( csq == null ) { csq = "null"; } return append( csq.subSequence( start, end ) ); } public SimpleWriter append ( final char c ) { write( c ); return this; } public void println ( final int[] array ) { println( array, ' ' ); } public void println ( final int[] array, final String str ) { print( array[0] ); for ( int i = 1; i < array.length; ++i ) { print( str ); print( array[i] ); } println(); } public void println ( final int[] array, final char c ) { print( array[0] ); for ( int i = 1; i < array.length; ++i ) { print( c ); print( array[i] ); } println(); } public void println ( final int[][] array ) { println( array, ' ' ); } public void println ( final int[][] arrays, final String str ) { for ( final int[] array: arrays ) { println( array, str ); } } public void println ( final int[][] arrays, final char c ) { for ( final int[] array: arrays ) { println( array, c ); } } public void println ( final long[] array ) { println( array, ' ' ); } public void println ( final long[] array, final String str ) { print( array[0] ); for ( int i = 1; i < array.length; ++i ) { print( str ); print( array[i] ); } println(); } public void println ( final long[] array, final char c ) { print( array[0] ); for ( int i = 1; i < array.length; ++i ) { print( c ); print( array[i] ); } println(); } public void println ( final long[][] array ) { println( array, ' ' ); } public void println ( final long[][] arrays, final String str ) { for ( final long[] array: arrays ) { println( array, str ); } } public void println ( final long[][] arrays, final char c ) { for ( final long[] array: arrays ) { println( array, c ); } } public void println ( final double[] array ) { println( array, ' ' ); } public void println ( final double[] array, final String str ) { print( array[0] ); for ( int i = 1; i < array.length; ++i ) { print( str ); print( array[i] ); } println(); } public void println ( final double[] array, final char c ) { print( array[0] ); for ( int i = 1; i < array.length; ++i ) { print( c ); print( array[i] ); } println(); } public void println ( final double[][] array ) { println( array, ' ' ); } public void println ( final double[][] arrays, final String str ) { for ( final double[] array: arrays ) { println( array, str ); } } public void println ( final double[][] arrays, final char c ) { for ( final double[] array: arrays ) { println( array, c ); } } public void println ( final char[] cs, final String str ) { print( cs[0] ); for ( int i = 1; i < cs.length; ++i ) { print( str ); print( cs[i] ); } println(); } public void println ( final char[] cs, final char c ) { print( cs[0] ); for ( int i = 1; i < cs.length; ++i ) { print( c ); print( cs[i] ); } println(); } public void println ( final char[][] cs ) { for ( final char[] c: cs ) { println( c ); } } public void println ( final char[][] cs, final String str ) { for ( final char[] c: cs ) { println( c, str ); } } public void println ( final char[][] cs, final char c ) { for ( final char[] cc: cs ) { println( cc, c ); } } public <E> void println ( final E[] array ) { println( array, ' ' ); } public <E> void println ( final E[] array, final String str ) { print( array[0] ); for ( int i = 1; i < array.length; ++i ) { print( str ); print( array[i] ); } println(); } public <E> void println ( final E[] array, final char c ) { print( array[0] ); for ( int i = 1; i < array.length; ++i ) { print( c ); print( array[i] ); } println(); } public <E> void println ( final E[][] arrays ) { println( arrays, ' ' ); } public <E> void println ( final E[][] arrays, final String str ) { for ( final E[] array: arrays ) { println( array, str ); } } public <E> void println ( final E[][] arrays, final char c ) { for ( final E[] array: arrays ) { println( array, c ); } } public <E> void println ( final List<E> list ) { println( list, ' ' ); } public <E> void println ( final List<E> list, final String str ) { if ( list.size() == 0 ) { println(); return; } print( list.get( 0 ) ); for ( int i = 1; i < list.size(); ++i ) { print( str ); print( list.get( i ) ); } println(); } public <E> void println ( final List<E> list, final char c ) { if ( list.size() == 0 ) { println(); return; } print( list.get( 0 ) ); for ( int i = 1; i < list.size(); ++i ) { print( c ); print( list.get( i ) ); } println(); } } import java.awt.Dimension; import java.awt.Point; import java.io.BufferedWriter; import java.io.Closeable; import java.io.FilterOutputStream; import java.io.Flushable; import java.io.InputStream; import java.io.InterruptedIOException; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintStream; import java.io.PrintWriter; import java.io.Writer; import java.math.BigInteger; import java.nio.charset.Charset; import java.nio.charset.IllegalCharsetNameException; import java.nio.charset.UnsupportedCharsetException; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.Formatter; import java.util.List; import java.util.Locale; import java.util.Random; import java.util.function.DoubleUnaryOperator; import java.util.function.IntBinaryOperator; import java.util.function.IntUnaryOperator; import java.util.function.LongBinaryOperator; import java.util.function.LongUnaryOperator; //Maybe use import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.ListIterator; import java.util.Map.Entry; import java.util.PriorityQueue; import java.util.TreeMap; import java.util.TreeSet; final class Main { private static final boolean autoFlush = false; private static final SimpleScanner sc = new SimpleScanner( System.in ); private static final SimpleWriter out = new SimpleWriter( System.out, autoFlush ); public static void main ( String[] args ) { int N = sc.nextInt(); long X = sc.nextLong(); long Y = sc.nextLong(); long[] A = sc.nextLong(N); HashMap<Long,Long> mapY1 = new HashMap<>(); mapY1.put(0L,0L); for(int i=0;i<N;i+=4){ HashMap<Long,Long> nextY = new HashMap<>(mapY1.size()<<1); for(Entry<Long,Long> subY:mapY1.entrySet()){ long key = subY.getKey(); long val = subY.getValue(); nextY.putIfAbsent(key+A[i],(val<<2)|1); nextY.putIfAbsent(key-A[i],val<<2); } mapY1 = nextY; } HashMap<Long,Long> mapX1 = new HashMap<>(); mapX1.put(0L,0L); for(int i=1;i<N;i+=4){ HashMap<Long,Long> nextX = new HashMap<>(mapX1.size()<<1); for(Entry<Long,Long> subX:mapX1.entrySet()){ long key = subX.getKey(); long val = subX.getValue(); nextX.putIfAbsent(key+A[i],(val<<2)|1); nextX.putIfAbsent(key-A[i],val<<2); } mapX1 = nextX; } HashMap<Long,Long> mapY2 = new HashMap<>(); mapY2.put(0L,0L); for(int i=2;i<N;i+=4){ HashMap<Long,Long> nextY = new HashMap<>(mapY2.size()<<1); for(Entry<Long,Long> subY:mapY2.entrySet()){ long key = subY.getKey(); long val = subY.getValue(); nextY.putIfAbsent(key+A[i],(val<<2)|1); nextY.putIfAbsent(key-A[i],val<<2); } mapY2 = nextY; } HashMap<Long,Long> mapX2 = new HashMap<>(); mapX2.put(0L,0L); for(int i=3;i<N;i+=4){ HashMap<Long,Long> nextX = new HashMap<>(mapX2.size()<<1); for(Entry<Long,Long> subX:mapX2.entrySet()){ long key = subX.getKey(); long val = subX.getValue(); nextX.putIfAbsent(key+A[i],(val<<2)|1); nextX.putIfAbsent(key-A[i],val<<2); } mapX2 = nextX; } String SX = null; for(long num:mapX1.keySet()){ if(mapX2.containsKey(X-num)){ StringBuilder sb = new StringBuilder(); long subX = (N>>1)%2==0? (mapX1.get(num)<<1)|mapX2.get(X-num): mapX1.get(num)|(mapX2.get(X-num)<<1); for(int i=1;i<N;i+=2){ sb.append((subX&1)>0?'U':'D'); subX >>>= 1; } SX = sb.reverse().toString(); break; } } if(SX==null){ System.out.println("No"); return; } String SY = null; for(long num:mapY1.keySet()){ if(mapY2.containsKey(Y-num)){ StringBuilder sb = new StringBuilder(); long subY = (N+1>>1)%2==0? (mapY1.get(num)<<1)|mapY2.get(Y-num): mapY1.get(num)|(mapY2.get(Y-num)<<1); for(int i=0;i<N;i+=2){ sb.append((subY&1)>0?'U':'D'); subY >>>= 1; } SY = sb.reverse().toString(); break; } } if(SY==null){ System.out.println("No"); return; } StringBuilder ans = new StringBuilder(); char bef = 'U'; for(int i=0;i<N;i++){ if(i%2==1){ ans.append(SX.charAt(i>>1)!=bef?'L':'R'); bef = SX.charAt(i>>1); } else{ ans.append(SY.charAt(i>>1)==bef?'L':'R'); bef = SY.charAt(i>>1); } } out.println("Yes"); out.println(ans.toString()); out.close(); } } /*                            ,.へ   ___                        ム  i 「ヒ_i〉                      ゝ 〈  ト ノ                      iニ()  i {            ____         | ヽ  i i          /_, ,‐-\         i  }  |  i       /(●) ( ● )\      {、  λ  ト-┤.     /   (__人__)  \    ,ノ ̄,!  i  ゝ、_    |    ´ ̄`     | ,. '´ハ ,!  ヽ、   ``、,__\           /" \ ヽ/    \ノ ノ   ハ ̄ r/::r―--―/:7 ノ    /      ヽ.     ヽ:<:'::'|:/  /   ,."         `ー 、   \ヽ:::|/   r'"      / ̄二二二二二二二二二二二二二二二二二ヽ      | 答 | A C │|       \_二二二二二二二二二二二二二二二二二ノ */ /*//////////////////////////////////////////////// * My Library * @author viral *///////////////////////////////////////////////// final class Factorial { //階乗とその逆元 private final long[] fact, inFact; private final long mod; /** * 1~Nの階乗とその逆元をmodで割ったあまりを事前に計算します。 * * @param N 計算範囲 * @param mod 法 */ public Factorial ( final int N, final long mod ) { fact = new long[N + 1]; fact[0] = fact[1] = 1; for ( int i = 2; i <= N; ++i ) { fact[i] = fact[i - 1] * i % mod; } inFact = new long[N + 1]; inFact[N] = MathFunction.modPow( fact[N], mod - 2, mod ); for ( int i = N; i > 0; --i ) { inFact[i - 1] = inFact[i] * i % mod; } inFact[0] = 1; this.mod = mod; } /** * num!をmodで割ったあまりを返します。 * * @param num * * @return num! */ public long getFact ( final int num ) { return fact[num]; } /** * num!^-1をmodで割ったあまりを返します。 * * @param num * * @return num! */ public long getInFact ( final int num ) { return inFact[num]; } /** * modを法とした時のnum^-1を返します。 * * @param num * * @return num! */ public long getInverse ( final int num ) { return fact[num - 1] * inFact[num] % mod; } /** * aCbをmodで割ったあまりを返します。 * * @param a * @param b * * @return aCb */ public long getCombi ( final int a, final int b ) { if ( a < b || a < 0 || b < 0 ) { return 0; } return ( fact[a] * inFact[a - b] % mod ) * inFact[b] % mod; } } final class ArrayFunction { /** * カウントソートによるソートです。 * 各要素が0以上であり最大値が十分小さい時はこちらの使用を推奨します。 * * @param array ソート対象のint型配列 * @param maximumLimit array内の最大要素 */ public static void countSort ( final int[] array, final int maximumLimit ) { final int[] list = new int[maximumLimit + 1]; for ( final int num: array ) { ++list[num]; } int temp = 0; for ( int i = 0; i < list.length; ++i ) { while ( list[i]-- > 0 ) { array[temp++] = i; } } } /** * 配列を右周りに90度回転させたものを返します。 * 長方形でない配列に関しての動作は保証していません。 * * @param array 回転させる行列 * * @return 回転させた配列 */ public static int[][] rotateR ( final int[][] array ) { final int[][] ans = new int[array[0].length][array.length]; for ( int i = 0; i < ans.length; ++i ) { for ( int j = 0; j < ans[i].length; ++j ) { ans[i][j] = array[ans[i].length - j - 1][i]; } } return ans; } /** * 配列を右周りに90度回転させたものを返します。 * 長方形でない配列に関しての動作は保証していません。 * * @param array 回転させる行列 * * @return 回転させた配列 */ public static long[][] rotateR ( final long[][] array ) { final long[][] ans = new long[array[0].length][array.length]; for ( int i = 0; i < ans.length; ++i ) { for ( int j = 0; j < ans[i].length; ++j ) { ans[i][j] = array[ans[i].length - j - 1][i]; } } return ans; } /** * 配列を右周りに90度回転させたものを返します。 * 長方形でない配列に関しての動作は保証していません。 * * @param array 回転させる行列 * * @return 回転させた配列 */ public static char[][] rotateR ( final char[][] array ) { final char[][] ans = new char[array[0].length][array.length]; for ( int i = 0; i < ans.length; ++i ) { for ( int j = 0; j < ans[i].length; ++j ) { ans[i][j] = array[ans[i].length - j - 1][i]; } } return ans; } /** * 配列を右周りに90度回転させたものを返します。 * 長方形でない配列に関しての動作は保証していません。 * * @param array 回転させる行列 * * @return 回転させた配列 */ public static double[][] rotateR ( final double[][] array ) { final double[][] ans = new double[array[0].length][array.length]; for ( int i = 0; i < ans.length; ++i ) { for ( int j = 0; j < ans[i].length; ++j ) { ans[i][j] = array[ans[i].length - j - 1][i]; } } return ans; } /** * 配列を右周りに90度回転させたものを返します。 * 長方形でない配列に関しての動作は保証していません。 * * @param array 回転させる行列 * * @return 回転させた配列 */ public static boolean[][] rotateR ( final boolean[][] array ) { final boolean[][] ans = new boolean[array[0].length][array.length]; for ( int i = 0; i < ans.length; ++i ) { for ( int j = 0; j < ans[i].length; ++j ) { ans[i][j] = array[ans[i].length - j - 1][i]; } } return ans; } /** * 配列を右周りに90度回転させたものを返します。 * 長方形でない配列に関しての動作は保証していません。 * * @param array 回転させる行列 * * @return 回転させた配列 */ public static <E> E[][] rotateR ( final E[][] array, final E[][] ans ) { for ( int i = 0; i < ans.length; ++i ) { for ( int j = 0; j < ans[i].length; ++j ) { ans[i][j] = array[ans[i].length - j - 1][i]; } } return ans; } /** * 配列を左周りに90度回転させたものを返します。 * 長方形でない配列に関しての動作は保証していません。 * * @param array 回転させる行列 * * @return 回転させた配列 */ public static int[][] rotateL ( final int[][] array ) { final int[][] ans = new int[array[0].length][array.length]; for ( int i = 0; i < ans.length; ++i ) { final int index = i; Arrays.setAll( ans[i], k -> array[k][ans.length - index - 1] ); } return ans; } /** * 配列を左周りに90度回転させたものを返します。 * 長方形でない配列に関しての動作は保証していません。 * * @param array 回転させる行列 * * @return 回転させた配列 */ public static long[][] rotateL ( final long[][] array ) { final long[][] ans = new long[array[0].length][array.length]; for ( int i = 0; i < ans.length; ++i ) { final int index = i; Arrays.setAll( ans[i], k -> array[k][ans.length - index - 1] ); } return ans; } /** * 配列を左周りに90度回転させたものを返します。 * 長方形でない配列に関しての動作は保証していません。 * * @param array 回転させる行列 * * @return 回転させた配列 */ public static char[][] rotateL ( final char[][] array ) { final char[][] ans = new char[array[0].length][array.length]; for ( int i = 0; i < ans.length; ++i ) { for ( int j = 0; j < ans[i].length; ++j ) { ans[i][j] = array[j][ans.length - i - 1]; } } return ans; } /** * 配列を左周りに90度回転させたものを返します。 * 長方形でない配列に関しての動作は保証していません。 * * @param array 回転させる行列 * * @return 回転させた配列 */ public static double[][] rotateL ( final double[][] array ) { final double[][] ans = new double[array[0].length][array.length]; for ( int i = 0; i < ans.length; ++i ) { for ( int j = 0; j < ans[i].length; ++j ) { ans[i][j] = array[j][ans.length - i - 1]; } } return ans; } /** * 配列を左周りに90度回転させたものを返します。 * 長方形でない配列に関しての動作は保証していません。 * * @param array 回転させる行列 * * @return 回転させた配列 */ public static boolean[][] rotateL ( final boolean[][] array ) { final boolean[][] ans = new boolean[array[0].length][array.length]; for ( int i = 0; i < ans.length; ++i ) { for ( int j = 0; j < ans[i].length; ++j ) { ans[i][j] = array[j][ans.length - i - 1]; } } return ans; } /** * 配列を左周りに90度回転させたものを返します。 * 長方形でない配列に関しての動作は保証していません。 * * @param array 回転させる行列 * * @return 回転させた配列 */ public static <E> E[][] rotateL ( final E[][] array, final E[][] ans ) { for ( int i = 0; i < ans.length; ++i ) { for ( int j = 0; j < ans[i].length; ++j ) { ans[i][j] = array[j][ans.length - i - 1]; } } return ans; } /** * 引数の配列の最長狭義増加部分列の長さを返します。 * * @param array 最長狭義増加部分列の長さを求める配列 * * @return 最長狭義増加部分列の長さ */ public static int lis ( int[] array ) { return lis( array, false ); } /** * 引数の配列指定されたインデックスの最長狭義増加部分列の長さを返します。 * * @param arrays 最長狭義増加部分列の長さを求める配列 * @param p 探索する配列のインデックス * * @return arrays[i][p](0 < = p < = arrays.length)の最長狭義増加部分列の長さ */ public static int lis ( int[][] arrays, int p ) { return lis( arrays, p, false ); } /** * 引数の配列の最長狭義増加部分列の長さを返します。 * * @param array 最長狭義増加部分列の長さを求める配列 * * @return 最長狭義増加部分列の長さ */ public static int lis ( long[] array ) { return lis( array, false ); } /** * 引数の配列指定されたインデックスの最長狭義増加部分列の長さを返します。 * * @param arrays 最長狭義増加部分列の長さを求める配列 * @param p 探索する配列のインデックス * * @return arrays[i][p](0 < = p < = arrays.length)の最長狭義増加部分列の長さ */ public static int lis ( long[][] arrays, int p ) { return lis( arrays, p, false ); } /** * 引数の配列の最長増加部分列の長さを返します。 * * @param array 最長増加部分列の長さを求める配列 * @param include 広義増加列ならtrue、狭義増加列ならfalse * * @return 最長狭義増加部分列の長さ */ public static int lis ( int[] array, boolean include ) { int[] list = new int[array.length]; Arrays.fill( list, Integer.MAX_VALUE ); for ( int num: array ) { int index = include ? Searcher.overSearch( list, num ) : Searcher.upSearch( list, num ); list[index] = Math.min( list[index], num ); } int answer = Searcher.underSearch( list, Integer.MAX_VALUE ); return answer + 1; } /** * 引数の配列指定されたインデックスの最長狭義増加部分列の長さを返します。 * * @param arrays 最長狭義増加部分列の長さを求める配列 * @param p 探索する配列のインデックス * @param include 広義増加列ならtrue、狭義増加列ならfalse * * @return arrays[i][p](0 < = p < = arrays.length)の最長狭義増加部分列の長さ */ public static int lis ( int[][] arrays, int p, boolean include ) { int[] list = new int[arrays.length]; Arrays.fill( list, Integer.MAX_VALUE ); for ( int[] array: arrays ) { int index = include ? Searcher.overSearch( list, array[p] ) : Searcher.upSearch( list, array[p] ); list[index] = Math.min( list[index], array[p] ); } int answer = Searcher.underSearch( list, Integer.MAX_VALUE ); return answer + 1; } /** * 引数の配列の最長増加部分列の長さを返します。 * * @param array 最長増加部分列の長さを求める配列 * @param include 広義増加列ならtrue、狭義増加列ならfalse * * @return 最長狭義増加部分列の長さ */ public static int lis ( long[] array, boolean include ) { long[] list = new long[array.length]; Arrays.fill( list, Long.MAX_VALUE ); for ( long num: array ) { int index = include ? Searcher.overSearch( list, num ) : Searcher.upSearch( list, num ); list[index] = Math.min( list[index], num ); } int answer = Searcher.underSearch( list, Long.MAX_VALUE ); return answer + 1; } /** * 引数の配列指定されたインデックスの最長狭義増加部分列の長さを返します。 * * @param arrays 最長狭義増加部分列の長さを求める配列 * @param p 探索する配列のインデックス * @param include 広義増加列ならtrue、狭義増加列ならfalse * * @return arrays[i][p](0 < = p < = arrays.length)の最長狭義増加部分列の長さ */ public static int lis ( long[][] arrays, int p, boolean include ) { long[] list = new long[arrays.length]; Arrays.fill( list, Long.MAX_VALUE ); for ( long[] array: arrays ) { int index = include ? Searcher.overSearch( list, array[p] ) : Searcher.upSearch( list, array[p] ); list[index] = Math.min( list[index], array[p] ); } int answer = Searcher.underSearch( list, Long.MAX_VALUE ); return answer + 1; } /** * 引数の情報から求められる有向辺に対してトポロジカルソートを行ないます。 * 戻り値は辺を表すint型二次元配列です。 * * @param route 有向グラフの隣接リスト * * @return トポロジカルソート済み有向グラフ */ public static int[][] topologicalSort ( final ArrayList<ArrayList<Integer>> route ) { final int[] count = new int[route.size()]; int pathCount = 0; for ( final ArrayList<Integer> path: route ) { for ( final int point: path ) { ++pathCount; ++count[point]; } } final ArrayDeque<Integer> deq = new ArrayDeque<>(); for ( int i = 1; i < count.length; ++i ) { if ( count[i] == 0 ) { deq.add( i ); } } final int[][] ans = new int[pathCount][2]; int index = 0; while ( deq.size() > 0 ) { int nowP = deq.pollFirst(); for ( final int nextP: route.get( nowP ) ) { ans[index][0] = nowP; ans[index++][1] = nextP; if ( --count[nextP] == 0 ) { deq.add( nextP ); } } } return ans; } /** * 引数の情報から求められる有向辺に対してトポロジカルソートを行ないます。 * 戻り値は辺を表すint型二次元配列です。 * * @param route 有向グラフの隣接リスト * * @return トポロジカルソート済み有向グラフ */ public static int[][] topologicalSort ( final int[][] route ) { final int[] count = new int[route.length]; int pathCount = 0; for ( final int[] path: route ) { for ( final int point: path ) { ++pathCount; ++count[point]; } } final ArrayDeque<Integer> deq = new ArrayDeque<>(); for ( int i = 1; i < count.length; ++i ) { if ( count[i] == 0 ) { deq.add( i ); } } final int[][] ans = new int[pathCount][2]; int index = 0; while ( deq.size() > 0 ) { int nowP = deq.pollFirst(); for ( final int nextP: route[nowP] ) { ans[index][0] = nowP; ans[index++][1] = nextP; if ( --count[nextP] == 0 ) { deq.add( nextP ); } } } return ans; } /** * 引数の位置の要素を交換します。 * * @param array 交換する要素の含まれる配列 * @param a 交換元 * @param b 交換先 */ public static void swap ( final int[] array, final int a, final int b ) { final int temp = array[a]; array[a] = array[b]; array[b] = temp; } /** * 引数の位置の要素を交換します。 * * @param array 交換する要素の含まれる配列 * @param a 交換元 * @param b 交換先 */ public static void swap ( final long[] array, final int a, final int b ) { final long temp = array[a]; array[a] = array[b]; array[b] = temp; } /** * 引数の位置の要素を交換します。 * * @param array 交換する要素の含まれる配列 * @param a 交換元 * @param b 交換先 */ public static void swap ( final double[] array, final int a, final int b ) { final double temp = array[a]; array[a] = array[b]; array[b] = temp; } /** * 引数の位置の要素を交換します。 * * @param array 交換する要素の含まれる配列 * @param a 交換元 * @param b 交換先 */ public static void swap ( final char[] array, final int a, final int b ) { final char temp = array[a]; array[a] = array[b]; array[b] = temp; } /** * 引数の位置の要素を交換します。 * * @param array 交換する要素の含まれる配列 * @param a 交換元 * @param b 交換先 */ public static void swap ( final boolean[] array, final int a, final int b ) { final boolean temp = array[a]; array[a] = array[b]; array[b] = temp; } /** * 引数の位置の要素を交換します。 * * @param array 交換する要素の含まれる配列 * @param a 交換元 * @param b 交換先 */ public static <E> void swap ( final E[] array, final int a, final int b ) { final E temp = array[a]; array[a] = array[b]; array[b] = temp; } /** * 引数の位置の要素を交換します。 * * @param array 交換する要素の含まれる配列 * @param a 交換元の第1インデックス * @param b 交換元の第2インデックス * @param c 交換先の第1インデックス * @param d 交換先の第2インデックス */ public static void swap ( final int[][] array, final int a, final int b, final int c, final int d ) { final int temp = array[a][b]; array[a][b] = array[c][d]; array[c][d] = temp; } /** * 引数の位置の要素を交換します。 * * @param array 交換する要素の含まれる配列 * @param a 交換元の第1インデックス * @param b 交換元の第2インデックス * @param c 交換先の第1インデックス * @param d 交換先の第2インデックス */ public static void swap ( final long[][] array, final int a, final int b, final int c, final int d ) { final long temp = array[a][b]; array[a][b] = array[c][d]; array[c][d] = temp; } /** * 引数の位置の要素を交換します。 * * @param array 交換する要素の含まれる配列 * @param a 交換元の第1インデックス * @param b 交換元の第2インデックス * @param c 交換先の第1インデックス * @param d 交換先の第2インデックス */ public static void swap ( final double[][] array, final int a, final int b, final int c, final int d ) { final double temp = array[a][b]; array[a][b] = array[c][d]; array[c][d] = temp; } /** * 引数の位置の要素を交換します。 * * @param array 交換する要素の含まれる配列 * @param a 交換元の第1インデックス * @param b 交換元の第2インデックス * @param c 交換先の第1インデックス * @param d 交換先の第2インデックス */ public static void swap ( final char[][] array, final int a, final int b, final int c, final int d ) { final char temp = array[a][b]; array[a][b] = array[c][d]; array[c][d] = temp; } /** * 引数の位置の要素を交換します。 * * @param array 交換する要素の含まれる配列 * @param a 交換元の第1インデックス * @param b 交換元の第2インデックス * @param c 交換先の第1インデックス * @param d 交換先の第2インデックス */ public static void swap ( final boolean[][] array, final int a, final int b, final int c, final int d ) { final boolean temp = array[a][b]; array[a][b] = array[c][d]; array[c][d] = temp; } /** * 引数の位置の要素を交換します。 * * @param array 交換する要素の含まれる配列 * @param a 交換元の第1インデックス * @param b 交換元の第2インデックス * @param c 交換先の第1インデックス * @param d 交換先の第2インデックス */ public static <E> void swap ( final E[][] array, final int a, final int b, final int c, final int d ) { final E temp = array[a][b]; array[a][b] = array[c][d]; array[c][d] = temp; } /** * 引数の配列の要素を並び替えた時の、辞書順で引数の次に当たる順番に並び替えます。 * 原則として、要素に重複が無いものとして処理します。重複がある場合の戻り値は保証されていません。 * * @param array 並び替え対象の配列 * * @return 辞書順で次に当たる配列がある場合はtrue、arrayが降順に並んでいるならfalse */ public static boolean nextPermutation ( final int[] array ) { int index1 = 0; for ( int i = 1; i < array.length; ++i ) { if ( array[i - 1] < array[i] ) { index1 = i; } } if ( --index1 < 0 ) { return false; } int index2 = 0; int min = Integer.MAX_VALUE; for ( int i = index1 + 1; i < array.length; ++i ) { if ( MathFunction.rangeCheckOpen( array[i], array[index1], min ) ) { min = array[i]; index2 = i; } } swap( array, index1, index2 ); Arrays.sort( array, index1 + 1, array.length ); return true; } /** * 引数の配列の要素を並び替えた時の、辞書順で引数の次に当たる順番に並び替えます。 * 原則として、要素に重複が無いものとして処理します。重複がある場合の戻り値は保証されていません。 * * @param array 並び替え対象の配列 * * @return 辞書順で次に当たる配列がある場合はtrue、arrayが降順に並んでいるならfalse */ public static boolean nextPermutation ( final long[] array ) { int index1 = 0; for ( int i = 1; i < array.length; ++i ) { if ( array[i - 1] < array[i] ) { index1 = i; } } if ( --index1 < 0 ) { return false; } int index2 = 0; long min = Long.MAX_VALUE; for ( int i = index1 + 1; i < array.length; ++i ) { if ( MathFunction.rangeCheckOpen( array[i], array[index1], min ) ) { min = array[i]; index2 = i; } } swap( array, index1, index2 ); Arrays.sort( array, index1 + 1, array.length ); return true; } /** * 引数の配列の要素を並び替えた時の、辞書順で引数の次に当たる順番に並び替えます。 * 原則として、要素に重複が無いものとして処理します。重複がある場合の戻り値は保証されていません。 * * @param array 並び替え対象の配列 * * @return 辞書順で次に当たる配列がある場合はtrue、arrayが降順に並んでいるならfalse */ public static boolean nextPermutation ( final char[] array ) { int index1 = 0; for ( int i = 1; i < array.length; ++i ) { if ( array[i - 1] < array[i] ) { index1 = i; } } if ( --index1 < 0 ) { return false; } int index2 = 0; int min = Integer.MAX_VALUE; for ( int i = index1 + 1; i < array.length; ++i ) { if ( MathFunction.rangeCheckOpen( array[i], array[index1], min ) ) { min = array[i]; index2 = i; } } swap( array, index1, index2 ); Arrays.sort( array, index1 + 1, array.length ); return true; } /** * 引数の配列の要素を並び替えた時の、辞書順で引数の次に当たる順番に並び替えます。 * 原則として、要素に重複が無いものとして処理します。重複がある場合の戻り値は保証されていません。 * * @param array 並び替え対象の配列 * * @return 辞書順で次に当たる配列がある場合はtrue、arrayが降順に並んでいるならfalse */ public static <E extends Comparable<E>> boolean nextPermutation ( final E[] array ) { int index1 = 0; for ( int i = 1; i < array.length; ++i ) { if ( array[i - 1].compareTo( array[i] ) < 0 ) { index1 = i; } } if ( --index1 < 0 ) { return false; } int index2 = -1; E min = MathFunction.max( array ); int subIndex = -1; E max = array[index1]; for ( int i = index1 + 1; i < array.length; ++i ) { if ( MathFunction.rangeCheckOpen( array[i], array[index1], min ) ) { min = array[i]; index2 = i; } if ( max.compareTo( array[i] ) < 0 ) { subIndex = i; max = array[i]; } } if ( index2 == -1 ) { swap( array, index1, subIndex ); } else { swap( array, index1, index2 ); } Arrays.sort( array, index1 + 1, array.length ); return true; } /** * 引数の配列と関数を元に配列を再構築します。 * * @param array * @param func */ public static int[] reBuild ( final int[] array, final IntUnaryOperator func ) { final int[] ans = new int[array.length]; for ( int i = 0; i < array.length; ++i ) { ans[i] = func.applyAsInt( array[i] ); } return ans; } /** * 引数の配列と関数を元に配列を再構築します。 * * @param array * @param func */ public static int[] reBuild ( final int[] array, final IntBinaryOperator func ) { final int[] ans = new int[array.length]; for ( int i = 0; i < array.length; ++i ) { ans[i] = func.applyAsInt( i, array[i] ); } return ans; } /** * 引数の配列と関数を元に配列を再構築します。 * * @param array * @param func */ public static long[] reBuild ( final long[] array, final LongUnaryOperator func ) { final long[] ans = new long[array.length]; for ( int i = 0; i < array.length; ++i ) { ans[i] = func.applyAsLong( array[i] ); } return ans; } /** * 引数の配列と関数を元に配列を再構築します。 * * @param array * @param func */ public static long[] reBuild ( final long[] array, final LongBinaryOperator func ) { final long[] ans = new long[array.length]; for ( int i = 0; i < array.length; ++i ) { ans[i] = func.applyAsLong( i, array[i] ); } return ans; } } final class Converter { /** * 渡された文字列を逆順にした文字列を返します。 * * @param str 元の文字列 * * @return strを逆順にした文字列 */ public static String reverse ( final String str ) { final StringBuilder sb = new StringBuilder(); for ( int i = str.length() - 1; i >= 0; --i ) { sb.append( str.charAt( i ) ); } return sb.toString(); } /** * 渡されたラッパークラスのListをプリミティヴ型の配列に変換します。 * * @param list 元のList * * @return listの要素をプリミティヴ型に変換した配列 */ public static int[] toIntArray ( final List<Integer> list ) { final int[] ans = new int[list.size()]; int index = 0; for ( int num : list ) { ans[index++] = num; } return ans; } /** * 渡されたラッパークラスのListをプリミティヴ型の配列に変換します。 * * @param list 元のList * * @return listの要素をプリミティヴ型に変換した配列 */ public static long[] toLongArray ( final List<Long> list ) { final long[] ans = new long[list.size()]; int index = 0; for ( long num : list ) { ans[index++] = num; } return ans; } /** * 渡されたラッパークラスのListをプリミティヴ型の配列に変換します。 * * @param list 元のList * * @return listの要素をプリミティヴ型に変換した配列 */ public static char[] toCharArray ( final List<Character> list ) { final char[] ans = new char[list.size()]; int index = 0; for ( char num : list ) { ans[index++] = num; } return ans; } /** * 渡されたラッパークラスのListをプリミティヴ型の配列に変換します。 * * @param list 元のList * * @return listの要素をプリミティヴ型に変換した配列 */ public static double[] toDoubleArray ( final List<Double> list ) { final double[] ans = new double[list.size()]; int index = 0; for ( double num : list ) { ans[index++] = num; } return ans; } /** * 渡されたラッパークラスのListをプリミティヴ型の配列に変換します。 * * @param list 元のList * * @return listの要素をプリミティヴ型に変換した配列 */ public static boolean[] toBooleanArray ( final List<Boolean> list ) { final boolean[] ans = new boolean[list.size()]; int index = 0; for ( boolean num : list ) { ans[index++] = num; } return ans; } /** * 渡されたラッパークラスのListをプリミティヴ型の配列に変換します。 * * @param list 元のList * * @return listの要素をプリミティヴ型に変換した配列 */ public static byte[] toByteArray ( final List<Byte> list ) { final byte[] ans = new byte[list.size()]; int index = 0; for ( byte num : list ) { ans[index++] = num; } return ans; } /** * 渡されたラッパークラスのListをプリミティヴ型の配列に変換します。 * * @param list 元のList * * @return listの要素をプリミティヴ型に変換した配列 */ public static short[] toShortArray ( final List<Short> list ) { final short[] ans = new short[list.size()]; int index = 0; for ( short num : list ) { ans[index++] = num; } return ans; } /** * 渡されたラッパークラスのListをプリミティヴ型の配列に変換します。 * * @param list 元のList * * @return listの要素をプリミティヴ型に変換した配列 */ public static float[] toFloatArray ( final List<Float> list ) { final float[] ans = new float[list.size()]; int index = 0; for ( float num : list ) { ans[index++] = num; } return ans; } } final class MathFunction { private static final int[] numberForIntPrime = {2, 7, 61}; private static final long[] numberForLongPrime = {2, 7, 61, 325, 9375, 28178, 450775, 9780504, 1795265022}; /** * aとbの最大公約数を求めます。戻り値は0以上であることが保証されます。 * * @param a 公約数を求める整数 * @param b 公約数を求める整数 * * @return aとbの最大公約数 */ public static long gcd ( long a, long b ) { a = Math.abs( a ); b = Math.abs( b ); if ( b == 0 ) { return a; } long temp; while ( ( temp = a % b ) != 0 ) { a = b; b = temp; } return b; } /** * aとbの最小公倍数を求めます。 * オーバーフロー検知は出来ません。 * * @param a 公倍数を求める整数 * @param b 公倍数を求める整数 * * @return aとbの最小公倍数 */ public static long lcm ( final long a, final long b ) { return a / gcd( a, b ) * b; } /** * 引数が素数か判定します。 * * @param n 検査対象 * * @return nが素数であるならtrue、素数でないならfalse */ public static boolean isPrime ( long n ) { n = Math.abs( n ); if ( n == 2L ) { return true; } if ( n == 1L || ( n & 1L ) == 0L ) { return false; } if ( n <= 4_759_123_141L ) { return isPrimeForInt( n ); } if ( n <= Long.MAX_VALUE / n ) { return isPrimeForLong( n ); } return isPrimeForBigInteger( n ); } private static boolean isPrimeForInt ( long n ) { long d = n - 1; while ( ( d & 1 ) == 0L ) { d >>= 1; } for ( final long a: numberForIntPrime ) { if ( a >= n ) { return true; } long t = d; long y = MathFunction.modPow( a, t, n ); while ( t < n - 1L && y != 1 && y != n - 1 ) { y = y * y % n; t <<= 1; } if ( y != n - 1 && ( t & 1L ) == 0 ) { return false; } } return true; } private static boolean isPrimeForLong ( long n ) { long d = n - 1L; while ( ( d & 1 ) == 0L ) { d >>= 1; } for ( final long a: numberForLongPrime ) { if ( a >= n ) { return true; } long t = d; long y = MathFunction.modPow( a, t, n ); while ( t < n - 1L && y != 1 && y != n - 1 ) { y = y * y % n; t <<= 1; } if ( y != n - 1 && ( t & 1L ) == 0 ) { return false; } } return true; } private static boolean isPrimeForBigInteger ( long n ) { long d = n - 1L; while ( ( d & 1 ) == 0L ) { d >>= 1; } final BigInteger bigN = BigInteger.valueOf( n ); final BigInteger bigNSubOne = bigN.subtract( BigInteger.ONE ); final BigInteger bigD = BigInteger.valueOf( d ); for ( final long a: numberForLongPrime ) { if ( a >= n ) { return true; } BigInteger t = bigD; BigInteger y = BigInteger.valueOf( a ).modPow( t, bigN ); while ( t.compareTo( bigNSubOne ) == -1 && !y.equals( BigInteger.ONE ) && !y.equals( bigNSubOne ) ) { y = y.multiply( y ).mod( bigN ); t = t.shiftLeft( 1 ); } if ( !y.equals( bigNSubOne ) && (t.intValue()&1) == 0 ) { return false; } } return true; } /** * num以下の素数を列挙します。 * * @param num 素数を探す上限値 * * @return num以下の素数のint型配列 */ public static int[] primes ( final int num ) { if ( num < 2 ) { return new int[0]; } final BitSet numbers = new BitSet( num + 1 ); numbers.set( 2, num + 1 ); final int limit = ( int )Math.sqrt( num ); for ( int i = 2; i <= limit; ++i ) { if ( numbers.get( i ) ) { for ( int j = i * i; j <= num; j += i ) { if ( numbers.get(j) ) { numbers.clear( j ); } } } } final int[] answer = new int[numbers.cardinality()]; int i = 2, index = 0; do { i = numbers.nextSetBit( i ); answer[index++] = i++; } while ( index != answer.length ); return answer; } /** * a**bを計算します。 * * @param a 被累乗数 * @param b 指数 * * @return a**b */ public static long pow ( long a, long b ) { long ans = 1; while ( b > 0 ) { if ( ( b & 1 ) == 1 ) { ans *= a; } a *= a; b >>= 1; } return ans; } /** * a**bをmodで割ったあまりを計算します。 * * @param a 被累乗数 * @param b 指数 * @param mod 法とする整数 * * @return a**bをmodで割ったあまり */ public static long modPow ( long a, long b, final long mod ) { long ans = 1; a %= mod; while ( b > 0 ) { if ( ( b & 1 ) == 1 ) { ans *= a; } ans %= mod; a *= a; a %= mod; b >>= 1; } return ans; } /** * N!を計算します。 * * @param N 階乗を求めるのに用いる値 * * @return N! */ public static long fact ( final int N ) { long ans = 1; for ( int i = 2; i <= N; ++i ) { ans *= i; } return ans; } /** * N!をmodで割ったあまりを計算します。 * * @param N 階乗を求めるのに用いる値 * @param mod 法とする整数 * * @return N!をmodで割ったあまり */ public static long modFact ( final int N, final long mod ) { long ans = 1; for ( int i = 2; i <= N; ++i ) { ans *= i; ans %= mod; } return ans; } /** * nCrを計算します。 * * @param n 二項係数を求めるのに用いる値 * @param r 二項係数を求めるのに用いる値 * * @return nCr */ public static long combi ( final long n, long r ) { if ( r < 0 || n < r ) { return 0; } long ans = 1; r = Math.min( n - r, r ); for ( int i = 0; i < r; ++i ) { ans *= n - i; ans /= i + 1; } return ans; } /** * nCrをmodで割ったあまりを計算します。 * * @param n 二項係数を求めるのに用いる値 * @param r 二項係数を求めるのに用いる値 * @param mod 法とする整数 * * @return nCrをmodで割ったあまり */ public static long modCombi ( final long n, long r, final long mod ) { if ( r < 0 || n < r ) { return 0; } long ans = 1; r = Math.min( n - r, r ); for ( int i = 0; i < r; ++i ) { ans *= ( n - i ) % mod; ans %= mod; ans *= modPow( i + 1, mod - 2, mod ); ans %= mod; } return ans; } /** * 引数の前半二点、後半二点で構成される二線分が交差しているか返します。 * * @param x1 点1のx座標 * @param y1 点1のy座標 * @param x2 点2のx座標 * @param y2 点2のy座標 * @param x3 点3のx座標 * @param y3 点3のy座標 * @param x4 点4のx座標 * @param y4 点4のy座標 * * @return 交差している(線分の端が他方の線分上に存在する場合も含む)場合は1、同一線分直線上なら0、それ以外は-1 */ public static int isCrossed ( final int x1, final int y1, final int x2, final int y2, final int x3, final int y3, final int x4, final int y4 ) { final long s1 = ( long )( x1 - x2 ) * ( y3 - y1 ) - ( long )( y1 - y2 ) * ( x3 - x1 ); final long t1 = ( long )( x1 - x2 ) * ( y4 - y1 ) - ( long )( y1 - y2 ) * ( x4 - x1 ); final long s2 = ( long )( x3 - x4 ) * ( y1 - y3 ) - ( long )( y3 - y4 ) * ( x1 - x3 ); final long t2 = ( long )( x3 - x4 ) * ( y2 - y3 ) - ( long )( y3 - y4 ) * ( x2 - x3 ); final long temp1 = s1 * t1; final long temp2 = s2 * t2; if ( temp1 > 0 || temp2 > 0 ) { return -1; } if ( temp1 == 0 && temp2 == 0 ) { return 0; } return 1; } /** * 引数の前半二点、後半二点で構成される二線分が交差しているか返します。 * * @param p1 点1 * @param p2 点2 * @param p3 点3 * @param p4 点4 * * @return 交差している(線分の端が他方の線分上に存在する場合も含む)場合は1、同一線分直線上なら0、それ以外は-1 */ public static int isCrossed ( final Point p1, final Point p2, final Point p3, final Point p4 ) { return isCrossed( p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, p4.x, p4.y ); } /** * 指定された頂点を順に結んで出来上がる多角形が凸多角形か判定します。 * * @param points 多角形を構成する点 * * @return 多角形が凸多角形ならtrue */ public static boolean isConvex ( final Point... points ) { final int n = points.length; if ( n < 3 ) { return false; } if ( n == 3 ) { return true; } boolean conv = true; for ( int i = 0; i < n; ++i ) { int result = isCrossed( points[i], points[( i + 2 ) % n], points[( i + 1 ) % n], points[( i + 1 + n / 2 ) % n] ); conv &= result >= 0; } return conv; } /** * numをmodで割ったあまりを返します。 * 戻り値は0以上mod未満であることが保証されます。 * * @param num 被除算数 * @param mod 法とする値 * * @return numをmodで割ったあまり */ public static long remainder ( long num, final long mod ) { num %= mod; if ( num < 0 ) { num += mod; } return num; } /** * numが何桁かを返します。 * 0は1桁として捉えます。 * * @param num 調べる整数 * * @return numの桁数 */ public static int digit ( final long num ) { if ( num < 10L ) { return 1; } if ( num < 100L ) { return 2; } if ( num < 1000L ) { return 3; } if ( num < 10000L ) { return 4; } if ( num < 100000L ) { return 5; } if ( num < 1000000L ) { return 6; } if ( num < 10000000L ) { return 7; } if ( num < 100000000L ) { return 8; } if ( num < 1000000000L ) { return 9; } if ( num < 10000000000L ) { return 10; } if ( num < 100000000000L ) { return 11; } if ( num < 1000000000000L ) { return 12; } if ( num < 10000000000000L ) { return 13; } if ( num < 100000000000000L ) { return 14; } if ( num < 1000000000000000L ) { return 15; } if ( num < 10000000000000000L ) { return 16; } if ( num < 100000000000000000L ) { return 17; } if ( num < 1000000000000000000L ) { return 18; } return 19; } public static int max ( final int... nums ) { int ans = Integer.MIN_VALUE; for ( int num: nums ) { ans = Math.max( ans, num ); } return ans; } public static long max ( final long... nums ) { long ans = Long.MIN_VALUE; for ( long num: nums ) { ans = Math.max( ans, num ); } return ans; } public static double max ( final double... nums ) { double ans = -Double.MIN_VALUE; for ( double num: nums ) { ans = Math.max( ans, num ); } return ans; } public static <E extends Comparable<E>> E max ( final E[] nums ) { E ans = nums[0]; for ( E value: nums ) { if ( ans.compareTo( value ) > 0 ) { ans = value; } } return ans; } public static int min ( final int... nums ) { int ans = Integer.MAX_VALUE; for ( int num: nums ) { ans = Math.min( ans, num ); } return ans; } public static long min ( final long... nums ) { long ans = Long.MAX_VALUE; for ( long num: nums ) { ans = Math.min( ans, num ); } return ans; } public static double min ( final double... nums ) { double ans = Double.MAX_VALUE; for ( double num: nums ) { ans = Math.min( ans, num ); } return ans; } public static <E extends Comparable<E>> E min ( final E[] nums ) { E ans = nums[0]; for ( E value: nums ) { if ( ans.compareTo( value ) < 0 ) { ans = value; } } return ans; } public static long sum ( final int... nums ) { long ans = 0; for ( int num: nums ) { ans += num; } return ans; } public static long sum ( final long... nums ) { long ans = 0; for ( long num: nums ) { ans += num; } return ans; } public static long modSum ( final long mod, final int... nums ) { long ans = 0; for ( int num: nums ) { ans += num; ans %= mod; } if ( ans < 0 ) { ans += mod; } return ans; } public static long modSum ( final long mod, final long... nums ) { long ans = 0; for ( long num: nums ) { ans += num; ans %= mod; } if ( ans < 0 ) { ans += mod; } return ans; } public static long sum ( final int[] nums, int from, int to ) { long ans = 0; for ( int i = from; i < to; ++i ) { ans += nums[i]; } return ans; } public static long sum ( final long[] nums, int from, int to ) { long ans = 0; for ( int i = from; i < to; ++i ) { ans += nums[i]; } return ans; } public static long modSum ( final int[] nums, int from, int to, long mod ) { long ans = 0; for ( int i = from; i < to; ++i ) { ans += nums[i]; ans %= mod; } if ( ans < 0 ) { ans += mod; } return ans; } public static long modSum ( final long[] nums, int from, int to, long mod ) { long ans = 0; for ( int i = from; i < to; ++i ) { ans += nums[i]; ans %= mod; } if ( ans < 0 ) { ans += mod; } return ans; } /** * 引数numがl以上r未満の範囲内か判定します。 * * @param num 判定する値 * @param l 下限(lを含む) * @param r 上限(rを含まない) * * @return l <= num < rを満たしていればtrue 、 満たしていなければfalse */ public static boolean rangeCheck ( final int num, final int l, final int r ) { return l <= num && num < r; } /** * 引数numがl以上r未満の範囲内か判定します。 * * @param num 判定する値 * @param l 下限(lを含む) * @param r 上限(rを含まない) * * @return l <= num < rを満たしていればtrue 、 満たしていなければfalse */ public static boolean rangeCheck ( final long num, final long l, final long r ) { return l <= num && num < r; } /** * 引数numがl以上r未満の範囲内か判定します。 * * @param num 判定する値 * @param l 下限(lを含む) * @param r 上限(rを含まない) * * @return l <= num < rを満たしていればtrue 、 満たしていなければfalse */ public static boolean rangeCheck ( final double num, final double l, final double r ) { return l <= num && num < r; } /** * 引数numがl以上r未満の範囲内か判定します。 * * @param num 判定する値 * @param l 下限(lを含む) * @param r 上限(rを含まない) * * @return l <= num < rを満たしていればtrue 、 満たしていなければfalse */ public static <E extends Comparable<E>> boolean rangeCheck ( final E num, final E l, final E r ) { return 0 <= l.compareTo( num ) && 0 < num.compareTo( r ); } /** * 引数numがlより大きく、r未満の範囲内か判定します。 * * @param num 判定する値 * @param l 下限(lを含まない) * @param r 上限(rを含まない) * * @return l < num < rを満たしていればtrue 、 満たしていなければfalse */ public static boolean rangeCheckOpen ( final int num, final int l, final int r ) { return l < num && num < r; } /** * 引数numがlより大きく、r未満の範囲内か判定します。 * * @param num 判定する値 * @param l 下限(lを含まない) * @param r 上限(rを含まない) * * @return l < num < rを満たしていればtrue 、 満たしていなければfalse */ public static boolean rangeCheckOpen ( final long num, final long l, final long r ) { return l < num && num < r; } /** * 引数numがlより大きく、r未満の範囲内か判定します。 * * @param num 判定する値 * @param l 下限(lを含まない) * @param r 上限(rを含まない) * * @return l < num < rを満たしていればtrue 、 満たしていなければfalse */ public static boolean rangeCheckOpen ( final double num, final double l, final double r ) { return l < num && num < r; } /** * 引数numがlより大きく、r未満の範囲内か判定します。 * * @param num 判定する値 * @param l 下限(lを含まない) * @param r 上限(rを含まない) * * @return l < num < rを満たしていればtrue 、 満たしていなければfalse */ public static <E extends Comparable<E>> boolean rangeCheckOpen ( final E num, final E l, final E r ) { return 0 < l.compareTo( num ) && 0 < num.compareTo( r ); } /** * 引数numがl以上r以下の範囲内か判定します。 * * @param num 判定する値 * @param l 下限(lを含む) * @param r 上限(rを含む) * * @return l <= num <= rを満たしていればtrue 、 満たしていなければfalse */ public static boolean rangeCheckClose ( final int num, final int l, final int r ) { return l <= num && num <= r; } /** * 引数numがl以上r以下の範囲内か判定します。 * * @param num 判定する値 * @param l 下限(lを含む) * @param r 上限(rを含む) * * @return l <= num <= rを満たしていればtrue 、 満たしていなければfalse */ public static boolean rangeCheckClose ( final long num, final long l, final long r ) { return l <= num && num <= r; } /** * 引数numがl以上r以下の範囲内か判定します。 * * @param num 判定する値 * @param l 下限(lを含む) * @param r 上限(rを含む) * * @return l <= num <= rを満たしていればtrue 、 満たしていなければfalse */ public static boolean rangeCheckClose ( final double num, final double l, final double r ) { return l <= num && num <= r; } /** * 引数numがl以上r以下の範囲内か判定します。 * * @param num 判定する値 * @param l 下限(lを含む) * @param r 上限(rを含む) * * @return l <= num <= rを満たしていればtrue 、 満たしていなければfalse */ public static <E extends Comparable<E>> boolean rangeCheckClose ( final E num, final E l, final E r ) { return 0 <= l.compareTo( num ) && 0 <= num.compareTo( r ); } /** * 引数の中でのmexを求めます。 * * @param num */ public static int mex ( final int... nums ) { final IntegerSet set = new IntegerSet( nums.length << 1 ); for ( final int num : nums ) { set.add( num ); } int ans = 0; while ( set.contains( ans ) ) { ++ans; } return ans; } } final class Searcher { private static final int CYCLE_COUNT = Double.MAX_EXPONENT + 53; /** * 配列内の指定された要素を探します。 * 配列内で見つかった場合はその要素と同一で最大のインデックスを返します。 * 見つからなかった場合は指定された要素未満で最大のインデックスを返します。 * もしそのような要素が存在しない場合は-1を返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 指定された要素以下で最大のインデックス */ public static int downSearch ( final int[] array, final int value ) { int a = 0, b = array.length - 1, ans = -1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] > value ) { b = c - 1; } else { a = ( ans = c ) + 1; } } return ans; } /** * 配列内の指定された要素を探します。 * 配列内で見つかった場合はその要素と同一で最大のインデックスを返します。 * 見つからなかった場合は指定された要素未満で最大のインデックスを返します。 * もしそのような要素が存在しない場合は-1を返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 指定された要素以下で最大のインデックス */ public static int downSearch ( final long[] array, final long value ) { int a = 0, b = array.length - 1, ans = -1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] > value ) { b = c - 1; } else { a = ( ans = c ) + 1; } } return ans; } /** * 配列内の指定された要素を探します。 * 配列内で見つかった場合はその要素と同一で最大のインデックスを返します。 * 見つからなかった場合は指定された要素未満で最大のインデックスを返します。 * もしそのような要素が存在しない場合は-1を返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 指定された要素以下で最大のインデックス */ public static int downSearch ( final double[] array, final double value ) { int a = 0, b = array.length - 1, ans = -1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] > value ) { b = c - 1; } else { a = ( ans = c ) + 1; } } return ans; } /** * 配列内の指定された要素を探します。 * 配列内で見つかった場合はその要素と同一で最大のインデックスを返します。 * 見つからなかった場合は指定された要素未満で最大のインデックスを返します。 * もしそのような要素が存在しない場合は-1を返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 指定された要素以下で最大のインデックス */ public static int downSearch ( final char[] array, final int value ) { int a = 0, b = array.length - 1, ans = -1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] > value ) { b = c - 1; } else { a = ( ans = c ) + 1; } } return ans; } /** * 配列内の指定された要素を探します。 * 配列内で見つかった場合はその要素と同一で最大のインデックスを返します。 * 見つからなかった場合は指定された要素未満で最大のインデックスを返します。 * もしそのような要素が存在しない場合は-1を返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 指定された要素以下で最大のインデックス */ public static <E extends Comparable<E>> int downSearch ( final E[] array, final E value ) { int a = 0, b = array.length - 1, ans = -1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c].compareTo( value ) > 0 ) { b = c - 1; } else { a = ( ans = c ) + 1; } } return ans; } /** * リスト内の指定された要素を探します。 * リスト内で見つかった場合はその要素と同一で最大のインデックスを返します。 * 見つからなかった場合は指定された要素未満で最大のインデックスを返します。 * もしそのような要素が存在しない場合は-1を返します。 * * @param list 探索対象のリスト * @param value 探索要素 * * @return 指定された要素以下で最大のインデックス */ public static <E extends Comparable<E>> int downSearch ( final List<E> list, final E value ) { int a = 0, b = list.size() - 1, ans = -1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( list.get( c ).compareTo( value ) > 0 ) { b = c - 1; } else { a = ( ans = c ) + 1; } } return ans; } /** * 広義単調増加な関数内の指定された値を探します。 * 関数内で見つかった場合はその値と同一で最大の引数を返します。 * 見つからなかった場合は指定された値未満で最大の引数を返します。 * もしそのような要素が存在しない場合は下限-1を返します。 * * @param a 探索範囲の下限 * @param b 探索範囲の上限 * @param value 探索値 * * @return 指定された値以下で最大の引数 */ public static int downSearch ( int a, int b, final int value, final IntUnaryOperator func ) { int ans = a - 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( func.applyAsInt( c ) > value ) { b = c - 1; } else { a = ( ans = c ) + 1; } } return ans; } /** * 広義単調増加な関数内の指定された値を探します。 * 関数内で見つかった場合はその値と同一で最大の引数を返します。 * 見つからなかった場合は指定された値未満で最大の引数を返します。 * もしそのような要素が存在しない場合は下限-1を返します。 * * @param a 探索範囲の下限 * @param b 探索範囲の上限 * @param value 探索値 * * @return 指定された値以下で最大の引数 */ public static long downSearch ( long a, long b, final long value, final LongUnaryOperator func ) { long ans = a - 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( func.applyAsLong( c ) > value ) { b = c - 1; } else { a = ( ans = c ) + 1; } } return ans; } /** * 広義単調増加な関数内の指定された値を探します。 * 関数内で見つかった場合はその値と同一で最大の引数を返します。 * 見つからなかった場合は指定された値未満で最大の引数を返します。 * もしそのような要素が存在しない場合は下限より小さい値を返します。 * * @param a 探索範囲の下限 * @param b 探索範囲の上限 * @param value 探索値 * * @return 指定された値以下で最大の引数 */ public static double search ( double a, double b, final double value, final DoubleUnaryOperator func ) { double ans = a - Math.abs( a ), c; for ( int $ = 0; $ < CYCLE_COUNT; ++$ ) { c = ( a + b ) / 2; if ( func.applyAsDouble( c ) > value ) { b = c; } else { a = ( ans = c ); } } return ans; } /** * 配列内の指定された要素を探します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return arrayにvalueが含まれているかを表すboolean */ public static boolean contains ( final int[] array, final int value ) { int a = 0, b = array.length - 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] > value ) { b = c - 1; } else if ( array[c] < value ) { a = c + 1; } else { return true; } } return false; } /** * 配列内の指定された要素を探します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return arrayにvalueが含まれているかを表すboolean */ public static boolean contains ( final long[] array, final long value ) { int a = 0, b = array.length - 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] > value ) { b = c - 1; } else if ( array[c] < value ) { a = c + 1; } else { return true; } } return false; } /** * 配列内の指定された要素を探します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return arrayにvalueが含まれているかを表すboolean */ public static boolean contains ( final double[] array, final double value ) { int a = 0, b = array.length - 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] > value ) { b = c - 1; } else if ( array[c] < value ) { a = c + 1; } else { return true; } } return false; } /** * 配列内の指定された要素を探します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return arrayにvalueが含まれているかを表すboolean */ public static boolean contains ( final char[] array, final char value ) { int a = 0, b = array.length - 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] > value ) { b = c - 1; } else if ( array[c] < value ) { a = c + 1; } else { return true; } } return false; } /** * 配列内の指定された要素を探します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return arrayにvalueが含まれているかを表すboolean */ public static <E extends Comparable<E>> boolean contains ( final E[] array, final E value ) { int a = 0, b = array.length - 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; int result = array[c].compareTo( value ); if ( result > 0 ) { b = c - 1; } else if ( result < 0 ) { a = c + 1; } else { return true; } } return false; } /** * リスト内の指定された要素を探します。 * * @param list 探索対象のリスト * @param value 探索要素 * * @return listにvalueが含まれているかを表すboolean */ public static <E extends Comparable<E>> boolean contains ( final List<E> list, final E value ) { int a = 0, b = list.size() - 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; int result = list.get( c ).compareTo( value ); if ( result > 0 ) { b = c - 1; } else if ( result < 0 ) { a = c + 1; } else { return true; } } return false; } /** * 広義単調増加な関数内の指定された値を探します。 * * @param a 探索範囲の下限 * @param b 探索範囲の上限 * @param value 探索値 * * @return この関数がa以上b以下で探索値を取るかを表すboolean */ public static boolean contains ( int a, int b, final int value, final IntUnaryOperator func ) { int c; while ( a - b < 1 ) { c = ( a + b ) / 2; int num = func.applyAsInt( c ); if ( num > value ) { b = c - 1; } else if ( num < value ) { a = c + 1; } else { return true; } } return false; } /** * 広義単調増加な関数内の指定された値を探します。 * * @param a 探索範囲の下限 * @param b 探索範囲の上限 * @param value 探索値 * * @return この関数がa以上b以下で探索値を取るかを表すboolean */ public static boolean contains ( long a, long b, final long value, final LongUnaryOperator func ) { long c; while ( a - b < 1 ) { c = ( a + b ) / 2; long num = func.applyAsLong( c ); if ( num > value ) { b = c - 1; } else if ( num < value ) { a = c + 1; } else { return true; } } return false; } /** * 配列内の指定された要素を探します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return arrayのvalueのインデックス(無ければ - 1) */ public static int search ( final int[] array, final int value ) { int a = 0, b = array.length - 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] > value ) { b = c - 1; } else if ( array[c] < value ) { a = c + 1; } else { return c; } } return -1; } /** * 配列内の指定された要素を探します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return arrayのvalueのインデックス(無ければ - 1) */ public static int search ( final long[] array, final long value ) { int a = 0, b = array.length - 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] > value ) { b = c - 1; } else if ( array[c] < value ) { a = c + 1; } else { return c; } } return -1; } /** * 配列内の指定された要素を探します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return arrayのvalueのインデックス(無ければ - 1) */ public static int search ( final double[] array, final double value ) { int a = 0, b = array.length - 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] > value ) { b = c - 1; } else if ( array[c] < value ) { a = c + 1; } else { return c; } } return -1; } /** * 配列内の指定された要素を探します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return arrayのvalueのインデックス(無ければ - 1) */ public static int search ( final char[] array, final char value ) { int a = 0, b = array.length - 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] > value ) { b = c - 1; } else if ( array[c] < value ) { a = c + 1; } else { return c; } } return -1; } /** * 配列内の指定された要素を探します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return arrayにvalueが含まれているかを表すboolean */ public static <E extends Comparable<E>> int search ( final E[] array, final E value ) { int a = 0, b = array.length - 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; int result = array[c].compareTo( value ); if ( result > 0 ) { b = c - 1; } else if ( result < 0 ) { a = c + 1; } else { return c; } } return -1; } /** * リスト内の指定された要素を探します。 * * @param list 探索対象のリスト * @param value 探索要素 * * @return listのvalueのインデックス(無ければ - 1) */ public static <E extends Comparable<E>> int search ( final List<E> list, final E value ) { int a = 0, b = list.size() - 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; int result = list.get( c ).compareTo( value ); if ( result > 0 ) { b = c - 1; } else if ( result < 0 ) { a = c + 1; } else { return c; } } return -1; } /** * 広義単調増加な関数内の指定された値を探します。 * * @param a 探索範囲の下限 * @param b 探索範囲の上限 * @param value 探索値 * * @return この関数がvalueを取る引数(無ければa - 1) */ public static int search ( int a, int b, final int value, final IntUnaryOperator func ) { int c; while ( a - b < 1 ) { c = ( a + b ) / 2; int num = func.applyAsInt( c ); if ( num > value ) { b = c - 1; } else if ( num < value ) { a = c + 1; } else { return c; } } return a - 1; } /** * 広義単調増加な関数内の指定された値を探します。 * * @param a 探索範囲の下限 * @param b 探索範囲の上限 * @param value 探索値 * * @return この関数がvalueを取る引数(無ければa - 1) */ public static long search ( long a, long b, final long value, final LongUnaryOperator func ) { long c; while ( a - b < 1 ) { c = ( a + b ) / 2; long num = func.applyAsLong( c ); if ( num > value ) { b = c - 1; } else if ( num < value ) { a = c + 1; } else { return c; } } return a - 1; } /** * 配列内の指定された要素を探します。 * 配列内で見つかった場合はその要素と同一で最小のインデックスを返します。 * 見つからなかった場合は指定された要素以上で最小のインデックスを返します。 * もしそのような要素が存在しない場合はarray.lengthを返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 指定された要素以上で最小のインデックス */ public static int upSearch ( final int[] array, final int value ) { int a = 0, b = array.length - 1, ans = array.length, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] >= value ) { b = ( ans = c ) - 1; } else { a = c + 1; } } return ans; } /** * 配列内の指定された要素を探します。 * 配列内で見つかった場合はその要素と同一で最小のインデックスを返します。 * 見つからなかった場合は指定された要素以上で最小のインデックスを返します。 * もしそのような要素が存在しない場合はarray.lengthを返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 指定された要素以上で最小のインデックス */ public static int upSearch ( final long[] array, final long value ) { int a = 0, b = array.length - 1, ans = array.length, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] >= value ) { b = ( ans = c ) - 1; } else { a = c + 1; } } return ans; } /** * 配列内の指定された要素を探します。 * 配列内で見つかった場合はその要素と同一で最小のインデックスを返します。 * 見つからなかった場合は指定された要素以上で最小のインデックスを返します。 * もしそのような要素が存在しない場合はarray.lengthを返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 指定された要素以上で最小のインデックス */ public static int upSearch ( final double[] array, final double value ) { int a = 0, b = array.length - 1, ans = array.length, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] >= value ) { b = ( ans = c ) - 1; } else { a = c + 1; } } return ans; } /** * 配列内の指定された要素を探します。 * 配列内で見つかった場合はその要素と同一で最小のインデックスを返します。 * 見つからなかった場合は指定された要素以上で最小のインデックスを返します。 * もしそのような要素が存在しない場合はarray.lengthを返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 指定された要素以上で最小のインデックス */ public static int upSearch ( final char[] array, final char value ) { int a = 0, b = array.length - 1, ans = array.length, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] >= value ) { b = ( ans = c ) - 1; } else { a = c + 1; } } return ans; } /** * 配列内の指定された要素を探します。 * 配列内で見つかった場合はその要素と同一で最小のインデックスを返します。 * 見つからなかった場合は指定された要素以上で最小のインデックスを返します。 * もしそのような要素が存在しない場合はarray.lengthを返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 指定された要素以上で最小のインデックス */ public static <E extends Comparable<E>> int upSearch ( final E[] array, final E value ) { int a = 0, b = array.length - 1, ans = array.length, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c].compareTo( value ) >= 0 ) { b = ( ans = c ) - 1; } else { a = c + 1; } } return ans; } /** * リスト内の指定された要素を探します。 * リスト内で見つかった場合はその要素と同一で最小のインデックスを返します。 * 見つからなかった場合は指定された要素以上で最小のインデックスを返します。 * もしそのような要素が存在しない場合はlist.size()を返します。 * * @param list 探索対象のリスト * @param value 探索要素 * * @return 指定された要素以上で最小のインデックス */ public static <E extends Comparable<E>> int upSearch ( final List<E> list, final E value ) { int a = 0, b = list.size() - 1, ans = list.size(), c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( list.get( c ).compareTo( value ) >= 0 ) { b = ( ans = c ) - 1; } else { a = c + 1; } } return ans; } /** * 広義単調増加な関数内の指定された値を探します。 * 関数内で見つかった場合はその値と同一で最小の引数を返します。 * 見つからなかった場合は指定された値以上で最大の引数を返します。 * もしそのような要素が存在しない場合は上限+1を返します。 * * @param a 探索範囲の下限 * @param b 探索範囲の上限 * @param value 探索値 * * @return 指定された値以上で最小の引数 */ public static int upSearch ( int a, int b, final int value, final IntUnaryOperator func ) { int ans = b + 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( func.applyAsInt( c ) >= value ) { b = ( ans = c ) - 1; } else { a = c + 1; } } return ans; } /** * 広義単調増加な関数内の指定された値を探します。 * 関数内で見つかった場合はその値と同一で最小の引数を返します。 * 見つからなかった場合は指定された値以上で最大の引数を返します。 * もしそのような要素が存在しない場合は上限+1を返します。 * * @param a 探索範囲の下限 * @param b 探索範囲の上限 * @param value 探索値 * * @return 指定された値以上で最小の引数 */ public static long upSearch ( long a, long b, final long value, final LongUnaryOperator func ) { long ans = b + 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( func.applyAsLong( c ) >= value ) { b = ( ans = c ) - 1; } else { a = c + 1; } } return ans; } /** * 配列内の指定された要素より小さい要素を探します。 * 配列内で見つかった場合は条件を満たす最大のインデックスを返します。 * もしそのような要素が存在しない場合は-1を返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 条件を満たす最大のインデックス */ public static int underSearch ( final int[] array, final int value ) { int a = 0, b = array.length - 1, ans = -1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] >= value ) { b = c - 1; } else { a = ( ans = c ) + 1; } } return ans; } /** * 配列内の指定された要素より小さい要素を探します。 * 配列内で見つかった場合は条件を満たす最大のインデックスを返します。 * もしそのような要素が存在しない場合は-1を返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 条件を満たす最大のインデックス */ public static int underSearch ( final long[] array, final long value ) { int a = 0, b = array.length - 1, ans = -1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] >= value ) { b = c - 1; } else { a = ( ans = c ) + 1; } } return ans; } /** * 配列内の指定された要素より小さい要素を探します。 * 配列内で見つかった場合は条件を満たす最大のインデックスを返します。 * もしそのような要素が存在しない場合は-1を返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 条件を満たす最大のインデックス */ public static int underSearch ( final double[] array, final double value ) { int a = 0, b = array.length - 1, ans = -1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] >= value ) { b = c - 1; } else { a = ( ans = c ) + 1; } } return ans; } /** * 配列内の指定された要素より小さい要素を探します。 * 配列内で見つかった場合は条件を満たす最大のインデックスを返します。 * もしそのような要素が存在しない場合は-1を返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 条件を満たす最大のインデックス */ public static int underSearch ( final char[] array, final char value ) { int a = 0, b = array.length - 1, ans = -1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] >= value ) { b = c - 1; } else { a = ( ans = c ) + 1; } } return ans; } /** * 配列内の指定された要素より小さい要素を探します。 * 配列内で見つかった場合は条件を満たす最大のインデックスを返します。 * もしそのような要素が存在しない場合は-1を返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 条件を満たす最大のインデックス */ public static <E extends Comparable<E>> int underSearch ( final E[] array, final E value ) { int a = 0, b = array.length - 1, ans = -1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c].compareTo( value ) >= 0 ) { b = c - 1; } else { a = ( ans = c ) + 1; } } return ans; } /** * リスト内の指定された要素より小さい要素を探します。 * リスト内で見つかった場合は条件を満たす最大のインデックスを返します。 * もしそのような要素が存在しない場合は-1を返します。 * * @param list 探索対象のリスト * @param value 探索要素 * * @return 条件を満たす最大のインデックス */ public static <E extends Comparable<E>> int underSearch ( final List<E> list, final E value ) { int a = 0, b = list.size() - 1, ans = -1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( list.get( c ).compareTo( value ) >= 0 ) { b = c - 1; } else { a = ( ans = c ) + 1; } } return ans; } /** * 広義単調増加な関数内の指定された値より小さい値を探します。 * 関数内で見つかった場合は条件を満たす最大の引数を返します。 * もしそのような要素が存在しない場合は下限-1を返します。 * * @param a 探索範囲の下限 * @param b 探索範囲の上限 * @param value 探索値 * * @return 条件を満たす最大の引数 */ public static int underSearch ( int a, int b, final int value, final IntUnaryOperator func ) { int ans = a - 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( func.applyAsInt( c ) >= value ) { b = c - 1; } else { a = ( ans = c ) + 1; } } return ans; } /** * 広義単調増加な関数内の指定された値より小さい値を探します。 * 関数内で見つかった場合は条件を満たす最大の引数を返します。 * もしそのような要素が存在しない場合は下限-1を返します。 * * @param a 探索範囲の下限 * @param b 探索範囲の上限 * @param value 探索値 * * @return 条件を満たす最大の引数 */ public static long underSearch ( long a, long b, final long value, final LongUnaryOperator func ) { long ans = a - 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( func.applyAsLong( c ) >= value ) { b = c - 1; } else { a = ( ans = c ) + 1; } } return ans; } /** * 配列内の指定された要素より大きい要素を探します。 * 配列内で見つかった場合は条件を満たす最小のインデックスを返します。 * もしそのような要素が存在しない場合はarray.lengthを返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 条件を満たす最小のインデックス */ public static int overSearch ( final int[] array, final int value ) { int a = 0, b = array.length - 1, ans = array.length, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] > value ) { b = ( ans = c ) - 1; } else { a = c + 1; } } return ans; } /** * 配列内の指定された要素より大きい要素を探します。 * 配列内で見つかった場合は条件を満たす最小のインデックスを返します。 * もしそのような要素が存在しない場合はarray.lengthを返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 条件を満たす最小のインデックス */ public static int overSearch ( final long[] array, final long value ) { int a = 0, b = array.length - 1, ans = array.length, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] > value ) { b = ( ans = c ) - 1; } else { a = c + 1; } } return ans; } /** * 配列内の指定された要素より大きい要素を探します。 * 配列内で見つかった場合は条件を満たす最小のインデックスを返します。 * もしそのような要素が存在しない場合はarray.lengthを返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 条件を満たす最小のインデックス */ public static int overSearch ( final double[] array, final double value ) { int a = 0, b = array.length - 1, ans = array.length, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] > value ) { b = ( ans = c ) - 1; } else { a = c + 1; } } return ans; } /** * 配列内の指定された要素より大きい要素を探します。 * 配列内で見つかった場合は条件を満たす最小のインデックスを返します。 * もしそのような要素が存在しない場合はarray.lengthを返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 条件を満たす最小のインデックス */ public static int overSearch ( final char[] array, final char value ) { int a = 0, b = array.length - 1, ans = array.length, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c] > value ) { b = ( ans = c ) - 1; } else { a = c + 1; } } return ans; } /** * 配列内の指定された要素より大きい要素を探します。 * 配列内で見つかった場合は条件を満たす最小のインデックスを返します。 * もしそのような要素が存在しない場合はarray.lengthを返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 条件を満たす最小のインデックス */ public static <E extends Comparable<E>> int overSearch ( final E[] array, final E value ) { int a = 0, b = array.length - 1, ans = array.length, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( array[c].compareTo( value ) > 0 ) { b = ( ans = c ) - 1; } else { a = c + 1; } } return ans; } /** * リスト内の指定された要素より大きい要素を探します。 * リスト内で見つかった場合は条件を満たす最小のインデックスを返します。 * もしそのような要素が存在しない場合はlist.size()を返します。 * * @param list 探索対象のリスト * @param value 探索要素 * * @return 条件を満たす最小のインデックス */ public static <E extends Comparable<E>> int overSearch ( final List<E> list, final E value ) { int a = 0, b = list.size() - 1, ans = list.size(), c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( list.get( c ).compareTo( value ) > 0 ) { b = ( ans = c ) - 1; } else { a = c + 1; } } return ans; } /** * 広義単調増加な関数内の指定された値より大きい値を探します。 * 関数内で見つかった場合は条件を満たす最小の引数を返します。 * もしそのような要素が存在しない場合は上限+1を返します。 * * @param a 探索範囲の下限 * @param b 探索範囲の上限 * @param value 探索値 * * @return 条件を満たす最小の引数 */ public static int overSearch ( int a, int b, final int value, final IntUnaryOperator func ) { int ans = b + 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( func.applyAsInt( c ) > value ) { b = ( ans = c ) - 1; } else { a = c + 1; } } return ans; } /** * 広義単調増加な関数内の指定された値より大きい値を探します。 * 関数内で見つかった場合は条件を満たす最小の引数を返します。 * もしそのような要素が存在しない場合は上限+1を返します。 * * @param a 探索範囲の下限 * @param b 探索範囲の上限 * @param value 探索値 * * @return 条件を満たす最小の引数 */ public static long overSearch ( long a, long b, final long value, final LongUnaryOperator func ) { long ans = b + 1, c; while ( a - b < 1 ) { c = ( a + b ) / 2; if ( func.applyAsLong( c ) > value ) { b = ( ans = c ) - 1; } else { a = c + 1; } } return ans; } /** * 配列内の指定された要素を探します。 * 配列内で見つかった場合は条件を満たす最小のインデックスを返します。 * もしそのような要素が存在しない場合は-1を返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 条件を満たす最小のインデックス */ public static int linearSearch ( final int[] array, final int value ) { for ( int i = 0; i < array.length; ++i ) { if ( array[i] == value ) { return i; } } return -1; } /** * 配列内の指定された要素を探します。 * 配列内で見つかった場合は条件を満たす最小のインデックスを返します。 * もしそのような要素が存在しない場合は-1を返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 条件を満たす最小のインデックス */ public static int linearSearch ( final long[] array, final long value ) { for ( int i = 0; i < array.length; ++i ) { if ( array[i] == value ) { return i; } } return -1; } /** * 配列内の指定された要素を探します。 * 配列内で見つかった場合は条件を満たす最小のインデックスを返します。 * もしそのような要素が存在しない場合は-1を返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 条件を満たす最小のインデックス */ public static int linearSearch ( final double[] array, final double value ) { for ( int i = 0; i < array.length; ++i ) { if ( array[i] == value ) { return i; } } return -1; } /** * 配列内の指定された要素を探します。 * 配列内で見つかった場合は条件を満たす最小のインデックスを返します。 * もしそのような要素が存在しない場合は-1を返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 条件を満たす最小のインデックス */ public static int linearSearch ( final char[] array, final char value ) { for ( int i = 0; i < array.length; ++i ) { if ( array[i] == value ) { return i; } } return -1; } /** * 配列内の指定された要素を探します。 * 配列内で見つかった場合は条件を満たす最小のインデックスを返します。 * もしそのような要素が存在しない場合は-1を返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 条件を満たす最小のインデックス */ public static <E extends Comparable<E>> int linearSearch ( final E[] array, final E value ) { for ( int i = 0; i < array.length; ++i ) { if ( array[i].compareTo( value ) == 0 ) { return i; } } return -1; } /** * 配列内の指定された要素を探します。 * 配列内で見つかった場合は条件を満たす最小のインデックスを返します。 * もしそのような要素が存在しない場合は-1を返します。 * * @param array 探索対象の配列 * @param value 探索要素 * * @return 条件を満たす最小のインデックス */ public static <E extends Comparable<E>> int linearSearch ( final List<E> list, final E value ) { for ( int i = 0; i < list.size(); ++i ) { if ( list.get( i ).compareTo( value ) == 0 ) { return i; } } return -1; } } // Binary Indexed Tree final class BIT { final int size; final private long[] tree; public BIT ( int n ) { size = n; tree = new long[n + 1]; } public long sum ( int i ) { long sum = 0; while ( i > 0 ) { sum += tree[i]; i ^= i & ( -i ); } return sum; } public void add ( int i, long x ) { while ( i <= size ) { tree[i] += x; i += i & ( -i ); } } public void clear () { Arrays.fill( tree, 0L ); } } // Binary Indexed Tree(int) final class BITInt { final int size; final private int[] tree; public BITInt ( int n ) { size = n; tree = new int[n + 1]; } public int sum ( int i ) { int sum = 0; while ( i > 0 ) { sum += tree[i]; i ^= i & ( -i ); } return sum; } public void add ( int i, int x ) { while ( i <= size ) { tree[i] += x; i += i & ( -i ); } } public void clear () { Arrays.fill( tree, 0 ); } } final class Bitset implements Cloneable { private final long[] bit; private final int size, len; private final long MASK; public Bitset ( final int len ) { this.len = len; size = ( len + 63 ) >> 6; bit = new long[size]; MASK = ( -1L ) >>> ( ( size << 6 ) - len ); } private Bitset ( final long[] arr ) { this( arr.length ); System.arraycopy( arr, 0, bit, 0, size ); } public void set ( final int index ) { if ( index >= size << 6 ) { throw new ArrayIndexOutOfBoundsException( index + " is out of this bitset's size " + size ); } bit[index >> 6] |= ( 1L << ( index & 0b111111 ) ); } public void clear ( final int index ) { if ( index >= size << 6 ) { throw new ArrayIndexOutOfBoundsException( index + " is out of this bitset's size " + size ); } long m = ~( 1L << ( index & 0b111111 ) ); bit[index >> 6] &= m; } public boolean get ( final int index ) { if ( index >= size << 6 ) { throw new ArrayIndexOutOfBoundsException( index + " is out of this bitset's size " + size ); } return ( bit[index >> 6] & ( 1L << ( index & 0b111111 ) ) ) != 0; } public void shiftLeft ( int num ) { if ( num >= size << 6 ) { Arrays.fill( bit, 0L ); return; } final int n = num >> 6; num &= 0b111111; for ( int i = size - 1; i >= n; --i ) { bit[i] = ( bit[i - n] << num ) | ( i != n && num != 0 ? bit[i - n - 1] >>> ( 64 - num ) : 0L ); } for ( int i = 0; i < n; ++i ) { bit[i] = 0L; } bit[size - 1] &= MASK; } public void shiftRight ( int num ) { if ( num >= size << 6 ) { Arrays.fill( bit, 0L ); return; } final int n = num >> 6; num &= 0b111111; for ( int i = 0; i < size - n; ++i ) { bit[i] = ( bit[i + n] >>> num ) | ( i + n + 1 != size && num != 0 ? bit[i + n + 1] << ( 64 - num ) : 0L ); } for ( int i = size - 1; i >= size - n; --i ) { bit[i] = 0L; } } public long[] longValues () { return bit; } public long longValue () { return bit[0]; } public void and ( final Bitset b ) { final long[] bit2 = b.longValues(); final int m = Math.min( bit2.length, size ); for ( int i = 0; i < m; ++i ) { bit[i] &= bit2[i]; } for ( int i = m; i < size; ++i ) { bit[i] = 0; } bit[size - 1] &= MASK; } public void or ( final Bitset b ) { final long[] bit2 = b.longValues(); final int m = Math.min( bit2.length, size ); for ( int i = 0; i < m; ++i ) { bit[i] |= bit2[i]; } bit[size - 1] &= MASK; } public void xor ( final Bitset b ) { final long[] bit2 = b.longValues(); final int m = Math.min( bit2.length, size ); for ( int i = 0; i < m; ++i ) { bit[i] ^= bit2[i]; } bit[size - 1] &= MASK; } public Bitset clone () throws CloneNotSupportedException { super.clone(); final Bitset b = new Bitset( bit ); return b; } } final class IntegerSet { private class Node { private final int value; private Node next; private Node ( final int val ) { value = val; } private boolean add ( final int x ) { if ( value == x ) { return false; } if ( next != null ) { return next.add( x ); } ++size; next = new Node( x ); return true; } private void add ( final Node n ) { if ( value != n.value ) { if ( next != null ) { next.add( n ); } else { next = n; } } } private boolean remove ( final int x ) { if ( next == null ) { return false; } if ( next.value != x ) { return next.remove( x ); } --size; next = next.next; return true; } private boolean contains ( final int x ) { if ( value == x ) { return true; } if ( next != null ) { return next.contains( x ); } return false; } } private Node[] table; private int size; public IntegerSet () { this( 16 ); } public IntegerSet ( final int s ) { if ( s < 1 ) { throw new NegativeArraySizeException( "hash table's size must be positive" ); } table = new Node[s]; size = 0; } public boolean add ( final int x ) { sizeCheck(); final int index = hash( x ); if ( table[index] != null ) { return table[index].add( x ); } ++size; table[index] = new Node( x ); return true; } private void add ( final Node n ) { final int index = hash( n.value ); if ( table[index] != null ) { table[index].add( n ); } else { table[index] = n; } } public boolean remove ( final int x ) { final int index = hash( x ); if ( table[index] == null ) { return false; } if ( table[index].value != x ) { return table[index].remove( x ); } --size; table[index] = table[index].next; return true; } public boolean contains ( final int x ) { final int index = hash( x ); if ( table[index] == null ) { return false; } return table[index].contains( x ); } private void reHash () { final Node[] oldTable = table; table = new Node[table.length << 1]; for ( Node node: oldTable ) { while ( node != null ) { final Node next = node.next; node.next = null; add( node ); node = next; } } } private void sizeCheck () { if ( table.length * 0.6 < size ) { reHash(); } } private int hash ( final int val ) { final int h = val % table.length; return h < 0 ? h + table.length : h; } public int size () { return size; } public int[] toArray () { final int[] array = new int[size]; int index = 0; for ( Node node: table ) { while ( node != null ) { array[index++] = node.value; node = node.next; } } return array; } public String toString () { return Arrays.toString( toArray() ); } } final class Matrix { private final long[][] matrix; public Matrix ( final int H, final int W, final long def ) { matrix = new long[H][W]; if ( def != 0 ) { for ( final long[] mat: matrix ) { Arrays.fill( mat, def ); } } } public Matrix ( final int H, final int W ) { this( H, W, 0 ); } public Matrix ( final Dimension d, final long def ) { this( d.height, d.width, def ); } public Matrix ( final long[][] mat ) { matrix = new long[mat.length][]; for ( int i = 0; i < mat.length; ++i ) { matrix[i] = Arrays.copyOf( mat[i], mat[i].length ); } } public long get ( final int i, final int j ) { return matrix[i][j]; } public long set ( final int i, final int j, final long value ) { return matrix[i][j] = value; } public Matrix copy () { return new Matrix( matrix ); } public Dimension size () { return new Dimension( matrix[0].length, matrix.length ); } public Matrix add ( final Matrix m ) { if ( !size().equals( m.size() ) ) { throw new IllegalArgumentException( "matrix size is not same" ); } final Matrix ans = new Matrix( size(), 0 ); for ( int i = 0; i < matrix.length; ++i ) { for ( int j = 0; j < matrix[i].length; ++j ) { ans.set( i, j, matrix[i][j] + m.get( i, j ) ); } } return ans; } public Matrix subtract ( final Matrix m ) { if ( !size().equals( m.size() ) ) { throw new IllegalArgumentException( "matrix size is not same" ); } final Matrix ans = new Matrix( size(), 0 ); for ( int i = 0; i < matrix.length; ++i ) { for ( int j = 0; j < matrix[i].length; ++j ) { ans.set( i, j, matrix[i][j] - m.get( i, j ) ); } } return ans; } public Matrix multiply ( final Matrix m ) { if ( size().width != m.size().height ) { throw new IllegalArgumentException( "matrix length is not same" ); } final Matrix ans = new Matrix( size().height, m.size().width ); final Dimension size = ans.size(); final int len = size().width; for ( int i = 0; i < size.height; ++i ) { for ( int j = 0; j < size.width; ++j ) { long sum = 0; for ( int k = 0; k < len; ++k ) { sum += matrix[i][k] * m.get( k, j ); } ans.set( i, j, sum ); } } return ans; } public Matrix modAdd ( final Matrix m, final long mod ) { if ( !size().equals( m.size() ) ) { throw new IllegalArgumentException( "matrix size is not same" ); } final Matrix ans = new Matrix( size(), 0 ); for ( int i = 0; i < matrix.length; ++i ) { for ( int j = 0; j < matrix[i].length; ++j ) { ans.set( i, j, MathFunction.remainder( matrix[i][j] + m.get( i, j ), mod ) ); } } return ans; } public Matrix modSubtract ( final Matrix m, final long mod ) { if ( !size().equals( m.size() ) ) { throw new IllegalArgumentException( "matrix size is not same" ); } final Matrix ans = new Matrix( size(), 0 ); for ( int i = 0; i < matrix.length; ++i ) { for ( int j = 0; j < matrix[i].length; ++j ) { ans.set( i, j, MathFunction.remainder( matrix[i][j] - m.get( i, j ), mod ) ); } } return ans; } public Matrix modMultiply ( final Matrix m, final long mod ) { if ( size().width != m.size().height ) { throw new IllegalArgumentException( "matrix length is not same" ); } final Matrix ans = new Matrix( size().height, m.size().width ); final Dimension size = ans.size(); final int len = size().width; for ( int i = 0; i < size.height; ++i ) { for ( int j = 0; j < size.width; ++j ) { long sum = 0; for ( int k = 0; k < len; ++k ) { sum = MathFunction.remainder( sum + matrix[i][k] * m.get( k, j ), mod ); } ans.set( i, j, sum ); } } return ans; } public static Matrix pow ( final Matrix original, final Matrix pw, long exp ) { Matrix a = original.copy(); Matrix b = pw.copy(); while ( 0 < exp ) { if ( ( exp & 1 ) == 1 ) { a = b.multiply( a ); } b = b.multiply( b ); exp >>= 1; } return a; } public static Matrix modPow ( final Matrix original, final Matrix pw, long exp, final long mod ) { Matrix a = original.copy(); Matrix b = pw.copy(); while ( 0 < exp ) { if ( ( exp & 1 ) == 1 ) { a = b.modMultiply( a, mod ); } b = b.modMultiply( b, mod ); exp >>= 1; } return a; } public long determinant () { return determinant( matrix ); } private static long determinant ( final long[][] mat ) { if ( mat.length == 1 ) { return mat[0][0]; } final long[][] miniMat = new long[mat.length - 1][mat.length - 1]; for ( int i = 1; i < mat.length; ++i ) { System.arraycopy( mat[i], 1, miniMat[i - 1], 0, miniMat.length ); } long ans = mat[0][0] * determinant( miniMat ); for ( int i = 1; i < mat.length; ++i ) { for ( int j = 1; j < mat.length; ++j ) { miniMat[j - 1][i - 1] = mat[j][i - 1]; } final long num = mat[0][i] * determinant( miniMat ); ans += i % 2 == 0 ? num : -num; } return ans; } @Override public String toString () { final StringBuilder ans = new StringBuilder(); ans.append( Arrays.toString( matrix[0] ) ); for ( int i = 1; i < matrix.length; ++i ) { ans.append( "\n" ); ans.append( Arrays.toString( matrix[i] ) ); } return ans.toString(); } } final class RollingHash implements Comparable<RollingHash> { private static final long BASE = new Random().nextInt( 1000 ) + Character.MAX_VALUE + 1; private static final long MASK30 = ( 1L << 30 ) - 1; private static final long MASK31 = ( 1L << 31 ) - 1; private static final long MOD = ( 1L << 61 ) - 1; private static final long MASK61 = MOD; private long[] hash; private String string; public RollingHash ( final String str ) { string = str; hash = new long[str.length() + 1]; roll(); } private void roll () { final int len = string.length(); for ( int i = 1; i <= len; ++i ) { hash[i] = multiply( hash[i - 1], BASE ) + string.charAt( i - 1 ) - ' ' + 1; if ( MOD <= hash[i] ) { hash[i] -= MOD; } } } private static long multiply ( final long a, final long b ) { final long au = a >> 31; final long ad = a & MASK31; final long bu = b >> 31; final long bd = b & MASK31; final long mid = ad * bu + au * bd; final long midu = mid >> 30; final long midd = mid & MASK30; return mod( au * bu * 2 + midu + ( midd << 31 ) + ad * bd ); } private static long mod ( final long x ) { final long xu = x >> 61; final long xd = x & MASK61; long ans = xu + xd; if ( MOD <= ans ) { ans -= MOD; } return ans; } public long getHash ( final int l, final int r ) { return ( hash[r] - multiply( hash[l], modBasePow( r - l ) ) + MOD ) % MOD; } private static long modBasePow ( long b ) { long ans = 1; long a = BASE; while ( b > 0 ) { if ( ( b & 1 ) == 1 ) { ans = multiply( ans, a ); } a = multiply( a, a ); b >>= 1; } return ans; } public boolean equals ( final RollingHash rh, final int l1, final int r1, final int l2, final int r2 ) { if ( r1 - l1 != r2 - l2 ) { return false; } return getHash( l1, r1 ) == rh.getHash( l2, r2 ); } public int length () { return string.length(); } @Override public int hashCode () { return string.hashCode(); } @Override public String toString () { return string; } @Override public boolean equals ( Object o ) { if ( o instanceof final RollingHash rh ) { return equals( rh, 0, length(), 0, rh.length() ); } return false; } @Override public int compareTo ( RollingHash rh ) { return string.compareTo( rh.toString() ); } public int compareTo ( String str ) { return string.compareTo( str ); } public char charAt ( final int i ) { return string.charAt( i ); } public int compareToIgnoreCase ( final RollingHash rh ) { return string.compareToIgnoreCase( rh.toString() ); } public int compareToIgnoreCase ( final String str ) { return string.compareToIgnoreCase( str ); } public void concat ( final RollingHash rh ) { concat( rh.toString() ); } public void concat ( final String str ) { string = string.concat( str ); hash = new long[string.length() + 1]; roll(); } public boolean contains ( final RollingHash rh ) { final long hash = rh.getHash( 0, rh.length() ); final int len = length() - rh.length(); for ( int i = 0; i <= len; ++i ) { if ( hash == getHash( i, rh.length() + i ) ) { return true; } } return false; } public boolean contains ( final String str ) { return indexOf( str ) != -1; } public int indexOf ( final int ch ) { return indexOf( ch, 0 ); } public int indexOf ( final int ch, final int fromIndex ) { final int len = length(); for ( int i = fromIndex; i < len; ++i ) { if ( string.charAt( i ) == ch ) { return i; } } return -1; } public int indexOf ( final String str ) { return indexOf( str, 0 ); } public int indexOf ( final String str, final int fromIndex ) { long hash = 0; for ( final char c: str.toCharArray() ) { hash = multiply( hash, BASE ) + c - ' ' + 1; if ( MOD <= hash ) { hash -= MOD; } } final int len = length() - str.length(); for ( int i = fromIndex; i <= len; ++i ) { if ( hash == getHash( i, str.length() + i ) ) { return i; } } return -1; } public boolean isEmpty () { return length() == 0; } public int lastIndexOf ( final int ch, final int fromIndex ) { for ( int i = fromIndex; i >= 0; --i ) { if ( string.charAt( i ) == ch ) { return i; } } return -1; } public int lastIndexOf ( final int ch ) { return lastIndexOf( ch, length() - 1 ); } public static RollingHash valueOf ( final boolean b ) { return new RollingHash( b ? "true" : "false" ); } public static RollingHash valueOf ( final char c ) { return new RollingHash( "" + c ); } public static RollingHash valueOf ( final char[] c ) { return new RollingHash( String.valueOf( c, 0, c.length ) ); } public static RollingHash valueOf ( final char[] c, final int offset, final int count ) { return new RollingHash( String.valueOf( c, offset, count ) ); } public static RollingHash valueOf ( final double d ) { return new RollingHash( String.valueOf( d ) ); } public static RollingHash valueOf ( final float f ) { return new RollingHash( String.valueOf( f ) ); } public static RollingHash valueOf ( final int i ) { return new RollingHash( String.valueOf( i ) ); } public static RollingHash valueOf ( final long l ) { return new RollingHash( String.valueOf( l ) ); } public static RollingHash valueOf ( final Object obj ) { return new RollingHash( String.valueOf( obj ) ); } } @SuppressWarnings( "unchecked" ) abstract class SegmentTree<E> { final int N, size; final E def; final Object[] node; public SegmentTree ( final int n, final E def, final boolean include ) { int num = 2; while ( num < n << 1 ) { num <<= 1; } N = num; size = num >> 1 - ( include ? 1 : 0 ); node = new Object[N]; this.def = def; Arrays.fill( node, this.def ); } public SegmentTree ( final E[] arr, final E def, final boolean include ) { int num = 2; while ( num < arr.length << 1 ) { num <<= 1; } N = num; size = num >> 1 - ( include ? 1 : 0 ); node = new Object[N]; this.def = def; System.arraycopy( arr, 0, node, N >> 1, arr.length ); for ( int i = arr.length + ( N >> 1 ); i < N; i++ ) { node[i] = def; } updateAll(); } public SegmentTree ( final int n, final E def ) { this( n, def, false ); } private void updateAll () { for ( int i = ( N >> 1 ) - 1; i > 0; i-- ) { node[i] = function( ( E )node[i << 1], ( E )node[( i << 1 ) + 1] ); } } public void update ( int n, final E value ) { n += size; node[n] = value; n >>= 1; while ( n > 0 ) { node[n] = function( ( E )node[n << 1], ( E )node[( n << 1 ) + 1] ); n >>= 1; } } public E get ( final int a ) { return ( E )node[a + size]; } public E answer () { return ( E )node[1]; } public E query ( int l, int r ) { l += size; r += size; E answer = def; while ( l > 0 && r > 0 && l <= r ) { if ( l % 2 == 1 ) { answer = function( ( E )node[l++], answer ); } l >>= 1; if ( r % 2 == 0 ) { answer = function( answer, ( E )node[r--] ); } r >>= 1; } return answer; } abstract public E function ( E a, E b ); } final class UnionFind { private final int[] par, rank, size, path; private int count; public UnionFind ( final int N ) { count = N; par = new int[N]; rank = new int[N]; size = new int[N]; path = new int[N]; Arrays.fill( par, -1 ); Arrays.fill( size, 1 ); } public int root ( final int ind ) { if ( par[ind] == -1 ) { return ind; } else { return par[ind] = root( par[ind] ); } } public boolean isSame ( final int x, final int y ) { return root( x ) == root( y ); } public boolean unite ( final int x, final int y ) { int rx = root( x ); int ry = root( y ); ++path[rx]; if ( rx == ry ) { return false; } if ( rank[rx] < rank[ry] ) { int temp = rx; rx = ry; ry = temp; } par[ry] = rx; if ( rank[rx] == rank[ry] ) { ++rank[rx]; } path[rx] += path[ry]; size[rx] += size[ry]; --count; return true; } public int groupCount () { return count; } public int pathCount ( final int x ) { return path[root( x )]; } public int size ( final int x ) { return size[root( x )]; } } final class Tree<E extends Comparable<E>> { private Node<E> root; private int size, hash; public Tree () { size = 0; root = null; hash = 0; } static final private class Node<E> { E value; int height, size; Node<E> left, right, parent; public Node ( final Node<E> p, final E v ) { value = v; parent = p; height = 1; size = 1; } } public boolean add ( final E x ) { boolean bool = true; if ( root == null ) { root = new Node<>( null, x ); } else { Node<E> par; Node<E> now = root; do { par = now; final int result = x.compareTo( now.value ); if ( result < 0 ) { now = now.left; } else if ( result > 0 ) { now = now.right; } else { bool = false; break; } } while ( now != null ); if ( bool ) { final int result = x.compareTo( par.value ); if ( result < 0 ) { par.left = new Node<>( par, x ); } else { par.right = new Node<>( par, x ); } fix( par ); } } if ( bool ) { ++size; hash ^= x.hashCode(); } return bool; } public E get ( int index ) { if ( root == null || size <= index ) { throw new NullPointerException(); } Node<E> now = root; while ( true ) { assert now != null; final int ls = now.left != null ? now.left.size : 0; if ( index < ls ) { now = now.left; } else if ( ls < index ) { now = now.right; index -= ls + 1; } else { break; } } return now.value; } public boolean remove ( final E x ) { final Node<E> n = getNode( x ); if ( n == null ) { return false; } --size; hash ^= n.value.hashCode(); delete( n ); return true; } private void delete ( final Node<E> node ) { if ( node != null ) { if ( node.left == null && node.right == null ) { if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = null; } else { node.parent.right = null; } fix( node.parent ); } else { root = null; } node.parent = null; } else { if ( node.left != null && node.right != null ) { final Node<E> rep = getFirstNode( node.right ); node.value = rep.value; delete( rep ); } else { final Node<E> rep = node.left != null ? node.left : node.right; rep.parent = node.parent; if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = rep; } else { node.parent.right = rep; } fix( node.parent ); } else { root = rep; } node.parent = null; } } } } private Node<E> getNode ( final E x ) { Node<E> now = root; while ( now != null ) { final int result = x.compareTo( now.value ); if ( result < 0 ) { now = now.left; } else if ( result > 0 ) { now = now.right; } else { break; } } return now; } public E first () { if ( root == null ) { return null; } return getFirstNode( root ).value; } private Node<E> getFirstNode ( Node<E> node ) { assert node != null; Node<E> par = null; while ( node != null ) { par = node; node = par.left; } return par; } public E last () { if ( root == null ) { return null; } return getLastNode( root ).value; } private Node<E> getLastNode ( Node<E> node ) { assert node != null; Node<E> par = null; while ( node != null ) { par = node; node = par.right; } return par; } public boolean contains ( final E x ) { if ( root == null ) { return false; } return getNode( x ) != null; } public E pollFirst () { if ( root == null ) { return null; } --size; final Node<E> min = getFirstNode( root ); hash ^= min.value.hashCode(); delete( min ); return min.value; } public E pollLast () { if ( root == null ) { return null; } --size; final Node<E> max = getLastNode( root ); hash ^= max.value.hashCode(); delete( max ); return max.value; } public E ceiling ( final E x ) { return ceiling( root, x ); } private E ceiling ( Node<E> node, final E x ) { Node<E> ans = null; while ( node != null ) { final int result = x.compareTo( node.value ); if ( result > 0 ) { node = node.right; } else if ( result < 0 ) { ans = node; node = node.left; } else { return x; } } return ans != null ? ans.value : null; } public E higher ( final E x ) { return higher( root, x ); } private E higher ( Node<E> node, final E x ) { Node<E> ans = null; while ( node != null ) { final int result = x.compareTo( node.value ); if ( result >= 0 ) { node = node.right; } else { ans = node; node = node.left; } } return ans != null ? ans.value : null; } public E floor ( final E x ) { return floor( root, x ); } private E floor ( Node<E> node, final E x ) { Node<E> ans = null; while ( node != null ) { final int result = x.compareTo( node.value ); if ( result < 0 ) { node = node.left; } else if ( result > 0 ) { ans = node; node = node.right; } else { return x; } } return ans != null ? ans.value : null; } public E lower ( final E x ) { return lower( root, x ); } private E lower ( Node<E> node, final E x ) { Node<E> ans = null; while ( node != null ) { final int result = x.compareTo( node.value ); if ( result <= 0 ) { node = node.left; } else { ans = node; node = node.right; } } return ans != null ? ans.value : null; } public void clear () { root = null; size = 0; hash = 0; } public boolean isEmpty () { return size == 0; } public int size () { return size; } public ArrayList<E> toList () { final ArrayList<E> list = new ArrayList<>(); if ( root != null ) { final ArrayDeque<Node<E>> deq = new ArrayDeque<>(); deq.add( root ); while ( !deq.isEmpty() ) { final Node<E> now = deq.pollLast(); if ( list.size() == 0 ) { if ( now.left != null ) { deq.add( now ); deq.add( now.left ); } else { list.add( now.value ); if ( now.right != null ) { deq.add( now.right ); } } } else if ( now.left != null && list.get( list.size() - 1 ).compareTo( now.left.value ) < 0 ) { deq.add( now ); deq.add( now.left ); } else { list.add( now.value ); if ( now.right != null ) { deq.add( now.right ); } } } } return list; } @Override public String toString () { final ArrayList<E> list = toList(); return list.toString(); } @Override public boolean equals ( final Object o ) { if ( o instanceof final Tree<?> tree ) { if ( size != tree.size() ) { return false; } final ArrayList<E> list1 = toList(); final ArrayList<?> list2 = tree.toList(); for ( int i = 0; i < size; ++i ) { if ( !list1.get( i ).equals( list2.get( i ) ) ) { return false; } } return true; } return false; } @Override public int hashCode () { return hash; } /* * 以下、平衡用メソッド */ private void fix ( Node<E> node ) { while ( node != null ) { final int lh = node.left == null ? 0 : node.left.height; final int rh = node.right == null ? 0 : node.right.height; if ( lh - rh > 1 ) { assert node.left != null; if ( node.left.right != null && node.left.right.height == lh - 1 ) { rotateL( node.left ); } rotateR( node ); } else if ( rh - lh > 1 ) { assert node.right != null; if ( node.right.left != null && node.right.left.height == rh - 1 ) { rotateR( node.right ); } rotateL( node ); } else { setStates( node ); } node = node.parent; } } private void rotateR ( final Node<E> node ) { final Node<E> temp = node.left; node.left = temp.right; if ( node.left != null ) { node.left.parent = node; } temp.right = node; temp.parent = node.parent; if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = temp; } else { node.parent.right = temp; } } else { root = temp; } node.parent = temp; setStates( node ); } private void rotateL ( final Node<E> node ) { final Node<E> temp = node.right; node.right = temp.left; if ( node.right != null ) { node.right.parent = node; } temp.left = node; temp.parent = node.parent; if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = temp; } else { node.parent.right = temp; } } else { root = temp; } node.parent = temp; setStates( node ); } private void setStates ( final Node<E> node ) { final int lh = node.left != null ? node.left.height : 0; final int rh = node.right != null ? node.right.height : 0; node.height = Math.max( lh, rh ) + 1; final int ls = node.left != null ? node.left.size : 0; final int rs = node.right != null ? node.right.size : 0; node.size = ls + rs + 1; } } final class TreeInt { private Node root; private int size, hash; public TreeInt () { size = 0; root = null; hash = 0; } static final private class Node { int value; int height, size; Node left, right, parent; public Node ( final Node p, final int v ) { value = v; parent = p; height = 1; size = 1; } } public boolean add ( final int x ) { boolean bool = true; if ( root == null ) { root = new Node( null, x ); } else { Node par; Node now = root; do { par = now; if ( x < now.value ) { now = now.left; } else if ( x > now.value ) { now = now.right; } else { bool = false; break; } } while ( now != null ); if ( bool ) { if ( x < par.value ) { par.left = new Node( par, x ); } else { par.right = new Node( par, x ); } fix( par ); } } if ( bool ) { ++size; hash ^= x; } return bool; } public int get ( int index ) { if ( root == null || size <= index ) { throw new NullPointerException(); } Node now = root; while ( true ) { assert now != null; final int ls = now.left != null ? now.left.size : 0; if ( index < ls ) { now = now.left; } else if ( ls < index ) { now = now.right; index -= ls + 1; } else { break; } } return now.value; } public boolean remove ( final int x ) { final Node n = getNode( x ); if ( n == null ) { return false; } --size; hash ^= n.value; delete( n ); return true; } private void delete ( final Node node ) { if ( node != null ) { if ( node.left == null && node.right == null ) { if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = null; } else { node.parent.right = null; } fix( node.parent ); } else { root = null; } node.parent = null; } else { if ( node.left != null && node.right != null ) { final Node rep = getFirstNode( node.right ); node.value = rep.value; delete( rep ); } else { final Node rep = node.left != null ? node.left : node.right; rep.parent = node.parent; if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = rep; } else { node.parent.right = rep; } fix( node.parent ); } else { root = rep; } node.parent = null; } } } } private Node getNode ( final int x ) { Node now = root; while ( now != null ) { if ( x < now.value ) { now = now.left; } else if ( x > now.value ) { now = now.right; } else { break; } } return now; } public int first () { if ( root == null ) { throw new NullPointerException(); } return getFirstNode( root ).value; } private Node getFirstNode ( Node node ) { assert node != null; Node par = null; while ( node != null ) { par = node; node = par.left; } return par; } public int last () { if ( root == null ) { throw new NullPointerException(); } return getLastNode( root ).value; } private Node getLastNode ( Node node ) { assert node != null; Node par = null; while ( node != null ) { par = node; node = par.right; } return par; } public boolean contains ( final int x ) { if ( root == null ) { return false; } return getNode( x ) != null; } public int pollFirst () { if ( root == null ) { throw new NullPointerException(); } --size; final Node min = getFirstNode( root ); hash ^= min.value; delete( min ); return min.value; } public int pollLast () { if ( root == null ) { throw new NullPointerException(); } --size; final Node max = getLastNode( root ); hash ^= max.value; delete( max ); return max.value; } public int ceiling ( final int x ) { return ceiling( root, x ); } private int ceiling ( Node node, final int x ) { Node ans = new Node( null, x - 1 ); while ( node != null ) { if ( x > node.value ) { node = node.right; } else if ( x < node.value ) { ans = node; node = node.left; } else { return x; } } return ans.value; } public int higher ( final int x ) { return higher( root, x ); } private int higher ( Node node, final int x ) { Node ans = new Node( null, x - 1 ); while ( node != null ) { if ( x >= node.value ) { node = node.right; } else { ans = node; node = node.left; } } return ans.value; } public int floor ( final int x ) { return floor( root, x ); } private int floor ( Node node, final int x ) { Node ans = new Node( null, x + 1 ); while ( node != null ) { if ( x < node.value ) { node = node.left; } else if ( x > node.value ) { ans = node; node = node.right; } else { return x; } } return ans.value; } public int lower ( final int x ) { return lower( root, x ); } private int lower ( Node node, final int x ) { Node ans = new Node( null, x + 1 ); while ( node != null ) { if ( x <= node.value ) { node = node.left; } else { ans = node; node = node.right; } } return ans.value; } public void clear () { root = null; size = 0; hash = 0; } public boolean isEmpty () { return size == 0; } public int size () { return size; } public int[] toArray () { final int[] list = new int[size]; if ( root != null ) { int index = 0; final ArrayDeque<Node> deq = new ArrayDeque<>(); deq.add( root ); while ( !deq.isEmpty() ) { final Node now = deq.pollLast(); if ( index == 0 ) { if ( now.left != null ) { deq.add( now ); deq.add( now.left ); } else { list[index++] = now.value; if ( now.right != null ) { deq.add( now.right ); } } } else if ( now.left != null && list[index - 1] < now.left.value ) { deq.add( now ); deq.add( now.left ); } else { list[index++] = now.value; if ( now.right != null ) { deq.add( now.right ); } } } } return list; } @Override public String toString () { final int[] list = toArray(); return Arrays.toString( list ); } @Override public boolean equals ( final Object o ) { if ( o instanceof final TreeInt tree ) { if ( size != tree.size() ) { return false; } final int[] array1 = toArray(); final int[] array2 = tree.toArray(); for ( int i = 0; i < size; ++i ) { if ( array1[i] != array2[i] ) { return false; } } return true; } return false; } @Override public int hashCode () { return hash; } /* * 以下、平衡用メソッド */ private void fix ( Node node ) { while ( node != null ) { final int lh = node.left == null ? 0 : node.left.height; final int rh = node.right == null ? 0 : node.right.height; if ( lh - rh > 1 ) { assert node.left != null; if ( node.left.right != null && node.left.right.height == lh - 1 ) { rotateL( node.left ); } rotateR( node ); } else if ( rh - lh > 1 ) { assert node.right != null; if ( node.right.left != null && node.right.left.height == rh - 1 ) { rotateR( node.right ); } rotateL( node ); } else { setStates( node ); } node = node.parent; } } private void rotateR ( final Node node ) { final Node temp = node.left; node.left = temp.right; if ( node.left != null ) { node.left.parent = node; } temp.right = node; temp.parent = node.parent; if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = temp; } else { node.parent.right = temp; } } else { root = temp; } node.parent = temp; setStates( node ); } private void rotateL ( final Node node ) { final Node temp = node.right; node.right = temp.left; if ( node.right != null ) { node.right.parent = node; } temp.left = node; temp.parent = node.parent; if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = temp; } else { node.parent.right = temp; } } else { root = temp; } node.parent = temp; setStates( node ); } private void setStates ( final Node node ) { final int lh = node.left != null ? node.left.height : 0; final int rh = node.right != null ? node.right.height : 0; node.height = Math.max( lh, rh ) + 1; final int ls = node.left != null ? node.left.size : 0; final int rs = node.right != null ? node.right.size : 0; node.size = ls + rs + 1; } } final class TreeLong { private Node root; private int size, hash; public TreeLong () { size = 0; root = null; hash = 0; } static final private class Node { long value; int height, size; Node left, right, parent; public Node ( final Node p, final long v ) { value = v; parent = p; height = 1; size = 1; } } public boolean add ( final long x ) { boolean bool = true; if ( root == null ) { root = new Node( null, x ); } else { Node par; Node now = root; do { par = now; if ( x < now.value ) { now = now.left; } else if ( x > now.value ) { now = now.right; } else { bool = false; break; } } while ( now != null ); if ( bool ) { if ( x < par.value ) { par.left = new Node( par, x ); } else { par.right = new Node( par, x ); } fix( par ); } } if ( bool ) { ++size; hash ^= ( int )x; } return bool; } public long get ( int index ) { if ( root == null || size <= index ) { throw new NullPointerException(); } Node now = root; while ( true ) { assert now != null; final int ls = now.left != null ? now.left.size : 0; if ( index < ls ) { now = now.left; } else if ( ls < index ) { now = now.right; index -= ls + 1; } else { break; } } return now.value; } public boolean remove ( final long x ) { final Node n = getNode( x ); if ( n == null ) { return false; } --size; hash ^= ( int )x; delete( n ); return true; } private void delete ( final Node node ) { if ( node != null ) { if ( node.left == null && node.right == null ) { if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = null; } else { node.parent.right = null; } fix( node.parent ); } else { root = null; } node.parent = null; } else { if ( node.left != null && node.right != null ) { final Node rep = getFirstNode( node.right ); node.value = rep.value; delete( rep ); } else { final Node rep = node.left != null ? node.left : node.right; rep.parent = node.parent; if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = rep; } else { node.parent.right = rep; } fix( node.parent ); } else { root = rep; } node.parent = null; } } } } private Node getNode ( final long x ) { Node now = root; while ( now != null ) { if ( x < now.value ) { now = now.left; } else if ( x > now.value ) { now = now.right; } else { break; } } return now; } public long first () { if ( root == null ) { throw new NullPointerException(); } return getFirstNode( root ).value; } private Node getFirstNode ( Node node ) { assert node != null; Node par = null; while ( node != null ) { par = node; node = par.left; } return par; } public long last () { if ( root == null ) { throw new NullPointerException(); } return getLastNode( root ).value; } private Node getLastNode ( Node node ) { assert node != null; Node par = null; while ( node != null ) { par = node; node = par.right; } return par; } public boolean contains ( final long x ) { if ( root == null ) { return false; } return getNode( x ) != null; } public long pollFirst () { if ( root == null ) { throw new NullPointerException(); } --size; final Node min = getFirstNode( root ); hash ^= ( int )min.value; delete( min ); return min.value; } public long pollLast () { if ( root == null ) { throw new NullPointerException(); } --size; final Node max = getLastNode( root ); hash ^= ( int )max.value; delete( max ); return max.value; } public long ceiling ( final long x ) { return ceiling( root, x ); } private long ceiling ( Node node, final long x ) { Node ans = new Node( null, x - 1 ); while ( node != null ) { if ( x > node.value ) { node = node.right; } else if ( x < node.value ) { ans = node; node = node.left; } else { return x; } } return ans.value; } public long higher ( final long x ) { return higher( root, x ); } private long higher ( Node node, final long x ) { Node ans = new Node( null, x - 1 ); while ( node != null ) { if ( x >= node.value ) { node = node.right; } else { ans = node; node = node.left; } } return ans.value; } public long floor ( final long x ) { return floor( root, x ); } private long floor ( Node node, final long x ) { Node ans = new Node( null, x + 1 ); while ( node != null ) { if ( x < node.value ) { node = node.left; } else if ( x > node.value ) { ans = node; node = node.right; } else { return x; } } return ans.value; } public long lower ( final long x ) { return lower( root, x ); } private long lower ( Node node, final long x ) { Node ans = new Node( null, x + 1 ); while ( node != null ) { if ( x <= node.value ) { node = node.left; } else { ans = node; node = node.right; } } return ans.value; } public int size () { return size; } public long[] toArray () { final long[] list = new long[size]; if ( root != null ) { int index = 0; final ArrayDeque<Node> deq = new ArrayDeque<>(); deq.add( root ); while ( !deq.isEmpty() ) { final Node now = deq.pollLast(); if ( index == 0 ) { if ( now.left != null ) { deq.add( now ); deq.add( now.left ); } else { list[index++] = now.value; if ( now.right != null ) { deq.add( now.right ); } } } else if ( now.left != null && list[index - 1] < now.left.value ) { deq.add( now ); deq.add( now.left ); } else { list[index++] = now.value; if ( now.right != null ) { deq.add( now.right ); } } } } return list; } @Override public String toString () { final long[] list = toArray(); return Arrays.toString( list ); } @Override public boolean equals ( final Object o ) { if ( o instanceof final TreeLong tree ) { if ( size != tree.size() ) { return false; } final long[] array1 = toArray(); final long[] array2 = tree.toArray(); for ( int i = 0; i < size; ++i ) { if ( array1[i] != array2[i] ) { return false; } } return true; } return false; } @Override public int hashCode () { return hash; } /* * 以下、平衡用メソッド */ private void fix ( Node node ) { while ( node != null ) { final int lh = node.left == null ? 0 : node.left.height; final int rh = node.right == null ? 0 : node.right.height; if ( lh - rh > 1 ) { assert node.left != null; if ( node.left.right != null && node.left.right.height == lh - 1 ) { rotateL( node.left ); } rotateR( node ); } else if ( rh - lh > 1 ) { assert node.right != null; if ( node.right.left != null && node.right.left.height == rh - 1 ) { rotateR( node.right ); } rotateL( node ); } else { setStates( node ); } node = node.parent; } } private void rotateR ( final Node node ) { final Node temp = node.left; node.left = temp.right; if ( node.left != null ) { node.left.parent = node; } temp.right = node; temp.parent = node.parent; if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = temp; } else { node.parent.right = temp; } } else { root = temp; } node.parent = temp; setStates( node ); } private void rotateL ( final Node node ) { final Node temp = node.right; node.right = temp.left; if ( node.right != null ) { node.right.parent = node; } temp.left = node; temp.parent = node.parent; if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = temp; } else { node.parent.right = temp; } } else { root = temp; } node.parent = temp; setStates( node ); } private void setStates ( final Node node ) { final int lh = node.left != null ? node.left.height : 0; final int rh = node.right != null ? node.right.height : 0; node.height = Math.max( lh, rh ) + 1; final int ls = node.left != null ? node.left.size : 0; final int rs = node.right != null ? node.right.size : 0; node.size = ls + rs + 1; } } final class TreeMulti<E extends Comparable<E>> { private Node<E> root; private long size; private int uniqueSize; private int hash; public TreeMulti () { size = 0; uniqueSize = 0; root = null; hash = 0; } static final private class Node<E> { E value; long count, size; int height; Node<E> left, right, parent; public Node ( final Node<E> p, final E v, final long c ) { value = v; parent = p; count = c; height = 1; size = c; } } public void add ( final E x ) { if ( root == null ) { root = new Node<>( null, x, 1 ); ++uniqueSize; } else { Node<E> par; Node<E> now = root; boolean bool = true; do { par = now; final int result = x.compareTo( now.value ); if ( result < 0 ) { now = now.left; } else if ( result > 0 ) { now = now.right; } else { bool = false; ++now.count; break; } } while ( now != null ); if ( bool ) { ++uniqueSize; final int result = x.compareTo( par.value ); if ( result < 0 ) { par.left = new Node<>( par, x, 1 ); } else { par.right = new Node<>( par, x, 1 ); } } fix( par ); } ++size; hash ^= x.hashCode(); } public void add ( final E x, final long sum ) { if ( root == null ) { root = new Node<>( null, x, sum ); ++uniqueSize; } else { Node<E> par; Node<E> now = root; boolean bool = true; do { par = now; final int result = x.compareTo( now.value ); if ( result < 0 ) { now = now.left; } else if ( result > 0 ) { now = now.right; } else { bool = false; now.count += sum; fix( now ); break; } } while ( now != null ); if ( bool ) { ++uniqueSize; final int result = x.compareTo( par.value ); if ( result < 0 ) { par.left = new Node<>( par, x, sum ); } else { par.right = new Node<>( par, x, sum ); } fix( par ); } } size += sum; hash ^= sum % 2 == 1 ? x.hashCode() : 0; } public E get ( long index ) { if ( root == null || size <= index ) { throw new NullPointerException(); } Node<E> now = root; while ( true ) { assert now != null; final long ls = now.left != null ? now.left.size : 0; if ( index < ls ) { now = now.left; } else if ( ls + now.count <= index ) { index -= ls + now.count; now = now.right; } else { break; } } return now.value; } public boolean remove ( final E x ) { final Node<E> n = getNode( x ); if ( n == null ) { return false; } --size; hash ^= n.hashCode(); delete( n ); return true; } public long remove ( final E x, final long sum ) { final Node<E> n = getNode( x ); if ( n == null ) { return 0; } final long ans = Math.min( sum, n.count ); size -= ans; hash ^= ans % 2 == 1 ? n.hashCode() : 0; n.count -= ans - 1; delete( n ); return ans; } public long removeAll ( final E x ) { final Node<E> n = getNode( x ); if ( n == null ) { return 0; } size -= n.count; final long ans = n.count; hash ^= ans % 2 == 1 ? n.hashCode() : 0; n.count = 0; delete( n ); return ans; } private void delete ( final Node<E> node ) { if ( node != null ) { if ( node.count > 1 ) { --node.count; fix( node ); return; } if ( node.left == null && node.right == null ) { if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = null; } else { node.parent.right = null; } fix( node.parent ); } else { root = null; } node.parent = null; --uniqueSize; } else { if ( node.left != null && node.right != null ) { final Node<E> rep = getFirstNode( node.right ); node.value = rep.value; node.count = rep.count; rep.count = 0; delete( rep ); } else { final Node<E> rep = node.left != null ? node.left : node.right; rep.parent = node.parent; if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = rep; } else { node.parent.right = rep; } fix( node.parent ); } else { root = rep; } node.parent = null; --uniqueSize; } } } } private Node<E> getNode ( final E x ) { Node<E> now = root; while ( now != null ) { final int result = x.compareTo( now.value ); if ( result < 0 ) { now = now.left; } else if ( result > 0 ) { now = now.right; } else { break; } } return now; } public E first () { if ( root == null ) { return null; } return getFirstNode( root ).value; } private Node<E> getFirstNode ( Node<E> node ) { assert node != null; Node<E> par = null; while ( node != null ) { par = node; node = par.left; } return par; } public E last () { if ( root == null ) { return null; } return getLastNode( root ).value; } private Node<E> getLastNode ( Node<E> node ) { assert node != null; Node<E> par = null; while ( node != null ) { par = node; node = par.right; } return par; } public boolean contains ( final E x ) { if ( root == null ) { return false; } return getNode( x ) != null; } public long sum ( final E x ) { if ( root == null ) { return 0; } Node<E> node = getNode( x ); return node != null ? node.count : 0; } public E pollFirst () { if ( root == null ) { return null; } --size; final Node<E> min = getFirstNode( root ); hash ^= min.value.hashCode(); final E ans = min.value; delete( min ); return ans; } public E pollLast () { if ( root == null ) { return null; } --size; final Node<E> max = getLastNode( root ); hash ^= max.value.hashCode(); final E ans = max.value; delete( max ); return ans; } public E ceiling ( final E x ) { return ceiling( root, x ); } private E ceiling ( Node<E> node, final E x ) { Node<E> ans = null; while ( node != null ) { final int result = x.compareTo( node.value ); if ( result > 0 ) { node = node.right; } else if ( result < 0 ) { ans = node; node = node.left; } else { return x; } } return ans != null ? ans.value : null; } public E higher ( final E x ) { return higher( root, x ); } private E higher ( Node<E> node, final E x ) { Node<E> ans = null; while ( node != null ) { final int result = x.compareTo( node.value ); if ( result >= 0 ) { node = node.right; } else { ans = node; node = node.left; } } return ans != null ? ans.value : null; } public E floor ( final E x ) { return floor( root, x ); } private E floor ( Node<E> node, final E x ) { Node<E> ans = null; while ( node != null ) { final int result = x.compareTo( node.value ); if ( result < 0 ) { node = node.left; } else if ( result > 0 ) { ans = node; node = node.right; } else { return x; } } return ans != null ? ans.value : null; } public E lower ( final E x ) { return lower( root, x ); } private E lower ( Node<E> node, final E x ) { Node<E> ans = null; while ( node != null ) { final int result = x.compareTo( node.value ); if ( result <= 0 ) { node = node.left; } else { ans = node; node = node.right; } } return ans != null ? ans.value : null; } public int size () { return uniqueSize; } public long sumSize () { return size; } public ArrayList<E> toList () { final ArrayList<E> list = new ArrayList<>(); if ( root != null ) { final ArrayDeque<Node<E>> deq = new ArrayDeque<>(); deq.add( root ); while ( !deq.isEmpty() ) { final Node<E> now = deq.pollLast(); if ( list.size() == 0 ) { if ( now.left != null ) { deq.add( now ); deq.add( now.left ); } else { for ( int i = 0; i < now.count; ++i ) { list.add( now.value ); } if ( now.right != null ) { deq.add( now.right ); } } } else if ( now.left != null && list.get( list.size() - 1 ).compareTo( now.left.value ) < 0 ) { deq.add( now ); deq.add( now.left ); } else { for ( int i = 0; i < now.count; ++i ) { list.add( now.value ); } if ( now.right != null ) { deq.add( now.right ); } } } } return list; } @Override public String toString () { final ArrayList<E> list = toList(); return list.toString(); } @Override public boolean equals ( final Object o ) { if ( o instanceof final TreeMulti<?> tree ) { if ( size != tree.size() ) { return false; } final ArrayList<E> array1 = toList(); final ArrayList<?> array2 = tree.toList(); for ( int i = 0; i < size; ++i ) { if ( !array1.get( i ).equals( array2.get( i ) ) ) { return false; } } return true; } return false; } @Override public int hashCode () { return hash; } /* * 以下、平衡用メソッド */ private void fix ( Node<E> node ) { while ( node != null ) { final int lh = node.left == null ? 0 : node.left.height; final int rh = node.right == null ? 0 : node.right.height; if ( lh - rh > 1 ) { assert node.left != null; if ( node.left.right != null && node.left.right.height == lh - 1 ) { rotateL( node.left ); setStates( node.left ); } rotateR( node ); } else if ( rh - lh > 1 ) { assert node.right != null; if ( node.right.left != null && node.right.left.height == rh - 1 ) { rotateR( node.right ); setStates( node.right ); } rotateL( node ); } setStates( node ); node = node.parent; } } private void rotateR ( final Node<E> node ) { final Node<E> temp = node.left; node.left = temp.right; if ( node.left != null ) { node.left.parent = node; } temp.right = node; temp.parent = node.parent; if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = temp; } else { node.parent.right = temp; } } else { root = temp; } node.parent = temp; setStates( node ); } private void rotateL ( final Node<E> node ) { final Node<E> temp = node.right; node.right = temp.left; if ( node.right != null ) { node.right.parent = node; } temp.left = node; temp.parent = node.parent; if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = temp; } else { node.parent.right = temp; } } else { root = temp; } node.parent = temp; setStates( node ); } private void setStates ( final Node<E> node ) { final int lh = node.left != null ? node.left.height : 0; final int rh = node.right != null ? node.right.height : 0; node.height = Math.max( lh, rh ) + 1; final long ls = node.left != null ? node.left.size : 0; final long rs = node.right != null ? node.right.size : 0; node.size = ls + rs + node.count; } } final class TreeMultiInt { private Node root; private long size; private int uniqueSize; private int hash; public TreeMultiInt () { size = 0; uniqueSize = 0; root = null; hash = 0; } static final private class Node { int value; long count, size; int height; Node left, right, parent; public Node ( final Node p, final int v, final long c ) { value = v; parent = p; count = c; height = 1; size = c; } } public void add ( final int x ) { if ( root == null ) { root = new Node( null, x, 1 ); ++uniqueSize; } else { Node par; Node now = root; boolean bool = true; do { par = now; if ( x < now.value ) { now = now.left; } else if ( x > now.value ) { now = now.right; } else { bool = false; ++now.count; break; } } while ( now != null ); if ( bool ) { ++uniqueSize; if ( x < par.value ) { par.left = new Node( par, x, 1 ); } else { par.right = new Node( par, x, 1 ); } } fix( par ); } ++size; hash ^= x; } public void add ( final int x, final long sum ) { if ( root == null ) { root = new Node( null, x, sum ); ++uniqueSize; } else { Node par; Node now = root; boolean bool = true; do { par = now; if ( x < now.value ) { now = now.left; } else if ( x > now.value ) { now = now.right; } else { bool = false; now.count += sum; fix( now ); break; } } while ( now != null ); if ( bool ) { ++uniqueSize; if ( x < par.value ) { par.left = new Node( par, x, sum ); } else { par.right = new Node( par, x, sum ); } fix( par ); } } size += sum; if ( sum % 2 == 1 ) { hash ^= x; } } public int get ( long index ) { if ( root == null || size <= index ) { throw new NullPointerException(); } Node now = root; while ( true ) { assert now != null; final long ls = now.left != null ? now.left.size : 0; if ( index < ls ) { now = now.left; } else if ( ls + now.count <= index ) { index -= ls + now.count; now = now.right; } else { break; } } return now.value; } public boolean remove ( final int x ) { final Node n = getNode( x ); if ( n == null ) { return false; } --size; hash ^= x; delete( n ); return true; } public long remove ( final int x, final long sum ) { final Node n = getNode( x ); if ( n == null ) { return 0; } final long ans = Math.min( sum, n.count ); size -= ans; n.count -= ans - 1; if ( ans % 2 == 1 ) { hash ^= x; } delete( n ); return ans; } public long removeAll ( final int x ) { final Node n = getNode( x ); if ( n == null ) { return 0; } size -= n.count; final long ans = n.count; if ( n.count % 2 == 1 ) { hash ^= x; } n.count = 0; delete( n ); return ans; } private void delete ( final Node node ) { if ( node != null ) { if ( node.count > 1 ) { --node.count; fix( node ); return; } if ( node.left == null && node.right == null ) { if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = null; } else { node.parent.right = null; } fix( node.parent ); } else { root = null; } node.parent = null; --uniqueSize; } else { if ( node.left != null && node.right != null ) { final Node rep = getFirstNode( node.right ); node.value = rep.value; node.count = rep.count; rep.count = 0; delete( rep ); } else { final Node rep = node.left != null ? node.left : node.right; rep.parent = node.parent; if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = rep; } else { node.parent.right = rep; } fix( node.parent ); } else { root = rep; } node.parent = null; --uniqueSize; } } } } private Node getNode ( final int x ) { Node now = root; while ( now != null ) { if ( x < now.value ) { now = now.left; } else if ( x > now.value ) { now = now.right; } else { break; } } return now; } public int first () { if ( root == null ) { throw new NullPointerException(); } return getFirstNode( root ).value; } private Node getFirstNode ( Node node ) { assert node != null; Node par = null; while ( node != null ) { par = node; node = par.left; } return par; } public int last () { if ( root == null ) { throw new NullPointerException(); } return getLastNode( root ).value; } private Node getLastNode ( Node node ) { assert node != null; Node par = null; while ( node != null ) { par = node; node = par.right; } return par; } public boolean contains ( final int x ) { if ( root == null ) { return false; } return getNode( x ) != null; } public long sum ( final int x ) { if ( root == null ) { return 0; } Node node = getNode( x ); return node != null ? node.count : 0; } public int pollFirst () { if ( root == null ) { throw new NullPointerException(); } --size; final Node min = getFirstNode( root ); hash ^= min.value; final int ans = min.value; delete( min ); return ans; } public int pollLast () { if ( root == null ) { throw new NullPointerException(); } --size; final Node max = getLastNode( root ); hash ^= max.value; final int ans = max.value; delete( max ); return ans; } public int ceiling ( final int x ) { return ceiling( root, x ); } private int ceiling ( Node node, final int x ) { Node ans = new Node( null, x - 1, 0 ); while ( node != null ) { if ( x > node.value ) { node = node.right; } else if ( x < node.value ) { ans = node; node = node.left; } else { return x; } } return ans.value; } public int higher ( final int x ) { return higher( root, x ); } private int higher ( Node node, final int x ) { Node ans = new Node( null, x - 1, 0 ); while ( node != null ) { if ( x >= node.value ) { node = node.right; } else { ans = node; node = node.left; } } return ans.value; } public int floor ( final int x ) { return floor( root, x ); } private int floor ( Node node, final int x ) { Node ans = new Node( null, x + 1, 0 ); while ( node != null ) { if ( x < node.value ) { node = node.left; } else if ( x > node.value ) { ans = node; node = node.right; } else { return x; } } return ans.value; } public int lower ( final int x ) { return lower( root, x ); } private int lower ( Node node, final int x ) { Node ans = new Node( null, x + 1, 0 ); while ( node != null ) { if ( x <= node.value ) { node = node.left; } else { ans = node; node = node.right; } } return ans.value; } public int size () { return uniqueSize; } public long sumSize () { return size; } public long[][] toArray () { final long[][] list = new long[uniqueSize][2]; if ( root != null ) { int index = 0; final ArrayDeque<Node> deq = new ArrayDeque<>( root.height << 1 ); deq.add( root ); while ( !deq.isEmpty() ) { final Node now = deq.pollLast(); if ( index == 0 ) { if ( now.left != null ) { deq.add( now ); deq.add( now.left ); } else { list[index][0] = now.value; list[index++][1] = now.count; if ( now.right != null ) { deq.add( now.right ); } } } else if ( now.left != null && list[index - 1][0] < now.left.value ) { deq.add( now ); deq.add( now.left ); } else { list[index][0] = now.value; list[index++][1] = now.count; if ( now.right != null ) { deq.add( now.right ); } } } } return list; } public int[] toOneArray () { final int[] list = new int[( int )size]; if ( root != null ) { int index = 0; final ArrayDeque<Node> deq = new ArrayDeque<>(); deq.add( root ); while ( !deq.isEmpty() ) { final Node now = deq.pollLast(); if ( index == 0 ) { if ( now.left != null ) { deq.add( now ); deq.add( now.left ); } else { for ( int i = 0; i < now.count; ++i ) { list[index++] = now.value; } if ( now.right != null ) { deq.add( now.right ); } } } else if ( now.left != null && list[index - 1] < now.left.value ) { deq.add( now ); deq.add( now.left ); } else { for ( int i = 0; i < now.count; ++i ) { list[index++] = now.value; } if ( now.right != null ) { deq.add( now.right ); } } } } return list; } @Override public String toString () { final int[] list = toOneArray(); return Arrays.toString( list ); } @Override public boolean equals ( final Object o ) { if ( o instanceof final TreeMultiInt tree ) { if ( size != tree.size() ) { return false; } final long[][] array1 = toArray(); final long[][] array2 = tree.toArray(); for ( int i = 0; i < size; ++i ) { if ( array1[i][0] != array2[i][0] || array1[i][1] != array2[i][1] ) { return false; } } return true; } return false; } @Override public int hashCode () { return hash; } /* * 以下、平衡用メソッド */ private void fix ( Node node ) { while ( node != null ) { final int lh = node.left == null ? 0 : node.left.height; final int rh = node.right == null ? 0 : node.right.height; if ( lh - rh > 1 ) { assert node.left != null; if ( node.left.right != null && node.left.right.height == lh - 1 ) { rotateL( node.left ); setStates( node.left ); } rotateR( node ); } else if ( rh - lh > 1 ) { assert node.right != null; if ( node.right.left != null && node.right.left.height == rh - 1 ) { rotateR( node.right ); setStates( node.right ); } rotateL( node ); } setStates( node ); node = node.parent; } } private void rotateR ( final Node node ) { final Node temp = node.left; node.left = temp.right; if ( node.left != null ) { node.left.parent = node; } temp.right = node; temp.parent = node.parent; if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = temp; } else { node.parent.right = temp; } } else { root = temp; } node.parent = temp; setStates( node ); } private void rotateL ( final Node node ) { final Node temp = node.right; node.right = temp.left; if ( node.right != null ) { node.right.parent = node; } temp.left = node; temp.parent = node.parent; if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = temp; } else { node.parent.right = temp; } } else { root = temp; } node.parent = temp; setStates( node ); } private void setStates ( final Node node ) { final int lh = node.left != null ? node.left.height : 0; final int rh = node.right != null ? node.right.height : 0; node.height = Math.max( lh, rh ) + 1; final long ls = node.left != null ? node.left.size : 0; final long rs = node.right != null ? node.right.size : 0; node.size = ls + rs + node.count; } } final class TreeMultiLong { private Node root; private long size; private int uniqueSize; private int hash; public TreeMultiLong () { size = 0; uniqueSize = 0; root = null; hash = 0; } static final private class Node { long value; long count, size; int height; Node left, right, parent; public Node ( final Node p, final long v, final long c ) { value = v; parent = p; count = c; height = 1; size = c; } } public void add ( final long x ) { if ( root == null ) { root = new Node( null, x, 1 ); ++uniqueSize; } else { Node par; Node now = root; boolean bool = true; do { par = now; if ( x < now.value ) { now = now.left; } else if ( x > now.value ) { now = now.right; } else { bool = false; ++now.count; break; } } while ( now != null ); if ( bool ) { ++uniqueSize; if ( x < par.value ) { par.left = new Node( par, x, 1 ); } else { par.right = new Node( par, x, 1 ); } } fix( par ); } ++size; hash ^= ( int )x; } public void add ( final long x, final long sum ) { if ( root == null ) { root = new Node( null, x, sum ); ++uniqueSize; } else { Node par; Node now = root; boolean bool = true; do { par = now; if ( x < now.value ) { now = now.left; } else if ( x > now.value ) { now = now.right; } else { bool = false; now.count += sum; fix( now ); break; } } while ( now != null ); if ( bool ) { ++uniqueSize; if ( x < par.value ) { par.left = new Node( par, x, sum ); } else { par.right = new Node( par, x, sum ); } fix( par ); } } size += sum; if ( sum % 2 == 1 ) { hash ^= ( int )x; } } public long get ( long index ) { if ( root == null || size <= index ) { throw new NullPointerException(); } Node now = root; while ( true ) { assert now != null; final long ls = now.left != null ? now.left.size : 0; if ( index < ls ) { now = now.left; } else if ( ls + now.count <= index ) { index -= ls + now.count; now = now.right; } else { break; } } return now.value; } public boolean remove ( final long x ) { final Node n = getNode( x ); if ( n == null ) { return false; } --size; hash ^= x; delete( n ); return true; } public long remove ( final long x, final long sum ) { final Node n = getNode( x ); if ( n == null ) { return 0; } final long ans = Math.min( sum, n.count ); size -= ans; n.count -= ans - 1; if ( ans % 2 == 1 ) { hash ^= ( int )x; } delete( n ); return ans; } public long removeAll ( final long x ) { final Node n = getNode( x ); if ( n == null ) { return 0; } size -= n.count; final long ans = n.count; if ( n.count % 2 == 1 ) { hash ^= ( int )x; } n.count = 0; delete( n ); return ans; } private void delete ( final Node node ) { if ( node != null ) { if ( node.count > 1 ) { --node.count; fix( node ); return; } if ( node.left == null && node.right == null ) { if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = null; } else { node.parent.right = null; } fix( node.parent ); } else { root = null; } node.parent = null; --uniqueSize; } else { if ( node.left != null && node.right != null ) { final Node rep = getFirstNode( node.right ); node.value = rep.value; node.count = rep.count; rep.count = 0; delete( rep ); } else { final Node rep = node.left != null ? node.left : node.right; rep.parent = node.parent; if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = rep; } else { node.parent.right = rep; } fix( node.parent ); } else { root = rep; } node.parent = null; --uniqueSize; } } } } private Node getNode ( final long x ) { Node now = root; while ( now != null ) { if ( x < now.value ) { now = now.left; } else if ( x > now.value ) { now = now.right; } else { break; } } return now; } public long first () { if ( root == null ) { throw new NullPointerException(); } return getFirstNode( root ).value; } private Node getFirstNode ( Node node ) { assert node != null; Node par = null; while ( node != null ) { par = node; node = par.left; } return par; } public long last () { if ( root == null ) { throw new NullPointerException(); } return getLastNode( root ).value; } private Node getLastNode ( Node node ) { assert node != null; Node par = null; while ( node != null ) { par = node; node = par.right; } return par; } public boolean contains ( final long x ) { if ( root == null ) { return false; } return getNode( x ) != null; } public long sum ( final long x ) { if ( root == null ) { return 0; } final Node node = getNode( x ); return node != null ? node.count : 0; } public long pollFirst () { if ( root == null ) { throw new NullPointerException(); } --size; final Node min = getFirstNode( root ); hash ^= ( int )min.value; final long ans = min.value; delete( min ); return ans; } public long pollLast () { if ( root == null ) { throw new NullPointerException(); } --size; final Node max = getLastNode( root ); hash ^= ( int )max.value; final long ans = max.value; delete( max ); return ans; } public long ceiling ( final long x ) { return ceiling( root, x ); } private long ceiling ( Node node, final long x ) { Node ans = new Node( null, x - 1, 0 ); while ( node != null ) { if ( x > node.value ) { node = node.right; } else if ( x < node.value ) { ans = node; node = node.left; } else { return x; } } return ans.value; } public long higher ( final long x ) { return higher( root, x ); } private long higher ( Node node, final long x ) { Node ans = new Node( null, x - 1, 0 ); while ( node != null ) { if ( x >= node.value ) { node = node.right; } else { ans = node; node = node.left; } } return ans.value; } public long floor ( final long x ) { return floor( root, x ); } private long floor ( Node node, final long x ) { Node ans = new Node( null, x + 1, 0 ); while ( node != null ) { if ( x < node.value ) { node = node.left; } else if ( x > node.value ) { ans = node; node = node.right; } else { return x; } } return ans.value; } public long lower ( final long x ) { return lower( root, x ); } private long lower ( Node node, final long x ) { Node ans = new Node( null, x + 1, 0 ); while ( node != null ) { if ( x <= node.value ) { node = node.left; } else { ans = node; node = node.right; } } return ans.value; } public int size () { return uniqueSize; } public long sumSize () { return size; } public long[][] toArray () { final long[][] list = new long[uniqueSize][2]; if ( root != null ) { int index = 0; ArrayDeque<Node> deq = new ArrayDeque<>( root.height << 1 ); deq.add( root ); while ( !deq.isEmpty() ) { final Node now = deq.pollLast(); if ( index == 0 ) { if ( now.left != null ) { deq.add( now ); deq.add( now.left ); } else { list[index][0] = now.value; list[index++][1] = now.count; if ( now.right != null ) { deq.add( now.right ); } } } else if ( now.left != null && list[index - 1][0] < now.left.value ) { deq.add( now ); deq.add( now.left ); } else { list[index][0] = now.value; list[index++][1] = now.count; if ( now.right != null ) { deq.add( now.right ); } } } } return list; } public long[] toOneArray () { final long[] list = new long[( int )size]; if ( root != null ) { int index = 0; final ArrayDeque<Node> deq = new ArrayDeque<>(); deq.add( root ); while ( !deq.isEmpty() ) { final Node now = deq.pollLast(); if ( index == 0 ) { if ( now.left != null ) { deq.add( now ); deq.add( now.left ); } else { for ( int i = 0; i < now.count; ++i ) { list[index++] = now.value; } if ( now.right != null ) { deq.add( now.right ); } } } else if ( now.left != null && list[index - 1] < now.left.value ) { deq.add( now ); deq.add( now.left ); } else { for ( int i = 0; i < now.count; ++i ) { list[index++] = now.value; } if ( now.right != null ) { deq.add( now.right ); } } } } return list; } @Override public String toString () { final long[] list = toOneArray(); return Arrays.toString( list ); } @Override public boolean equals ( final Object o ) { if ( o instanceof final TreeMultiInt tree ) { if ( size != tree.size() ) { return false; } final long[][] array1 = toArray(); final long[][] array2 = tree.toArray(); for ( int i = 0; i < size; ++i ) { if ( array1[i][0] != array2[i][0] || array1[i][1] != array2[i][1] ) { return false; } } return true; } return false; } @Override public int hashCode () { return hash; } /* * 以下、平衡用メソッド */ private void fix ( Node node ) { while ( node != null ) { final int lh = node.left == null ? 0 : node.left.height; final int rh = node.right == null ? 0 : node.right.height; if ( lh - rh > 1 ) { assert node.left != null; if ( node.left.right != null && node.left.right.height == lh - 1 ) { rotateL( node.left ); setStates( node.left ); } rotateR( node ); } else if ( rh - lh > 1 ) { assert node.right != null; if ( node.right.left != null && node.right.left.height == rh - 1 ) { rotateR( node.right ); setStates( node.right ); } rotateL( node ); } setStates( node ); node = node.parent; } } private void rotateR ( final Node node ) { final Node temp = node.left; node.left = temp.right; if ( node.left != null ) { node.left.parent = node; } temp.right = node; temp.parent = node.parent; if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = temp; } else { node.parent.right = temp; } } else { root = temp; } node.parent = temp; setStates( node ); } private void rotateL ( final Node node ) { final Node temp = node.right; node.right = temp.left; if ( node.right != null ) { node.right.parent = node; } temp.left = node; temp.parent = node.parent; if ( node.parent != null ) { if ( node.parent.left == node ) { node.parent.left = temp; } else { node.parent.right = temp; } } else { root = temp; } node.parent = temp; setStates( node ); } private void setStates ( final Node node ) { final int lh = node.left != null ? node.left.height : 0; final int rh = node.right != null ? node.right.height : 0; node.height = Math.max( lh, rh ) + 1; final long ls = node.left != null ? node.left.size : 0; final long rs = node.right != null ? node.right.size : 0; node.size = ls + rs + node.count; } } final class SimpleScanner { private final int BUFF_SIZE = 1 << 17; private final InputStream is; private final byte[] buff; private int point, length; public SimpleScanner ( final InputStream is ) { this.is = is; buff = new byte[BUFF_SIZE]; point = length = 0; } private void reload () { do { try { length = is.read( buff, point = 0, BUFF_SIZE ); } catch ( final IOException e ) { e.printStackTrace(); System.exit( 1 ); } } while ( length == -1 ); } private byte read () { if ( point == length ) { reload(); } return buff[point++]; } public byte nextByte () { byte c = read(); while ( c <= ' ' ) { c = read(); } return c; } public int nextInt () { int ans = 0; byte c = nextByte(); final boolean negate = c == '-'; if ( !MathFunction.rangeCheckClose( c, '0', '9' ) ) { c = read(); } while ( MathFunction.rangeCheckClose( c, '0', '9' ) ) { ans = ans * 10 + c - '0'; c = read(); } return negate ? -ans : ans; } public long nextLong () { long ans = 0; byte c = nextByte(); final boolean negate = c == '-'; if ( !MathFunction.rangeCheckClose( c, '0', '9' ) ) { c = read(); } while ( MathFunction.rangeCheckClose( c, '0', '9' ) ) { ans = ans * 10L + c - '0'; c = read(); } return negate ? -ans : ans; } public char nextChar () { return ( char )nextByte(); } public String next () { final StringBuilder ans = new StringBuilder(); byte c = nextByte(); while ( c > ' ' ) { ans.append( ( char )c ); c = read(); } return ans.toString(); } public BigInteger nextBigInteger () { return new BigInteger( next() ); } public byte[] nextByte ( final int n ) { final byte[] ans = new byte[n]; for ( int i = 0; i < n; ++i ) { ans[i] = nextByte(); } return ans; } public int[] nextInt ( final int n ) { final int[] ans = new int[n]; for ( int i = 0; i < n; ++i ) { ans[i] = nextInt(); } return ans; } public long[] nextLong ( final int n ) { final long[] ans = new long[n]; for ( int i = 0; i < n; ++i ) { ans[i] = nextLong(); } return ans; } public String[] next ( final int n ) { final String[] ans = new String[n]; for ( int i = 0; i < n; ++i ) { ans[i] = next(); } return ans; } public byte[][] nextByte ( final int n, final int m ) { final byte[][] ans = new byte[n][]; for ( int i = 0; i < n; ++i ) { ans[i] = nextByte( m ); } return ans; } public int[][] nextInt ( final int n, final int m ) { final int[][] ans = new int[n][]; for ( int i = 0; i < n; ++i ) { ans[i] = nextInt( m ); } return ans; } public long[][] nextLong ( final int n, final int m ) { final long[][] ans = new long[n][]; for ( int i = 0; i < n; ++i ) { ans[i] = nextLong( m ); } return ans; } public String[][] next ( final int n, final int m ) { final String[][] ans = new String[n][]; for ( int i = 0; i < n; ++i ) { ans[i] = next( m ); } return ans; } public char[] nextCharArray () { return next().toCharArray(); } public char[][] nextCharArray ( final int n ) { final char[][] ans = new char[n][]; for ( int i = 0; i < n; ++i ) { ans[i] = nextCharArray(); } return ans; } public int[][] nextGraph ( final int N, final int M ) { if ( M == 0 ) { return new int[N + 1][0]; } final int[][] ans = new int[N + 1][]; final int[] count = new int[N + 1]; final int[][] path = nextInt( M, 2 ); for ( final int[] temp: path ) { ++count[temp[0]]; ++count[temp[1]]; } for ( int i = 1; i <= N; ++i ) { ans[i] = new int[count[i]]; } for ( final int[] temp: path ) { ans[temp[0]][--count[temp[0]]] = temp[1]; ans[temp[1]][--count[temp[1]]] = temp[0]; } ans[0] = new int[0]; return ans; } public Point nextPoint () { return new Point( nextInt(), nextInt() ); } public Point[] nextPoint ( final int n ) { final Point[] ans = new Point[n]; for ( int i = 0; i < n; ++i ) { ans[i] = nextPoint(); } return ans; } public void close () { try { is.close(); } catch ( final IOException e ) { e.printStackTrace(); System.exit( 1 ); } } } final class SimpleOutputStream extends FilterOutputStream { private final byte buf[]; private int count; public SimpleOutputStream(final OutputStream out) { this(out, 1<<17); } public SimpleOutputStream(final OutputStream out, final int size) { super(out); if (size <= 0) { throw new IllegalArgumentException("Buffer size <= 0"); } buf = new byte[size]; } private void flushBuffer() throws IOException { if (count > 0) { out.write(buf, 0, count); count = 0; } } public void write(final int b) throws IOException { if (count >= buf.length) { flushBuffer(); } buf[count++] = (byte)b; } public void write(final byte b[], final int off, final int len) throws IOException { if (len >= buf.length) { flushBuffer(); out.write(b, off, len); return; } if (len > buf.length - count) { flushBuffer(); } System.arraycopy(b, off, buf, count, len); count += len; } public void flush() throws IOException { flushBuffer(); out.flush(); } } final class SimpleWriter implements Appendable, Closeable, Flushable, AutoCloseable{ private Writer out; private final boolean autoFlush; private boolean trouble = false; private Formatter formatter; private PrintStream psOut = null; private static Charset toCharset ( final String csn ) { if ( csn == null ) { throw new NullPointerException( "charsetName" ); } try { return Charset.forName( csn ); } catch ( IllegalCharsetNameException | UnsupportedCharsetException e ) { e.printStackTrace(); System.exit( 1 ); return null; } } public SimpleWriter ( final Writer out ) { this( out, false ); } public SimpleWriter ( final Writer out, final boolean autoFlush ) { this.out = out; this.autoFlush = autoFlush; } public SimpleWriter ( final OutputStream out ) { this( out, false ); } public SimpleWriter ( final OutputStream out, final boolean autoFlush ) { this(out, autoFlush, Charset.defaultCharset()); } public SimpleWriter(final OutputStream out, final boolean autoFlush, final Charset charset) { this(new BufferedWriter(new OutputStreamWriter(new SimpleOutputStream(out), charset)), autoFlush); if (out instanceof PrintStream) { psOut = (PrintStream) out; } } private void ensureOpen () throws IOException { if ( out == null ) { throw new IOException( "Stream closed" ); } } public void flush () { try { ensureOpen(); out.flush(); } catch ( IOException x ) { trouble = true; } } public void close () { try { if ( out == null ) { return; } out.close(); out = null; } catch ( IOException x ) { trouble = true; } } public boolean checkError () { if ( out != null ) { flush(); } else if ( psOut != null ) { return psOut.checkError(); } return trouble; } private void setError () { trouble = true; } private void clearError () { trouble = false; } public void write ( final int c ) { try { ensureOpen(); out.write( c ); } catch ( InterruptedIOException x ) { Thread.currentThread().interrupt(); } catch ( IOException x ) { trouble = true; } } public void write ( final char[] buf, final int off, final int len ) { try { ensureOpen(); out.write( buf, off, len ); } catch ( InterruptedIOException x ) { Thread.currentThread().interrupt(); } catch ( IOException x ) { trouble = true; } } public void write ( final char[] buf ) { write( buf, 0, buf.length ); } public void write ( final String s, final int off, final int len ) { try { ensureOpen(); out.write( s, off, len ); } catch ( InterruptedIOException x ) { Thread.currentThread().interrupt(); } catch ( IOException x ) { trouble = true; } } public void write ( final String s ) { write( s, 0, s.length() ); } private void newLine () { try { ensureOpen(); out.write( System.lineSeparator() ); if ( autoFlush ) { out.flush(); } } catch ( InterruptedIOException x ) { Thread.currentThread().interrupt(); } catch ( IOException x ) { trouble = true; } } public void print ( final boolean b ) { write( b ? "true" : "false" ); } public void print ( final char c ) { write( c ); } public void print ( final int i ) { write( String.valueOf( i ) ); } public void print ( final long l ) { write( String.valueOf( l ) ); } public void print ( final float f ) { write( String.valueOf( f ) ); } public void print ( final double d ) { write( String.valueOf( d ) ); } public void print ( final char[] s ) { write( s ); } public void print ( final String s ) { write( s ); } public void print ( final Object obj ) { write( obj.toString() ); } public void println () { newLine(); } public void println ( final boolean x ) { print( x ); println(); } public void println ( final char x ) { print( x ); println(); } public void println ( final int x ) { print( x ); println(); } public void println ( final long x ) { print( x ); println(); } public void println ( final float x ) { print( x ); println(); } public void println ( final double x ) { print( x ); println(); } public void println ( final char[] x ) { print( x ); println(); } public void println ( final String x ) { print( x ); println(); } public void println ( final Object x ) { print( x.toString() ); println(); } public SimpleWriter printf ( final String format, final Object... args ) { return format( format, args ); } public SimpleWriter printf ( final Locale l, final String format, final Object... args ) { return format( l, format, args ); } public SimpleWriter format ( final String format, final Object... args ) { try { ensureOpen(); if ( ( formatter == null ) || ( formatter.locale() != Locale.getDefault() ) ) { formatter = new Formatter( this ); } formatter.format( Locale.getDefault(), format, args ); if ( autoFlush ) { out.flush(); } } catch ( InterruptedIOException x ) { Thread.currentThread().interrupt(); } catch ( IOException x ) { trouble = true; } return this; } public SimpleWriter format ( final Locale l, final String format, final Object... args ) { try { ensureOpen(); if ( ( formatter == null ) || ( formatter.locale() != l ) ) { formatter = new Formatter( this, l ); } formatter.format( l, format, args ); if ( autoFlush ) { out.flush(); } } catch ( InterruptedIOException x ) { Thread.currentThread().interrupt(); } catch ( IOException x ) { trouble = true; } return this; } public SimpleWriter append ( final CharSequence csq ) { write( String.valueOf( csq ) ); return this; } public SimpleWriter append ( CharSequence csq, final int start, final int end ) { if ( csq == null ) { csq = "null"; } return append( csq.subSequence( start, end ) ); } public SimpleWriter append ( final char c ) { write( c ); return this; } public void println ( final int[] array ) { println( array, ' ' ); } public void println ( final int[] array, final String str ) { print( array[0] ); for ( int i = 1; i < array.length; ++i ) { print( str ); print( array[i] ); } println(); } public void println ( final int[] array, final char c ) { print( array[0] ); for ( int i = 1; i < array.length; ++i ) { print( c ); print( array[i] ); } println(); } public void println ( final int[][] array ) { println( array, ' ' ); } public void println ( final int[][] arrays, final String str ) { for ( final int[] array: arrays ) { println( array, str ); } } public void println ( final int[][] arrays, final char c ) { for ( final int[] array: arrays ) { println( array, c ); } } public void println ( final long[] array ) { println( array, ' ' ); } public void println ( final long[] array, final String str ) { print( array[0] ); for ( int i = 1; i < array.length; ++i ) { print( str ); print( array[i] ); } println(); } public void println ( final long[] array, final char c ) { print( array[0] ); for ( int i = 1; i < array.length; ++i ) { print( c ); print( array[i] ); } println(); } public void println ( final long[][] array ) { println( array, ' ' ); } public void println ( final long[][] arrays, final String str ) { for ( final long[] array: arrays ) { println( array, str ); } } public void println ( final long[][] arrays, final char c ) { for ( final long[] array: arrays ) { println( array, c ); } } public void println ( final double[] array ) { println( array, ' ' ); } public void println ( final double[] array, final String str ) { print( array[0] ); for ( int i = 1; i < array.length; ++i ) { print( str ); print( array[i] ); } println(); } public void println ( final double[] array, final char c ) { print( array[0] ); for ( int i = 1; i < array.length; ++i ) { print( c ); print( array[i] ); } println(); } public void println ( final double[][] array ) { println( array, ' ' ); } public void println ( final double[][] arrays, final String str ) { for ( final double[] array: arrays ) { println( array, str ); } } public void println ( final double[][] arrays, final char c ) { for ( final double[] array: arrays ) { println( array, c ); } } public void println ( final char[] cs, final String str ) { print( cs[0] ); for ( int i = 1; i < cs.length; ++i ) { print( str ); print( cs[i] ); } println(); } public void println ( final char[] cs, final char c ) { print( cs[0] ); for ( int i = 1; i < cs.length; ++i ) { print( c ); print( cs[i] ); } println(); } public void println ( final char[][] cs ) { for ( final char[] c: cs ) { println( c ); } } public void println ( final char[][] cs, final String str ) { for ( final char[] c: cs ) { println( c, str ); } } public void println ( final char[][] cs, final char c ) { for ( final char[] cc: cs ) { println( cc, c ); } } public <E> void println ( final E[] array ) { println( array, ' ' ); } public <E> void println ( final E[] array, final String str ) { print( array[0] ); for ( int i = 1; i < array.length; ++i ) { print( str ); print( array[i] ); } println(); } public <E> void println ( final E[] array, final char c ) { print( array[0] ); for ( int i = 1; i < array.length; ++i ) { print( c ); print( array[i] ); } println(); } public <E> void println ( final E[][] arrays ) { println( arrays, ' ' ); } public <E> void println ( final E[][] arrays, final String str ) { for ( final E[] array: arrays ) { println( array, str ); } } public <E> void println ( final E[][] arrays, final char c ) { for ( final E[] array: arrays ) { println( array, c ); } } public <E> void println ( final List<E> list ) { println( list, ' ' ); } public <E> void println ( final List<E> list, final String str ) { if ( list.size() == 0 ) { println(); return; } print( list.get( 0 ) ); for ( int i = 1; i < list.size(); ++i ) { print( str ); print( list.get( i ) ); } println(); } public <E> void println ( final List<E> list, final char c ) { if ( list.size() == 0 ) { println(); return; } print( list.get( 0 ) ); for ( int i = 1; i < list.size(); ++i ) { print( c ); print( list.get( i ) ); } println(); } }
ConDefects/ConDefects/Code/abc326_f/Java/47049223
condefects-java_data_348
import java.io.*; import java.util.*; public class Main { public static int INF = 0x3f3f3f3f, mod = 1000000007, mod9 = 998244353; public static void main(String args[]){ try { PrintWriter o = new PrintWriter(System.out); boolean multiTest = false; // init if(multiTest) { int t = fReader.nextInt(), loop = 0; while (loop < t) {loop++;solve(o);} } else solve(o); o.close(); } catch (Exception e) {e.printStackTrace();} } static void solve(PrintWriter o) { try { //int n = 80; int n = fReader.nextInt(); int X = fReader.nextInt(), Y = fReader.nextInt(); List<Integer> xli = new ArrayList<>(); List<Integer> yli = new ArrayList<>(); int[] a = new int[n]; for(int i=0;i<n;i++) { //a[i] = (int)(10000000*Math.random()); a[i] = fReader.nextInt(); if(i%2 == 0) yli.add(a[i]); else xli.add(a[i]); } String arr1 = check(xli, X); String arr2 = check(yli, Y); if(arr1 == null || arr2 == null) o.println("No"); else { StringBuilder bstr = new StringBuilder(); for(int i=0;i<arr1.length();i++) { bstr.append(arr2.charAt(i)); bstr.append(arr1.charAt(i)); } if(arr2.length() > arr1.length()) bstr.append(arr2.charAt(arr2.length()-1)); StringBuilder res = new StringBuilder(); char pre = '+'; for(int i=0;i<bstr.length();i++) { if(pre == '+') { if(bstr.charAt(i) == '+') { if(i%2 == 0) res.append("L"); else res.append("R"); } else { if(i%2 == 0) res.append("R"); else res.append("L"); } } else { if(bstr.charAt(i) == '+') { if(i%2 == 0) res.append("R"); else res.append("L"); } else { if(i%2 == 0) res.append("L"); else res.append("R"); } } pre = bstr.charAt(i); } o.println("Yes"); o.println(res); } } catch (Exception e) { e.printStackTrace(); } } public static String check(List<Integer> li, Integer target) { // meet in the middle int n = li.size(); List<Integer> subLi1 = new ArrayList<>(); List<Integer> subLi2 = new ArrayList<>(); for(int i=0;i<n/2;i++) subLi1.add(li.get(i)); for(int i=n/2;i<n;i++) subLi2.add(li.get(i)); Map<Integer, Integer> map1 = convert(subLi1); Map<Integer, Integer> map2 = convert(subLi2); for(Map.Entry<Integer, Integer> entry1: map1.entrySet()) { if(map2.containsKey(target-entry1.getKey())) { return toStr(entry1.getValue(), subLi1.size()) + toStr(map2.get(target-entry1.getKey()), subLi2.size()); } } return null; } static Map<Integer, Integer> convert(List<Integer> li) { int n = li.size(); int[] dp = new int[1<<n]; Arrays.fill(dp, Integer.MAX_VALUE); dp[0] = 0; for(int i=0;i<n;i++) dp[0] -= li.get(i); for(int s=1;s<(1<<n);s++) { if(dp[s] < Integer.MAX_VALUE) continue; for(int j=0;j<n;j++) if((s>>j&1) == 1) { dp[s] = dp[s^(1<<j)]+2*li.get(j); break; } } Map<Integer, Integer> ret = new HashMap<>(); for(int i=0;i<(1<<n);i++) ret.put(dp[i], i); return ret; } static String toStr(int x, int n) { StringBuilder ret = new StringBuilder(); for(int j=0;j<n;j++) { if((x>>j&1) == 1) ret.append("+"); else ret.append("-"); } return ret.reverse().toString(); } public static int upper_bound(List<Integer> a, int val){ int l = 0, r = a.size(); while(l < r){ int mid = l + (r - l) / 2; if(a.get(mid) <= val) l = mid + 1; else r = mid; } return l; } public static int lower_bound(List<Integer> a, int val){ int l = 0, r = a.size(); while(l < r){ int mid = l + (r - l) / 2; if(a.get(mid) < val) l = mid + 1; else r = mid; } return l; } public static long gcd(long a, long b){ return b == 0 ? a : gcd(b, a%b); } public static long lcm(long a, long b){ return a / gcd(a,b)*b; } public static long qpow(long a, long n, int md){ a %= md; long ret = 1l; while(n > 0){ if((n & 1) == 1){ ret = ret * a % md; } n >>= 1; a = a * a % md; } return ret; } public static class DSU { int[] parent; int[] size; int n; public DSU(int n){ this.n = n; parent = new int[n]; size = new int[n]; for(int i=0;i<n;i++){ parent[i] = i; size[i] = 1; } } public int find(int p){ while(parent[p] != p){ parent[p] = parent[parent[p]]; p = parent[p]; } return p; } public void union(int p, int q){ int root_p = find(p); int root_q = find(q); if(root_p == root_q) return; if(size[root_p] >= size[root_q]){ parent[root_q] = root_p; size[root_p] += size[root_q]; size[root_q] = 0; } else{ parent[root_p] = root_q; size[root_q] += size[root_p]; size[root_p] = 0; } n--; } public int getTotalComNum(){ return n; } public int getSize(int i){ return size[find(i)]; } } public static class FenWick { int n; long[] a; long[] tree; public FenWick(int n){ this.n = n; a = new long[n+1]; tree = new long[n+1]; } private void add(int x, long val){ while(x <= n){ tree[x] += val; x += x&-x; } } private void addMx(int x, long val) { a[x] += val; tree[x] = a[x]; while(x <= n) { for(int i=1;i<(x&-x);i<<=1) { tree[x] = Math.max(tree[x], tree[x-i]); } x += x&-x; } } private long query(int x){ long ret = 0l; while(x > 0){ ret += tree[x]; x -= x&-x; } return ret; } private long queryMx(int l, int r) { long res = 0l; while(l <= r) { if(r-(r&-r) >= l) { res = Math.max(res, tree[r]); r -= r&-r; } else { res = Math.max(res, a[r]); r--; } } return res; } } public static class Pair{ Integer c1; String str; public Pair(Integer c1, String str) { this.c1 = c1; this.str = str; } @Override public int hashCode() { int prime = 31, ret = 1; ret = ret*prime + c1.hashCode(); return ret; } @Override public boolean equals(Object obj) { if(obj instanceof Pair) { return c1.equals(((Pair) obj).c1); } return false; } } public static class fReader { private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private static StringTokenizer tokenizer = new StringTokenizer(""); private static String next() throws IOException{ while(!tokenizer.hasMoreTokens()){tokenizer = new StringTokenizer(reader.readLine());} return tokenizer.nextToken(); } public static int nextInt() throws IOException {return Integer.parseInt(next());} public static Long nextLong() throws IOException {return Long.parseLong(next());} public static double nextDouble() throws IOException {return Double.parseDouble(next());} public static char nextChar() throws IOException {return next().toCharArray()[0];} public static String nextString() throws IOException {return next();} public static String nextLine() throws IOException {return reader.readLine();} } } import java.io.*; import java.util.*; public class Main { public static int INF = 0x3f3f3f3f, mod = 1000000007, mod9 = 998244353; public static void main(String args[]){ try { PrintWriter o = new PrintWriter(System.out); boolean multiTest = false; // init if(multiTest) { int t = fReader.nextInt(), loop = 0; while (loop < t) {loop++;solve(o);} } else solve(o); o.close(); } catch (Exception e) {e.printStackTrace();} } static void solve(PrintWriter o) { try { //int n = 80; int n = fReader.nextInt(); int X = fReader.nextInt(), Y = fReader.nextInt(); List<Integer> xli = new ArrayList<>(); List<Integer> yli = new ArrayList<>(); int[] a = new int[n]; for(int i=0;i<n;i++) { //a[i] = (int)(10000000*Math.random()); a[i] = fReader.nextInt(); if(i%2 == 0) yli.add(a[i]); else xli.add(a[i]); } String arr1 = check(xli, X); String arr2 = check(yli, Y); if(arr1 == null || arr2 == null) o.println("No"); else { StringBuilder bstr = new StringBuilder(); for(int i=0;i<arr1.length();i++) { bstr.append(arr2.charAt(i)); bstr.append(arr1.charAt(i)); } if(arr2.length() > arr1.length()) bstr.append(arr2.charAt(arr2.length()-1)); StringBuilder res = new StringBuilder(); char pre = '+'; for(int i=0;i<bstr.length();i++) { if(pre == '+') { if(bstr.charAt(i) == '+') { if(i%2 == 0) res.append("L"); else res.append("R"); } else { if(i%2 == 0) res.append("R"); else res.append("L"); } } else { if(bstr.charAt(i) == '+') { if(i%2 == 0) res.append("R"); else res.append("L"); } else { if(i%2 == 0) res.append("L"); else res.append("R"); } } pre = bstr.charAt(i); } o.println("Yes"); o.println(res); } } catch (Exception e) { e.printStackTrace(); } } public static String check(List<Integer> li, Integer target) { // meet in the middle int n = li.size(); List<Integer> subLi1 = new ArrayList<>(); List<Integer> subLi2 = new ArrayList<>(); for(int i=0;i<n/2;i++) subLi1.add(li.get(i)); for(int i=n/2;i<n;i++) subLi2.add(li.get(i)); Map<Integer, Integer> map1 = convert(subLi1); Map<Integer, Integer> map2 = convert(subLi2); for(Map.Entry<Integer, Integer> entry1: map1.entrySet()) { if(map2.containsKey(target-entry1.getKey())) { return toStr(entry1.getValue(), subLi1.size()) + toStr(map2.get(target-entry1.getKey()), subLi2.size()); } } return null; } static Map<Integer, Integer> convert(List<Integer> li) { int n = li.size(); int[] dp = new int[1<<n]; Arrays.fill(dp, Integer.MAX_VALUE); dp[0] = 0; for(int i=0;i<n;i++) dp[0] -= li.get(i); for(int s=1;s<(1<<n);s++) { if(dp[s] < Integer.MAX_VALUE) continue; for(int j=0;j<n;j++) if((s>>j&1) == 1) { dp[s] = dp[s^(1<<j)]+2*li.get(j); break; } } Map<Integer, Integer> ret = new HashMap<>(); for(int i=0;i<(1<<n);i++) ret.put(dp[i], i); return ret; } static String toStr(int x, int n) { StringBuilder ret = new StringBuilder(); for(int j=0;j<n;j++) { if((x>>j&1) == 1) ret.append("+"); else ret.append("-"); } return ret.toString(); } public static int upper_bound(List<Integer> a, int val){ int l = 0, r = a.size(); while(l < r){ int mid = l + (r - l) / 2; if(a.get(mid) <= val) l = mid + 1; else r = mid; } return l; } public static int lower_bound(List<Integer> a, int val){ int l = 0, r = a.size(); while(l < r){ int mid = l + (r - l) / 2; if(a.get(mid) < val) l = mid + 1; else r = mid; } return l; } public static long gcd(long a, long b){ return b == 0 ? a : gcd(b, a%b); } public static long lcm(long a, long b){ return a / gcd(a,b)*b; } public static long qpow(long a, long n, int md){ a %= md; long ret = 1l; while(n > 0){ if((n & 1) == 1){ ret = ret * a % md; } n >>= 1; a = a * a % md; } return ret; } public static class DSU { int[] parent; int[] size; int n; public DSU(int n){ this.n = n; parent = new int[n]; size = new int[n]; for(int i=0;i<n;i++){ parent[i] = i; size[i] = 1; } } public int find(int p){ while(parent[p] != p){ parent[p] = parent[parent[p]]; p = parent[p]; } return p; } public void union(int p, int q){ int root_p = find(p); int root_q = find(q); if(root_p == root_q) return; if(size[root_p] >= size[root_q]){ parent[root_q] = root_p; size[root_p] += size[root_q]; size[root_q] = 0; } else{ parent[root_p] = root_q; size[root_q] += size[root_p]; size[root_p] = 0; } n--; } public int getTotalComNum(){ return n; } public int getSize(int i){ return size[find(i)]; } } public static class FenWick { int n; long[] a; long[] tree; public FenWick(int n){ this.n = n; a = new long[n+1]; tree = new long[n+1]; } private void add(int x, long val){ while(x <= n){ tree[x] += val; x += x&-x; } } private void addMx(int x, long val) { a[x] += val; tree[x] = a[x]; while(x <= n) { for(int i=1;i<(x&-x);i<<=1) { tree[x] = Math.max(tree[x], tree[x-i]); } x += x&-x; } } private long query(int x){ long ret = 0l; while(x > 0){ ret += tree[x]; x -= x&-x; } return ret; } private long queryMx(int l, int r) { long res = 0l; while(l <= r) { if(r-(r&-r) >= l) { res = Math.max(res, tree[r]); r -= r&-r; } else { res = Math.max(res, a[r]); r--; } } return res; } } public static class Pair{ Integer c1; String str; public Pair(Integer c1, String str) { this.c1 = c1; this.str = str; } @Override public int hashCode() { int prime = 31, ret = 1; ret = ret*prime + c1.hashCode(); return ret; } @Override public boolean equals(Object obj) { if(obj instanceof Pair) { return c1.equals(((Pair) obj).c1); } return false; } } public static class fReader { private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private static StringTokenizer tokenizer = new StringTokenizer(""); private static String next() throws IOException{ while(!tokenizer.hasMoreTokens()){tokenizer = new StringTokenizer(reader.readLine());} return tokenizer.nextToken(); } public static int nextInt() throws IOException {return Integer.parseInt(next());} public static Long nextLong() throws IOException {return Long.parseLong(next());} public static double nextDouble() throws IOException {return Double.parseDouble(next());} public static char nextChar() throws IOException {return next().toCharArray()[0];} public static String nextString() throws IOException {return next();} public static String nextLine() throws IOException {return reader.readLine();} } }
ConDefects/ConDefects/Code/abc326_f/Java/47080973
condefects-java_data_349
import java.util.Scanner; public class Main{ public static void main(String...args) { Scanner sc = new Scanner(System.in); final char[] string = sc.next().toCharArray(); //文字列をi列目まで見たとき(個がj個ある int[][] dp=new int[string.length+1][string.length/2+1]; dp[0][0]=1; final int M=string.length/2; for(int i=1;i<=string.length;i++){ for(int j=0;j<M;j++){ switch (string[i-1]){ case '(':{ if(0<j){ dp[i][j]=(dp[i-1][j-1]+dp[i][j])%998244353; } break; } case ')':{ if(j<M-1){ dp[i][j]=(dp[i-1][j+1]+dp[i][j])%998244353; } break; } default:{ if(j<M-1){ dp[i][j]=(dp[i-1][j+1]+dp[i][j])%998244353; } if(0<j){ dp[i][j]=(dp[i-1][j-1]+dp[i][j])%998244353; } } } } } System.out.println(dp[string.length][0]); } } import java.util.Scanner; public class Main{ public static void main(String...args) { Scanner sc = new Scanner(System.in); final char[] string = sc.next().toCharArray(); //文字列をi列目まで見たとき(個がj個ある int[][] dp=new int[string.length+1][string.length/2+1]; dp[0][0]=1; final int M=string.length/2+1; for(int i=1;i<=string.length;i++){ for(int j=0;j<M;j++){ switch (string[i-1]){ case '(':{ if(0<j){ dp[i][j]=(dp[i-1][j-1]+dp[i][j])%998244353; } break; } case ')':{ if(j<M-1){ dp[i][j]=(dp[i-1][j+1]+dp[i][j])%998244353; } break; } default:{ if(j<M-1){ dp[i][j]=(dp[i-1][j+1]+dp[i][j])%998244353; } if(0<j){ dp[i][j]=(dp[i-1][j-1]+dp[i][j])%998244353; } } } } } System.out.println(dp[string.length][0]); } }
ConDefects/ConDefects/Code/abc312_d/Java/44080512
condefects-java_data_350
import java.util.*; import java.util.stream.*; import java.io.*; public class Main { public static void main(String[] args) { // 自分の得意な言語で // Let's チャレンジ!! FastScanner sc = new FastScanner(System.in); Map<Long,List<Long>> map = new HashMap<>(); Queue<Long> que = new ArrayDeque<>(); int a = sc.nextInt(); int N = sc.nextInt(); long g = 0; long x = 1; int memo[] = new int[10000000]; Arrays.fill(memo,-1); StringBuilder sb = new StringBuilder(); que.add(x); memo[1] = 0; while(que.size() > 0){ x = que.poll(); if(String.valueOf(x).length() <= String.valueOf(N).length()){ if(map.get(x) == null){ map.put(x,new ArrayList<>()); g = x * a; if(g < N){ map.get(x).add(g); if(memo[(int)g] == -1){ memo[(int)g] = memo[(int)x] + 1; }else{ if(memo[(int)g] > memo[(int)x] + 1){ memo[(int)g] = memo[(int)x] + 1; } } que.add(g); } if(x > 10 && (x % 10 != 0)){ sb = new StringBuilder(); sb.append(String.valueOf(x)); sb.insert(0,sb.charAt(sb.length() - 1)); sb.delete(sb.length() - 1, sb.length()); map.get(x).add(Long.parseLong(sb.toString())); if(memo[Integer.parseInt(sb.toString())] == -1){ memo[Integer.parseInt(sb.toString())] = memo[(int)x] + 1; }else{ if(memo[Integer.parseInt(sb.toString())] > memo[(int)x] + 1){ memo[Integer.parseInt(sb.toString())] = memo[(int)x] + 1; } } que.add(Long.parseLong(sb.toString())); } } } } System.out.println(memo[N]); } } class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; public FastScanner(InputStream in2) { } private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;} private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;} public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();} public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while(true){ if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next());} } import java.util.*; import java.util.stream.*; import java.io.*; public class Main { public static void main(String[] args) { // 自分の得意な言語で // Let's チャレンジ!! FastScanner sc = new FastScanner(System.in); Map<Long,List<Long>> map = new HashMap<>(); Queue<Long> que = new ArrayDeque<>(); int a = sc.nextInt(); int N = sc.nextInt(); long g = 0; long x = 1; int memo[] = new int[10000000]; Arrays.fill(memo,-1); StringBuilder sb = new StringBuilder(); que.add(x); memo[1] = 0; while(que.size() > 0){ x = que.poll(); if(String.valueOf(x).length() <= String.valueOf(N).length()){ if(map.get(x) == null){ map.put(x,new ArrayList<>()); g = x * a; if(g <= N * 5){ map.get(x).add(g); if(memo[(int)g] == -1){ memo[(int)g] = memo[(int)x] + 1; }else{ if(memo[(int)g] > memo[(int)x] + 1){ memo[(int)g] = memo[(int)x] + 1; } } que.add(g); } if(x > 10 && (x % 10 != 0)){ sb = new StringBuilder(); sb.append(String.valueOf(x)); sb.insert(0,sb.charAt(sb.length() - 1)); sb.delete(sb.length() - 1, sb.length()); map.get(x).add(Long.parseLong(sb.toString())); if(memo[Integer.parseInt(sb.toString())] == -1){ memo[Integer.parseInt(sb.toString())] = memo[(int)x] + 1; }else{ if(memo[Integer.parseInt(sb.toString())] > memo[(int)x] + 1){ memo[Integer.parseInt(sb.toString())] = memo[(int)x] + 1; } } que.add(Long.parseLong(sb.toString())); } } } } System.out.println(memo[N]); } } class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; public FastScanner(InputStream in2) { } private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;} private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;} public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();} public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while(true){ if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next());} }
ConDefects/ConDefects/Code/abc235_d/Java/31611189
condefects-java_data_351
import java.util.*; public class Main { public static void main(String[] args) throws Exception { // Your code here! Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int C = sc.nextInt(); long[] Array = new long[N]; long frag = 1; if (C < 0) frag *= -1; for (int i = 0; i < N; i++) { Array[i] = sc.nextInt() * frag; } // 累積和の配列 long[] sumArray = new long[N+1]; // indexまでの累積和の最小値を記録する配列 long[] sumMinArray = new long[N+1]; // 累積和の計算 for (int i = 1; i < N+1; i++) { // 累積和の計算 sumArray[i] = sumArray[i-1] + Array[i-1]; // iまでの累積和の最小値 sumMinArray[i] = Math.min(sumArray[i], sumMinArray[i-1]); } long partSum = sumArray[N]; for (int i = 1; i < N+1; i++) { long sub = sumArray[i] - sumMinArray[i]; partSum = Math.max(partSum, sub); } System.out.println((partSum * (C-1) + sumArray[N]) * frag); // // 尺取り法 // for (int l = 0; l < N+1; l++) { // for (int r = 0; r < N+1; r++) { // } // } } } import java.util.*; public class Main { public static void main(String[] args) throws Exception { // Your code here! Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int C = sc.nextInt(); long[] Array = new long[N]; long frag = 1; if (C <= 0) frag *= -1; for (int i = 0; i < N; i++) { Array[i] = sc.nextInt() * frag; } // 累積和の配列 long[] sumArray = new long[N+1]; // indexまでの累積和の最小値を記録する配列 long[] sumMinArray = new long[N+1]; // 累積和の計算 for (int i = 1; i < N+1; i++) { // 累積和の計算 sumArray[i] = sumArray[i-1] + Array[i-1]; // iまでの累積和の最小値 sumMinArray[i] = Math.min(sumArray[i], sumMinArray[i-1]); } long partSum = sumArray[N]; for (int i = 1; i < N+1; i++) { long sub = sumArray[i] - sumMinArray[i]; partSum = Math.max(partSum, sub); } System.out.println((partSum * (C-1) + sumArray[N]) * frag); // // 尺取り法 // for (int l = 0; l < N+1; l++) { // for (int r = 0; r < N+1; r++) { // } // } } }
ConDefects/ConDefects/Code/arc174_a/Java/54243786
condefects-java_data_352
import java.util.Arrays; import java.util.Collections; import java.util.LinkedList; import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int c = sc.nextInt(); int a[] = Arrays.stream(new int[n]).map(e -> sc.nextInt()).toArray(); long sum = Arrays.stream(a).sum(); long dpMax[] = new long[n]; long dpMin[] = new long[n]; dpMax[0] = dpMin[0] = a[0]; for(int i = 1; i < n; i++) { dpMax[i] = Math.max(dpMax[i - 1] + a[i], a[i]); dpMin[i] = Math.min(dpMin[i - 1] + a[i], a[i]); // System.out.println("min " + dpMin[i]); } long result = sum; if(c > 0) { long max = Arrays.stream(dpMax).max().getAsLong(); if(max > 0) { result = sum + (c - 1) * max; } } else { long min = Arrays.stream(dpMin).min().getAsLong(); // System.out.println(sum); // System.out.println(min); if(min <= 0) { result = sum + (c - 1) * min; } } System.out.println(result); } } import java.util.Arrays; import java.util.Collections; import java.util.LinkedList; import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int c = sc.nextInt(); long a[] = Arrays.stream(new long[n]).map(e -> sc.nextLong()).toArray(); long sum = Arrays.stream(a).sum(); long dpMax[] = new long[n]; long dpMin[] = new long[n]; dpMax[0] = dpMin[0] = a[0]; for(int i = 1; i < n; i++) { dpMax[i] = Math.max(dpMax[i - 1] + a[i], a[i]); dpMin[i] = Math.min(dpMin[i - 1] + a[i], a[i]); // System.out.println("min " + dpMin[i]); } long result = sum; if(c > 0) { long max = Arrays.stream(dpMax).max().getAsLong(); if(max > 0) { result = sum + (c - 1) * max; } } else { long min = Arrays.stream(dpMin).min().getAsLong(); // System.out.println(sum); // System.out.println(min); if(min <= 0) { result = sum + (c - 1) * min; } } System.out.println(result); } }
ConDefects/ConDefects/Code/arc174_a/Java/51410124
condefects-java_data_353
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.TreeMap; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] sa = br.readLine().split(" "); int n = Integer.parseInt(sa[0]); int k = Integer.parseInt(sa[1]); sa = br.readLine().split(" "); long[] a = new long[n + 1]; for (int i = 0; i < n; i++) { a[i] = Long.parseLong(sa[i]); } br.close(); long inf = 1000000000000000000L; a[n] = inf; Arrays.sort(a); TreeMap<Long, Long> map = new TreeMap<>(); map.put(0L, 0L); for (int i = 0; i < n; i++) { Long[] arr = map.keySet().toArray(new Long[0]); for (Long e1 : arr) { long e2 = e1 + a[i]; long v1 = map.get(e1); map.put(e2, Math.max(v1 + a[i], map.getOrDefault(e2, 0L))); } long x = 0; while (true) { Long k1 = map.ceilingKey(x); Long k2 = map.higherKey(x); if (k2 == null) { break; } if (map.get(k1) + 1 >= k2) { map.put(k1, Math.max(map.get(k1), map.get(k2))); map.remove(k2); } else { x = k2; } } List<Long> ans = new ArrayList<>(); x = 0; while (x < a[i + 1] && ans.size() < k) { Long k1 = map.ceilingKey(x); long v1 = map.get(k1); Long k2 = map.higherKey(v1); long lim = a[i + 1]; if (k2 != null) { lim = Math.min(lim, k2); x = k2; } else { x = a[i + 1]; } for (long j = v1 + 1; j < lim && ans.size() < k; j++) { ans.add(j); } } if (ans.size() >= k) { out(ans, k); return; } } } static void out(List<Long> ans, int k) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < k; i++) { sb.append(ans.get(i)).append(' '); } sb.deleteCharAt(sb.length() - 1); System.out.println(sb.toString()); } } import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.TreeMap; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] sa = br.readLine().split(" "); int n = Integer.parseInt(sa[0]); int k = Integer.parseInt(sa[1]); sa = br.readLine().split(" "); long[] a = new long[n + 1]; for (int i = 0; i < n; i++) { a[i] = Long.parseLong(sa[i]); } br.close(); long inf = 1000000000000000000L; a[n] = inf; Arrays.sort(a); TreeMap<Long, Long> map = new TreeMap<>(); map.put(0L, 0L); for (int i = 0; i < n; i++) { Long[] arr = map.descendingKeySet().toArray(new Long[0]); for (Long e1 : arr) { long e2 = e1 + a[i]; long v1 = map.get(e1); map.put(e2, Math.max(v1 + a[i], map.getOrDefault(e2, 0L))); } long x = 0; while (true) { Long k1 = map.ceilingKey(x); Long k2 = map.higherKey(x); if (k2 == null) { break; } if (map.get(k1) + 1 >= k2) { map.put(k1, Math.max(map.get(k1), map.get(k2))); map.remove(k2); } else { x = k2; } } List<Long> ans = new ArrayList<>(); x = 0; while (x < a[i + 1] && ans.size() < k) { Long k1 = map.ceilingKey(x); long v1 = map.get(k1); Long k2 = map.higherKey(v1); long lim = a[i + 1]; if (k2 != null) { lim = Math.min(lim, k2); x = k2; } else { x = a[i + 1]; } for (long j = v1 + 1; j < lim && ans.size() < k; j++) { ans.add(j); } } if (ans.size() >= k) { out(ans, k); return; } } } static void out(List<Long> ans, int k) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < k; i++) { sb.append(ans.get(i)).append(' '); } sb.deleteCharAt(sb.length() - 1); System.out.println(sb.toString()); } }
ConDefects/ConDefects/Code/agc062_c/Java/41612340
condefects-java_data_354
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; /** * @author YC * @version 1.0 */ public class Main { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static char[][][] g = new char[4][5][5]; // 第一维表示是哪个矩形, static char[][][] temp = new char[4][5][5]; // 临时图, 防止原图被修改 static int[] tx = new int[4]; // 记录移动变量 static int[] ty = new int[4]; static int[][] v = new int[10][10]; // 一张新图 用来判断答案是否正确 static char[][][] op = new char[4][5][5]; // 暂时记录某一种合法情况 因为旋转会改变原数组 static boolean flag = false; public static void main(String[] args) throws IOException { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 4; j++) { char[] ch = br.readLine().toCharArray(); for (int k = 1; k <= 4; k++) { g[i][j][k] = ch[k - 1]; } } } dfs(1); System.out.println(flag ? "Yes" : "No"); } public static boolean check() { for(int i = 1 ; i <= 4 ; i ++) { for(int j = 1 ; j <= 4 ; j ++) { if(v[i][j] != 1) return false; } } return true; } public static void init() { for(int i = 1 ; i <= 4 ; i ++) { Arrays.fill(v[i],0); } for(int k = 1 ; k <= 3 ; k ++) { for (int i = 1; i <= 4; i++) { for (int j = 1; j <= 4; j++) { int newX = i + tx[k]; int newY = j + ty[k]; if(newX < 0 || newY < 0) return; v[newX][newY] += op[k][i][j] == '#' ? 1 : 0; } } } } // u 代表现在正在移动第u个的方块 public static void dfs(int u) { if(flag) return; if (u > 3) { init(); if(check()) { flag = true; } return; } for(int i = 1 ; i <= 4 ; i ++) { temp[u][i] = g[u][i].clone(); } // 枚举旋转次数 for (int t = 1; t <= 3; t++) { // 枚举移动距离 for (int i = -3; i <= 3; i++) { for (int j = -3; j <= 3; j++) { // 如果移动合法 if (move(u, i, j)) { // 存储当前图 for(int x = 1 ; x <= 4 ; x ++) { op[u][x] = temp[u][x].clone(); } // 记录偏移量 tx[u] = i; ty[u] = j; dfs(u + 1); } } } // 进行旋转 circle(u); } } public static void circle(int u) { char[][] t = new char[5][5]; for(int i = 1; i <= 4 ; i ++) { t[i] = temp[u][i].clone(); } for (int i = 1; i <= 4; i++) { for (int j = 1; j <= 4; j++) { // temp[u][i][j] = t[4-j+1][i];//旋转 temp[u][j][4 - i + 1] = t[i][j]; // temp[u][i][j] = t[4 - j + 1][i]; } } } // 查看移动是否合法 x 表示x方向移动的距离 public static boolean move(int u, int x, int y) { for (int i = 1; i <= 4; i++) { for (int j = 1; j <= 4; j++) { // 只关心为#的移动 如果移动不合法 return false if (temp[u][i][j] == '#' && !(i + x >= 1 && i + x <= 4 && j + y <= 4 && j + y >= 1)) return false; } } return true; } } import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; /** * @author YC * @version 1.0 */ public class Main { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static char[][][] g = new char[4][5][5]; // 第一维表示是哪个矩形, static char[][][] temp = new char[4][5][5]; // 临时图, 防止原图被修改 static int[] tx = new int[4]; // 记录移动变量 static int[] ty = new int[4]; static int[][] v = new int[10][10]; // 一张新图 用来判断答案是否正确 static char[][][] op = new char[4][5][5]; // 暂时记录某一种合法情况 因为旋转会改变原数组 static boolean flag = false; public static void main(String[] args) throws IOException { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 4; j++) { char[] ch = br.readLine().toCharArray(); for (int k = 1; k <= 4; k++) { g[i][j][k] = ch[k - 1]; } } } dfs(1); System.out.println(flag ? "Yes" : "No"); } public static boolean check() { for(int i = 1 ; i <= 4 ; i ++) { for(int j = 1 ; j <= 4 ; j ++) { if(v[i][j] != 1) return false; } } return true; } public static void init() { for(int i = 1 ; i <= 4 ; i ++) { Arrays.fill(v[i],0); } for(int k = 1 ; k <= 3 ; k ++) { for (int i = 1; i <= 4; i++) { for (int j = 1; j <= 4; j++) { int newX = i + tx[k]; int newY = j + ty[k]; if(newX < 0 || newY < 0) return; v[newX][newY] += op[k][i][j] == '#' ? 1 : 0; } } } } // u 代表现在正在移动第u个的方块 public static void dfs(int u) { if(flag) return; if (u > 3) { init(); if(check()) { flag = true; } return; } for(int i = 1 ; i <= 4 ; i ++) { temp[u][i] = g[u][i].clone(); } // 枚举旋转次数 for (int t = 1; t <= 4; t++) { // 枚举移动距离 for (int i = -3; i <= 3; i++) { for (int j = -3; j <= 3; j++) { // 如果移动合法 if (move(u, i, j)) { // 存储当前图 for(int x = 1 ; x <= 4 ; x ++) { op[u][x] = temp[u][x].clone(); } // 记录偏移量 tx[u] = i; ty[u] = j; dfs(u + 1); } } } // 进行旋转 circle(u); } } public static void circle(int u) { char[][] t = new char[5][5]; for(int i = 1; i <= 4 ; i ++) { t[i] = temp[u][i].clone(); } for (int i = 1; i <= 4; i++) { for (int j = 1; j <= 4; j++) { // temp[u][i][j] = t[4-j+1][i];//旋转 temp[u][j][4 - i + 1] = t[i][j]; // temp[u][i][j] = t[4 - j + 1][i]; } } } // 查看移动是否合法 x 表示x方向移动的距离 public static boolean move(int u, int x, int y) { for (int i = 1; i <= 4; i++) { for (int j = 1; j <= 4; j++) { // 只关心为#的移动 如果移动不合法 return false if (temp[u][i][j] == '#' && !(i + x >= 1 && i + x <= 4 && j + y <= 4 && j + y >= 1)) return false; } } return true; } }
ConDefects/ConDefects/Code/abc322_d/Java/53516314
condefects-java_data_355
import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner scn = new Scanner(System.in); int num = scn.nextInt(); if(num%5 < 2) { System.out.println((num/5)*5); } else if(num%5>2) { System.out.println((num/5)*5+5); }else if(num%5==0) { System.out.println(num); } scn.close(); } } import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner scn = new Scanner(System.in); int num = scn.nextInt(); if(num%5 <= 2) { System.out.println((num/5)*5); } else if(num%5>2) { System.out.println((num/5)*5+5); }else if(num%5==0) { System.out.println(num); } scn.close(); } }
ConDefects/ConDefects/Code/abc305_a/Java/43271170
condefects-java_data_356
import java.lang.reflect.Array; import java.math.BigInteger; import java.util.*; public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); int rem = n % 5; int ans = n; if(rem >= 0 & rem < 3) { ans = ans - rem ; }else { ans = ans + rem - 1; } System.out.println(ans); } } import java.lang.reflect.Array; import java.math.BigInteger; import java.util.*; public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); int rem = n % 5; int ans = n; if(rem >= 0 & rem < 3) { ans = ans - rem ; }else { ans = ans - rem + 5; } System.out.println(ans); } }
ConDefects/ConDefects/Code/abc305_a/Java/43000556
condefects-java_data_357
import java.util.*; public class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); int n = in.nextInt(); double dn = (double)n; dn *= 0.2; int ans = (int)Math.round(dn); n = (int)(dn*5); System.out.println(ans); } } import java.util.*; public class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); int n = in.nextInt(); double dn = (double)n; dn *= 0.2; int ans = (int)Math.round(dn); ans *= 5; System.out.println(ans); } }
ConDefects/ConDefects/Code/abc305_a/Java/44587604
condefects-java_data_358
import java.util.*; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n=sc.nextInt(); int ns=n%5; if(ns==1 || ns==2){ System.out.println(n-ns); }else System.out.println(n+(5-ns)); } } import java.util.*; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n=sc.nextInt(); int ns=n%5; if(ns==0 || ns==1 || ns==2){ System.out.println(n-ns); }else System.out.println(n+(5-ns)); } }
ConDefects/ConDefects/Code/abc305_a/Java/43769649
condefects-java_data_359
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { Kattio io = new Kattio(); int n = io.nextInt(), ans = -1, dist = Integer.MAX_VALUE; for (int i = 1; i <= 20; i++) { int curr = 5 * i, d = Math.abs(curr - n); if (d < dist) { ans = curr; dist = d; } } io.println(ans); io.close(); } private static class Kattio extends PrintWriter { private BufferedReader br; private StringTokenizer st; private String line, token; public Kattio() { this(System.in, System.out); } public Kattio(InputStream i, OutputStream o) { super(o); br = new BufferedReader(new InputStreamReader(i)); } public boolean hasMoreTokens() { return peekToken() != null; } public String next() { return nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public double nextDouble() { return Double.parseDouble(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } private String peekToken() { if (token == null) try { while (st == null || !st.hasMoreTokens()) { line = br.readLine(); if (line == null) return null; st = new StringTokenizer(line); } token = st.nextToken(); } catch (IOException e) { } return token; } private String nextToken() { String ans = peekToken(); token = null; return ans; } } } import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { Kattio io = new Kattio(); int n = io.nextInt(), ans = -1, dist = Integer.MAX_VALUE; for (int i = 0; i <= 20; i++) { int curr = 5 * i, d = Math.abs(curr - n); if (d < dist) { ans = curr; dist = d; } } io.println(ans); io.close(); } private static class Kattio extends PrintWriter { private BufferedReader br; private StringTokenizer st; private String line, token; public Kattio() { this(System.in, System.out); } public Kattio(InputStream i, OutputStream o) { super(o); br = new BufferedReader(new InputStreamReader(i)); } public boolean hasMoreTokens() { return peekToken() != null; } public String next() { return nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public double nextDouble() { return Double.parseDouble(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } private String peekToken() { if (token == null) try { while (st == null || !st.hasMoreTokens()) { line = br.readLine(); if (line == null) return null; st = new StringTokenizer(line); } token = st.nextToken(); } catch (IOException e) { } return token; } private String nextToken() { String ans = peekToken(); token = null; return ans; } } }
ConDefects/ConDefects/Code/abc305_a/Java/43035984
condefects-java_data_360
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int point = sc.nextInt(); if (point % 5 >= 3) { System.out.println((point - 1) + point % 5); } else { System.out.println(point - point % 5); } sc.close(); } } import java.util.Scanner; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int point = sc.nextInt(); if (point % 5 >= 3) { System.out.println(point + (5 - point % 5)); } else { System.out.println(point - point % 5); } sc.close(); } }
ConDefects/ConDefects/Code/abc305_a/Java/43012650
condefects-java_data_361
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); if(n / 5 == 0){ System.out.println(n); }else { int tmp = n % 5; if(tmp == 1){ System.out.println(n - 1); }else if(tmp == 2){ System.out.println(n - 2); }else if(tmp == 3){ System.out.println(n + 2); }else if(tmp == 4){ System.out.println(n + 1); } } } } import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); if(n % 5 == 0){ System.out.println(n); }else { int tmp = n % 5; if(tmp == 1){ System.out.println(n - 1); }else if(tmp == 2){ System.out.println(n - 2); }else if(tmp == 3){ System.out.println(n + 2); }else if(tmp == 4){ System.out.println(n + 1); } } } }
ConDefects/ConDefects/Code/abc305_a/Java/45398018
condefects-java_data_362
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int a=n%5; if(a<2.5){ System.out.println(5*(n/5)); } else if(a>2.5){ System.out.println(5*(n/5+124)); } } } import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int a=n%5; if(a<2.5){ System.out.println(5*(n/5)); } else if(a>2.5){ System.out.println(5*(n/5+1)); } } }
ConDefects/ConDefects/Code/abc305_a/Java/42366996
condefects-java_data_363
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); System.out.println(((N/5)*5)); sc.close(); } } import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); System.out.println((Math.round(N/5.0)*5)); sc.close(); } }
ConDefects/ConDefects/Code/abc305_a/Java/43627742
condefects-java_data_364
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); double n = Integer.parseInt(sc.next()); if(n % 5 == 0) { System.out.println(n); return; } else { System.out.println(Math.round(n / 5.0) * 5); } } } import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); double n = Integer.parseInt(sc.next()); if(n % 5 == 0) { System.out.println((int)n); return; } else { System.out.println(Math.round(n / 5.0) * 5); } } }
ConDefects/ConDefects/Code/abc305_a/Java/43288246
condefects-java_data_365
import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); String s = sc.next(); String t = sc.next(); if(0<s.indexOf(t)){ System.out.print("Yes"); }else{ System.out.print("No"); } } } import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); String s = sc.next(); String t = sc.next(); if(0<=s.indexOf(t)){ System.out.print("Yes"); }else{ System.out.print("No"); } } }
ConDefects/ConDefects/Code/abc279_b/Java/37234654
condefects-java_data_366
import java.util.Scanner; class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); RollingHash S = new RollingHash(sc.next()); RollingHash T = new RollingHash(sc.next()); int lenS = S.length(); int lenT = T.length(); boolean isContain = false; for(int i=0;i<=lenS-lenT;i++) isContain |= S.equals(T,i+1,i+lenT,1,lenT); System.out.println(isContain?"Yes":"No"); } } final class RollingHash implements Comparable<RollingHash>{ private static final int base = 100; private static final int mod1 = 1_000_000_007; private static final int mod2 = Integer.MAX_VALUE-1; private long[] hash1,hash2; private String string; public RollingHash(String str){ string = str; hash1 = new long[str.length()+1]; hash2 = new long[str.length()+1]; roll(); } private void roll(){ int len = string.length(); for(int i=1;i<=len;i++){ hash1[i] = hash1[i-1]*base+string.charAt(i-1)-' '+1; hash2[i] = hash2[i-1]*base+string.charAt(i-1)-' '+1; hash1[i] %= mod1; hash2[i] %= mod2; } } public long getHash1(int l,int r){ return (hash1[r]-hash1[l]*modPow(base,r-l,mod1)%mod1+mod1)%mod1; } public long getHash2(int l,int r){ return (hash2[r]-hash2[l]*modPow(base,r-l,mod2)%mod2+mod2)%mod2; } private long modPow(long a,long b,long mod){ a %= mod; b %= mod-1; long ans = 1; while(b>0){ if((b&1)==1){ ans *= a; ans %= mod; } a *= a; a %= mod; b >>= 1; } return ans; } public boolean equals(RollingHash rh,int l1,int r1,int l2,int r2){ if(r1-l1!=r2-l2) return false; long hashValue1 = getHash1(l1,r1); long hashValue2 = getHash2(l1,r1); return hashValue1==rh.getHash1(l2,r2) &&hashValue2==rh.getHash2(l2,r2) &&check(rh,l1,l2,r1-l1); } private boolean check(RollingHash rh,int l1,int l2,int len){ return check(rh.toString(),l1,l2,len); } private boolean check(String str,int l1,int l2,int len){ for(int i=0;i<len;i++) if(string.charAt(l1+i)!=str.charAt(l2+i)) return false; return true; } public int length(){ return string.length(); } @Override public int hashCode(){ return string.hashCode(); } @Override public String toString(){ return string; } @Override public boolean equals(Object o){ if(o instanceof RollingHash){ RollingHash rh = (RollingHash)o; return equals(rh,1,length(),1,rh.length()); } return false; } @Override public int compareTo(RollingHash rh){ return string.compareTo(rh.toString()); } public int compareTo(String str){ return string.compareTo(str); } public char charAt(int i){ return string.charAt(i); } public int compareToIgnoreCase(RollingHash rh){ return string.compareToIgnoreCase(rh.toString()); } public int compareToIgnoreCase(String str){ return string.compareToIgnoreCase(str); } public void concat(RollingHash rh){ concat(rh.toString()); } public void concat(String str){ string.concat(str); hash1 = new long[string.length()+1]; hash2 = new long[string.length()+1]; roll(); } public boolean contains(RollingHash rh){ long hash1 = rh.getHash1(0,rh.length()); long hash2 = rh.getHash2(0,rh.length()); boolean isContain = false; int len = length()-rh.length(); for(int i=0;i<=len;i++){ if(hash1==getHash1(i,rh.length()+i) &&hash2==getHash2(i,rh.length()+i)) isContain |= check(rh,i,0,rh.length()); } return isContain; } public boolean contains(String str){ return indexOf(str)!=-1; } public int indexOf(int ch){ return indexOf(ch,0); } public int indexOf(int ch,int fromIndex){ int len = length(); for(int i=fromIndex;i<len;i++) if(string.charAt(i)==ch) return i; return -1; } public int indexOf(String str){ return indexOf(str,0); } public int indexOf(String str,int fromIndex){ long hash1 = 0; long hash2 = 0; for(char c:str.toCharArray()){ hash1 = hash1*base + c-' '+1; hash2 = hash2*base + c-' '+1; hash1 %= mod1; hash2 %= mod2; } boolean isContain = false; int len = length()-str.length(); for(int i=fromIndex;i<=len;i++){ if(hash1==getHash1(i,str.length()+i) &&hash2==getHash2(i,str.length()+i) &&check(str,i,0,str.length())) return i; } return -1; } public boolean isEmpty(){ return length()==0; } public int lastIndexOf(int ch,int fromIndex){ for(int i=fromIndex;i>=0;i--) if(string.charAt(i)==ch) return i; return -1; } public int lastIndexOf(int ch){ return lastIndexOf(ch,length()-1); } public static RollingHash valueOf(boolean b){ return new RollingHash(b?"true":"false"); } public static RollingHash valueOf(char c){ return new RollingHash(""+c); } public static RollingHash valueOf(char[] c){ return new RollingHash(String.valueOf(c,0,c.length)); } public static RollingHash valueOf(char[] c,int offset,int count){ return new RollingHash(String.valueOf(c,offset,count)); } public static RollingHash valueOf(double d){ return new RollingHash(String.valueOf(d)); } public static RollingHash valueOf(float f){ return new RollingHash(String.valueOf(f)); } public static RollingHash valueOf(int i){ return new RollingHash(String.valueOf(i)); } public static RollingHash valueOf(long l){ return new RollingHash(String.valueOf(l)); } public static RollingHash valueOf(Object obj){ return new RollingHash(String.valueOf(obj)); } } import java.util.Scanner; class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); RollingHash S = new RollingHash(sc.next()); RollingHash T = new RollingHash(sc.next()); int lenS = S.length(); int lenT = T.length(); boolean isContain = false; for(int i=0;i<=lenS-lenT;i++) isContain |= S.equals(T,i,i+lenT,0,lenT); System.out.println(isContain?"Yes":"No"); } } final class RollingHash implements Comparable<RollingHash>{ private static final int base = 100; private static final int mod1 = 1_000_000_007; private static final int mod2 = Integer.MAX_VALUE-1; private long[] hash1,hash2; private String string; public RollingHash(String str){ string = str; hash1 = new long[str.length()+1]; hash2 = new long[str.length()+1]; roll(); } private void roll(){ int len = string.length(); for(int i=1;i<=len;i++){ hash1[i] = hash1[i-1]*base+string.charAt(i-1)-' '+1; hash2[i] = hash2[i-1]*base+string.charAt(i-1)-' '+1; hash1[i] %= mod1; hash2[i] %= mod2; } } public long getHash1(int l,int r){ return (hash1[r]-hash1[l]*modPow(base,r-l,mod1)%mod1+mod1)%mod1; } public long getHash2(int l,int r){ return (hash2[r]-hash2[l]*modPow(base,r-l,mod2)%mod2+mod2)%mod2; } private long modPow(long a,long b,long mod){ a %= mod; b %= mod-1; long ans = 1; while(b>0){ if((b&1)==1){ ans *= a; ans %= mod; } a *= a; a %= mod; b >>= 1; } return ans; } public boolean equals(RollingHash rh,int l1,int r1,int l2,int r2){ if(r1-l1!=r2-l2) return false; long hashValue1 = getHash1(l1,r1); long hashValue2 = getHash2(l1,r1); return hashValue1==rh.getHash1(l2,r2) &&hashValue2==rh.getHash2(l2,r2) &&check(rh,l1,l2,r1-l1); } private boolean check(RollingHash rh,int l1,int l2,int len){ return check(rh.toString(),l1,l2,len); } private boolean check(String str,int l1,int l2,int len){ for(int i=0;i<len;i++) if(string.charAt(l1+i)!=str.charAt(l2+i)) return false; return true; } public int length(){ return string.length(); } @Override public int hashCode(){ return string.hashCode(); } @Override public String toString(){ return string; } @Override public boolean equals(Object o){ if(o instanceof RollingHash){ RollingHash rh = (RollingHash)o; return equals(rh,1,length(),1,rh.length()); } return false; } @Override public int compareTo(RollingHash rh){ return string.compareTo(rh.toString()); } public int compareTo(String str){ return string.compareTo(str); } public char charAt(int i){ return string.charAt(i); } public int compareToIgnoreCase(RollingHash rh){ return string.compareToIgnoreCase(rh.toString()); } public int compareToIgnoreCase(String str){ return string.compareToIgnoreCase(str); } public void concat(RollingHash rh){ concat(rh.toString()); } public void concat(String str){ string.concat(str); hash1 = new long[string.length()+1]; hash2 = new long[string.length()+1]; roll(); } public boolean contains(RollingHash rh){ long hash1 = rh.getHash1(0,rh.length()); long hash2 = rh.getHash2(0,rh.length()); boolean isContain = false; int len = length()-rh.length(); for(int i=0;i<=len;i++){ if(hash1==getHash1(i,rh.length()+i) &&hash2==getHash2(i,rh.length()+i)) isContain |= check(rh,i,0,rh.length()); } return isContain; } public boolean contains(String str){ return indexOf(str)!=-1; } public int indexOf(int ch){ return indexOf(ch,0); } public int indexOf(int ch,int fromIndex){ int len = length(); for(int i=fromIndex;i<len;i++) if(string.charAt(i)==ch) return i; return -1; } public int indexOf(String str){ return indexOf(str,0); } public int indexOf(String str,int fromIndex){ long hash1 = 0; long hash2 = 0; for(char c:str.toCharArray()){ hash1 = hash1*base + c-' '+1; hash2 = hash2*base + c-' '+1; hash1 %= mod1; hash2 %= mod2; } boolean isContain = false; int len = length()-str.length(); for(int i=fromIndex;i<=len;i++){ if(hash1==getHash1(i,str.length()+i) &&hash2==getHash2(i,str.length()+i) &&check(str,i,0,str.length())) return i; } return -1; } public boolean isEmpty(){ return length()==0; } public int lastIndexOf(int ch,int fromIndex){ for(int i=fromIndex;i>=0;i--) if(string.charAt(i)==ch) return i; return -1; } public int lastIndexOf(int ch){ return lastIndexOf(ch,length()-1); } public static RollingHash valueOf(boolean b){ return new RollingHash(b?"true":"false"); } public static RollingHash valueOf(char c){ return new RollingHash(""+c); } public static RollingHash valueOf(char[] c){ return new RollingHash(String.valueOf(c,0,c.length)); } public static RollingHash valueOf(char[] c,int offset,int count){ return new RollingHash(String.valueOf(c,offset,count)); } public static RollingHash valueOf(double d){ return new RollingHash(String.valueOf(d)); } public static RollingHash valueOf(float f){ return new RollingHash(String.valueOf(f)); } public static RollingHash valueOf(int i){ return new RollingHash(String.valueOf(i)); } public static RollingHash valueOf(long l){ return new RollingHash(String.valueOf(l)); } public static RollingHash valueOf(Object obj){ return new RollingHash(String.valueOf(obj)); } }
ConDefects/ConDefects/Code/abc279_b/Java/38919108
condefects-java_data_367
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner scan = new Scanner(System.in); String s = scan.next(); String t = scan.next(); if(s.matches(".*t.*$")){ System.out.println("Yes"); }else{ System.out.println("No"); } } } import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner scan = new Scanner(System.in); String s = scan.next(); String t = scan.next(); if(s.contains(t)){ System.out.println("Yes"); }else{ System.out.println("No"); } } }
ConDefects/ConDefects/Code/abc279_b/Java/41994888
condefects-java_data_368
import java.util.*; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.next(), t = sc.next(); sc.close(); if (s.equals(t)) { System.out.println("Yes"); System.exit(0); } for (int i = 0; i < s.length()-t.length(); i++) { String tmp = s.substring(i, i+t.length()); if (tmp.equals(t)) { System.out.println("Yes"); System.exit(0); } else { continue; } } System.out.println("No"); } } import java.util.*; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.next(), t = sc.next(); sc.close(); if (s.equals(t)) { System.out.println("Yes"); System.exit(0); } for (int i = 0; i <= s.length()-t.length(); i++) { String tmp = s.substring(i, i+t.length()); if (tmp.equals(t)) { System.out.println("Yes"); System.exit(0); } else { continue; } } System.out.println("No"); } }
ConDefects/ConDefects/Code/abc279_b/Java/40962045
condefects-java_data_369
import java.util.*; public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); String s1 = s.next(); String s2 = s.next(); int n = s1.length(), m = s2.length(); if(n < m) { System.out.println("No"); } for(int i = 0; i < n ; i++) { if(i + m > n) continue; String cmp = s1.substring(i, i + m); // System.out.println(cmp); if(cmp.equals(s2)) { System.out.println("Yes"); return; } } System.out.println("No"); } } import java.util.*; public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); String s1 = s.next(); String s2 = s.next(); int n = s1.length(), m = s2.length(); if(n < m) { System.out.println("No"); return; } for(int i = 0; i < n ; i++) { if(i + m > n) continue; String cmp = s1.substring(i, i + m); if(cmp.equals(s2)) { System.out.println("Yes"); return; } } System.out.println("No"); } }
ConDefects/ConDefects/Code/abc279_b/Java/43084389
condefects-java_data_370
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc1 = new Scanner(System.in); String nums = sc1.nextLine(); String nums2 = sc1.nextLine(); int a = nums.indexOf(nums2); String answer = "No"; if(a < 0) { answer = "Yes"; } System.out.println(answer); } } import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc1 = new Scanner(System.in); String nums = sc1.nextLine(); String nums2 = sc1.nextLine(); int a = nums.indexOf(nums2); String answer = "No"; if(a >= 0) { answer = "Yes"; } System.out.println(answer); } }
ConDefects/ConDefects/Code/abc279_b/Java/37396442
condefects-java_data_371
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); List<String> strList = new ArrayList<>(); for(int i=1; i<=n; i++){ if(i%3 == 0){ strList.add("×"); }else{ strList.add("o"); } } for(String str : strList){ System.out.print(str); } } } import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); List<String> strList = new ArrayList<>(); for(int i=1; i<=n; i++){ if(i%3 == 0){ strList.add("x"); }else{ strList.add("o"); } } for(String str : strList){ System.out.print(str); } } }
ConDefects/ConDefects/Code/abc348_a/Java/53730966
condefects-java_data_372
import java.util.Scanner; class Main{ public static void main(String[] args){ Scanner def = new Scanner(System.in); int N = def.nextInt(); for(int i = 1; i <= N ; i++){ System.out.println(i % 3 == 0 ? "x" : "o"); } } } import java.util.Scanner; class Main{ public static void main(String[] args){ Scanner def = new Scanner(System.in); int N = def.nextInt(); for(int i = 1; i <= N ; i++){ System.out.print(i % 3 == 0 ? "x" : "o"); } } }
ConDefects/ConDefects/Code/abc348_a/Java/54982778
condefects-java_data_373
import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; class Input { int n; int[] a; public Input() { Scanner scanner = new Scanner(System.in); this.n = scanner.nextInt(); scanner.nextLine(); this.a = new int[this.n]; for (int i = 0; i < this.n-1; i++) { this.a[i] = scanner.nextInt(); } } } public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); String result = new String(); for (int i=1;i<n+17;i++){ if (i%3==0){ result = result + "x"; } else { result = result + "o"; } } System.out.println(result); } } import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; class Input { int n; int[] a; public Input() { Scanner scanner = new Scanner(System.in); this.n = scanner.nextInt(); scanner.nextLine(); this.a = new int[this.n]; for (int i = 0; i < this.n-1; i++) { this.a[i] = scanner.nextInt(); } } } public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); String result = new String(); for (int i=1;i<n+1;i++){ if (i%3==0){ result = result + "x"; } else { result = result + "o"; } } System.out.println(result); } }
ConDefects/ConDefects/Code/abc348_a/Java/53310462
condefects-java_data_374
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.close(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { if (i % 3 == 0) { sb.append("x"); } else { sb.append("o"); } } System.out.println(sb); } } import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.close(); StringBuilder sb = new StringBuilder(); for (int i = 1; i <= n; i++) { if (i % 3 == 0) { sb.append("x"); } else { sb.append("o"); } } System.out.println(sb); } }
ConDefects/ConDefects/Code/abc348_a/Java/54049429
condefects-java_data_375
import java.util.*; public class Main { public static void main (String[] args) { Scanner in = new Scanner(System.in); int N = in.nextInt(); String result = ""; for (int i = 0; i < N; ++i) { if (i % 3 != 0) { result += "o"; } else { result += "x"; } } System.out.print(result); } } import java.util.*; public class Main { public static void main (String[] args) { Scanner in = new Scanner(System.in); int N = in.nextInt(); String result = ""; for (int i = 1; i <= N; ++i) { if (i % 3 != 0) { result += "o"; } else { result += "x"; } } System.out.print(result); } }
ConDefects/ConDefects/Code/abc348_a/Java/53394238
condefects-java_data_376
import java.io.InputStream; import java.lang.reflect.Array; import java.util.*; public class Main { public static void main(String[] args) { // String source = "8 3 1000000000\n" + // "1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\n" + // "\n"; InputStream source = System.in; Scanner sc = new Scanner(source); int N = sc.nextInt(); int D = sc.nextInt(); int P = sc.nextInt(); float passValue = (float) P / (float) D; int[] planArray = new int[N]; int countパスを使ったほうがいい日 = 0; long sum = 0; for (int i=0; i<N; i++) { int F = sc.nextInt(); sum += F; planArray[i] = F; if (passValue < (float)F) { countパスを使ったほうがいい日++; } } if (countパスを使ったほうがいい日 == 0) { System.out.println(sum); return; } Arrays.sort(planArray); for (int f = 0, l = planArray.length - 1; f < l; f++, l--){ int temp = planArray[f]; planArray[f] = planArray[l]; planArray[l] = temp; } int パスを買う可能性のある最大 = countパスを使ったほうがいい日 / D + 1; long min1 = (long) パスを買う可能性のある最大 * P; int パスの枚数1パターン目 = D * パスを買う可能性のある最大; for (int i=パスの枚数1パターン目; i<N; i++) { min1 += planArray[i]; } long min2 = (long) (パスを買う可能性のある最大 - 1) * P; int パスの枚数2パターン目 = D * (パスを買う可能性のある最大 - 1); for (int i=パスの枚数2パターン目; i<N; i++) { min2 += planArray[i]; } System.out.println(min(min1, min2, sum)); } /** * Math.minが引数2つしか受け付けないため、拡張したメソッド. * 配列の要素は1以上存在する前提. * * @param numbers 数値の配列 * @return 最小値 */ public static long min(long... numbers) { long min = numbers[0]; for (int i=1; i<numbers.length; i++) { min = Math.min(min, numbers[i]); } return min; } } import java.io.InputStream; import java.lang.reflect.Array; import java.util.*; public class Main { public static void main(String[] args) { // String source = "8 3 1000000000\n" + // "1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\n" + // "\n"; InputStream source = System.in; Scanner sc = new Scanner(source); int N = sc.nextInt(); int D = sc.nextInt(); int P = sc.nextInt(); float passValue = (float) P / (float) D; int[] planArray = new int[N]; int countパスを使ったほうがいい日 = 0; long sum = 0; for (int i=0; i<N; i++) { int F = sc.nextInt(); sum += F; planArray[i] = F; if (passValue <= (float)F) { countパスを使ったほうがいい日++; } } if (countパスを使ったほうがいい日 == 0) { System.out.println(sum); return; } Arrays.sort(planArray); for (int f = 0, l = planArray.length - 1; f < l; f++, l--){ int temp = planArray[f]; planArray[f] = planArray[l]; planArray[l] = temp; } int パスを買う可能性のある最大 = countパスを使ったほうがいい日 / D + 1; long min1 = (long) パスを買う可能性のある最大 * P; int パスの枚数1パターン目 = D * パスを買う可能性のある最大; for (int i=パスの枚数1パターン目; i<N; i++) { min1 += planArray[i]; } long min2 = (long) (パスを買う可能性のある最大 - 1) * P; int パスの枚数2パターン目 = D * (パスを買う可能性のある最大 - 1); for (int i=パスの枚数2パターン目; i<N; i++) { min2 += planArray[i]; } System.out.println(min(min1, min2, sum)); } /** * Math.minが引数2つしか受け付けないため、拡張したメソッド. * 配列の要素は1以上存在する前提. * * @param numbers 数値の配列 * @return 最小値 */ public static long min(long... numbers) { long min = numbers[0]; for (int i=1; i<numbers.length; i++) { min = Math.min(min, numbers[i]); } return min; } }
ConDefects/ConDefects/Code/abc318_c/Java/45657296
condefects-java_data_377
import java.util.*; public class Main { public static void main(String[] args) { IOHandler io = new IOHandler(); int n = io.nextInt(); int d = io.nextInt(); int p = io.nextInt(); int[] f = io.nextIntArray(n); io.close(); /* * 方針 * (1)パスを購入しない場合 * ⌈N/D⌉*P (⌈x⌉は x以上の最小の整数) * (2)パスを購入する場合 * パスを何回買うか試す、パスは運賃の高い日順に割り当てる */ // (1) int k = (n+d-1)/d; long result = p*k; // (2) Arrays.sort(f); // 事前に累積和を求める(O(n)) long[] cumulativeSumOfF = new long[n+1]; for (int i = 0; i < n; i++) cumulativeSumOfF[i+1] = cumulativeSumOfF[i] + f[i]; // 適当な回数ループして、運賃の高いr日間にパスを割り当てる for (int i = 0; i < k; i++) { int r = Math.max(0, n-i*d); result = Math.min(result, cumulativeSumOfF[r] + (long)p*i); if (r == 0) break; } io.output(result); } private static class IOHandler { private Scanner sc = new Scanner(System.in); private void close() {this.sc.close();} private int nextInt() {return this.sc.nextInt();} private int[] nextIntArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = this.sc.nextInt(); return array; } private void output(long result) {System.out.println(result);} } } import java.util.*; public class Main { public static void main(String[] args) { IOHandler io = new IOHandler(); int n = io.nextInt(); int d = io.nextInt(); int p = io.nextInt(); int[] f = io.nextIntArray(n); io.close(); /* * 方針 * (1)パスを購入しない場合 * ⌈N/D⌉*P (⌈x⌉は x以上の最小の整数) * (2)パスを購入する場合 * パスを何回買うか試す、パスは運賃の高い日順に割り当てる */ // (1) long k = (n+d-1)/d; long result = p*k; // (2) Arrays.sort(f); // 事前に累積和を求める(O(n)) long[] cumulativeSumOfF = new long[n+1]; for (int i = 0; i < n; i++) cumulativeSumOfF[i+1] = cumulativeSumOfF[i] + f[i]; // 適当な回数ループして、運賃の高いr日間にパスを割り当てる for (int i = 0; i < k; i++) { int r = Math.max(0, n-i*d); result = Math.min(result, cumulativeSumOfF[r] + (long)p*i); if (r == 0) break; } io.output(result); } private static class IOHandler { private Scanner sc = new Scanner(System.in); private void close() {this.sc.close();} private int nextInt() {return this.sc.nextInt();} private int[] nextIntArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = this.sc.nextInt(); return array; } private void output(long result) {System.out.println(result);} } }
ConDefects/ConDefects/Code/abc318_c/Java/45226569
condefects-java_data_378
import java.util.Scanner; import java.util.Arrays; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); TrainTravel trainTravel = new TrainTravel(sc); sc.close(); System.out.println(trainTravel.getMinTotalCost()); } } class TrainTravel { private int N, D, P; private int[] F; TrainTravel(Scanner sc) { N = Integer.parseInt(sc.next()); D = Integer.parseInt(sc.next()); P = Integer.parseInt(sc.next()); F = new int[N]; for (int i = 0; i < N; i++) F[i] = Integer.parseInt(sc.next()); } long getMinTotalCost() { Arrays.sort(F); long min = Long.MAX_VALUE; long[] sum = new long[N + 1]; sum[0] = 0; for (int i = 0; i < N; i++) { sum[i + 1] = sum[i] + F[i]; } for (int k = 0; k * D <= N; k++) { long tmp = (long) k * P + sum[N - k * D]; min = (min > tmp) ? tmp : min; } min = (min < (N / D + 1) * P) ? min : ((long) (N / D + 1) * P); return min; } } import java.util.Scanner; import java.util.Arrays; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); TrainTravel trainTravel = new TrainTravel(sc); sc.close(); System.out.println(trainTravel.getMinTotalCost()); } } class TrainTravel { private int N, D, P; private int[] F; TrainTravel(Scanner sc) { N = Integer.parseInt(sc.next()); D = Integer.parseInt(sc.next()); P = Integer.parseInt(sc.next()); F = new int[N]; for (int i = 0; i < N; i++) F[i] = Integer.parseInt(sc.next()); } long getMinTotalCost() { Arrays.sort(F); long min = Long.MAX_VALUE; long[] sum = new long[N + 1]; sum[0] = 0; for (int i = 0; i < N; i++) { sum[i + 1] = sum[i] + F[i]; } for (int k = 0; k * D <= N; k++) { long tmp = (long) k * P + sum[N - k * D]; min = (min > tmp) ? tmp : min; } min = (min < (long) (N / D + 1) * P) ? min : ((long) (N / D + 1) * P); return min; } }
ConDefects/ConDefects/Code/abc318_c/Java/45330872
condefects-java_data_379
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.nio.Buffer; import java.util.HashMap; import java.util.StringTokenizer; /* * Solution: 1m * Coding: 4m * Time: 5m * */ public class Main { public static void main(String[] args) throws IOException { //BufferedReader br = new BufferedReader(new FileReader("atcoder_abc/input.in")); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); int t = Integer.parseInt(st.nextToken()); st = new StringTokenizer(br.readLine()); int[] arr = new int[n - 1]; for(int i = 0;i < n - 1;i++){ arr[i] = Integer.parseInt(st.nextToken()); } HashMap<Integer, Integer> map = new HashMap<>(); for(int i = 0;i < m;i++){ st = new StringTokenizer(br.readLine()); map.put(Integer.parseInt(st.nextToken()) - 1, Integer.parseInt(st.nextToken())); } br.close(); for(int i = 0;i < n - 1;i++){ if(map.containsKey(i)){ t += map.get(i); } t -= arr[i]; if(t <= 0){ System.out.println("No"); return; } // System.out.println(t); } System.out.println("Yes"); } } import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.nio.Buffer; import java.util.HashMap; import java.util.StringTokenizer; /* * Solution: 1m * Coding: 4m * Time: 5m * */ public class Main { public static void main(String[] args) throws IOException { //BufferedReader br = new BufferedReader(new FileReader("atcoder_abc/input.in")); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(st.nextToken()); long t = Long.parseLong(st.nextToken()); st = new StringTokenizer(br.readLine()); int[] arr = new int[n - 1]; for(int i = 0;i < n - 1;i++){ arr[i] = Integer.parseInt(st.nextToken()); } HashMap<Integer, Integer> map = new HashMap<>(); for(int i = 0;i < m;i++){ st = new StringTokenizer(br.readLine()); map.put(Integer.parseInt(st.nextToken()) - 1, Integer.parseInt(st.nextToken())); } br.close(); for(int i = 0;i < n - 1;i++){ if(map.containsKey(i)){ t += map.get(i); } t -= arr[i]; if(t <= 0){ System.out.println("No"); return; } // System.out.println(t); } System.out.println("Yes"); } }
ConDefects/ConDefects/Code/abc265_b/Java/40816235
condefects-java_data_380
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Comparator; import java.util.StringTokenizer; public class Main { public static void main(String[] args) { AReader sc = new AReader(); int n = sc.nextInt(); int m = sc.nextInt(); long t = sc.nextLong(); int[] arr = new int[n]; for (int i = 1; i < n; i++) { arr[i] = sc.nextInt(); } int[][] bag = new int[m][2]; for (int i = 0; i < m; i++) { bag[i][0] = sc.nextInt(); bag[i][1] = sc.nextInt(); } Arrays.sort(bag, Comparator.comparing(x -> x[0])); boolean f = true; int j = 0; long at = 0; for (int i = 1; f && i < n; i++) { while (j < bag.length && bag[j][0] <= i) { t += bag[j][1]; j++; } if (at + arr[i] > t) { f = false; break; } at += arr[i]; } System.out.println(f ? "Yes" : "No"); } static class AReader { private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private StringTokenizer tokenizer = new StringTokenizer(""); private String innerNextLine() { try { return reader.readLine(); } catch (IOException ex) { return null; } } public boolean hasNext() { while (!tokenizer.hasMoreTokens()) { String nextLine = innerNextLine(); if (nextLine == null) { return false; } tokenizer = new StringTokenizer(nextLine); } return true; } public String nextLine() { tokenizer = new StringTokenizer(""); return innerNextLine(); } public String next() { hasNext(); return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } // public BigInteger nextBigInt() { // return new BigInteger(next()); // } // 若需要nextDouble等方法,请自行调用Double.parseDouble包装 } } import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Comparator; import java.util.StringTokenizer; public class Main { public static void main(String[] args) { AReader sc = new AReader(); int n = sc.nextInt(); int m = sc.nextInt(); long t = sc.nextLong(); int[] arr = new int[n]; for (int i = 1; i < n; i++) { arr[i] = sc.nextInt(); } int[][] bag = new int[m][2]; for (int i = 0; i < m; i++) { bag[i][0] = sc.nextInt(); bag[i][1] = sc.nextInt(); } Arrays.sort(bag, Comparator.comparing(x -> x[0])); boolean f = true; int j = 0; long at = 0; for (int i = 1; f && i < n; i++) { while (j < bag.length && bag[j][0] <= i) { t += bag[j][1]; j++; } if (at + arr[i] >= t) { f = false; break; } at += arr[i]; } System.out.println(f ? "Yes" : "No"); } static class AReader { private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private StringTokenizer tokenizer = new StringTokenizer(""); private String innerNextLine() { try { return reader.readLine(); } catch (IOException ex) { return null; } } public boolean hasNext() { while (!tokenizer.hasMoreTokens()) { String nextLine = innerNextLine(); if (nextLine == null) { return false; } tokenizer = new StringTokenizer(nextLine); } return true; } public String nextLine() { tokenizer = new StringTokenizer(""); return innerNextLine(); } public String next() { hasNext(); return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } // public BigInteger nextBigInt() { // return new BigInteger(next()); // } // 若需要nextDouble等方法,请自行调用Double.parseDouble包装 } }
ConDefects/ConDefects/Code/abc265_b/Java/37749460
condefects-java_data_381
import static java.lang.Math.*; import static java.util.Arrays.*; import java.io.*; import java.util.*; public class Main { void solve(){ int n = scanner.nextInt(), m = scanner.nextInt(); long t = scanner.nextLong(); long[] arr = nextLongArray(n-1, 1); long[] bonus = new long[n+1]; for(int i = 1; i <= m; i++){ int idx = scanner.nextInt(); long p = scanner.nextLong(); bonus[idx] = p; } long curr = t; for(int i = 1; i <= n - 1; i++){ curr += bonus[i]; long cost = arr[i]; if(cost > curr){ out.println("No"); return; } curr -= cost; } out.println(curr >= 0? "Yes" : "No"); } private static final boolean memory = false; private static final boolean singleTest = true; // ----- runner templates ----- // void run() { int numOfTests = singleTest? 1: scanner.nextInt(); for(int testIdx = 1; testIdx <= numOfTests; testIdx++){ solve(); } out.flush(); out.close(); } // ----- runner templates ----- // public static void main(String[] args) { if(memory) { new Thread(null, () -> new Main().run(), "go", 1 << 26).start(); } else{ new Main().run(); } } //------ input and output ------// public static FastScanner scanner = new FastScanner(System.in); public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static class FastScanner { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar, numChars; public FastScanner(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { return (int) nextLong(); } public long nextLong() { int c = read(); while (isWhitespace(c)) { c = read(); } boolean negative = false; if (c == '-') { negative = true; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = res * 10 + c - '0'; c = read(); } while (!isWhitespace(c)); return negative ? -res : res; } public double nextDouble() { return Double.parseDouble(next()); } public char nextChar() { int c = read(); while (isWhitespace(c)) { c = read(); } return (char) c; } public String next() { int c = read(); while (isWhitespace(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isWhitespace(c)); return res.toString(); } private boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } int[] nextIntArray(int n, int base){ int[] arr = new int[n + base]; for(int i = base; i < n + base; i++){arr[i] = scanner.nextInt();} return arr; } long[] nextLongArray(int n, int base){ long[] arr = new long[n + base]; for(int i = base; i < n + base; i++){arr[i] = scanner.nextLong();} return arr; } int[][] nextIntGrid(int n, int m, int base){ int[][] grid = new int[n + base][m + base]; for(int i = base; i < n + base; i++){for(int j = base; j < m + base; j++){grid[i][j] = scanner.nextInt();}} return grid; } char[][] nextCharGrid(int n, int m, int base){ char[][] grid = new char[n + base][m + base]; for(int i = base; i < n + base; i++){for(int j = base; j < m + base; j++){grid[i][j] = scanner.nextChar();}} return grid; } //------ debug and print functions ------// void debug(Object...os){out.println(deepToString(os));} void print(int[] arr, int start, int end){for(int i = start; i <= end; i++){out.print(arr[i]);out.print(i==end? '\n':' ');}} void print(long[] arr, int start, int end){for(int i = start; i <= end; i++){out.print(arr[i]);out.print(i==end? '\n':' ');}} void print(char[] arr, int start, int end){for(int i = start; i <= end; i++){out.print(arr[i]);out.print(i==end? '\n':' ');}} void print(Object... o){for(int i = 0; i < o.length; i++){out.print(o[i]);out.print(i==o.length-1?'\n':' ');}} <T> void printArrayList(List<T> arr, int start, int end){for(int i = start; i <= end; i++){out.print(arr.get(i));out.print(i==end? '\n':' ');}} //------ sort primitive type arrays ------// static void sort(int[] arr){ List<Integer> temp = new ArrayList<>(); for(int val: arr){temp.add(val);} Collections.sort(temp); for(int i = 0; i < arr.length; i++){arr[i] = temp.get(i);} } static void sort(long[] arr){ List<Long> temp = new ArrayList<>(); for(long val: arr){temp.add(val);} Collections.sort(temp); for(int i = 0; i < arr.length; i++){arr[i] = temp.get(i);} } static void sort(char[] arr) { List<Character> temp = new ArrayList<>(); for (char val : arr) {temp.add(val);} Collections.sort(temp); for (int i = 0; i < arr.length; i++) {arr[i] = temp.get(i);} } } import static java.lang.Math.*; import static java.util.Arrays.*; import java.io.*; import java.util.*; public class Main { void solve(){ int n = scanner.nextInt(), m = scanner.nextInt(); long t = scanner.nextLong(); long[] arr = nextLongArray(n-1, 1); long[] bonus = new long[n+1]; for(int i = 1; i <= m; i++){ int idx = scanner.nextInt(); long p = scanner.nextLong(); bonus[idx] = p; } long curr = t; for(int i = 1; i <= n - 1; i++){ curr += bonus[i]; long cost = arr[i]; if(cost >= curr){ out.println("No"); return; } curr -= cost; } out.println(curr >= 0? "Yes" : "No"); } private static final boolean memory = false; private static final boolean singleTest = true; // ----- runner templates ----- // void run() { int numOfTests = singleTest? 1: scanner.nextInt(); for(int testIdx = 1; testIdx <= numOfTests; testIdx++){ solve(); } out.flush(); out.close(); } // ----- runner templates ----- // public static void main(String[] args) { if(memory) { new Thread(null, () -> new Main().run(), "go", 1 << 26).start(); } else{ new Main().run(); } } //------ input and output ------// public static FastScanner scanner = new FastScanner(System.in); public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static class FastScanner { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar, numChars; public FastScanner(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { return (int) nextLong(); } public long nextLong() { int c = read(); while (isWhitespace(c)) { c = read(); } boolean negative = false; if (c == '-') { negative = true; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = res * 10 + c - '0'; c = read(); } while (!isWhitespace(c)); return negative ? -res : res; } public double nextDouble() { return Double.parseDouble(next()); } public char nextChar() { int c = read(); while (isWhitespace(c)) { c = read(); } return (char) c; } public String next() { int c = read(); while (isWhitespace(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isWhitespace(c)); return res.toString(); } private boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } int[] nextIntArray(int n, int base){ int[] arr = new int[n + base]; for(int i = base; i < n + base; i++){arr[i] = scanner.nextInt();} return arr; } long[] nextLongArray(int n, int base){ long[] arr = new long[n + base]; for(int i = base; i < n + base; i++){arr[i] = scanner.nextLong();} return arr; } int[][] nextIntGrid(int n, int m, int base){ int[][] grid = new int[n + base][m + base]; for(int i = base; i < n + base; i++){for(int j = base; j < m + base; j++){grid[i][j] = scanner.nextInt();}} return grid; } char[][] nextCharGrid(int n, int m, int base){ char[][] grid = new char[n + base][m + base]; for(int i = base; i < n + base; i++){for(int j = base; j < m + base; j++){grid[i][j] = scanner.nextChar();}} return grid; } //------ debug and print functions ------// void debug(Object...os){out.println(deepToString(os));} void print(int[] arr, int start, int end){for(int i = start; i <= end; i++){out.print(arr[i]);out.print(i==end? '\n':' ');}} void print(long[] arr, int start, int end){for(int i = start; i <= end; i++){out.print(arr[i]);out.print(i==end? '\n':' ');}} void print(char[] arr, int start, int end){for(int i = start; i <= end; i++){out.print(arr[i]);out.print(i==end? '\n':' ');}} void print(Object... o){for(int i = 0; i < o.length; i++){out.print(o[i]);out.print(i==o.length-1?'\n':' ');}} <T> void printArrayList(List<T> arr, int start, int end){for(int i = start; i <= end; i++){out.print(arr.get(i));out.print(i==end? '\n':' ');}} //------ sort primitive type arrays ------// static void sort(int[] arr){ List<Integer> temp = new ArrayList<>(); for(int val: arr){temp.add(val);} Collections.sort(temp); for(int i = 0; i < arr.length; i++){arr[i] = temp.get(i);} } static void sort(long[] arr){ List<Long> temp = new ArrayList<>(); for(long val: arr){temp.add(val);} Collections.sort(temp); for(int i = 0; i < arr.length; i++){arr[i] = temp.get(i);} } static void sort(char[] arr) { List<Character> temp = new ArrayList<>(); for (char val : arr) {temp.add(val);} Collections.sort(temp); for (int i = 0; i < arr.length; i++) {arr[i] = temp.get(i);} } }
ConDefects/ConDefects/Code/abc265_b/Java/39934518
condefects-java_data_382
import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Main { public static void main(final String[] args) { try (final Scanner sc = new Scanner(System.in)) { final int N = Integer.parseInt(sc.next()); final int M = Integer.parseInt(sc.next()); int T = Integer.parseInt(sc.next()); final int[] A = new int[N]; for (int i = 0; i < N - 1; i++) { A[i] = Integer.parseInt(sc.next()); } final Map<Integer, Integer> XY = new HashMap<>(); for (int i = 0; i < M; i++) { XY.put(Integer.parseInt(sc.next()), Integer.parseInt(sc.next())); } for (int i = 0; i <= N - 1; i++) { T -= A[i]; if (T <= 0) { System.out.println("No"); return; } if (XY.containsKey(i + 2)) { T += XY.get(i + 2); } } System.out.println("Yes"); } } } import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Main { public static void main(final String[] args) { try (final Scanner sc = new Scanner(System.in)) { final int N = Integer.parseInt(sc.next()); final int M = Integer.parseInt(sc.next()); long T = Long.parseLong(sc.next()); final int[] A = new int[N]; for (int i = 0; i < N - 1; i++) { A[i] = Integer.parseInt(sc.next()); } final Map<Integer, Integer> XY = new HashMap<>(); for (int i = 0; i < M; i++) { XY.put(Integer.parseInt(sc.next()), Integer.parseInt(sc.next())); } for (int i = 0; i <= N - 1; i++) { T -= A[i]; if (T <= 0) { System.out.println("No"); return; } if (XY.containsKey(i + 2)) { T += XY.get(i + 2); } } System.out.println("Yes"); } } }
ConDefects/ConDefects/Code/abc265_b/Java/45076816
condefects-java_data_383
import java.io.*; import java.util.*; class Main { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int N, M, T; int[] A, X, Y; static int i(String s) { return Integer.parseInt(s); } void calc() throws Exception { String[] nmt = br.readLine().split(" "); N = i(nmt[0]); M = i(nmt[1]); T = i(nmt[2]); String[] as = br.readLine().split(" "); A = Arrays.stream(as).mapToInt(s -> i(s)).toArray(); X = new int[M]; Y = new int[M]; for (int i = 0; i < M; i++) { String[] xy = br.readLine().split(" "); X[i] = i(xy[0]) - 1; Y[i] = i(xy[1]); } int m = 0; int t = T; for (int i = 0; i < N-1; i++) { if (t <= A[i]) { System.out.println("No"); return; } t -= A[i]; if (m < M && i+1 == X[m]) { t += Y[m]; m++; } } System.out.println("Yes"); } public static void main(String[] args) throws Exception { new Main().calc(); } } import java.io.*; import java.util.*; class Main { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int N, M, T; int[] A, X, Y; static int i(String s) { return Integer.parseInt(s); } void calc() throws Exception { String[] nmt = br.readLine().split(" "); N = i(nmt[0]); M = i(nmt[1]); T = i(nmt[2]); String[] as = br.readLine().split(" "); A = Arrays.stream(as).mapToInt(s -> i(s)).toArray(); X = new int[M]; Y = new int[M]; for (int i = 0; i < M; i++) { String[] xy = br.readLine().split(" "); X[i] = i(xy[0]) - 1; Y[i] = i(xy[1]); } int m = 0; long t = T; for (int i = 0; i < N-1; i++) { if (t <= A[i]) { System.out.println("No"); return; } t -= A[i]; if (m < M && i+1 == X[m]) { t += Y[m]; m++; } } System.out.println("Yes"); } public static void main(String[] args) throws Exception { new Main().calc(); } }
ConDefects/ConDefects/Code/abc265_b/Java/39222532
condefects-java_data_384
import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int M = sc.nextInt(); int T = sc.nextInt(); sc.nextLine(); int[] A = new int[N]; for (int i = 1; i < N; i++) { A[i] = sc.nextInt(); } sc.nextLine(); Map bonusRoomMap = new HashMap(); for (int i = 0; i < M; i++) { bonusRoomMap.put(sc.nextInt(), sc.nextInt()); sc.nextLine(); } for (int i = 1; i < N; i++) { T -= A[i]; if (T <= 0) { System.out.println("No"); return; } T += (int) bonusRoomMap.getOrDefault(i+1, 0); } System.out.println("Yes"); } } import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int M = sc.nextInt(); long T = sc.nextLong(); sc.nextLine(); int[] A = new int[N]; for (int i = 1; i < N; i++) { A[i] = sc.nextInt(); } sc.nextLine(); Map bonusRoomMap = new HashMap(); for (int i = 0; i < M; i++) { bonusRoomMap.put(sc.nextInt(), sc.nextInt()); sc.nextLine(); } for (int i = 1; i < N; i++) { T -= A[i]; if (T <= 0) { System.out.println("No"); return; } T += (int) bonusRoomMap.getOrDefault(i+1, 0); } System.out.println("Yes"); } }
ConDefects/ConDefects/Code/abc265_b/Java/45007102
condefects-java_data_385
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // 部屋数 int n = sc.nextInt(); // ボーナス部屋数 int m = sc.nextInt(); // 持ち時間 long t = sc.nextLong(); // 各部屋の次への所要時間 long[] nt = new long[n-1]; for (int i=0; i <= n-2; i++) { nt[i] = sc.nextLong(); } // 得られるボーナス時間 long[] bt = new long[n]; Arrays.fill(bt, 0); for (int i=0; i<m; i++) { int r = sc.nextInt(); long g = sc.nextLong(); bt[r] = g; } // 洞窟探索開始 for (int i=0; i<n-1; i++) { // ボーナス時間を獲得 t += bt[i]; // 次の部屋に行けるか if (t <= nt[i]) { System.out.println("No"); return; } // 行けるなら持ち時間を消費して次へ t -= nt[i] ; } System.out.println("Yes"); } } import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // 部屋数 int n = sc.nextInt(); // ボーナス部屋数 int m = sc.nextInt(); // 持ち時間 long t = sc.nextLong(); // 各部屋の次への所要時間 long[] nt = new long[n-1]; for (int i=0; i <= n-2; i++) { nt[i] = sc.nextLong(); } // 得られるボーナス時間 long[] bt = new long[n]; Arrays.fill(bt, 0); for (int i=0; i<m; i++) { int r = sc.nextInt()-1; long g = sc.nextLong(); bt[r] = g; } // 洞窟探索開始 for (int i=0; i<n-1; i++) { // ボーナス時間を獲得 t += bt[i]; // 次の部屋に行けるか if (t <= nt[i]) { System.out.println("No"); return; } // 行けるなら持ち時間を消費して次へ t -= nt[i] ; } System.out.println("Yes"); } }
ConDefects/ConDefects/Code/abc265_b/Java/46000992
condefects-java_data_386
import java.net.InterfaceAddress; import java.util.*; public class Main { public static int N; public static int M; public static long T; public static int[]A= new int[100009]; public static int[] booleanBonus = new int[100009]; public static Scanner sc = new Scanner(System.in); public static void main(String[] args) { N = sc.nextInt(); M = sc.nextInt(); T = sc.nextInt(); for(int i=1;i<=N-1;i++)A[i]=sc.nextInt(); for(int i=1;i<=M;i++) { int X,Y; X = sc.nextInt(); Y = sc.nextInt(); booleanBonus[X]=Y; } for(int i=1;i<=N-1;i++) { T+=booleanBonus[i]; if(T<A[i]) { System.out.print("No"); System.exit(0); } T-=A[i]; } System.out.println("Yes"); System.exit(0); } } import java.net.InterfaceAddress; import java.util.*; public class Main { public static int N; public static int M; public static long T; public static int[]A= new int[100009]; public static int[] booleanBonus = new int[100009]; public static Scanner sc = new Scanner(System.in); public static void main(String[] args) { N = sc.nextInt(); M = sc.nextInt(); T = sc.nextInt(); for(int i=1;i<=N-1;i++)A[i]=sc.nextInt(); for(int i=1;i<=M;i++) { int X,Y; X = sc.nextInt(); Y = sc.nextInt(); booleanBonus[X]=Y; } for(int i=1;i<=N-1;i++) { T+=booleanBonus[i]; if(T<=A[i]) { System.out.print("No"); System.exit(0); } T-=A[i]; } System.out.println("Yes"); System.exit(0); } }
ConDefects/ConDefects/Code/abc265_b/Java/45229510
condefects-java_data_387
import java.util.*; class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int N = scan.nextInt(); int M = scan.nextInt(); int T = scan.nextInt(); int[] A = new int[N]; for(int i = 0; i < N - 1; i++){ A[i] = scan.nextInt(); } Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for(int i = 0; i < M; i++){ int k = scan.nextInt(); int v = scan.nextInt(); if(map.containsKey(k)){ map.put(k, map.get(k) + v); }else{ map.put(k, v); } } for(int i = 0; i < N - 1; i++){ T -= A[i]; if(T <= 0){ System.out.println("No"); return; } if(map.containsKey(i + 2)){ T += map.get(i + 2); } } System.out.println("Yes"); } } import java.util.*; class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int N = scan.nextInt(); int M = scan.nextInt(); long T = scan.nextLong(); int[] A = new int[N]; for(int i = 0; i < N - 1; i++){ A[i] = scan.nextInt(); } Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for(int i = 0; i < M; i++){ int k = scan.nextInt(); int v = scan.nextInt(); if(map.containsKey(k)){ map.put(k, map.get(k) + v); }else{ map.put(k, v); } } for(int i = 0; i < N - 1; i++){ T -= A[i]; if(T <= 0){ System.out.println("No"); return; } if(map.containsKey(i + 2)){ T += map.get(i + 2); } } System.out.println("Yes"); } }
ConDefects/ConDefects/Code/abc265_b/Java/38010760
condefects-java_data_388
import java.util.*; class Main { public static void main(String[] args) { Scanner scanner =new Scanner(System.in); int n=scanner.nextInt(); int m=scanner.nextInt(); int t=scanner.nextInt(); int[]a=new int[n-1]; for(int i=0;i<n-1;i++){ a[i]=scanner.nextInt(); } int[]bonas=new int[n]; for(int i=0;i<m;i++){ int x=scanner.nextInt(); int y=scanner.nextInt(); bonas[x-1]=y; } for(int i=0;i<n-1;i++){ t-=a[i]; if(t<=0){ break; } t+=bonas[i+1]; } if(t>0){ System.out.println("Yes"); } else{ System.out.println("No"); } } } import java.util.*; class Main { public static void main(String[] args) { Scanner scanner =new Scanner(System.in); int n=scanner.nextInt(); int m=scanner.nextInt(); long t=scanner.nextLong(); int[]a=new int[n-1]; for(int i=0;i<n-1;i++){ a[i]=scanner.nextInt(); } int[]bonas=new int[n]; for(int i=0;i<m;i++){ int x=scanner.nextInt(); int y=scanner.nextInt(); bonas[x-1]=y; } for(int i=0;i<n-1;i++){ t-=a[i]; if(t<=0){ break; } t+=bonas[i+1]; } if(t>0){ System.out.println("Yes"); } else{ System.out.println("No"); } } }
ConDefects/ConDefects/Code/abc265_b/Java/42032187
condefects-java_data_389
// package abc; import java.io.PrintWriter; import java.util.Scanner; public class Main { static PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int m = scanner.nextInt(); int t = scanner.nextInt(); int[] a = new int[n]; int[] bonus = new int[n]; for (int i = 0; i < n - 1; i++) { a[i] = scanner.nextInt(); } for (int i = 0; i < m; i++) { int x = scanner.nextInt(); int y = scanner.nextInt(); bonus[x - 1] = y; } int now = 0; for (int i = 0; i < n; i++) { t += bonus[now]; t -= a[i]; now = i + 1; if (t <= 0) { System.out.println("No"); System.exit(0); } } System.out.println("Yes"); pw.close(); scanner.close(); } } // package abc; import java.io.PrintWriter; import java.util.Scanner; public class Main { static PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int m = scanner.nextInt(); long t = scanner.nextInt(); int[] a = new int[n]; int[] bonus = new int[n]; for (int i = 0; i < n - 1; i++) { a[i] = scanner.nextInt(); } for (int i = 0; i < m; i++) { int x = scanner.nextInt(); int y = scanner.nextInt(); bonus[x - 1] = y; } int now = 0; for (int i = 0; i < n; i++) { t += bonus[now]; t -= a[i]; now = i + 1; if (t <= 0) { System.out.println("No"); System.exit(0); } } System.out.println("Yes"); pw.close(); scanner.close(); } }
ConDefects/ConDefects/Code/abc265_b/Java/39759478
condefects-java_data_390
import java.util.*; @SuppressWarnings("unused") public class Main { private static void solve() { int n = ni(); int m = ni(); char[][] s = new char[m][]; for (int i = 0; i < m; i++) { s[i] = ns(); } int[] a = new int[m]; int[] mask = new int[m]; for (int i = 0; i < m; i++) { mask[i] = (1 << s[i].length) - 1; for (int j = 0; j < s[i].length; j++) { if (s[i][j] == 'b') { a[i] |= 1 << (s[i].length - j - 1); } } } int mod = 998244353; int[] v = new int[64]; v[0] = 1; for (int i = 0; i < Math.min(6, n); i ++) { var mat = getMat(a, mask, i + 1); v = mul(mat, v, mod); } if (n >= 6) { var mat = getMat(a, mask, 6); v = pow(mat, v, n - 6, mod); } long ret = 0; for (var x : v) { ret += x; ret %= mod; } System.out.println(ret); } public static int[] pow(int[][] A, int[] v, long e, int mod) { int[][] MUL = A; for(int i = 0;i < v.length;i++)v[i] %= mod; for(;e > 0;e>>>=1) { if((e&1)==1)v = mul(MUL, v, mod); MUL = p2(MUL, mod); } return v; } public static int[][] p2(int[][] A, int mod) { int n = A.length; int[][] C = new int[n][n]; for(int i = 0;i < n;i++){ for(int j = 0;j < n;j++){ long sum = 0; for(int k = 0;k < n;k++){ sum += (long)A[i][k] * A[k][j]; sum %= mod; } C[i][j] = (int)sum; } } return C; } public static int[] mul(int[][] A, int[] v, int mod) { int m = A.length; int n = v.length; int[] w = new int[m]; for(int i = 0;i < m;i++){ long sum = 0; for(int k = 0;k < n;k++){ sum += (long)A[i][k] * v[k]; sum %= mod; } w[i] = (int)sum; } return w; } private static int[][] getMat(int[] a, int[] mask, int len) { int m = a.length; int[][] mat = new int[64][64]; for (int i = 0; i < 64; i++) { for (int k = 0; k < 2; k ++) { int j = (i << 1) & (64 - 1) | k; var ok = true; for (int p = 0; p < m; p ++) { int x = (1 << len) - 1; if (x >= mask[p] && (j & mask[p]) == a[p]) { ok = false; break; } } if (ok) { mat[j][i] += 1; } } } return mat; } public static void main(String[] args) { new Thread(null, new Runnable() { @Override public void run() { long start = System.currentTimeMillis(); String debug = args.length > 0 ? args[0] : null; if (debug != null) { try { is = java.nio.file.Files.newInputStream(java.nio.file.Paths.get(debug)); } catch (Exception e) { throw new RuntimeException(e); } } reader = new java.io.BufferedReader(new java.io.InputStreamReader(is), 32768); solve(); out.flush(); tr((System.currentTimeMillis() - start) + "ms"); } }, "", 64000000).start(); } private static java.io.InputStream is = System.in; private static java.io.PrintWriter out = new java.io.PrintWriter(System.out); private static java.util.StringTokenizer tokenizer = null; private static java.io.BufferedReader reader; public static String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new java.util.StringTokenizer(reader.readLine()); } catch (Exception e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } private static double nd() { return Double.parseDouble(next()); } private static long nl() { return Long.parseLong(next()); } private static int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private static char[] ns() { return next().toCharArray(); } private static long[] nal(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } private static int[][] ntable(int n, int m) { int[][] table = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { table[i][j] = ni(); } } return table; } private static int[][] nlist(int n, int m) { int[][] table = new int[m][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { table[j][i] = ni(); } } return table; } private static int ni() { return Integer.parseInt(next()); } private static void tr(Object... o) { if (is != System.in) System.out.println(java.util.Arrays.deepToString(o)); } } import java.util.*; @SuppressWarnings("unused") public class Main { private static void solve() { long n = nl(); int m = ni(); char[][] s = new char[m][]; for (int i = 0; i < m; i++) { s[i] = ns(); } int[] a = new int[m]; int[] mask = new int[m]; for (int i = 0; i < m; i++) { mask[i] = (1 << s[i].length) - 1; for (int j = 0; j < s[i].length; j++) { if (s[i][j] == 'b') { a[i] |= 1 << (s[i].length - j - 1); } } } int mod = 998244353; int[] v = new int[64]; v[0] = 1; for (int i = 0; i < Math.min(6, n); i ++) { var mat = getMat(a, mask, i + 1); v = mul(mat, v, mod); } if (n >= 6) { var mat = getMat(a, mask, 6); v = pow(mat, v, n - 6, mod); } long ret = 0; for (var x : v) { ret += x; ret %= mod; } System.out.println(ret); } public static int[] pow(int[][] A, int[] v, long e, int mod) { int[][] MUL = A; for(int i = 0;i < v.length;i++)v[i] %= mod; for(;e > 0;e>>>=1) { if((e&1)==1)v = mul(MUL, v, mod); MUL = p2(MUL, mod); } return v; } public static int[][] p2(int[][] A, int mod) { int n = A.length; int[][] C = new int[n][n]; for(int i = 0;i < n;i++){ for(int j = 0;j < n;j++){ long sum = 0; for(int k = 0;k < n;k++){ sum += (long)A[i][k] * A[k][j]; sum %= mod; } C[i][j] = (int)sum; } } return C; } public static int[] mul(int[][] A, int[] v, int mod) { int m = A.length; int n = v.length; int[] w = new int[m]; for(int i = 0;i < m;i++){ long sum = 0; for(int k = 0;k < n;k++){ sum += (long)A[i][k] * v[k]; sum %= mod; } w[i] = (int)sum; } return w; } private static int[][] getMat(int[] a, int[] mask, int len) { int m = a.length; int[][] mat = new int[64][64]; for (int i = 0; i < 64; i++) { for (int k = 0; k < 2; k ++) { int j = (i << 1) & (64 - 1) | k; var ok = true; for (int p = 0; p < m; p ++) { int x = (1 << len) - 1; if (x >= mask[p] && (j & mask[p]) == a[p]) { ok = false; break; } } if (ok) { mat[j][i] += 1; } } } return mat; } public static void main(String[] args) { new Thread(null, new Runnable() { @Override public void run() { long start = System.currentTimeMillis(); String debug = args.length > 0 ? args[0] : null; if (debug != null) { try { is = java.nio.file.Files.newInputStream(java.nio.file.Paths.get(debug)); } catch (Exception e) { throw new RuntimeException(e); } } reader = new java.io.BufferedReader(new java.io.InputStreamReader(is), 32768); solve(); out.flush(); tr((System.currentTimeMillis() - start) + "ms"); } }, "", 64000000).start(); } private static java.io.InputStream is = System.in; private static java.io.PrintWriter out = new java.io.PrintWriter(System.out); private static java.util.StringTokenizer tokenizer = null; private static java.io.BufferedReader reader; public static String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new java.util.StringTokenizer(reader.readLine()); } catch (Exception e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } private static double nd() { return Double.parseDouble(next()); } private static long nl() { return Long.parseLong(next()); } private static int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private static char[] ns() { return next().toCharArray(); } private static long[] nal(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } private static int[][] ntable(int n, int m) { int[][] table = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { table[i][j] = ni(); } } return table; } private static int[][] nlist(int n, int m) { int[][] table = new int[m][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { table[j][i] = ni(); } } return table; } private static int ni() { return Integer.parseInt(next()); } private static void tr(Object... o) { if (is != System.in) System.out.println(java.util.Arrays.deepToString(o)); } }
ConDefects/ConDefects/Code/abc305_g/Java/43330208
condefects-java_data_391
import java.io.*; import java.util.*; class Main { int N; Point[] p; Line[] lines; Map<Integer, Integer> mulinv; void calc() { N = nextInt(); p = new Point[N]; for (int i = 0; i < N; i++) { int[] c = nextInts(); p[i] = new Point(c[0], c[1], c[2]); } mulinv = new HashMap<>(); for (int i = 2; i <= N; i++) mulinv.put(i*(i-1), i); lines = new Line[N*(N-1)/2]; int lc = 0; for (int i = 1; i < N; i++) { for (int j = 0; j < i; j++) { lines[lc++] = new Line(p[i], p[j]); } } for (int i = 0; i < lines.length - 1; i++) { for (int j = i+1; j < lines.length; j++) { if (lines[i] == null) break; if (lines[j] == null) continue; Point v1 = lines[i].p2.sub(lines[i].p1); Point v2 = lines[j].p2.sub(lines[j].p1); Point op = v1.outProd(v2); if (op.isZero()) { Point v3 = lines[j].p2.sub(lines[i].p1); Point op2 = v1.outProd(v3); if (op2.isZero()) { lines[j].mul += lines[i].mul; lines[i] = null; } } } } List<Line> ll = new ArrayList<>(); for (int i = 0; i < lines.length; i++) if (lines[i] != null) { ll.add(lines[i]); lines[i].mul = mulinv.get(lines[i].mul) - 1; } Line[] l = ll.toArray(new Line[0]); Map<Point, Set<Integer>> list = new HashMap<>(); for (int i = 0; i < l.length-1; i++) { for (int j = i+1; j < l.length; j++) { Point v1 = l[i].p2.sub(l[i].p1); Point v2 = l[j].p2.sub(l[j].p1); Point op = v1.outProd(v2); Point pi1 = l[i].p1, pj1 = l[j].p1; if (lv(op.x * (pj1.x - pi1.x) + op.y * (pj1.y - pi1.y) + op.z * (pj1.z - pi1.z)) != 0) { continue; } if (lv(op.y) == 0 && lv(op.z) == 0) continue; double t; if (lv(op.z) != 0) t = - ((pi1.x * v1.y - pi1.y * v1.x ) - (pj1.x * v1.y - pj1.y * v1.x)) / op.z; else if (lv(op.x) != 0) t = - ((pi1.y * v1.z - pi1.z * v1.y ) - (pj1.y * v1.z - pj1.z * v1.y)) / op.x; else t = - ((pi1.z * v1.x - pi1.x * v1.z ) - (pj1.z * v1.x - pj1.x * v1.z)) / op.y; double x = pj1.x + t * v2.x; if (lv(x) >= 0) continue; double y = pj1.y + t * v2.y; double z = pj1.z + t * v2.z; Point p = new Point(x, y, z); Set<Integer> s = list.get(p); if (s == null) s = new HashSet<>(); s.add(i); s.add(j); list.put(p, s); } } int max = 0; for (Point p: list.keySet()) { int c = 0; for (int i: list.get(p)) c += l[i].mul; max = Math.max(max, c); } for (Line line: l) max = Math.max(max, line.mul); System.out.println(N-max); } static long lv(double x) { return Point.lv(x); } // --- BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String next() { try { return br.readLine(); } catch (Exception e) { return null; } } String[] nexts() { return next().split(" "); } static int i(String s) { return Integer.parseInt(s); } int nextInt() { return i(next()); } int[] nextInts() { return Arrays.stream(nexts()).mapToInt(Main::i).toArray(); } public static void main(String[] args) { new Main().calc(); } } final class Point { static final double EPS = 1e-7; double x, y, z; Point(double a, double b, double c) { x = a; y = b; z = c; } Point sub(Point p) { return new Point(x-p.x, y-p.y, z-p.z); } Point outProd(Point p) { return new Point(y*p.z - z*p.y, z*p.x - x*p.z, x*p.y - y*p.x); } double sqLen() { return x*x + y*y + z*z; } boolean isZero() { return lv(sqLen()) == 0; } @Override public boolean equals(Object o) { if (!(o instanceof Point)) return false; Point p = (Point)o; return lv(x) == lv(p.x) && lv(y) == lv(p.y) && lv(z) == lv(p.z); } @Override public int hashCode() { long v = lv(x) + lv(y) * 41 + lv(z) * 97; return (int)(v ^ (v >>> 32)); } static long lv(double x) { return (long)Math.floor(x/EPS + 0.5); } } class Line { Point p1, p2; int mul = 2; Line(Point p, Point q) { if (p.x < q.x) { p1 = p; p2 = q; } else {p1 = q; p2 = p; } } } import java.io.*; import java.util.*; class Main { int N; Point[] p; Line[] lines; Map<Integer, Integer> mulinv; void calc() { N = nextInt(); p = new Point[N]; for (int i = 0; i < N; i++) { int[] c = nextInts(); p[i] = new Point(c[0], c[1], c[2]); } mulinv = new HashMap<>(); for (int i = 2; i <= N; i++) mulinv.put(i*(i-1), i); lines = new Line[N*(N-1)/2]; int lc = 0; for (int i = 1; i < N; i++) { for (int j = 0; j < i; j++) { lines[lc++] = new Line(p[i], p[j]); } } for (int i = 0; i < lines.length - 1; i++) { for (int j = i+1; j < lines.length; j++) { if (lines[i] == null) break; if (lines[j] == null) continue; Point v1 = lines[i].p2.sub(lines[i].p1); Point v2 = lines[j].p2.sub(lines[j].p1); Point op = v1.outProd(v2); if (op.isZero()) { Point v3 = lines[j].p2.sub(lines[i].p1); Point op2 = v1.outProd(v3); if (op2.isZero()) { lines[j].mul += lines[i].mul; lines[i] = null; } } } } List<Line> ll = new ArrayList<>(); for (int i = 0; i < lines.length; i++) if (lines[i] != null) { ll.add(lines[i]); lines[i].mul = mulinv.get(lines[i].mul) - 1; } Line[] l = ll.toArray(new Line[0]); Map<Point, Set<Integer>> list = new HashMap<>(); for (int i = 0; i < l.length-1; i++) { for (int j = i+1; j < l.length; j++) { Point v1 = l[i].p2.sub(l[i].p1); Point v2 = l[j].p2.sub(l[j].p1); Point op = v1.outProd(v2); Point pi1 = l[i].p1, pj1 = l[j].p1; if (lv(op.x * (pj1.x - pi1.x) + op.y * (pj1.y - pi1.y) + op.z * (pj1.z - pi1.z)) != 0) { continue; } if (lv(op.y) == 0 && lv(op.z) == 0) continue; double t; if (lv(op.z) != 0) t = - ((pi1.x * v1.y - pi1.y * v1.x ) - (pj1.x * v1.y - pj1.y * v1.x)) / op.z; else if (lv(op.x) != 0) t = - ((pi1.y * v1.z - pi1.z * v1.y ) - (pj1.y * v1.z - pj1.z * v1.y)) / op.x; else t = - ((pi1.z * v1.x - pi1.x * v1.z ) - (pj1.z * v1.x - pj1.x * v1.z)) / op.y; double x = pj1.x + t * v2.x; if (lv(x) >= 0) continue; double y = pj1.y + t * v2.y; double z = pj1.z + t * v2.z; Point p = new Point(x, y, z); Set<Integer> s = list.get(p); if (s == null) s = new HashSet<>(); s.add(i); s.add(j); list.put(p, s); } } int max = 0; for (Point p: list.keySet()) { int c = 0; for (int i: list.get(p)) c += l[i].mul; max = Math.max(max, c); } for (Line line: l) if (line.p1.x != line.p2.x) max = Math.max(max, line.mul); System.out.println(N-max); } static long lv(double x) { return Point.lv(x); } // --- BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String next() { try { return br.readLine(); } catch (Exception e) { return null; } } String[] nexts() { return next().split(" "); } static int i(String s) { return Integer.parseInt(s); } int nextInt() { return i(next()); } int[] nextInts() { return Arrays.stream(nexts()).mapToInt(Main::i).toArray(); } public static void main(String[] args) { new Main().calc(); } } final class Point { static final double EPS = 1e-7; double x, y, z; Point(double a, double b, double c) { x = a; y = b; z = c; } Point sub(Point p) { return new Point(x-p.x, y-p.y, z-p.z); } Point outProd(Point p) { return new Point(y*p.z - z*p.y, z*p.x - x*p.z, x*p.y - y*p.x); } double sqLen() { return x*x + y*y + z*z; } boolean isZero() { return lv(sqLen()) == 0; } @Override public boolean equals(Object o) { if (!(o instanceof Point)) return false; Point p = (Point)o; return lv(x) == lv(p.x) && lv(y) == lv(p.y) && lv(z) == lv(p.z); } @Override public int hashCode() { long v = lv(x) + lv(y) * 41 + lv(z) * 97; return (int)(v ^ (v >>> 32)); } static long lv(double x) { return (long)Math.floor(x/EPS + 0.5); } } class Line { Point p1, p2; int mul = 2; Line(Point p, Point q) { if (p.x < q.x) { p1 = p; p2 = q; } else {p1 = q; p2 = p; } } }
ConDefects/ConDefects/Code/abc301_g/Java/41676369
condefects-java_data_392
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); long L = sc.nextLong(); int N1 = sc.nextInt(); int N2 = sc.nextInt(); Pair[] x1 = new Pair[N1]; Pair[] x2 = new Pair[N2]; for(int i = 0; i < N1; i++) { x1[i] = new Pair(sc.nextLong(), sc.nextLong()); } for(int i = 0; i < N2; i++) { x2[i] = new Pair(sc.nextLong(), sc.nextLong()); } long ans = 0; int i1 = 0; int i2 = 0; Pair c1 = x1[i1]; Pair c2 = x2[i2]; boolean update = true; while(update) { update = false; if ( c1.v.equals(c2.v) ) { long cnt = Math.min(c1.l, c2.l); if ( cnt > 0 ) { ans += cnt; c1 = c1.add(-cnt); c2 = c2.add(-cnt); update = true; } } else { long shift = Math.min(c1.l, c2.l); if ( shift > 0 ) { c1 = c1.add(-shift); c2 = c2.add(-shift); update = true; } } if ( c1.isZero() && (i1 + 1 < N1) ) { c1 = x1[++i1]; update = true; } if ( c2.isZero() && (i2 + 1 < N2) ) { c2 = x2[++i2]; update = true; } } System.out.println(ans); } private static class Pair { public final Long v; public final Long l; public Pair(Long v, Long l) { this.v = v; this.l = l; } public Pair add(long shift) { return new Pair(this.v, this.l + shift); } public boolean isZero() { return this.l.equals(0); } } } import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); long L = sc.nextLong(); int N1 = sc.nextInt(); int N2 = sc.nextInt(); Pair[] x1 = new Pair[N1]; Pair[] x2 = new Pair[N2]; for(int i = 0; i < N1; i++) { x1[i] = new Pair(sc.nextLong(), sc.nextLong()); } for(int i = 0; i < N2; i++) { x2[i] = new Pair(sc.nextLong(), sc.nextLong()); } long ans = 0; int i1 = 0; int i2 = 0; Pair c1 = x1[i1]; Pair c2 = x2[i2]; boolean update = true; while(update) { update = false; if ( c1.v.equals(c2.v) ) { long cnt = Math.min(c1.l, c2.l); if ( cnt > 0 ) { ans += cnt; c1 = c1.add(-cnt); c2 = c2.add(-cnt); update = true; } } else { long shift = Math.min(c1.l, c2.l); if ( shift > 0 ) { c1 = c1.add(-shift); c2 = c2.add(-shift); update = true; } } if ( c1.isZero() && (i1 + 1 < N1) ) { c1 = x1[++i1]; update = true; } if ( c2.isZero() && (i2 + 1 < N2) ) { c2 = x2[++i2]; update = true; } } System.out.println(ans); } private static class Pair { public final Long v; public final Long l; public Pair(Long v, Long l) { this.v = v; this.l = l; } public Pair add(long shift) { return new Pair(this.v, this.l + shift); } public boolean isZero() { return this.l.equals(0L); } } }
ConDefects/ConDefects/Code/abc294_e/Java/48660205
condefects-java_data_393
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class Main { public static int H; public static int W; public static int[][] A= new int [15][15]; public static void main(String[] args) { Scanner sc = new Scanner(System.in); H = sc.nextInt(); W = sc.nextInt(); for(int i=1;i<=H;i++)for(int j=1;j<=W;j++)A[i][j]=sc.nextInt(); int ans =0; int bit = (1<<(H+W-2)); for(int i=0;i<=bit-1;i++) { int count =0; for(int x=H+W-3;x>=0;x--) { int wari = (1<<x); if((i/wari)%2==1)count++; } if(count==H-1) { ArrayList<Integer>list = new ArrayList<>(); int x =1;int y=1; list.add(A[1][1]); for(int m=H+W-3;m>=0;m--) { int wari = (1<<m); if((i/wari)%2==1)x++; else y++; list.add(A[x][y]); } Collections.sort(list); boolean ok = true; for(int k=0;k<list.size()-1;k++) { if(list.get(k)== list.get(k+1)) { ok = false; break; } } if(ok)ans++; } } System.out.println(ans); } } import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class Main { public static int H; public static int W; public static int[][] A= new int [15][15]; public static void main(String[] args) { Scanner sc = new Scanner(System.in); H = sc.nextInt(); W = sc.nextInt(); for(int i=1;i<=H;i++)for(int j=1;j<=W;j++)A[i][j]=sc.nextInt(); int ans =0; int bit = (1<<(H+W-2)); for(int i=0;i<=bit-1;i++) { int count =0; for(int x=H+W-3;x>=0;x--) { int wari = (1<<x); if((i/wari)%2==1)count++; } if(count==H-1) { ArrayList<Integer>list = new ArrayList<>(); int x =1;int y=1; list.add(A[1][1]); for(int m=H+W-3;m>=0;m--) { int wari = (1<<m); if((i/wari)%2==1)x++; else y++; list.add(A[x][y]); } Collections.sort(list); boolean ok = true; for(int k=0;k<list.size()-1;k++) { if(list.get(k).equals(list.get(k+1))) { ok = false; break; } } if(ok)ans++; } } System.out.println(ans); } }
ConDefects/ConDefects/Code/abc293_c/Java/44668898
condefects-java_data_394
import java.io.*; import java.util.*; public class Main { public static int INF = 0x3f3f3f3f, mod = 1000000007, mod9 = 998244353, base = (int)2e9+10; public static void main(String args[]){ try { PrintWriter o = new PrintWriter(System.out); boolean multiTest = false; // init if(multiTest) { int t = fReader.nextInt(), loop = 0; while (loop < t) {loop++;solve(o);} } else solve(o); o.close(); } catch (Exception e) {e.printStackTrace();} } static void solve(PrintWriter o) { try { int n = fReader.nextInt(), m = fReader.nextInt(); int[][] dxy = new int[3][2]; for(int i=0;i<3;i++) { int dx = fReader.nextInt(), dy = fReader.nextInt(); dxy[i][0] = dx; dxy[i][1] = dy; } Set<Long> set = new HashSet<>(); int[] X = new int[m], Y = new int[m]; for(int i=0;i<m;i++) { X[i] = fReader.nextInt(); Y[i] = fReader.nextInt(); set.add(1l*X[i]*base+Y[i]); } long[][][] dp = new long[n+10][n+10][n+10]; dp[0][0][0] = 1; long mx = 0l; for(int i=1;i<=n;i++) { for(int j=0;j<=i;j++) { for(int k=0;k<=i-j;k++) { long nx = 1l*j*dxy[0][0] + 1l*k*dxy[1][0] + 1l*(i-j-k)*dxy[2][0]; long ny = 1l*j*dxy[0][1] + 1l*k*dxy[1][1] + 1l*(i-j-k)*dxy[2][1]; mx = Math.max(mx, nx); if(nx <= 1e9 && nx >= -1e9 || ny <= 1e9 || ny >= -1e9 && set.contains(nx*base+ny)) continue; dp[i][j][k] += dp[i-1][j][k]; if(j > 0) dp[i][j][k] += dp[i-1][j-1][k]; if(k > 0) dp[i][j][k] += dp[i-1][j][k-1]; dp[i][j][k] %= mod9; } } } Long res = 0l; for(int i=0;i<=n;i++) { for(int j=0;j<=n-i;j++) { res += dp[n][i][j]; res %= mod9; } } o.println(res); } catch (Exception e){e.printStackTrace();} } public static int upper_bound(List<Integer> a, int val){ int l = 0, r = a.size(); while(l < r){ int mid = l + (r - l) / 2; if(a.get(mid) <= val) l = mid + 1; else r = mid; } return l; } public static int lower_bound(List<Integer> a, int val){ int l = 0, r = a.size(); while(l < r){ int mid = l + (r - l) / 2; if(a.get(mid) < val) l = mid + 1; else r = mid; } return l; } public static long gcd(long a, long b){ return b == 0 ? a : gcd(b, a%b); } public static long lcm(long a, long b){ return a / gcd(a,b)*b; } public static boolean isPrime(long x){ boolean ok = true; for(long i=2;i<=Math.sqrt(x);i++){ if(x % i == 0){ ok = false; break; } } return ok; } public static void reverse(int[] array){ reverse(array, 0 , array.length-1); } public static void reverse(int[] array, int left, int right) { if (array != null) { int i = left; for(int j = right; j > i; ++i) { int tmp = array[j]; array[j] = array[i]; array[i] = tmp; --j; } } } public static long qpow(long a, long n){ long ret = 1l; while(n > 0){ if((n & 1) == 1){ ret = ret * a % mod; } n >>= 1; a = a * a % mod; } return ret; } public static class DSU { int[] parent; int[] size; int n; public DSU(int n){ this.n = n; parent = new int[n]; size = new int[n]; for(int i=0;i<n;i++){ parent[i] = i; size[i] = 1; } } public int find(int p){ while(parent[p] != p){ parent[p] = parent[parent[p]]; p = parent[p]; } return p; } public void union(int p, int q){ int root_p = find(p); int root_q = find(q); if(root_p == root_q) return; if(size[root_p] >= size[root_q]){ parent[root_q] = root_p; size[root_p] += size[root_q]; size[root_q] = 0; } else{ parent[root_p] = root_q; size[root_q] += size[root_p]; size[root_p] = 0; } n--; } public int getTotalComNum(){ return n; } public int getSize(int i){ return size[find(i)]; } } public static class FenWick { int n; long[] tree; public FenWick(int n){ this.n = n; tree = new long[n+1]; } private void add(int x, long val){ while(x <= n){ tree[x] += val; x += x&-x; } } private long query(int x){ long ret = 0l; while(x > 0){ ret += tree[x]; x -= x&-x; } return ret; } } public static class fReader { private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private static StringTokenizer tokenizer = new StringTokenizer(""); private static String next() throws IOException{ while(!tokenizer.hasMoreTokens()){tokenizer = new StringTokenizer(reader.readLine());} return tokenizer.nextToken(); } public static int nextInt() throws IOException {return Integer.parseInt(next());} public static Long nextLong() throws IOException {return Long.parseLong(next());} public static double nextDouble() throws IOException {return Double.parseDouble(next());} public static char nextChar() throws IOException {return next().toCharArray()[0];} public static String nextString() throws IOException {return next();} public static String nextLine() throws IOException {return reader.readLine();} } } import java.io.*; import java.util.*; public class Main { public static int INF = 0x3f3f3f3f, mod = 1000000007, mod9 = 998244353, base = (int)2e9+10; public static void main(String args[]){ try { PrintWriter o = new PrintWriter(System.out); boolean multiTest = false; // init if(multiTest) { int t = fReader.nextInt(), loop = 0; while (loop < t) {loop++;solve(o);} } else solve(o); o.close(); } catch (Exception e) {e.printStackTrace();} } static void solve(PrintWriter o) { try { int n = fReader.nextInt(), m = fReader.nextInt(); int[][] dxy = new int[3][2]; for(int i=0;i<3;i++) { int dx = fReader.nextInt(), dy = fReader.nextInt(); dxy[i][0] = dx; dxy[i][1] = dy; } Set<Long> set = new HashSet<>(); int[] X = new int[m], Y = new int[m]; for(int i=0;i<m;i++) { X[i] = fReader.nextInt(); Y[i] = fReader.nextInt(); set.add(1l*X[i]*base+Y[i]); } long[][][] dp = new long[n+10][n+10][n+10]; dp[0][0][0] = 1; long mx = 0l; for(int i=1;i<=n;i++) { for(int j=0;j<=i;j++) { for(int k=0;k<=i-j;k++) { long nx = 1l*j*dxy[0][0] + 1l*k*dxy[1][0] + 1l*(i-j-k)*dxy[2][0]; long ny = 1l*j*dxy[0][1] + 1l*k*dxy[1][1] + 1l*(i-j-k)*dxy[2][1]; mx = Math.max(mx, nx); if(nx <= 1e9 && nx >= -1e9 && ny <= 1e9 && ny >= -1e9 && set.contains(nx*base+ny)) continue; dp[i][j][k] += dp[i-1][j][k]; if(j > 0) dp[i][j][k] += dp[i-1][j-1][k]; if(k > 0) dp[i][j][k] += dp[i-1][j][k-1]; dp[i][j][k] %= mod9; } } } Long res = 0l; for(int i=0;i<=n;i++) { for(int j=0;j<=n-i;j++) { res += dp[n][i][j]; res %= mod9; } } o.println(res); } catch (Exception e){e.printStackTrace();} } public static int upper_bound(List<Integer> a, int val){ int l = 0, r = a.size(); while(l < r){ int mid = l + (r - l) / 2; if(a.get(mid) <= val) l = mid + 1; else r = mid; } return l; } public static int lower_bound(List<Integer> a, int val){ int l = 0, r = a.size(); while(l < r){ int mid = l + (r - l) / 2; if(a.get(mid) < val) l = mid + 1; else r = mid; } return l; } public static long gcd(long a, long b){ return b == 0 ? a : gcd(b, a%b); } public static long lcm(long a, long b){ return a / gcd(a,b)*b; } public static boolean isPrime(long x){ boolean ok = true; for(long i=2;i<=Math.sqrt(x);i++){ if(x % i == 0){ ok = false; break; } } return ok; } public static void reverse(int[] array){ reverse(array, 0 , array.length-1); } public static void reverse(int[] array, int left, int right) { if (array != null) { int i = left; for(int j = right; j > i; ++i) { int tmp = array[j]; array[j] = array[i]; array[i] = tmp; --j; } } } public static long qpow(long a, long n){ long ret = 1l; while(n > 0){ if((n & 1) == 1){ ret = ret * a % mod; } n >>= 1; a = a * a % mod; } return ret; } public static class DSU { int[] parent; int[] size; int n; public DSU(int n){ this.n = n; parent = new int[n]; size = new int[n]; for(int i=0;i<n;i++){ parent[i] = i; size[i] = 1; } } public int find(int p){ while(parent[p] != p){ parent[p] = parent[parent[p]]; p = parent[p]; } return p; } public void union(int p, int q){ int root_p = find(p); int root_q = find(q); if(root_p == root_q) return; if(size[root_p] >= size[root_q]){ parent[root_q] = root_p; size[root_p] += size[root_q]; size[root_q] = 0; } else{ parent[root_p] = root_q; size[root_q] += size[root_p]; size[root_p] = 0; } n--; } public int getTotalComNum(){ return n; } public int getSize(int i){ return size[find(i)]; } } public static class FenWick { int n; long[] tree; public FenWick(int n){ this.n = n; tree = new long[n+1]; } private void add(int x, long val){ while(x <= n){ tree[x] += val; x += x&-x; } } private long query(int x){ long ret = 0l; while(x > 0){ ret += tree[x]; x -= x&-x; } return ret; } } public static class fReader { private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private static StringTokenizer tokenizer = new StringTokenizer(""); private static String next() throws IOException{ while(!tokenizer.hasMoreTokens()){tokenizer = new StringTokenizer(reader.readLine());} return tokenizer.nextToken(); } public static int nextInt() throws IOException {return Integer.parseInt(next());} public static Long nextLong() throws IOException {return Long.parseLong(next());} public static double nextDouble() throws IOException {return Double.parseDouble(next());} public static char nextChar() throws IOException {return next().toCharArray()[0];} public static String nextString() throws IOException {return next();} public static String nextLine() throws IOException {return reader.readLine();} } }
ConDefects/ConDefects/Code/abc265_e/Java/34267583
condefects-java_data_395
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[][] a = new int[n][n]; int[][] b = new int[n][n]; for (int i = 0; i < n * 2; i++){ for (int j = 0; j < n; j++){ if (i < n){ a[i][j] = sc.nextInt(); }else{ b[i - n][j] = sc.nextInt(); } } } sc.close(); for(int i = 0; i < 3; i++) { boolean bool = true; for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { if(a[j][k] ==1 && b[j][k] != 1) { bool = false; } } } if(bool) { System.out.println("Yes"); return; } rotate(a, n); } System.out.println("No"); } public static void rotate(int array[][], int n){ int[][] temp = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { temp[i][j] = array[(n - 1) - j][i]; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { array[i][j] = temp[i][j]; } } } } import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[][] a = new int[n][n]; int[][] b = new int[n][n]; for (int i = 0; i < n * 2; i++){ for (int j = 0; j < n; j++){ if (i < n){ a[i][j] = sc.nextInt(); }else{ b[i - n][j] = sc.nextInt(); } } } sc.close(); for(int i = 0; i < 4; i++) { boolean bool = true; for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { if(a[j][k] ==1 && b[j][k] != 1) { bool = false; } } } if(bool) { System.out.println("Yes"); return; } rotate(a, n); } System.out.println("No"); } public static void rotate(int array[][], int n){ int[][] temp = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { temp[i][j] = array[(n - 1) - j][i]; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { array[i][j] = temp[i][j]; } } } }
ConDefects/ConDefects/Code/abc298_b/Java/41644303
condefects-java_data_396
import com.sun.source.tree.WhileLoopTree; import java.io.*; import java.util.*; public class Main { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); static StringTokenizer st; static int MOD = (int) 1e9+7; public static void main(String[] args) throws IOException { int n = readInt(); List<int[]> prA = new ArrayList<>(); List<int[]> prB = new ArrayList<>(); int[] A = new int[n], B = new int[n]; for (int i = 0; i < n; i++) { A[i] = readInt(); prA.add(new int[] {A[i], i}); } for (int i = 0; i < n; i++) { B[i] = readInt(); prB.add(new int[] {B[i], i}); } int ans = 0; // A fully sorted prA.sort(Comparator.comparingInt(v -> v[0])); List<Integer> lst = new ArrayList<>(); for (int[] v : prA) { lst.add(B[v[1]]); } ans = Math.max(ans, n + LIS(lst)); // B fully sorted prB.sort(Comparator.comparingInt(v -> v[0])); lst = new ArrayList<>(); for (int[] v : prB) { lst.add(A[v[1]]); } ans = Math.max(ans, n + LIS(lst)); System.out.println(ans); } static int LIS(List<Integer> a) { List<Integer> lst = new ArrayList<>(); for (int v : a) { if (lst.isEmpty() || lst.get(lst.size() - 1) < v) { lst.add(v); } else { int p = Collections.binarySearch(lst, v); if (p < 0) p = -p - 1; a.set(p, v); } } return lst.size(); } static String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine().trim()); return st.nextToken(); } static long readLong() throws IOException { return Long.parseLong(next()); } static int readInt() throws IOException { return Integer.parseInt(next()); } static double readDouble() throws IOException { return Double.parseDouble(next()); } static char readCharacter() throws IOException { return next().charAt(0); } static String readLine() throws IOException { return br.readLine().trim(); } static int readLongLineInt() throws IOException{ int x = 0, c; while((c = br.read()) != ' ' && c != '\n') x = x * 10 + (c - '0'); return x; } } import com.sun.source.tree.WhileLoopTree; import java.io.*; import java.util.*; public class Main { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); static StringTokenizer st; static int MOD = (int) 1e9+7; public static void main(String[] args) throws IOException { int n = readInt(); List<int[]> prA = new ArrayList<>(); List<int[]> prB = new ArrayList<>(); int[] A = new int[n], B = new int[n]; for (int i = 0; i < n; i++) { A[i] = readInt(); prA.add(new int[] {A[i], i}); } for (int i = 0; i < n; i++) { B[i] = readInt(); prB.add(new int[] {B[i], i}); } int ans = 0; // A fully sorted prA.sort(Comparator.comparingInt(v -> v[0])); List<Integer> lst = new ArrayList<>(); for (int[] v : prA) { lst.add(B[v[1]]); } ans = Math.max(ans, n + LIS(lst)); // B fully sorted prB.sort(Comparator.comparingInt(v -> v[0])); lst = new ArrayList<>(); for (int[] v : prB) { lst.add(A[v[1]]); } ans = Math.max(ans, n + LIS(lst)); System.out.println(ans); } static int LIS(List<Integer> a) { List<Integer> lst = new ArrayList<>(); for (int v : a) { if (lst.isEmpty() || lst.get(lst.size() - 1) < v) { lst.add(v); } else { int p = Collections.binarySearch(lst, v); if (p < 0) p = -p - 1; lst.set(p, v); } } return lst.size(); } static String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine().trim()); return st.nextToken(); } static long readLong() throws IOException { return Long.parseLong(next()); } static int readInt() throws IOException { return Integer.parseInt(next()); } static double readDouble() throws IOException { return Double.parseDouble(next()); } static char readCharacter() throws IOException { return next().charAt(0); } static String readLine() throws IOException { return br.readLine().trim(); } static int readLongLineInt() throws IOException{ int x = 0, c; while((c = br.read()) != ' ' && c != '\n') x = x * 10 + (c - '0'); return x; } }
ConDefects/ConDefects/Code/arc149_b/Java/42005594
condefects-java_data_397
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; Map<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); map.put(a[i], i); } int[] b = new int[n]; for (int i = 0; i < n; i++) { b[i] = sc.nextInt(); } for (int i = 0; i < n; i++) { int loc = map.get(i+1); swap(a, i, loc); map.put(i+1, i); map.put(a[i],loc); swap(b, i, loc); } int ans = 0; ans += n; ans += lengthOfLIS(b); System.out.println(ans); } public static int lengthOfLIS(int[] nums) { int maxL = 0; int[] dp = new int[nums.length]; for(int num : nums) { // 二分法查找, 也可以调用库函数如binary_search int lo = 0, hi = maxL; while(lo < hi) { int mid = lo+(hi-lo)/2; if(dp[mid] < num) lo = mid+1; else hi = mid; } dp[lo] = num; if(lo == maxL) maxL++; } return maxL; } public static void swap(int[] nums, int i,int j) { int tem = nums[i]; nums[i] = nums[j]; nums[j] = tem; } } import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; Map<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); map.put(a[i], i); } int[] b = new int[n]; for (int i = 0; i < n; i++) { b[i] = sc.nextInt(); } for (int i = 0; i < n; i++) { int loc = map.get(i+1); map.put(i+1, i); map.put(a[i],loc); swap(a, i, loc); swap(b, i, loc); } int ans = 0; ans += n; ans += lengthOfLIS(b); System.out.println(ans); } public static int lengthOfLIS(int[] nums) { int maxL = 0; int[] dp = new int[nums.length]; for(int num : nums) { // 二分法查找, 也可以调用库函数如binary_search int lo = 0, hi = maxL; while(lo < hi) { int mid = lo+(hi-lo)/2; if(dp[mid] < num) lo = mid+1; else hi = mid; } dp[lo] = num; if(lo == maxL) maxL++; } return maxL; } public static void swap(int[] nums, int i,int j) { int tem = nums[i]; nums[i] = nums[j]; nums[j] = tem; } }
ConDefects/ConDefects/Code/arc149_b/Java/35363309
condefects-java_data_398
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); B_TwoLISSum solver = new B_TwoLISSum(); solver.solve(1, in, out); out.close(); } static class B_TwoLISSum { public void solve(int testNumber, FastScanner in, PrintWriter out) { int n = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt() - 1; } int[] b = new int[n]; for (int i = 0; i < n; i++) { b[i] = in.nextInt() - 1; } Integer[] o = new Integer[n]; for (int i = 0; i < n; i++) { o[i] = i; } int[] c = new int[n]; int ans = 0; for (int step = 0; step < 2; step++) { final int[] fa = a; Arrays.sort(o, (u, v) -> Integer.compare(fa[u], fa[v])); for (int i = 0; i < n; i++) { c[i] = b[o[i]]; } ans = Math.max(ans, n + lis(b)); int[] t = a; a = b; b = t; } out.println(ans); } private int lis(int[] a) { int n = a.length; int[] lis = new int[n]; int[] smallest = new int[n]; int k = 0; for (int i = 0; i < n; i++) { int l = -1; int r = k; while (r - l > 1) { int m = (l + r) / 2; if (smallest[m] <= a[i]) { l = m; } else { r = m; } } smallest[r] = a[i]; if (r == k) { ++k; } lis[i] = r + 1; } int max = 0; for (int x : lis) { max = Math.max(max, x); } return max; } } static class FastScanner { private BufferedReader in; private StringTokenizer st; public FastScanner(InputStream stream) { try { in = new BufferedReader(new InputStreamReader(stream, "UTF-8")); } catch (Exception e) { throw new AssertionError(); } } public String next() { while (st == null || !st.hasMoreTokens()) { try { String rl = in.readLine(); if (rl == null) { return null; } st = new StringTokenizer(rl); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } } import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); B_TwoLISSum solver = new B_TwoLISSum(); solver.solve(1, in, out); out.close(); } static class B_TwoLISSum { public void solve(int testNumber, FastScanner in, PrintWriter out) { int n = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt() - 1; } int[] b = new int[n]; for (int i = 0; i < n; i++) { b[i] = in.nextInt() - 1; } Integer[] o = new Integer[n]; for (int i = 0; i < n; i++) { o[i] = i; } int[] c = new int[n]; int ans = 0; for (int step = 0; step < 2; step++) { final int[] fa = a; Arrays.sort(o, (u, v) -> Integer.compare(fa[u], fa[v])); for (int i = 0; i < n; i++) { c[i] = b[o[i]]; } ans = Math.max(ans, n + lis(c)); int[] t = a; a = b; b = t; } out.println(ans); } private int lis(int[] a) { int n = a.length; int[] lis = new int[n]; int[] smallest = new int[n]; int k = 0; for (int i = 0; i < n; i++) { int l = -1; int r = k; while (r - l > 1) { int m = (l + r) / 2; if (smallest[m] <= a[i]) { l = m; } else { r = m; } } smallest[r] = a[i]; if (r == k) { ++k; } lis[i] = r + 1; } int max = 0; for (int x : lis) { max = Math.max(max, x); } return max; } } static class FastScanner { private BufferedReader in; private StringTokenizer st; public FastScanner(InputStream stream) { try { in = new BufferedReader(new InputStreamReader(stream, "UTF-8")); } catch (Exception e) { throw new AssertionError(); } } public String next() { while (st == null || !st.hasMoreTokens()) { try { String rl = in.readLine(); if (rl == null) { return null; } st = new StringTokenizer(rl); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
ConDefects/ConDefects/Code/arc149_b/Java/35349242
condefects-java_data_399
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; import java.util.function.BinaryOperator; import java.util.function.Predicate; public class Main { public static void main(String[] args) { Solver.SOLVE(); } } class UnionFind { private int[] roots; public UnionFind(int n){ roots = new int[n]; for (int i = 0; i < n; i++) { roots[i] = i; } } public int root(int x){ if(roots[x] == x){ return x; } return roots[x] = root(roots[x]); } public void unite(int x,int y){ int rx = root(x); int ry = root(y); if(rx == ry){ return; } roots[rx] = ry; } public boolean same(int x,int y){ int rx = root(x); int ry = root(y); return rx == ry; } } class DSU { private int n; private int[] parentOrSize; public DSU(int n) { this.n = n; this.parentOrSize = new int[n]; java.util.Arrays.fill(parentOrSize, -1); } int merge(int a, int b) { if (!(0 <= a && a < n)) throw new IndexOutOfBoundsException("a=" + a); if (!(0 <= b && b < n)) throw new IndexOutOfBoundsException("b=" + b); int x = leader(a); int y = leader(b); if (x == y) return x; if (-parentOrSize[x] < -parentOrSize[y]) { int tmp = x; x = y; y = tmp; } parentOrSize[x] += parentOrSize[y]; parentOrSize[y] = x; return x; } boolean same(int a, int b) { if (!(0 <= a && a < n)) throw new IndexOutOfBoundsException("a=" + a); if (!(0 <= b && b < n)) throw new IndexOutOfBoundsException("b=" + b); return leader(a) == leader(b); } int leader(int a) { if (parentOrSize[a] < 0) { return a; } else { parentOrSize[a] = leader(parentOrSize[a]); return parentOrSize[a]; } } int size(int a) { if (!(0 <= a && a < n)) throw new IndexOutOfBoundsException("" + a); return -parentOrSize[leader(a)]; } ArrayList<ArrayList<Integer>> groups() { int[] leaderBuf = new int[n]; int[] groupSize = new int[n]; for (int i = 0; i < n; i++) { leaderBuf[i] = leader(i); groupSize[leaderBuf[i]]++; } ArrayList<ArrayList<Integer>> result = new ArrayList<>(n); for (int i = 0; i < n; i++) { result.add(new ArrayList<>(groupSize[i])); } for (int i = 0; i < n; i++) { result.get(leaderBuf[i]).add(i); } result.removeIf(ArrayList::isEmpty); return result; } } class PairL implements Comparable<PairL>, Comparator<PairL> { public long x,y; public PairL(long x,long y) { this.x = x; this.y = y; } public void swap(){ long t = x; x = y; y = t; } @Override public int compare(PairL o1, PairL o2) { return o1.compareTo(o2); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; PairL pairl = (PairL) o; return x == pairl.x && y == pairl.y; } @Override public int hashCode() { return Objects.hash(x, y); } public PairL add(PairL p){ return new PairL(x+p.x,y+p.y); } @Override public int compareTo(PairL o) { return Long.compare(x,o.x); } } class PairI implements Comparable<PairI>, Comparator<PairI> { public int x,y; public PairI(int x,int y) { this.x = x; this.y = y; } public void swap(){ int t = x; x = y; y = t; } @Override public int compare(PairI o1, PairI o2) { if(o1.x == o2.x){ return Integer.compare(o1.y,o2.y); } return Integer.compare(o1.x,o2.x); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; PairI pairI = (PairI) o; return x == pairI.x && y == pairI.y; } @Override public int hashCode() { return Objects.hash(x, y); } @Override public int compareTo(PairI o) { if(x == o.x){ return Integer.compare(y,o.y); } return Integer.compare(x,o.x); } public PairI add(PairI p){ return new PairI(x+p.x,y+p.y); } public PairI sub(PairI p){ return new PairI(x-p.x,y-p.y); } public PairI addG(PairI p,int h,int w) { int x = this.x + p.x; int y = this.y + p.y; if(0 <= x&&x < w&&0 <= y&&y < h){ return new PairI(x,y); } return null; } } class Dist extends PairI{ int d; public Dist(int x,int y,int d){ super(x,y); this.d = d; } public Dist addG(PairI p,int h,int w) { int x = this.x + p.x; int y = this.y + p.y; if(0 <= x&&x < w&&0 <= y&&y < h){ return new Dist(x,y,d+1); } return null; } } class Tuple implements Comparable<Tuple>{ public int x,y,z; public Tuple(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Tuple three = (Tuple) o; return x == three.x && y == three.y && z == three.z; } @Override public int hashCode() { return Objects.hash(x, y, z); } @Override public int compareTo(Tuple o) { return Integer.compare(z,o.z); } } class Node implements Comparable<Node>{ public int from; public int to; public long d; public Node(int to,long d){ this.to = to; this.d = d; } public Node(int from,int to,long d){ this.to = to; this.from = from; this.d = d; } @Override public int compareTo(Node o) { return Long.compare(d,o.d); } } class PairC{ char a,b; public PairC(char a, char b){ this.a = a; this.b = b; } } class IB{ int i; boolean b; IB(int i,boolean b){ this.i = i; this.b = b; } } class CI{ char c; int i; CI(char c,int i){ this.c = c; this.i = i; } } class Solver { public static final int MOD1 = 1000000007; public static final int MOD9 = 998244353; public static Scanner sc = new Scanner(System.in); public static final int inf = 2000000000; public static final int ninf = -inf; public static final char[] alpha = "abcdefghijklmnopqrstuvwxyz".toCharArray(); public static final char[] ALPHA = "abcdefghijklmnopqrstuvwxyz".toUpperCase().toCharArray(); public static FastScanner fs = new FastScanner(); public static PrintWriter out = new PrintWriter(System.out); public static final PairI[] move = new PairI[]{new PairI(1,0),new PairI(0,1),new PairI(-1,0),new PairI(0,-1)}; public static void solve() { int n = rI(); int[] a = rIv(n); int[] b = rIv(n); int[] f = new int[n]; for (int i = 0; i < n; i++) { f[a[i]-1] = b[i]; } int[] dp = new int[n+1]; Arrays.fill(dp,inf); dp[0] = -1; for (int i = 0; i < n; i++) { int ng = -1; int ok = a.length; while (abs(ng-ok) > 1){ int mid = (ok+ng)/2; if(dp[mid] >= f[i]){ ok = mid; }else { ng = mid; } } dp[ok] = min(a[i],dp[ok]); } int max = 0; for (int i = 0; i < n+1; i++) { if(dp[i] != inf) max = max(max,i); } oI(n+max); } static boolean check(long t,long a,long b){ for (long x = 0; x < t; x++) { long y = t-x; if((b+y)%(a+x) == 0){ return true; } } return false; } static int degL(long x){ return String.valueOf(x).length(); } static int degN(long x,int d){ x/=pow(10,d); return (int)(x%10); } static long comb(int n,int r){ if(r*2 > n){ r = n-r; } long ans = 1; for (int i = 0; i < r; i++) { ans*=(n-i); ans/=(i+1); } return ans; } static boolean isPalindrome(int a,int b,String s){ int dif = b-a; boolean ok = true; for (int i = 0; i < dif; i++) { if (s.charAt(i + a) != s.charAt(b - i)) { ok = false; break; } } return ok; } static int intValue(char c){ return Integer.parseInt(String.valueOf(c)); } @SuppressWarnings("unchecked") static class SegTree<T>{ T[] data; int size; BinaryOperator<T> op; T e; int inisize; SegTree(T[] a,BinaryOperator<T> op,T e){ size = 1; inisize = a.length; while (size < a.length){ size <<= 1; } this.e = e; this.op = op; data = (T[])new Object[size*2]; Arrays.fill(data,e); System.arraycopy(a,0,data,size,a.length); for (int i = size-1; i > 0; i--) { data[i] = op.apply(data[i*2],data[i*2+1]); } } void update(int i,T x){ if(i < 0) throw new IllegalArgumentException(); i += size; data[i] = x; while (i > 1){ data[i >> 1] = op.apply(data[i],data[i^1]); i >>= 1; } } T query(int l,int r){ if(l < 0) throw new IllegalArgumentException(); if(r < 0) throw new IllegalArgumentException(); T res = e; l += size; r += size; while (l < r){ if((l & 1) == 1) { res = op.apply(res,data[l]); l++; } if((r & 1) == 1){ res = op.apply(res,data[r-1]); } l >>= 1; r >>= 1; } return res; } int maxRight(int l,Predicate<T> pr){ if (!pr.test(e)) { throw new IllegalArgumentException(); } if(l == inisize) return l; l+=size; T sum = e; do { l >>= Integer.numberOfTrailingZeros(l); if (!pr.test(op.apply(sum, data[l]))) { while (l < size) { l = l << 1; if (pr.test(op.apply(sum, data[l]))) { sum = op.apply(sum, data[l]); l++; } } return l - size; } sum = op.apply(sum, data[l]); l++; } while ((l & -l) != l); return inisize; } public int minLeft(int r,Predicate<T> pr) { if (!pr.test(e)) { throw new IllegalArgumentException(); } if (r == 0) { return 0; } r += size; T sum = e; do { r--; while (r > 1 && (r & 1) == 1) { r >>= 1; } if (!pr.test(op.apply(data[r], sum))) { while (r < size) { r = r << 1 | 1; if (pr.test(op.apply(data[r], sum))) { sum = op.apply(data[r], sum); r--; } } return r + 1 - size; } sum = op.apply(data[r], sum); } while ((r & -r) != r); return 0; } } public static int toIntC(char c){ for (int i = 0; i < ALPHA.length; i++) { if(c == ALPHA[i]){ return i+1; } } throw new IllegalArgumentException("not an alphabet"); } public static int toInt(char c){ for (int i = 0; i < alpha.length; i++) { if(c == alpha[i]){ return i+1; } } throw new IllegalArgumentException("not an alphabet"); } public static void reverse(int[] a){ int[] tmp = a.clone(); for (int i = 0; i < a.length; i++) { a[i] = tmp[a.length - 1 - i]; } } public static int[] compress(int[] a){ int[] b = erase(a); Arrays.sort(b); for (int i = 0; i < a.length; i++) { a[i] = lower(b,a[i]); } return a; } public static int lower(int[] a,int x){ int low = 0, high = a.length; int mid; while (low < high) { mid = low + (high - low) / 2; if (x <= a[mid]) { high = mid; } else { low = mid + 1; } } if (low < a.length && a[low] < x) { low++; } return low; } public static int lower(long[] a,long x){ int low = 0, high = a.length; int mid; while (low < high) { mid = low + (high - low) / 2; if (x <= a[mid]) { high = mid; } else { low = mid + 1; } } if (low < a.length && a[low] < x) { low++; } return low; } public static int upper(int[] a,int x){ int low = 0, high = a.length; int mid; while (low < high && low != a.length) { mid = low + (high - low) / 2; if (x >= a[mid]) { low = mid + 1; } else { high = mid; } } return low; } public static int upper(long[] a,long x){ int low = 0, high = a.length; int mid; while (low < high && low != a.length) { mid = low + (high - low) / 2; if (x >= a[mid]) { low = mid + 1; } else { high = mid; } } return low; } public static int[] erase(int[] a){ HashSet<Integer> used = new HashSet<>(); ArrayList<Integer> ans = new ArrayList<>(); for (int i = 0; i < a.length; i++) { if(!used.contains(a[i])){ used.add(a[i]); ans.add(a[i]); } } return convI(ans); } public static int abs(int a){ return Math.abs(a); } public static long abs(long a){ return Math.abs(a); } public static int max(int a,int b){ return Math.max(a,b); } public static int max(int... a){ int max = ninf; for (int i = 0; i < a.length; i++) { max = max(a[i],max); } return max; } public static long max(long a,long b){ return Math.max(a,b); } public static long max(long... a){ long max = Long.MAX_VALUE; for (int i = 0; i < a.length; i++) { max = max(a[i],max); } return max; } public static int min(int a,int b){ return Math.min(a,b); } public static int min(int... a){ int min = inf; for (int i = 0; i < a.length; i++) { min = min(a[i],min); } return min; } public static long min(long a,long b){ return Math.min(a, b); } public static long min(long... a){ long min = inf; for (int i = 0; i < a.length; i++) { min = min(a[i],min); } return min; } public static final class MC { private final int mod; public MC(final int mod) { this.mod = mod; } public long mod(long x) { x %= mod; if (x < 0) { x += mod; } return x; } public long add(final long a, final long b) { return mod(mod(a) + mod(b)); } public long add(final long... a){ long ans = a[0]; for (int i = 1; i < a.length; i++) { ans = add(ans,a[i]); } return mod(ans); } public long mul(final long a, final long b) { return mod(mod(a) * mod(b)); } public long mul(final long... a){ long ans = a[0]; for (int i = 1; i < a.length; i++) { ans = mul(ans,a[i]); } return mod(ans); } public long div(final long numerator, final long denominator) { return mod(numerator * inverse(denominator)); } public long power(long base, long exp) { long ret = 1; base %= mod; while (exp > 0) { if ((exp & 1) == 1) { ret = mul(ret, base); } base = mul(base, base); exp >>= 1; } return ret; } public long inverse(final long x) { return power(x, mod - 2); } public long factorial(final int n) { return product(1, n); } public long product(final int start, final int end) { long result = 1; for (int i = start; i <= end; i++) { result *= i; result %= mod; } return result; } public long combination(final int n, int r) { if (r > n) { return 0; } return div(product(n - r + 1, n), factorial(r)); } } public static long pow(long x,long n){ long ans = 1L; long tmp = x; while (true){ if(n < 1L){ break; } if(n % 2L == 1L){ ans*=tmp; } tmp *=tmp; n = n >> 1; } return ans; } public static long modPow(long x,long n,long m){ long ans = 1L; long tmp = x; while (true){ if(n < 1L){ break; } if(n % 2L == 1L){ ans*=tmp; ans%=m; } tmp *=tmp; tmp%=m; n = n >> 1; } return ans; } public static int gcd(int a,int b){ if(b == 0) return a; else return gcd(b,a%b); } public static long gcd(long a,long b){ if(b == 0) return a; else return gcd(b,a%b); } public static int gcd(int... a){ int ans = a[0]; for (int i = 1; i < a.length; i++) { ans = gcd(ans,a[i]); } return ans; } public static long gcd(long... a){ long ans = a[0]; for (int i = 1; i < a.length; i++) { ans = gcd(ans,a[i]); } return ans; } public static long lcm(int a,int b){ return (long) a / gcd(a, b) * b; } public static long lcm(long a,long b){ return a / gcd(a,b) * b; } public static boolean isPrime(long x){ if(x < 2) return false; else if(x == 2) return true; if(x%2 == 0) return false; for(long i = 3; i*i <= x; i+= 2){ if(x%i == 0) return false; } return true; } public static int rI() { return fs.nextInt(); } public static int[] rIv(int length) { int[] res = new int[length]; for (int i = 0; i < length; i++) { res[i] = fs.nextInt(); } return res; } public static String rS() { return fs.next(); } public static String[] rSv(int length) { String[] res = new String[length]; for (int i = 0; i < length; i++) res[i] = fs.next(); return res; } public static long rL() { return fs.nextLong(); } public static long[] rLv(int length) { long[] res = new long[length]; for (int i = 0; i < length; i++) res[i] = fs.nextLong(); return res; } public static double rD(){ return fs.nextDouble(); } public static double[] rDv(int length){ double[] res = new double[length]; for (int i = 0; i < length; i++) res[i] = rD(); return res; } public static String aiS(int[] a){ StringBuilder ans = new StringBuilder(); for (int i = 0; i < a.length; i++) { if(i != 0){ ans.append(' '); } ans.append(a[i]); } return ans.toString(); } public static String alS(long[] a){ StringBuilder ans = new StringBuilder(); for (int i = 0; i < a.length; i++) { if(i != 0){ ans.append(' '); } ans.append(a[i]); } return ans.toString(); } public static String adS(double[] a){ StringBuilder ans = new StringBuilder(); for (int i = 0; i < a.length; i++) { if(i != 0){ ans.append(' '); } ans.append(a[i]); } return ans.toString(); } public static String acS(char[] a){ StringBuilder ans = new StringBuilder(); for (int i = 0; i < a.length; i++) { if(i != 0){ ans.append(' '); } ans.append(a[i]); } return ans.toString(); } public static String asS(String[] a){ StringBuilder ans = new StringBuilder(); for (int i = 0; i < a.length; i++) { if(i != 0){ ans.append(' '); } ans.append(a[i]); } return ans.toString(); } public static String liS(ArrayList<Integer> a){ StringBuilder ans = new StringBuilder(); for (int i = 0; i < a.size(); i++) { if(i != 0){ ans.append(' '); } ans.append(a.get(i)); } return ans.toString(); } public static String llS(ArrayList<Long> a){ StringBuilder ans = new StringBuilder(); for (int i = 0; i < a.size(); i++) { if(i != 0){ ans.append(' '); } ans.append(a.get(i)); } return ans.toString(); } public static String ldS(ArrayList<Double> a){ StringBuilder ans = new StringBuilder(); for (int i = 0; i < a.size(); i++) { if(i != 0){ ans.append(' '); } ans.append(a.get(i)); } return ans.toString(); } public static String lcS(ArrayList<Character> a){ StringBuilder ans = new StringBuilder(); for (int i = 0; i < a.size(); i++) { if(i != 0){ ans.append(' '); } ans.append(a.get(i)); } return ans.toString(); } public static String lsS(ArrayList<String> a){ StringBuilder ans = new StringBuilder(); for (int i = 0; i < a.size(); i++) { if(i != 0){ ans.append(' '); } ans.append(a.get(i)); } return ans.toString(); } public static void nL(){ out.println(); } public static void oI(int a) { out.println(a); } public static void onI(int a){ out.print(a); } public static void oIv(int[] a) { oS(aiS(a)); } public static void oS(String s) { out.println(s); } public static void onS(String s) { out.print(s); } public static void oSv(String[] a) { oS(asS(a)); } public static void oL(long l) { out.println(l); } public static void onL(long l) { out.print(l); } public static void oLv(long[] a) { oS(alS(a)); } public static void oD(double d){ out.println(d); } public static void onD(double d){ out.print(d); } public static void oDv(double[] d){ oS(adS(d)); } public static void oC(char c){ out.println(c); } public static void onC(char c){ out.print(c); } public static void oCv(char[] c){ oS(acS(c)); } public static void yes_no(boolean yes){ if(yes){ oS("Yes"); return; } oS("No"); } public static int fact(int num) { if (num == 0) { return 1; } else if (num == 1) { return 1; } else if (num < 0) { throw new IllegalArgumentException("factorial should be bigger than 0"); } return num * fact(num - 1); } public static int[] convI(ArrayList<Integer> list) { int[] res = new int[list.size()]; for (int i = 0; i < list.size(); i++) res[i] = list.get(i); return res; } public static long[] convL(ArrayList<Long> list) { long[] res = new long[list.size()]; for (int i = 0; i < list.size(); i++) res[i] = list.get(i); return res; } public static String[] convS(ArrayList<String> list) { String[] res = new String[list.size()]; for (int i = 0; i < list.size(); i++) res[i] = list.get(i); return res; } public static ArrayList<Integer> convI(int[] vec) { ArrayList<Integer> list = new ArrayList<>(); for (int i : vec) list.add(i); return list; } public static ArrayList<Long> convL(long[] vec) { ArrayList<Long> list = new ArrayList<>(); for (long i : vec) list.add(i); return list; } public static ArrayList<String> convS(String[] vec) { return new ArrayList<>(Arrays.asList(vec)); } public static ArrayList<ArrayList<Integer>> permutation(int a) { int[] list = new int[a]; for (int i = 0; i < a; i++) { list[i] = i; } return permutation(list); } public static ArrayList<ArrayList<Integer>> permutation(int[] seed) { ArrayList<ArrayList<Integer>> res = new ArrayList<>(); int[] perm = new int[seed.length]; boolean[] used = new boolean[seed.length]; buildPerm(seed, perm, used, 0,res); return res; } private static void buildPerm(int[] seed, int[] perm, boolean[] used, int index,ArrayList<ArrayList<Integer>> res) { if (index == seed.length) { res.add(convI(perm)); return; } for (int i = 0; i < seed.length; i++) { if (used[i]) continue; perm[index] = seed[i]; used[i] = true; buildPerm(seed, perm, used, index + 1,res); used[i] = false; } } public static ArrayList<ArrayList<String>> permutation(String[] seed) { ArrayList<ArrayList<String>> res = new ArrayList<>(); String[] perm = new String[seed.length]; boolean[] used = new boolean[seed.length]; buildPerm(seed, perm, used, 0,res); return res; } private static void buildPerm(String[] seed, String[] perm, boolean[] used, int index,ArrayList<ArrayList<String>> res) { if (index == seed.length) { res.add(convS(perm)); return; } for (int i = 0; i < seed.length; i++) { if (used[i]) continue; perm[index] = seed[i]; used[i] = true; buildPerm(seed, perm, used, index + 1,res); used[i] = false; } } public static void swap(int[] a,int i1,int i2){ int t = a[i1]; a[i1] = a[i2]; a[i2] = t; } public static void swap(char[] a,int i1,int i2){ char t = a[i1]; a[i1] = a[i2]; a[i2] = t; } public static void SOLVE(){ solve(); out.flush(); } } class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;} private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;} public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();} public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while(true){ if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next());} } import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; import java.util.function.BinaryOperator; import java.util.function.Predicate; public class Main { public static void main(String[] args) { Solver.SOLVE(); } } class UnionFind { private int[] roots; public UnionFind(int n){ roots = new int[n]; for (int i = 0; i < n; i++) { roots[i] = i; } } public int root(int x){ if(roots[x] == x){ return x; } return roots[x] = root(roots[x]); } public void unite(int x,int y){ int rx = root(x); int ry = root(y); if(rx == ry){ return; } roots[rx] = ry; } public boolean same(int x,int y){ int rx = root(x); int ry = root(y); return rx == ry; } } class DSU { private int n; private int[] parentOrSize; public DSU(int n) { this.n = n; this.parentOrSize = new int[n]; java.util.Arrays.fill(parentOrSize, -1); } int merge(int a, int b) { if (!(0 <= a && a < n)) throw new IndexOutOfBoundsException("a=" + a); if (!(0 <= b && b < n)) throw new IndexOutOfBoundsException("b=" + b); int x = leader(a); int y = leader(b); if (x == y) return x; if (-parentOrSize[x] < -parentOrSize[y]) { int tmp = x; x = y; y = tmp; } parentOrSize[x] += parentOrSize[y]; parentOrSize[y] = x; return x; } boolean same(int a, int b) { if (!(0 <= a && a < n)) throw new IndexOutOfBoundsException("a=" + a); if (!(0 <= b && b < n)) throw new IndexOutOfBoundsException("b=" + b); return leader(a) == leader(b); } int leader(int a) { if (parentOrSize[a] < 0) { return a; } else { parentOrSize[a] = leader(parentOrSize[a]); return parentOrSize[a]; } } int size(int a) { if (!(0 <= a && a < n)) throw new IndexOutOfBoundsException("" + a); return -parentOrSize[leader(a)]; } ArrayList<ArrayList<Integer>> groups() { int[] leaderBuf = new int[n]; int[] groupSize = new int[n]; for (int i = 0; i < n; i++) { leaderBuf[i] = leader(i); groupSize[leaderBuf[i]]++; } ArrayList<ArrayList<Integer>> result = new ArrayList<>(n); for (int i = 0; i < n; i++) { result.add(new ArrayList<>(groupSize[i])); } for (int i = 0; i < n; i++) { result.get(leaderBuf[i]).add(i); } result.removeIf(ArrayList::isEmpty); return result; } } class PairL implements Comparable<PairL>, Comparator<PairL> { public long x,y; public PairL(long x,long y) { this.x = x; this.y = y; } public void swap(){ long t = x; x = y; y = t; } @Override public int compare(PairL o1, PairL o2) { return o1.compareTo(o2); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; PairL pairl = (PairL) o; return x == pairl.x && y == pairl.y; } @Override public int hashCode() { return Objects.hash(x, y); } public PairL add(PairL p){ return new PairL(x+p.x,y+p.y); } @Override public int compareTo(PairL o) { return Long.compare(x,o.x); } } class PairI implements Comparable<PairI>, Comparator<PairI> { public int x,y; public PairI(int x,int y) { this.x = x; this.y = y; } public void swap(){ int t = x; x = y; y = t; } @Override public int compare(PairI o1, PairI o2) { if(o1.x == o2.x){ return Integer.compare(o1.y,o2.y); } return Integer.compare(o1.x,o2.x); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; PairI pairI = (PairI) o; return x == pairI.x && y == pairI.y; } @Override public int hashCode() { return Objects.hash(x, y); } @Override public int compareTo(PairI o) { if(x == o.x){ return Integer.compare(y,o.y); } return Integer.compare(x,o.x); } public PairI add(PairI p){ return new PairI(x+p.x,y+p.y); } public PairI sub(PairI p){ return new PairI(x-p.x,y-p.y); } public PairI addG(PairI p,int h,int w) { int x = this.x + p.x; int y = this.y + p.y; if(0 <= x&&x < w&&0 <= y&&y < h){ return new PairI(x,y); } return null; } } class Dist extends PairI{ int d; public Dist(int x,int y,int d){ super(x,y); this.d = d; } public Dist addG(PairI p,int h,int w) { int x = this.x + p.x; int y = this.y + p.y; if(0 <= x&&x < w&&0 <= y&&y < h){ return new Dist(x,y,d+1); } return null; } } class Tuple implements Comparable<Tuple>{ public int x,y,z; public Tuple(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Tuple three = (Tuple) o; return x == three.x && y == three.y && z == three.z; } @Override public int hashCode() { return Objects.hash(x, y, z); } @Override public int compareTo(Tuple o) { return Integer.compare(z,o.z); } } class Node implements Comparable<Node>{ public int from; public int to; public long d; public Node(int to,long d){ this.to = to; this.d = d; } public Node(int from,int to,long d){ this.to = to; this.from = from; this.d = d; } @Override public int compareTo(Node o) { return Long.compare(d,o.d); } } class PairC{ char a,b; public PairC(char a, char b){ this.a = a; this.b = b; } } class IB{ int i; boolean b; IB(int i,boolean b){ this.i = i; this.b = b; } } class CI{ char c; int i; CI(char c,int i){ this.c = c; this.i = i; } } class Solver { public static final int MOD1 = 1000000007; public static final int MOD9 = 998244353; public static Scanner sc = new Scanner(System.in); public static final int inf = 2000000000; public static final int ninf = -inf; public static final char[] alpha = "abcdefghijklmnopqrstuvwxyz".toCharArray(); public static final char[] ALPHA = "abcdefghijklmnopqrstuvwxyz".toUpperCase().toCharArray(); public static FastScanner fs = new FastScanner(); public static PrintWriter out = new PrintWriter(System.out); public static final PairI[] move = new PairI[]{new PairI(1,0),new PairI(0,1),new PairI(-1,0),new PairI(0,-1)}; public static void solve() { int n = rI(); int[] a = rIv(n); int[] b = rIv(n); int[] f = new int[n]; for (int i = 0; i < n; i++) { f[a[i]-1] = b[i]; } int[] dp = new int[n+1]; Arrays.fill(dp,inf); dp[0] = -1; for (int i = 0; i < n; i++) { int ng = -1; int ok = a.length; while (abs(ng-ok) > 1){ int mid = (ok+ng)/2; if(dp[mid] >= f[i]){ ok = mid; }else { ng = mid; } } dp[ok] = min(f[i],dp[ok]); } int max = 0; for (int i = 0; i < n+1; i++) { if(dp[i] != inf) max = max(max,i); } oI(n+max); } static boolean check(long t,long a,long b){ for (long x = 0; x < t; x++) { long y = t-x; if((b+y)%(a+x) == 0){ return true; } } return false; } static int degL(long x){ return String.valueOf(x).length(); } static int degN(long x,int d){ x/=pow(10,d); return (int)(x%10); } static long comb(int n,int r){ if(r*2 > n){ r = n-r; } long ans = 1; for (int i = 0; i < r; i++) { ans*=(n-i); ans/=(i+1); } return ans; } static boolean isPalindrome(int a,int b,String s){ int dif = b-a; boolean ok = true; for (int i = 0; i < dif; i++) { if (s.charAt(i + a) != s.charAt(b - i)) { ok = false; break; } } return ok; } static int intValue(char c){ return Integer.parseInt(String.valueOf(c)); } @SuppressWarnings("unchecked") static class SegTree<T>{ T[] data; int size; BinaryOperator<T> op; T e; int inisize; SegTree(T[] a,BinaryOperator<T> op,T e){ size = 1; inisize = a.length; while (size < a.length){ size <<= 1; } this.e = e; this.op = op; data = (T[])new Object[size*2]; Arrays.fill(data,e); System.arraycopy(a,0,data,size,a.length); for (int i = size-1; i > 0; i--) { data[i] = op.apply(data[i*2],data[i*2+1]); } } void update(int i,T x){ if(i < 0) throw new IllegalArgumentException(); i += size; data[i] = x; while (i > 1){ data[i >> 1] = op.apply(data[i],data[i^1]); i >>= 1; } } T query(int l,int r){ if(l < 0) throw new IllegalArgumentException(); if(r < 0) throw new IllegalArgumentException(); T res = e; l += size; r += size; while (l < r){ if((l & 1) == 1) { res = op.apply(res,data[l]); l++; } if((r & 1) == 1){ res = op.apply(res,data[r-1]); } l >>= 1; r >>= 1; } return res; } int maxRight(int l,Predicate<T> pr){ if (!pr.test(e)) { throw new IllegalArgumentException(); } if(l == inisize) return l; l+=size; T sum = e; do { l >>= Integer.numberOfTrailingZeros(l); if (!pr.test(op.apply(sum, data[l]))) { while (l < size) { l = l << 1; if (pr.test(op.apply(sum, data[l]))) { sum = op.apply(sum, data[l]); l++; } } return l - size; } sum = op.apply(sum, data[l]); l++; } while ((l & -l) != l); return inisize; } public int minLeft(int r,Predicate<T> pr) { if (!pr.test(e)) { throw new IllegalArgumentException(); } if (r == 0) { return 0; } r += size; T sum = e; do { r--; while (r > 1 && (r & 1) == 1) { r >>= 1; } if (!pr.test(op.apply(data[r], sum))) { while (r < size) { r = r << 1 | 1; if (pr.test(op.apply(data[r], sum))) { sum = op.apply(data[r], sum); r--; } } return r + 1 - size; } sum = op.apply(data[r], sum); } while ((r & -r) != r); return 0; } } public static int toIntC(char c){ for (int i = 0; i < ALPHA.length; i++) { if(c == ALPHA[i]){ return i+1; } } throw new IllegalArgumentException("not an alphabet"); } public static int toInt(char c){ for (int i = 0; i < alpha.length; i++) { if(c == alpha[i]){ return i+1; } } throw new IllegalArgumentException("not an alphabet"); } public static void reverse(int[] a){ int[] tmp = a.clone(); for (int i = 0; i < a.length; i++) { a[i] = tmp[a.length - 1 - i]; } } public static int[] compress(int[] a){ int[] b = erase(a); Arrays.sort(b); for (int i = 0; i < a.length; i++) { a[i] = lower(b,a[i]); } return a; } public static int lower(int[] a,int x){ int low = 0, high = a.length; int mid; while (low < high) { mid = low + (high - low) / 2; if (x <= a[mid]) { high = mid; } else { low = mid + 1; } } if (low < a.length && a[low] < x) { low++; } return low; } public static int lower(long[] a,long x){ int low = 0, high = a.length; int mid; while (low < high) { mid = low + (high - low) / 2; if (x <= a[mid]) { high = mid; } else { low = mid + 1; } } if (low < a.length && a[low] < x) { low++; } return low; } public static int upper(int[] a,int x){ int low = 0, high = a.length; int mid; while (low < high && low != a.length) { mid = low + (high - low) / 2; if (x >= a[mid]) { low = mid + 1; } else { high = mid; } } return low; } public static int upper(long[] a,long x){ int low = 0, high = a.length; int mid; while (low < high && low != a.length) { mid = low + (high - low) / 2; if (x >= a[mid]) { low = mid + 1; } else { high = mid; } } return low; } public static int[] erase(int[] a){ HashSet<Integer> used = new HashSet<>(); ArrayList<Integer> ans = new ArrayList<>(); for (int i = 0; i < a.length; i++) { if(!used.contains(a[i])){ used.add(a[i]); ans.add(a[i]); } } return convI(ans); } public static int abs(int a){ return Math.abs(a); } public static long abs(long a){ return Math.abs(a); } public static int max(int a,int b){ return Math.max(a,b); } public static int max(int... a){ int max = ninf; for (int i = 0; i < a.length; i++) { max = max(a[i],max); } return max; } public static long max(long a,long b){ return Math.max(a,b); } public static long max(long... a){ long max = Long.MAX_VALUE; for (int i = 0; i < a.length; i++) { max = max(a[i],max); } return max; } public static int min(int a,int b){ return Math.min(a,b); } public static int min(int... a){ int min = inf; for (int i = 0; i < a.length; i++) { min = min(a[i],min); } return min; } public static long min(long a,long b){ return Math.min(a, b); } public static long min(long... a){ long min = inf; for (int i = 0; i < a.length; i++) { min = min(a[i],min); } return min; } public static final class MC { private final int mod; public MC(final int mod) { this.mod = mod; } public long mod(long x) { x %= mod; if (x < 0) { x += mod; } return x; } public long add(final long a, final long b) { return mod(mod(a) + mod(b)); } public long add(final long... a){ long ans = a[0]; for (int i = 1; i < a.length; i++) { ans = add(ans,a[i]); } return mod(ans); } public long mul(final long a, final long b) { return mod(mod(a) * mod(b)); } public long mul(final long... a){ long ans = a[0]; for (int i = 1; i < a.length; i++) { ans = mul(ans,a[i]); } return mod(ans); } public long div(final long numerator, final long denominator) { return mod(numerator * inverse(denominator)); } public long power(long base, long exp) { long ret = 1; base %= mod; while (exp > 0) { if ((exp & 1) == 1) { ret = mul(ret, base); } base = mul(base, base); exp >>= 1; } return ret; } public long inverse(final long x) { return power(x, mod - 2); } public long factorial(final int n) { return product(1, n); } public long product(final int start, final int end) { long result = 1; for (int i = start; i <= end; i++) { result *= i; result %= mod; } return result; } public long combination(final int n, int r) { if (r > n) { return 0; } return div(product(n - r + 1, n), factorial(r)); } } public static long pow(long x,long n){ long ans = 1L; long tmp = x; while (true){ if(n < 1L){ break; } if(n % 2L == 1L){ ans*=tmp; } tmp *=tmp; n = n >> 1; } return ans; } public static long modPow(long x,long n,long m){ long ans = 1L; long tmp = x; while (true){ if(n < 1L){ break; } if(n % 2L == 1L){ ans*=tmp; ans%=m; } tmp *=tmp; tmp%=m; n = n >> 1; } return ans; } public static int gcd(int a,int b){ if(b == 0) return a; else return gcd(b,a%b); } public static long gcd(long a,long b){ if(b == 0) return a; else return gcd(b,a%b); } public static int gcd(int... a){ int ans = a[0]; for (int i = 1; i < a.length; i++) { ans = gcd(ans,a[i]); } return ans; } public static long gcd(long... a){ long ans = a[0]; for (int i = 1; i < a.length; i++) { ans = gcd(ans,a[i]); } return ans; } public static long lcm(int a,int b){ return (long) a / gcd(a, b) * b; } public static long lcm(long a,long b){ return a / gcd(a,b) * b; } public static boolean isPrime(long x){ if(x < 2) return false; else if(x == 2) return true; if(x%2 == 0) return false; for(long i = 3; i*i <= x; i+= 2){ if(x%i == 0) return false; } return true; } public static int rI() { return fs.nextInt(); } public static int[] rIv(int length) { int[] res = new int[length]; for (int i = 0; i < length; i++) { res[i] = fs.nextInt(); } return res; } public static String rS() { return fs.next(); } public static String[] rSv(int length) { String[] res = new String[length]; for (int i = 0; i < length; i++) res[i] = fs.next(); return res; } public static long rL() { return fs.nextLong(); } public static long[] rLv(int length) { long[] res = new long[length]; for (int i = 0; i < length; i++) res[i] = fs.nextLong(); return res; } public static double rD(){ return fs.nextDouble(); } public static double[] rDv(int length){ double[] res = new double[length]; for (int i = 0; i < length; i++) res[i] = rD(); return res; } public static String aiS(int[] a){ StringBuilder ans = new StringBuilder(); for (int i = 0; i < a.length; i++) { if(i != 0){ ans.append(' '); } ans.append(a[i]); } return ans.toString(); } public static String alS(long[] a){ StringBuilder ans = new StringBuilder(); for (int i = 0; i < a.length; i++) { if(i != 0){ ans.append(' '); } ans.append(a[i]); } return ans.toString(); } public static String adS(double[] a){ StringBuilder ans = new StringBuilder(); for (int i = 0; i < a.length; i++) { if(i != 0){ ans.append(' '); } ans.append(a[i]); } return ans.toString(); } public static String acS(char[] a){ StringBuilder ans = new StringBuilder(); for (int i = 0; i < a.length; i++) { if(i != 0){ ans.append(' '); } ans.append(a[i]); } return ans.toString(); } public static String asS(String[] a){ StringBuilder ans = new StringBuilder(); for (int i = 0; i < a.length; i++) { if(i != 0){ ans.append(' '); } ans.append(a[i]); } return ans.toString(); } public static String liS(ArrayList<Integer> a){ StringBuilder ans = new StringBuilder(); for (int i = 0; i < a.size(); i++) { if(i != 0){ ans.append(' '); } ans.append(a.get(i)); } return ans.toString(); } public static String llS(ArrayList<Long> a){ StringBuilder ans = new StringBuilder(); for (int i = 0; i < a.size(); i++) { if(i != 0){ ans.append(' '); } ans.append(a.get(i)); } return ans.toString(); } public static String ldS(ArrayList<Double> a){ StringBuilder ans = new StringBuilder(); for (int i = 0; i < a.size(); i++) { if(i != 0){ ans.append(' '); } ans.append(a.get(i)); } return ans.toString(); } public static String lcS(ArrayList<Character> a){ StringBuilder ans = new StringBuilder(); for (int i = 0; i < a.size(); i++) { if(i != 0){ ans.append(' '); } ans.append(a.get(i)); } return ans.toString(); } public static String lsS(ArrayList<String> a){ StringBuilder ans = new StringBuilder(); for (int i = 0; i < a.size(); i++) { if(i != 0){ ans.append(' '); } ans.append(a.get(i)); } return ans.toString(); } public static void nL(){ out.println(); } public static void oI(int a) { out.println(a); } public static void onI(int a){ out.print(a); } public static void oIv(int[] a) { oS(aiS(a)); } public static void oS(String s) { out.println(s); } public static void onS(String s) { out.print(s); } public static void oSv(String[] a) { oS(asS(a)); } public static void oL(long l) { out.println(l); } public static void onL(long l) { out.print(l); } public static void oLv(long[] a) { oS(alS(a)); } public static void oD(double d){ out.println(d); } public static void onD(double d){ out.print(d); } public static void oDv(double[] d){ oS(adS(d)); } public static void oC(char c){ out.println(c); } public static void onC(char c){ out.print(c); } public static void oCv(char[] c){ oS(acS(c)); } public static void yes_no(boolean yes){ if(yes){ oS("Yes"); return; } oS("No"); } public static int fact(int num) { if (num == 0) { return 1; } else if (num == 1) { return 1; } else if (num < 0) { throw new IllegalArgumentException("factorial should be bigger than 0"); } return num * fact(num - 1); } public static int[] convI(ArrayList<Integer> list) { int[] res = new int[list.size()]; for (int i = 0; i < list.size(); i++) res[i] = list.get(i); return res; } public static long[] convL(ArrayList<Long> list) { long[] res = new long[list.size()]; for (int i = 0; i < list.size(); i++) res[i] = list.get(i); return res; } public static String[] convS(ArrayList<String> list) { String[] res = new String[list.size()]; for (int i = 0; i < list.size(); i++) res[i] = list.get(i); return res; } public static ArrayList<Integer> convI(int[] vec) { ArrayList<Integer> list = new ArrayList<>(); for (int i : vec) list.add(i); return list; } public static ArrayList<Long> convL(long[] vec) { ArrayList<Long> list = new ArrayList<>(); for (long i : vec) list.add(i); return list; } public static ArrayList<String> convS(String[] vec) { return new ArrayList<>(Arrays.asList(vec)); } public static ArrayList<ArrayList<Integer>> permutation(int a) { int[] list = new int[a]; for (int i = 0; i < a; i++) { list[i] = i; } return permutation(list); } public static ArrayList<ArrayList<Integer>> permutation(int[] seed) { ArrayList<ArrayList<Integer>> res = new ArrayList<>(); int[] perm = new int[seed.length]; boolean[] used = new boolean[seed.length]; buildPerm(seed, perm, used, 0,res); return res; } private static void buildPerm(int[] seed, int[] perm, boolean[] used, int index,ArrayList<ArrayList<Integer>> res) { if (index == seed.length) { res.add(convI(perm)); return; } for (int i = 0; i < seed.length; i++) { if (used[i]) continue; perm[index] = seed[i]; used[i] = true; buildPerm(seed, perm, used, index + 1,res); used[i] = false; } } public static ArrayList<ArrayList<String>> permutation(String[] seed) { ArrayList<ArrayList<String>> res = new ArrayList<>(); String[] perm = new String[seed.length]; boolean[] used = new boolean[seed.length]; buildPerm(seed, perm, used, 0,res); return res; } private static void buildPerm(String[] seed, String[] perm, boolean[] used, int index,ArrayList<ArrayList<String>> res) { if (index == seed.length) { res.add(convS(perm)); return; } for (int i = 0; i < seed.length; i++) { if (used[i]) continue; perm[index] = seed[i]; used[i] = true; buildPerm(seed, perm, used, index + 1,res); used[i] = false; } } public static void swap(int[] a,int i1,int i2){ int t = a[i1]; a[i1] = a[i2]; a[i2] = t; } public static void swap(char[] a,int i1,int i2){ char t = a[i1]; a[i1] = a[i2]; a[i2] = t; } public static void SOLVE(){ solve(); out.flush(); } } class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;} private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;} public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();} public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while(true){ if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next());} }
ConDefects/ConDefects/Code/arc149_b/Java/38897478
condefects-java_data_400
import java.util.*; import java.io.*; public class Main { static ContestScanner sc = new ContestScanner(System.in); static ContestPrinter pw = new ContestPrinter(System.out); static StringBuilder sb = new StringBuilder(); static long mod = (long) 1e9 + 7; public static void main(String[] args) throws Exception { //int T = sc.nextInt(); //for(int i = 0; i < T; i++)solve(); solve(); pw.flush(); } public static void solve() { int N = sc.nextInt(); int[] arr = new int[N]; int[] A = sc.nextIntArray(N); int[] B = sc.nextIntArray(N); for(int i = 0; i < N; i++){ arr[A[i]-1] = B[i]-1; } int[] lis = new int[N+1]; Arrays.fill(lis,(int)1e9); for(int i = 0; i < N; i++){ int left = -1; int right = N; while(right - left > 1){ int mid = (right+left)/2; if(arr[i] >= lis[mid]){ left = mid; }else{ right = mid; } } lis[right] = arr[i]; //pw.printArray(lis); } int ans = N; for(int i = 0; i <= N; i++){ if(lis[i] > N+1){ break; } ans++; } for(int i = 0; i < N; i++){ arr[B[i]-1] = A[i]-1; } Arrays.fill(lis,(int)1e9); for(int i = 0; i < N; i++){ int left = -1; int right = N; while(right - left > 1){ int mid = (right+left)/2; if(arr[i] >= lis[mid]){ left = mid; }else{ right = mid; } } lis[right] = B[i]; //pw.printArray(lis); } int ans2 = N; for(int i = 0; i <= N; i++){ if(lis[i] > N+1){ break; } ans2++; } pw.println(Math.max(ans,ans2)); } static class GeekInteger { public static void save_sort(int[] array) { shuffle(array); Arrays.sort(array); } public static int[] shuffle(int[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); int randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } public static void save_sort(long[] array) { shuffle(array); Arrays.sort(array); } public static long[] shuffle(long[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); long randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } } } /** * refercence : https://github.com/NASU41/AtCoderLibraryForJava/blob/master/ContestIO/ContestScanner.java */ class ContestScanner { private final java.io.InputStream in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private static final long LONG_MAX_TENTHS = 922337203685477580L; private static final int LONG_MAX_LAST_DIGIT = 7; private static final int LONG_MIN_LAST_DIGIT = 8; public ContestScanner(java.io.InputStream in){ this.in = in; } public ContestScanner(java.io.File file) throws java.io.FileNotFoundException { this(new java.io.BufferedInputStream(new java.io.FileInputStream(file))); } public ContestScanner(){ this(System.in); } private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (java.io.IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } public String next() { if (!hasNext()) throw new java.util.NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new java.util.NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { int digit = b - '0'; if (n >= LONG_MAX_TENTHS) { if (n == LONG_MAX_TENTHS) { if (minus) { if (digit <= LONG_MIN_LAST_DIGIT) { n = -n * 10 - digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString(b)) ); } } } else { if (digit <= LONG_MAX_LAST_DIGIT) { n = n * 10 + digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString(b)) ); } } } } throw new ArithmeticException( String.format("%s%d%d... overflows long.", minus ? "-" : "", n, digit) ); } n = n * 10 + digit; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } public long[] nextLongArray(int length){ long[] array = new long[length]; for(int i=0; i<length; i++) array[i] = this.nextLong(); return array; } public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map){ long[] array = new long[length]; for(int i=0; i<length; i++) array[i] = map.applyAsLong(this.nextLong()); return array; } public int[] nextIntArray(int length){ int[] array = new int[length]; for(int i=0; i<length; i++) array[i] = this.nextInt(); return array; } public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map){ int[] array = new int[length]; for(int i=0; i<length; i++) array[i] = map.applyAsInt(this.nextInt()); return array; } public double[] nextDoubleArray(int length){ double[] array = new double[length]; for(int i=0; i<length; i++) array[i] = this.nextDouble(); return array; } public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map){ double[] array = new double[length]; for(int i=0; i<length; i++) array[i] = map.applyAsDouble(this.nextDouble()); return array; } public long[][] nextLongMatrix(int height, int width){ long[][] mat = new long[height][width]; for(int h=0; h<height; h++) for(int w=0; w<width; w++){ mat[h][w] = this.nextLong(); } return mat; } public int[][] nextIntMatrix(int height, int width){ int[][] mat = new int[height][width]; for(int h=0; h<height; h++) for(int w=0; w<width; w++){ mat[h][w] = this.nextInt(); } return mat; } public double[][] nextDoubleMatrix(int height, int width){ double[][] mat = new double[height][width]; for(int h=0; h<height; h++) for(int w=0; w<width; w++){ mat[h][w] = this.nextDouble(); } return mat; } public char[][] nextCharMatrix(int height, int width){ char[][] mat = new char[height][width]; for(int h=0; h<height; h++){ String s = this.next(); for(int w=0; w<width; w++){ mat[h][w] = s.charAt(w); } } return mat; } } class ContestPrinter extends PrintWriter { public ContestPrinter(PrintStream stream) { super(stream); } public ContestPrinter() { super(System.out); } private static String dtos(double x, int n) { StringBuilder sb = new StringBuilder(); if (x < 0) { sb.append('-'); x = -x; } x += Math.pow(10, -n) / 2; sb.append((long) x); sb.append("."); x -= (long) x; for (int i = 0; i < n; i++) { x *= 10; sb.append((int) x); x -= (int) x; } return sb.toString(); } @Override public void print(float f) { super.print(dtos(f, 20)); } @Override public void println(float f) { super.println(dtos(f, 20)); } @Override public void print(double d) { super.print(dtos(d, 20)); } @Override public void println(double d) { super.println(dtos(d, 20)); } public void printlnArray(String[] array) { for (String i : array) super.println(i); } public void printSpace(Object... o) { int n = o.length - 1; for (int i = 0; i < n; i++) { super.print(o[i]); super.print(" "); } super.println(o[n]); } public void println(Object... o) { int n = o.length - 1; for (int i = 0; i < n; i++) super.print(o[i]); super.println(o[n]); } public void printYN(boolean o) { super.println(o ? "Yes" : "No"); } public void print(Object... o) { int n = o.length - 1; for (int i = 0; i < n; i++) super.print(o[i]); super.print(o[n]); } public void printArray(Object[] array) { int n = array.length - 1; for (int i = 0; i < n; i++) { super.print(array[i]); super.print(" "); } super.println(array[n]); } public void printlnArray(Object[] array) { for (Object i : array) super.println(i); } public void printArray(int[] array, String separator) { int n = array.length - 1; if (n == -1) return; for (int i = 0; i < n; i++) { super.print(array[i]); super.print(separator); } super.println(array[n]); } public void printArray(int[] array) { this.printArray(array, " "); } public void printArray(Integer[] array) { this.printArray(array, " "); } public void printArray(Integer[] array, String separator) { int n = array.length - 1; for (int i = 0; i < n; i++) { super.print(array[i]); super.print(separator); } super.println(array[n]); } public void printlnArray(int[] array) { for (int i : array) super.println(i); } public void printArray(int[] array, String separator, java.util.function.IntUnaryOperator map) { int n = array.length - 1; for (int i = 0; i < n; i++) { super.print(map.applyAsInt(array[i])); super.print(separator); } super.println(map.applyAsInt(array[n])); } public void printlnArray(int[] array, java.util.function.IntUnaryOperator map) { for (int i : array) super.println(map.applyAsInt(i)); } public void printlnArray(long[] array, java.util.function.LongUnaryOperator map) { for (long i : array) super.println(map.applyAsLong(i)); } public void printArray(int[] array, java.util.function.IntUnaryOperator map) { this.printArray(array, " ", map); } public void printArray(long[] array, String separator) { int n = array.length - 1; for (int i = 0; i < n; i++) { super.print(array[i]); super.print(separator); } super.println(array[n]); } public void printArray(long[] array) { this.printArray(array, " "); } public void printlnArray(long[] array) { for (long i : array) super.println(i); } public void printArray(double[] array) { printArray(array, " "); } public void printArray(double[] array, String separator) { int n = array.length - 1; for (int i = 0; i < n; i++) { super.print(array[i]); super.print(separator); } super.println(array[n]); } public void printlnArray(double[] array) { for (double i : array) super.println(i); } public void printArray(boolean[] array, String a, String b) { int n = array.length - 1; for (int i = 0; i < n; i++) super.print((array[i] ? a : b) + " "); super.println(array[n] ? a : b); } public void printArray(boolean[] array) { this.printArray(array, "Y", "N"); } public void printArray(char[] array) { for (char c : array) this.print(c); this.println(); } public void printArray(long[] array, String separator, java.util.function.LongUnaryOperator map) { int n = array.length - 1; for (int i = 0; i < n; i++) { super.print(map.applyAsLong(array[i])); super.print(separator); } super.println(map.applyAsLong(array[n])); } public void printArray(long[] array, java.util.function.LongUnaryOperator map) { this.printArray(array, " ", map); } public void printArray(ArrayList<?> array) { this.printArray(array, " "); } public void printArray(ArrayList<?> array, String separator) { int n = array.size() - 1; if (n == -1) return; for (int i = 0; i < n; i++) { super.print(array.get(i).toString()); super.print(separator); } super.println(array.get(n).toString()); } public void printlnArray(ArrayList<?> array) { int n = array.size(); for (int i = 0; i < n; i++) super.println(array.get(i).toString()); } public void printlnArray(ArrayList<Integer> array, java.util.function.IntUnaryOperator map) { int n = array.size(); for (int i = 0; i < n; i++) super.println(map.applyAsInt(array.get(i))); } public void printlnArray(ArrayList<Long> array, java.util.function.LongUnaryOperator map) { int n = array.size(); for (int i = 0; i < n; i++) super.println(map.applyAsLong(array.get(i))); } public void printArray(int[][] array) { for (int[] a : array) this.printArray(a); } public void printArray(int[][] array, java.util.function.IntUnaryOperator map) { for (int[] a : array) this.printArray(a, map); } public void printArray(long[][] array) { int n = array.length; if (n == 0) return; int m = array[0].length - 1; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) super.print(array[i][j] + " "); super.println(array[i][m]); } } public void printArray(long[][] array, java.util.function.LongUnaryOperator map) { int n = array.length; if (n == 0) return; int m = array[0].length - 1; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { super.print(map.applyAsLong(array[i][j])); super.print(" "); } super.println(map.applyAsLong(array[i][m])); } } public void printArray(boolean[][] array) { int n = array.length; if (n == 0) return; int m = array[0].length - 1; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) super.print(array[i][j] ? "○ " : "× "); super.println(array[i][m] ? "○" : "×"); } } public void printArray(char[][] array) { int n = array.length; if (n == 0) return; int m = array[0].length; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) super.print(array[i][j]); super.println(); } } } import java.util.*; import java.io.*; public class Main { static ContestScanner sc = new ContestScanner(System.in); static ContestPrinter pw = new ContestPrinter(System.out); static StringBuilder sb = new StringBuilder(); static long mod = (long) 1e9 + 7; public static void main(String[] args) throws Exception { //int T = sc.nextInt(); //for(int i = 0; i < T; i++)solve(); solve(); pw.flush(); } public static void solve() { int N = sc.nextInt(); int[] arr = new int[N]; int[] A = sc.nextIntArray(N); int[] B = sc.nextIntArray(N); for(int i = 0; i < N; i++){ arr[A[i]-1] = B[i]-1; } int[] lis = new int[N+1]; Arrays.fill(lis,(int)1e9); for(int i = 0; i < N; i++){ int left = -1; int right = N; while(right - left > 1){ int mid = (right+left)/2; if(arr[i] >= lis[mid]){ left = mid; }else{ right = mid; } } lis[right] = arr[i]; //pw.printArray(lis); } int ans = N; for(int i = 0; i <= N; i++){ if(lis[i] > N+1){ break; } ans++; } for(int i = 0; i < N; i++){ arr[B[i]-1] = A[i]-1; } Arrays.fill(lis,(int)1e9); for(int i = 0; i < N; i++){ int left = -1; int right = N; while(right - left > 1){ int mid = (right+left)/2; if(arr[i] >= lis[mid]){ left = mid; }else{ right = mid; } } lis[right] = arr[i]; //pw.printArray(lis); } int ans2 = N; for(int i = 0; i <= N; i++){ if(lis[i] > N+1){ break; } ans2++; } pw.println(Math.max(ans,ans2)); } static class GeekInteger { public static void save_sort(int[] array) { shuffle(array); Arrays.sort(array); } public static int[] shuffle(int[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); int randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } public static void save_sort(long[] array) { shuffle(array); Arrays.sort(array); } public static long[] shuffle(long[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); long randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } } } /** * refercence : https://github.com/NASU41/AtCoderLibraryForJava/blob/master/ContestIO/ContestScanner.java */ class ContestScanner { private final java.io.InputStream in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private static final long LONG_MAX_TENTHS = 922337203685477580L; private static final int LONG_MAX_LAST_DIGIT = 7; private static final int LONG_MIN_LAST_DIGIT = 8; public ContestScanner(java.io.InputStream in){ this.in = in; } public ContestScanner(java.io.File file) throws java.io.FileNotFoundException { this(new java.io.BufferedInputStream(new java.io.FileInputStream(file))); } public ContestScanner(){ this(System.in); } private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (java.io.IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } public String next() { if (!hasNext()) throw new java.util.NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new java.util.NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { int digit = b - '0'; if (n >= LONG_MAX_TENTHS) { if (n == LONG_MAX_TENTHS) { if (minus) { if (digit <= LONG_MIN_LAST_DIGIT) { n = -n * 10 - digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString(b)) ); } } } else { if (digit <= LONG_MAX_LAST_DIGIT) { n = n * 10 + digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("%d%s... is not number", n, Character.toString(b)) ); } } } } throw new ArithmeticException( String.format("%s%d%d... overflows long.", minus ? "-" : "", n, digit) ); } n = n * 10 + digit; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } public long[] nextLongArray(int length){ long[] array = new long[length]; for(int i=0; i<length; i++) array[i] = this.nextLong(); return array; } public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map){ long[] array = new long[length]; for(int i=0; i<length; i++) array[i] = map.applyAsLong(this.nextLong()); return array; } public int[] nextIntArray(int length){ int[] array = new int[length]; for(int i=0; i<length; i++) array[i] = this.nextInt(); return array; } public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map){ int[] array = new int[length]; for(int i=0; i<length; i++) array[i] = map.applyAsInt(this.nextInt()); return array; } public double[] nextDoubleArray(int length){ double[] array = new double[length]; for(int i=0; i<length; i++) array[i] = this.nextDouble(); return array; } public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map){ double[] array = new double[length]; for(int i=0; i<length; i++) array[i] = map.applyAsDouble(this.nextDouble()); return array; } public long[][] nextLongMatrix(int height, int width){ long[][] mat = new long[height][width]; for(int h=0; h<height; h++) for(int w=0; w<width; w++){ mat[h][w] = this.nextLong(); } return mat; } public int[][] nextIntMatrix(int height, int width){ int[][] mat = new int[height][width]; for(int h=0; h<height; h++) for(int w=0; w<width; w++){ mat[h][w] = this.nextInt(); } return mat; } public double[][] nextDoubleMatrix(int height, int width){ double[][] mat = new double[height][width]; for(int h=0; h<height; h++) for(int w=0; w<width; w++){ mat[h][w] = this.nextDouble(); } return mat; } public char[][] nextCharMatrix(int height, int width){ char[][] mat = new char[height][width]; for(int h=0; h<height; h++){ String s = this.next(); for(int w=0; w<width; w++){ mat[h][w] = s.charAt(w); } } return mat; } } class ContestPrinter extends PrintWriter { public ContestPrinter(PrintStream stream) { super(stream); } public ContestPrinter() { super(System.out); } private static String dtos(double x, int n) { StringBuilder sb = new StringBuilder(); if (x < 0) { sb.append('-'); x = -x; } x += Math.pow(10, -n) / 2; sb.append((long) x); sb.append("."); x -= (long) x; for (int i = 0; i < n; i++) { x *= 10; sb.append((int) x); x -= (int) x; } return sb.toString(); } @Override public void print(float f) { super.print(dtos(f, 20)); } @Override public void println(float f) { super.println(dtos(f, 20)); } @Override public void print(double d) { super.print(dtos(d, 20)); } @Override public void println(double d) { super.println(dtos(d, 20)); } public void printlnArray(String[] array) { for (String i : array) super.println(i); } public void printSpace(Object... o) { int n = o.length - 1; for (int i = 0; i < n; i++) { super.print(o[i]); super.print(" "); } super.println(o[n]); } public void println(Object... o) { int n = o.length - 1; for (int i = 0; i < n; i++) super.print(o[i]); super.println(o[n]); } public void printYN(boolean o) { super.println(o ? "Yes" : "No"); } public void print(Object... o) { int n = o.length - 1; for (int i = 0; i < n; i++) super.print(o[i]); super.print(o[n]); } public void printArray(Object[] array) { int n = array.length - 1; for (int i = 0; i < n; i++) { super.print(array[i]); super.print(" "); } super.println(array[n]); } public void printlnArray(Object[] array) { for (Object i : array) super.println(i); } public void printArray(int[] array, String separator) { int n = array.length - 1; if (n == -1) return; for (int i = 0; i < n; i++) { super.print(array[i]); super.print(separator); } super.println(array[n]); } public void printArray(int[] array) { this.printArray(array, " "); } public void printArray(Integer[] array) { this.printArray(array, " "); } public void printArray(Integer[] array, String separator) { int n = array.length - 1; for (int i = 0; i < n; i++) { super.print(array[i]); super.print(separator); } super.println(array[n]); } public void printlnArray(int[] array) { for (int i : array) super.println(i); } public void printArray(int[] array, String separator, java.util.function.IntUnaryOperator map) { int n = array.length - 1; for (int i = 0; i < n; i++) { super.print(map.applyAsInt(array[i])); super.print(separator); } super.println(map.applyAsInt(array[n])); } public void printlnArray(int[] array, java.util.function.IntUnaryOperator map) { for (int i : array) super.println(map.applyAsInt(i)); } public void printlnArray(long[] array, java.util.function.LongUnaryOperator map) { for (long i : array) super.println(map.applyAsLong(i)); } public void printArray(int[] array, java.util.function.IntUnaryOperator map) { this.printArray(array, " ", map); } public void printArray(long[] array, String separator) { int n = array.length - 1; for (int i = 0; i < n; i++) { super.print(array[i]); super.print(separator); } super.println(array[n]); } public void printArray(long[] array) { this.printArray(array, " "); } public void printlnArray(long[] array) { for (long i : array) super.println(i); } public void printArray(double[] array) { printArray(array, " "); } public void printArray(double[] array, String separator) { int n = array.length - 1; for (int i = 0; i < n; i++) { super.print(array[i]); super.print(separator); } super.println(array[n]); } public void printlnArray(double[] array) { for (double i : array) super.println(i); } public void printArray(boolean[] array, String a, String b) { int n = array.length - 1; for (int i = 0; i < n; i++) super.print((array[i] ? a : b) + " "); super.println(array[n] ? a : b); } public void printArray(boolean[] array) { this.printArray(array, "Y", "N"); } public void printArray(char[] array) { for (char c : array) this.print(c); this.println(); } public void printArray(long[] array, String separator, java.util.function.LongUnaryOperator map) { int n = array.length - 1; for (int i = 0; i < n; i++) { super.print(map.applyAsLong(array[i])); super.print(separator); } super.println(map.applyAsLong(array[n])); } public void printArray(long[] array, java.util.function.LongUnaryOperator map) { this.printArray(array, " ", map); } public void printArray(ArrayList<?> array) { this.printArray(array, " "); } public void printArray(ArrayList<?> array, String separator) { int n = array.size() - 1; if (n == -1) return; for (int i = 0; i < n; i++) { super.print(array.get(i).toString()); super.print(separator); } super.println(array.get(n).toString()); } public void printlnArray(ArrayList<?> array) { int n = array.size(); for (int i = 0; i < n; i++) super.println(array.get(i).toString()); } public void printlnArray(ArrayList<Integer> array, java.util.function.IntUnaryOperator map) { int n = array.size(); for (int i = 0; i < n; i++) super.println(map.applyAsInt(array.get(i))); } public void printlnArray(ArrayList<Long> array, java.util.function.LongUnaryOperator map) { int n = array.size(); for (int i = 0; i < n; i++) super.println(map.applyAsLong(array.get(i))); } public void printArray(int[][] array) { for (int[] a : array) this.printArray(a); } public void printArray(int[][] array, java.util.function.IntUnaryOperator map) { for (int[] a : array) this.printArray(a, map); } public void printArray(long[][] array) { int n = array.length; if (n == 0) return; int m = array[0].length - 1; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) super.print(array[i][j] + " "); super.println(array[i][m]); } } public void printArray(long[][] array, java.util.function.LongUnaryOperator map) { int n = array.length; if (n == 0) return; int m = array[0].length - 1; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { super.print(map.applyAsLong(array[i][j])); super.print(" "); } super.println(map.applyAsLong(array[i][m])); } } public void printArray(boolean[][] array) { int n = array.length; if (n == 0) return; int m = array[0].length - 1; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) super.print(array[i][j] ? "○ " : "× "); super.println(array[i][m] ? "○" : "×"); } } public void printArray(char[][] array) { int n = array.length; if (n == 0) return; int m = array[0].length; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) super.print(array[i][j]); super.println(); } } }
ConDefects/ConDefects/Code/arc149_b/Java/35360361