+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ -editor-tabs value="false" />
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..e375c85
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,9 @@
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
new file mode 100644
index 0000000..029f392
--- /dev/null
+++ b/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml
new file mode 100644
index 0000000..e96534f
--- /dev/null
+++ b/.idea/uiDesigner.xml
@@ -0,0 +1,124 @@
+
+
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+
+
+ -
+
+
+ -
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..35eb1dd
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/A.java b/A.java
deleted file mode 100644
index bbe1eb4..0000000
--- a/A.java
+++ /dev/null
@@ -1,393 +0,0 @@
-import java.util.*;
-import java.lang.*;
-import java.io.*;
-import java.math.*;
-
-/**
- * Author : joney_000[developer.jaswant@gmail.com]
- * Algorithm : Default Template
- * Platform : Codeforces
- * Ref : N/A
- */
-
-public class A{
-
- private InputStream inputStream ;
- private OutputStream outputStream ;
- private FastReader in ;
- private PrintWriter out ;
-
- private final int BUFFER = 100005;
-
- private final long mod = 1000000000+7;
- private final int INF = Integer.MAX_VALUE;
- private final long INF_L = Long.MAX_VALUE / 10;
-
- public A(){}
- public A(boolean stdIO)throws FileNotFoundException{
- // stdIO = false;
- if(stdIO){
- inputStream = System.in;
- outputStream = System.out;
- }else{
- inputStream = new FileInputStream("input.txt");
- outputStream = new FileOutputStream("output.txt");
- }
- in = new FastReader(inputStream);
- out = new PrintWriter(outputStream);
- }
-
- void run()throws Exception{
- int n = i();
- long ans = 0;
- out.write(""+ans+"\n");
- }
-
- void clear(){
-
- }
-
- long gcd(long a, long b){
- if(b == 0)return a;
- return gcd(b, a % b);
- }
-
- long lcm(long a, long b){
- if(a == 0 || b == 0)return 0;
- return (a * b)/gcd(a, b);
- }
-
- long mulMod(long a, long b, long mod){
- if(a == 0 || b == 0)return 0;
- if(b == 1)return a;
- long ans = mulMod(a, b/2, mod);
- ans = (ans * 2) % mod;
- if(b % 2 == 1)ans = (a + ans)% mod;
- return ans;
- }
-
- long pow(long a, long b, long mod){
- if(b == 0)return 1;
- if(b == 1)return a;
- long ans = pow(a, b/2, mod);
- ans = mulMod(ans, ans, mod);
- if(ans>= mod)ans %= mod;
-
- if(b % 2 == 1)ans = mulMod(a, ans, mod);
- if(ans>= mod)ans %= mod;
-
- return ans;
- }
-
- // 20*20 nCr Pascal Table
- long[][] ncrTable(){
- long ncr[][] = new long[21][21];
-
- for(int i = 0; i <= 20; i++){ - ncr[i][0] = ncr[i][i] = 1L; - } - - for(int j = 0; j <= 20; j++){ - for(int i = j + 1; i <= 20; i++){ - ncr[i][j] = ncr[i-1][j] + ncr[i-1][j-1]; - } - } - - return ncr; - } - - int i()throws Exception{ - return in.nextInt(); - } - - long l()throws Exception{ - return in.nextLong(); - } - - double d()throws Exception{ - return in.nextDouble(); - } - - char c()throws Exception{ - return in.nextCharacter(); - } - - String s()throws Exception{ - return in.nextLine(); - } - - BigInteger bi()throws Exception{ - return in.nextBigInteger(); - } - - private void closeResources(){ - out.flush(); - out.close(); - return; - } - -// IMP: roundoff upto 2 digits -// double roundOff = Math.round(a * 100.0) / 100.0; -// or -// System.out.printf("%.2f", val); - -// print upto 2 digits after decimal -// val = ((long)(val * 100.0))/100.0; - - public static void main(String[] args) throws java.lang.Exception{ - - A driver = new A(true); - driver.run(); - driver.closeResources(); - } -} - -class FastReader{ - - private boolean finished = false; - - private InputStream stream; - private byte[] buf = new byte[4 * 1024]; - private int curChar; - private int numChars; - private SpaceCharFilter filter; - - public FastReader(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 peek(){ - if (numChars == -1){ - return -1; - } - if (curChar>= numChars){
- curChar = 0;
- try{
- numChars = stream.read (buf);
- } catch (IOException e){
- return -1;
- }
- if (numChars <= 0){ - return -1; - } - } - return buf[curChar]; - } - - public int nextInt(){ - int c = read (); - while (isSpaceChar (c)) - c = read (); - int sgn = 1; - if (c == '-'){ - sgn = -1; - c = read (); - } - int res = 0; - do{ - if(c==','){ - c = read(); - } - if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public long nextLong(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- long res = 0;
- do{
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public String nextString(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- StringBuilder res = new StringBuilder ();
- do{
- res.appendCodePoint (c);
- c = read ();
- } while (!isSpaceChar (c));
- return res.toString ();
- }
-
- public boolean isSpaceChar(int c){
- if (filter != null){
- return filter.isSpaceChar (c);
- }
- return isWhitespace (c);
- }
-
- public static boolean isWhitespace(int c){
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
-
- private String readLine0(){
- StringBuilder buf = new StringBuilder ();
- int c = read ();
- while (c != '\n' && c != -1){
- if (c != '\r'){
- buf.appendCodePoint (c);
- }
- c = read ();
- }
- return buf.toString ();
- }
-
- public String nextLine(){
- String s = readLine0 ();
- while (s.trim ().length () == 0)
- s = readLine0 ();
- return s;
- }
-
- public String nextLine(boolean ignoreEmptyLines){
- if (ignoreEmptyLines){
- return nextLine ();
- }else{
- return readLine0 ();
- }
- }
-
- public BigInteger nextBigInteger(){
- try{
- return new BigInteger (nextString ());
- } catch (NumberFormatException e){
- throw new InputMismatchException ();
- }
- }
-
- public char nextCharacter(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- return (char) c;
- }
-
- public double nextDouble(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- double res = 0;
- while (!isSpaceChar (c) && c != '.'){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- }
- if (c == '.'){
- c = read ();
- double m = 1;
- while (!isSpaceChar (c)){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- m /= 10;
- res += (c - '0') * m;
- c = read ();
- }
- }
- return res * sgn;
- }
-
- public boolean isExhausted(){
- int value;
- while (isSpaceChar (value = peek ()) && value != -1)
- read ();
- return value == -1;
- }
-
- public String next(){
- return nextString ();
- }
-
- public SpaceCharFilter getFilter(){
- return filter;
- }
-
- public void setFilter(SpaceCharFilter filter){
- this.filter = filter;
- }
-
- public interface SpaceCharFilter{
- public boolean isSpaceChar(int ch);
- }
-}
-
-class Pair implements Comparable
{
- public int a;
- public int b;
-
- public Pair(){
- this.a = 0;
- this.b = 0;
- }
-
- public Pair(int a,int b){
- this.a = a;
- this.b = b;
- }
-
- public int compareTo(Pair p){
- if(this.a == p.a){
- return this.b - p.b;
- }
- return this.a - p.a;
- }
-
- @Override
- public String toString(){
- return "a = " + this.a + " b = " + this.b;
- }
-}
\ No newline at end of file
diff --git a/Algorithms/AlgorithmsMathematicalOperationsUtility.java b/Algorithms/AlgorithmsMathematicalOperationsUtility.java
new file mode 100644
index 0000000..945b408
--- /dev/null
+++ b/Algorithms/AlgorithmsMathematicalOperationsUtility.java
@@ -0,0 +1,36 @@
+package Algorithms;
+
+public class AlgorithmsMathematicalOperationsUtility {
+ public long gcd(long a, long b){
+ if(b == 0)return a;
+ return gcd(b, a % b);
+ }
+
+ long lcm(long a, long b){
+ if(a == 0 || b == 0)return 0;
+ return (a * b)/gcd(a, b);
+ }
+
+ long mulMod(long a, long b, long mod){
+ if(a == 0 || b == 0)return 0;
+ if(b == 1)return a;
+ long ans = mulMod(a, b/2, mod);
+ ans = (ans * 2) % mod;
+ if(b % 2 == 1)ans = (a + ans)% mod;
+ return ans;
+ }
+
+ long pow(long a, long b, long mod){
+ if(b == 0)return 1;
+ if(b == 1)return a;
+ long ans = pow(a, b/2, mod);
+ ans = mulMod(ans, ans, mod);
+ if(ans>= mod)ans %= mod;
+
+ if(b % 2 == 1)ans = mulMod(a, ans, mod);
+ if(ans>= mod)ans %= mod;
+
+ return ans;
+ }
+
+}
diff --git a/Algorithms/AllPairShortestPath.java b/Algorithms/AllPairShortestPath.java
index cfcf6f6..07d6911 100644
--- a/Algorithms/AllPairShortestPath.java
+++ b/Algorithms/AllPairShortestPath.java
@@ -1,4 +1,5 @@
-import java.util.*;
+package Algorithms;
+
import java.lang.*;
import java.io.*;
import java.math.*;
@@ -10,11 +11,11 @@
* Ref : Time Complexity: O(N^3), Space Complexity: O(N^2)
*/
-class A{
+class AllPairShortestPath extends AlgorithmsMathematicalOperationsUtility {
private InputStream inputStream ;
private OutputStream outputStream ;
- private FastReader in ;
+ private InputReaderAndProcessor in ;
private PrintWriter out ;
private final int BUFFER = 100005;
@@ -23,8 +24,8 @@ class A{
private final int INF = Integer.MAX_VALUE;
private final long INF_L = Long.MAX_VALUE / 10;
- public A(){}
- public A(boolean stdIO)throws FileNotFoundException{
+ public AllPairShortestPath(){}
+ public AllPairShortestPath(boolean stdIO)throws FileNotFoundException{
// stdIO = false;
if(stdIO){
inputStream = System.in;
@@ -33,7 +34,7 @@ public A(boolean stdIO)throws FileNotFoundException{
inputStream = new FileInputStream("input.txt");
outputStream = new FileOutputStream("output.txt");
}
- in = new FastReader(inputStream);
+ in = new InputReaderAndProcessor(inputStream);
out = new PrintWriter(outputStream);
}
@@ -75,37 +76,6 @@ void allPairShortestPath(int n){
}
}
- long gcd(long a, long b){
- if(b == 0)return a;
- return gcd(b, a % b);
- }
-
- long lcm(long a, long b){
- if(a == 0 || b == 0)return 0;
- return (a * b)/gcd(a, b);
- }
-
- long mulMod(long a, long b, long mod){
- if(a == 0 || b == 0)return 0;
- if(b == 1)return a;
- long ans = mulMod(a, b/2, mod);
- ans = (ans * 2) % mod;
- if(b % 2 == 1)ans = (a + ans)% mod;
- return ans;
- }
-
- long pow(long a, long b, long mod){
- if(b == 0)return 1;
- if(b == 1)return a;
- long ans = pow(a, b/2, mod);
- ans = mulMod(ans, ans, mod);
- if(ans>= mod)ans %= mod;
-
- if(b % 2 == 1)ans = mulMod(a, ans, mod);
- if(ans>= mod)ans %= mod;
-
- return ans;
- }
// 20*20 nCr Pascal Table
long[][] ncrTable(){
@@ -155,7 +125,7 @@ private void closeResources(){
}
// IMP: roundoff upto 2 digits
-// double roundOff = Math.round(a * 100.0) / 100.0;
+// double roundOff = Math.round(number1 * 100.0) / 100.0;
// or
// System.out.printf("%.2f", val);
@@ -163,259 +133,9 @@ private void closeResources(){
// val = ((long)(val * 100.0))/100.0;
public static void main(String[] args) throws java.lang.Exception{
-
- A driver = new A(true);
+
+ AllPairShortestPath driver = new AllPairShortestPath(true);
driver.run();
driver.closeResources();
}
}
-
-class FastReader{
-
- private boolean finished = false;
-
- private InputStream stream;
- private byte[] buf = new byte[4 * 1024];
- private int curChar;
- private int numChars;
- private SpaceCharFilter filter;
-
- public FastReader(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 peek(){ - if (numChars == -1){ - return -1; - } - if (curChar>= numChars){
- curChar = 0;
- try{
- numChars = stream.read (buf);
- } catch (IOException e){
- return -1;
- }
- if (numChars <= 0){ - return -1; - } - } - return buf[curChar]; - } - - public int nextInt(){ - int c = read (); - while (isSpaceChar (c)) - c = read (); - int sgn = 1; - if (c == '-'){ - sgn = -1; - c = read (); - } - int res = 0; - do{ - if(c==','){ - c = read(); - } - if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public long nextLong(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- long res = 0;
- do{
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public String nextString(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- StringBuilder res = new StringBuilder ();
- do{
- res.appendCodePoint (c);
- c = read ();
- } while (!isSpaceChar (c));
- return res.toString ();
- }
-
- public boolean isSpaceChar(int c){
- if (filter != null){
- return filter.isSpaceChar (c);
- }
- return isWhitespace (c);
- }
-
- public static boolean isWhitespace(int c){
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
-
- private String readLine0(){
- StringBuilder buf = new StringBuilder ();
- int c = read ();
- while (c != '\n' && c != -1){
- if (c != '\r'){
- buf.appendCodePoint (c);
- }
- c = read ();
- }
- return buf.toString ();
- }
-
- public String nextLine(){
- String s = readLine0 ();
- while (s.trim ().length () == 0)
- s = readLine0 ();
- return s;
- }
-
- public String nextLine(boolean ignoreEmptyLines){
- if (ignoreEmptyLines){
- return nextLine ();
- }else{
- return readLine0 ();
- }
- }
-
- public BigInteger nextBigInteger(){
- try{
- return new BigInteger (nextString ());
- } catch (NumberFormatException e){
- throw new InputMismatchException ();
- }
- }
-
- public char nextCharacter(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- return (char) c;
- }
-
- public double nextDouble(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- double res = 0;
- while (!isSpaceChar (c) && c != '.'){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- }
- if (c == '.'){
- c = read ();
- double m = 1;
- while (!isSpaceChar (c)){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- m /= 10;
- res += (c - '0') * m;
- c = read ();
- }
- }
- return res * sgn;
- }
-
- public boolean isExhausted(){
- int value;
- while (isSpaceChar (value = peek ()) && value != -1)
- read ();
- return value == -1;
- }
-
- public String next(){
- return nextString ();
- }
-
- public SpaceCharFilter getFilter(){
- return filter;
- }
-
- public void setFilter(SpaceCharFilter filter){
- this.filter = filter;
- }
-
- public interface SpaceCharFilter{
- public boolean isSpaceChar(int ch);
- }
-}
-
-class Pair implements Comparable{
- public int a;
- public int b;
-
- public Pair(){
- this.a = 0;
- this.b = 0;
- }
-
- public Pair(int a,int b){
- this.a = a;
- this.b = b;
- }
-
- public int compareTo(Pair p){
- if(this.a == p.a){
- return this.b - p.b;
- }
- return this.a - p.a;
- }
-
- @Override
- public String toString(){
- return "a = " + this.a + " b = " + this.b;
- }
-}
\ No newline at end of file
diff --git a/Algorithms/BFSAndLCA.java b/Algorithms/BFSAndLCA.java
index 32bf400..481d500 100755
--- a/Algorithms/BFSAndLCA.java
+++ b/Algorithms/BFSAndLCA.java
@@ -1,3 +1,5 @@
+package Algorithms;
+
import java.util.*;
import java.lang.*;
import java.io.*;
@@ -10,7 +12,7 @@
* Ref :
*/
-public class A{
+public class BFSAndLCA extends AlgorithmsMathematicalOperationsUtility{
private InputStream inputStream ;
private OutputStream outputStream ;
@@ -27,8 +29,8 @@ public class A{
private final int INF = Integer.MAX_VALUE;
private final long INF_L = Long.MAX_VALUE / 10;
- public A(){}
- public A(boolean stdIO)throws FileNotFoundException{
+ public BFSAndLCA(){}
+ public BFSAndLCA(boolean stdIO)throws FileNotFoundException{
// stdIO = false;
if(stdIO){
inputStream = System.in;
@@ -122,12 +124,6 @@ int lca(int u, int v){
}
return u;
}
-
- long gcd(long a, long b){
- if(b == 0)return a;
- return gcd(b, a % b);
- }
-
long lcm(long a, long b){
if(a == 0 || b == 0)return 0;
return (a * b)/gcd(a, b);
@@ -223,7 +219,7 @@ private void closeResources(){
}
// IMP: roundoff upto 2 digits
-// double roundOff = Math.round(a * 100.0) / 100.0;
+// double roundOff = Math.round(number1 * 100.0) / 100.0;
// or
// System.out.printf("%.2f", val);
@@ -232,258 +228,10 @@ private void closeResources(){
public static void main(String[] args) throws java.lang.Exception{
- A driver = new A(true);
+ BFSAndLCA driver = new BFSAndLCA(true);
driver.run();
driver.closeResources();
}
}
-class FastReader{
-
- private boolean finished = false;
-
- private InputStream stream;
- private byte[] buf = new byte[4 * 1024];
- private int curChar;
- private int numChars;
- private SpaceCharFilter filter;
-
- public FastReader(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 peek(){ - if (numChars == -1){ - return -1; - } - if (curChar>= numChars){
- curChar = 0;
- try{
- numChars = stream.read (buf);
- } catch (IOException e){
- return -1;
- }
- if (numChars <= 0){ - return -1; - } - } - return buf[curChar]; - } - - public int nextInt(){ - int c = read (); - while (isSpaceChar (c)) - c = read (); - int sgn = 1; - if (c == '-'){ - sgn = -1; - c = read (); - } - int res = 0; - do{ - if(c==','){ - c = read(); - } - if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public long nextLong(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- long res = 0;
- do{
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public String nextString(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- StringBuilder res = new StringBuilder ();
- do{
- res.appendCodePoint (c);
- c = read ();
- } while (!isSpaceChar (c));
- return res.toString ();
- }
-
- public boolean isSpaceChar(int c){
- if (filter != null){
- return filter.isSpaceChar (c);
- }
- return isWhitespace (c);
- }
-
- public static boolean isWhitespace(int c){
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
-
- private String readLine0(){
- StringBuilder buf = new StringBuilder ();
- int c = read ();
- while (c != '\n' && c != -1){
- if (c != '\r'){
- buf.appendCodePoint (c);
- }
- c = read ();
- }
- return buf.toString ();
- }
- public String nextLine(){
- String s = readLine0 ();
- while (s.trim ().length () == 0)
- s = readLine0 ();
- return s;
- }
-
- public String nextLine(boolean ignoreEmptyLines){
- if (ignoreEmptyLines){
- return nextLine ();
- }else{
- return readLine0 ();
- }
- }
-
- public BigInteger nextBigInteger(){
- try{
- return new BigInteger (nextString ());
- } catch (NumberFormatException e){
- throw new InputMismatchException ();
- }
- }
-
- public char nextCharacter(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- return (char) c;
- }
-
- public double nextDouble(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- double res = 0;
- while (!isSpaceChar (c) && c != '.'){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- }
- if (c == '.'){
- c = read ();
- double m = 1;
- while (!isSpaceChar (c)){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- m /= 10;
- res += (c - '0') * m;
- c = read ();
- }
- }
- return res * sgn;
- }
-
- public boolean isExhausted(){
- int value;
- while (isSpaceChar (value = peek ()) && value != -1)
- read ();
- return value == -1;
- }
-
- public String next(){
- return nextString ();
- }
-
- public SpaceCharFilter getFilter(){
- return filter;
- }
-
- public void setFilter(SpaceCharFilter filter){
- this.filter = filter;
- }
-
- public interface SpaceCharFilter{
- public boolean isSpaceChar(int ch);
- }
-}
-
-class Pair implements Comparable{
- public int a;
- public int b;
-
- public Pair(){
- this.a = 0;
- this.b = 0;
- }
-
- public Pair(int a,int b){
- this.a = a;
- this.b = b;
- }
-
- public int compareTo(Pair p){
- if(this.a == p.a){
- return this.b - p.b;
- }
- return this.a - p.a;
- }
-
- @Override
- public String toString(){
- return "a = " + this.a + " b = " + this.b;
- }
-}
\ No newline at end of file
diff --git a/Algorithms/BFS_GRID.java b/Algorithms/BFS_GRID.java
deleted file mode 100755
index aa3f281..0000000
--- a/Algorithms/BFS_GRID.java
+++ /dev/null
@@ -1,492 +0,0 @@
-//pakage joney_000[let_me_start]
-//
-import java.util.*;
-import java.lang.*;
-import java.io.*;
-import java.math.*;
-/*
- * Author : joney_000[let_me_start]
- * Algorithm : BinarySearch UpperBound & Lower Bound
- * Platform : N/A
- *
- */
-public class A {
-
- private InputStream inputStream ;
- private OutputStream outputStream ;
- private FastReader in ;
- private PrintWriter out ;
- /*
- Overhead [Additional Temporary Strorage] but provides memory reusibility for multiple test cases.
- Size Limit : 10^5 + 4
- */
- private final int BUFFER = 10005;
- private int tempints[] = new int[BUFFER];
- private long templongs[] = new long[BUFFER];
- private double tempdoubles[] = new double[BUFFER];
- private char tempchars[] = new char[BUFFER];
- private final long mod = 1000000000+7;
- private final int INF = Integer.MAX_VALUE / 10;
- private final long INF_L = Long.MAX_VALUE / 10;
-
- public A(){}
- public A(boolean stdIO)throws FileNotFoundException{
- // stdIO = false;
- if(stdIO){
- inputStream = System.in;
- outputStream = System.out;
- }else{
- inputStream = new FileInputStream("output.txt");
- outputStream = new FileOutputStream("output1.txt");
- }
- in = new FastReader(inputStream);
- out = new PrintWriter(outputStream);
-
- }
- int n, m, q;
- // 8 Dir
- // int dx[] = {-1 ,-1 , -1 , 0 , 0, 1 ,1 ,1};
- // int dy[] = {-1 , 0 , 1 ,-1 , 1,-1 ,0 ,1};
- // 4 Dir
- int dx[] = {-1, 1, 0, 0};
- int dy[] = { 0, 0, 1, -1};
-
- int cnt[][] = new int[1005][1005];
-
- void run()throws Exception{
- // int tests = i();
- // for(int t = 1 ; t <= tests; t++){ - n = i(); m = i(); q = i(); - clear(); - for(int i = 1; i <= n; i++){ - for(int j = 1; j <= m; j++){ - char ch = c(); - mat[i][j] = ch; - } - } - for(int i = 1; i <= n; i++){ - for(int j = 1; j <= m; j++){ - if(mat[i][j] == '.' && !vis[i][j])bfs(i, j); - } - } - for(int i = 1; i <= q; i++){ - out.write(""+cnt[i()][i()]+"\n"); - } - - // } - }// end run - - boolean vis[][] = new boolean[1005][1005]; - char mat[][] = new char[1005][1005]; - - void clear(){ - - } - - boolean isValid(int i , int j){ - if(i <= n && i>= 1 && j <= m && j>= 1 && (!vis[i][j]))return true;
- else return false;
- }
-
- void bfs(int xroot, int yroot){
-
- LinkedList xq = new LinkedList();
- LinkedList yq = new LinkedList();
-
- // int l = 0;//level and will be marked at the time of adding into queue
- LinkedList level_q = new LinkedList();
- xq.add(xroot);
- yq.add(yroot);
- vis[xroot][yroot] = true;
- //level[root] = 0;
- level_q.add(0);
-
- while(!xq.isEmpty()){
-
- int ux = xq.removeFirst(); //first
- int uy = yq.removeFirst(); //first
- // l = level_q.removeFirst();
- //level[u] = l;
- for(int i = 0 ; i <= 3 ; i++){ - int vx = ux + dx[i] ; - int vy = uy + dy[i]; - if(isValid(vx ,vy) && mat[vx][vy] == '.'){ - vis[vx][vy] = true; - xq.add(vx); yq.add(vy); - - // level_q.add(l+1); - // f[v] = u; - // level[v] = l+1; - } - } - } - - } - - long gcd(long a , long b){ - if(b==0)return a; - return gcd(b , a%b); - } - - long lcm(long a , long b){ - if(a==0||b==0)return 0; - return (a*b)/gcd(a,b); - } - - long mulmod(long a , long b ,long mod){ - if(a==0||b==0)return 0; - if(b==1)return a; - long ans = mulmod(a,b/2,mod); - ans = (ans*2)% mod; - if(b%2==1)ans = (a + ans)% mod; - return ans; - } - - long pow(long a , long b ,long mod){ - if(b==0)return 1; - if(b==1)return a; - long ans = pow(a,b/2,mod); - ans = (ans * ans); - if(ans>= mod )ans %= mod;
-
- if(b%2==1)ans = (a * ans);
- if(ans>= mod )ans %= mod;
-
- return ans;
- }
- // 20*20 nCr Pascal Table
- long[][] ncrTable(){
- long ncr[][] = new long[21][21];
- for(int i=0 ;i<=20 ;i++){ncr[i][0]=1;ncr[i][i]=1;} - for(int j=0;j<=20 ;j++){ - for(int i=j+1;i<= 20 ;i++){ - ncr[i][j] = ncr[i-1][j]+ncr[i-1][j-1]; - } - } - return ncr; - } - //*******************************I/O******************************// - int i()throws Exception{ - //return Integer.parseInt(br.readLine().trim()); - return in.nextInt(); - } - int[] is(int n)throws Exception{ - //int arr[] = new int[n+1]; - for(int i=1 ; i <= n ;i++)tempints[i] = in.nextInt(); - return tempints; - } - long l()throws Exception{ - return in.nextLong(); - } - long[] ls(int n)throws Exception{ - for(int i=1 ; i <= n ;i++)templongs[i] = in.nextLong(); - return templongs; - } - - double d()throws Exception{ - return in.nextDouble(); - } - double[] ds(int n)throws Exception{ - for(int i=1 ; i <= n ;i++)tempdoubles[i] = in.nextDouble(); - return tempdoubles; - } - char c()throws Exception{ - return in.nextCharacter(); - } - char[] cs(int n)throws Exception{ - for(int i=1 ; i <= n ;i++)tempchars[i] = in.nextCharacter(); - return tempchars; - } - String s()throws Exception{ - return in.nextLine(); - } - BigInteger bi()throws Exception{ - return in.nextBigInteger(); - } -//***********************I/O ENDS ***********************// -//*********************** 0.3%f [precision]***********************// -/* roundoff upto 2 digits - double roundOff = Math.round(a * 100.0) / 100.0; - or - System.out.printf("%.2f", val); - -*/ -/* - print upto 2 digits after decimal - val = ((long)(val * 100.0))/100.0; - -*/ - - private void closeResources(){ - out.flush(); - out.close(); - return; - } - public static void main(String[] args) throws java.lang.Exception{ - //let_me_start Shinch Returns - - - /* - // Old Reader Writer - BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); - BufferedWriter out=new BufferedWriter(new OutputStreamWriter(System.out)); - BufferedReader br=new BufferedReader(new FileReader("input.txt")); - BufferedWriter out=new BufferedWriter(new FileWriter("output.txt")); - */ - A driver = new A(true); - - driver.run(); - - driver.closeResources(); - return ; - - } - - } - - class FastReader{ - - private boolean finished = false; - - private InputStream stream; - private byte[] buf = new byte[4 * 1024]; - private int curChar; - private int numChars; - private SpaceCharFilter filter; - - public FastReader(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 peek(){ - if (numChars == -1){ - return -1; - } - if (curChar>= numChars){
- curChar = 0;
- try{
- numChars = stream.read (buf);
- } catch (IOException e){
- return -1;
- }
- if (numChars <= 0){ - return -1; - } - } - return buf[curChar]; - } - - public int nextInt(){ - int c = read (); - while (isSpaceChar (c)) - c = read (); - int sgn = 1; - if (c == '-'){ - sgn = -1; - c = read (); - } - int res = 0; - do{ - if(c==','){ - c = read(); - } - if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public long nextLong(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- long res = 0;
- do{
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public String nextString(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- StringBuilder res = new StringBuilder ();
- do{
- res.appendCodePoint (c);
- c = read ();
- } while (!isSpaceChar (c));
- return res.toString ();
- }
-
- public boolean isSpaceChar(int c){
- if (filter != null){
- return filter.isSpaceChar (c);
- }
- return isWhitespace (c);
- }
-
- public static boolean isWhitespace(int c){
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
-
- private String readLine0(){
- StringBuilder buf = new StringBuilder ();
- int c = read ();
- while (c != '\n' && c != -1){
- if (c != '\r'){
- buf.appendCodePoint (c);
- }
- c = read ();
- }
- return buf.toString ();
- }
-
- public String nextLine(){
- String s = readLine0 ();
- while (s.trim ().length () == 0)
- s = readLine0 ();
- return s;
- }
-
- public String nextLine(boolean ignoreEmptyLines){
- if (ignoreEmptyLines){
- return nextLine ();
- }else{
- return readLine0 ();
- }
- }
-
- public BigInteger nextBigInteger(){
- try{
- return new BigInteger (nextString ());
- } catch (NumberFormatException e){
- throw new InputMismatchException ();
- }
- }
-
- public char nextCharacter(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- return (char) c;
- }
-
- public double nextDouble(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- double res = 0;
- while (!isSpaceChar (c) && c != '.'){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- }
- if (c == '.'){
- c = read ();
- double m = 1;
- while (!isSpaceChar (c)){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- m /= 10;
- res += (c - '0') * m;
- c = read ();
- }
- }
- return res * sgn;
- }
-
- public boolean isExhausted(){
- int value;
- while (isSpaceChar (value = peek ()) && value != -1)
- read ();
- return value == -1;
- }
-
- public String next(){
- return nextString ();
- }
-
- public SpaceCharFilter getFilter(){
- return filter;
- }
-
- public void setFilter(SpaceCharFilter filter){
- this.filter = filter;
- }
-
- public interface SpaceCharFilter{
- public boolean isSpaceChar(int ch);
- }
- }
- /******************** Pair class ***********************/
-
- class Pair implements Comparable{
- public int a;
- public int b;
- public Pair(){
- this.a = 0;
- this.b = 0;
- }
- public Pair(int a,int b){
- this.a = a;
- this.b = b;
- }
- public int compareTo(Pair p){
- if(this.b==p.b){
- return this.a-p.a;
- }
- return (this.b - p.b);
- }
- public String toString(){
- return "a="+this.a+" b="+this.b;
- }
-
-}
diff --git a/Algorithms/BIT-RangeUpdate.java b/Algorithms/BIT-RangeUpdate.java
index 2e63ff2..7888822 100644
--- a/Algorithms/BIT-RangeUpdate.java
+++ b/Algorithms/BIT-RangeUpdate.java
@@ -1,3 +1,7 @@
+package Algorithms;
+
+import Algorithms.InputReaderAndProcessor;
+
import java.util.*;
import java.lang.*;
import java.io.*;
@@ -14,7 +18,7 @@ class Main{
private InputStream inputStream ;
private OutputStream outputStream ;
- private FastReader in ;
+ private InputReaderAndProcessor in ;
private PrintWriter out ;
private final int BUFFER = 200005;
@@ -37,7 +41,7 @@ public Main(boolean stdIO)throws FileNotFoundException{
inputStream = new FileInputStream("input.txt");
outputStream = new FileOutputStream("output.txt");
}
- in = new FastReader(inputStream);
+ in = new InputReaderAndProcessor(inputStream);
out = new PrintWriter(outputStream);
}
@@ -182,7 +186,7 @@ private void closeResources(){
}
// IMP: roundoff upto 2 digits
-// double roundOff = Math.round(a * 100.0) / 100.0;
+// double roundOff = Math.round(number1 * 100.0) / 100.0;
// or
// System.out.printf("%.2f", val);
@@ -197,252 +201,3 @@ public static void main(String[] args) throws java.lang.Exception{
}
}
-class FastReader{
-
- private boolean finished = false;
-
- private InputStream stream;
- private byte[] buf = new byte[4 * 1024];
- private int curChar;
- private int numChars;
- private SpaceCharFilter filter;
-
- public FastReader(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 peek(){ - if (numChars == -1){ - return -1; - } - if (curChar>= numChars){
- curChar = 0;
- try{
- numChars = stream.read (buf);
- } catch (IOException e){
- return -1;
- }
- if (numChars <= 0){ - return -1; - } - } - return buf[curChar]; - } - - public int nextInt(){ - int c = read (); - while (isSpaceChar (c)) - c = read (); - int sgn = 1; - if (c == '-'){ - sgn = -1; - c = read (); - } - int res = 0; - do{ - if(c==','){ - c = read(); - } - if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public long nextLong(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- long res = 0;
- do{
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public String nextString(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- StringBuilder res = new StringBuilder ();
- do{
- res.appendCodePoint (c);
- c = read ();
- } while (!isSpaceChar (c));
- return res.toString ();
- }
-
- public boolean isSpaceChar(int c){
- if (filter != null){
- return filter.isSpaceChar (c);
- }
- return isWhitespace (c);
- }
-
- public static boolean isWhitespace(int c){
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
-
- private String readLine0(){
- StringBuilder buf = new StringBuilder ();
- int c = read ();
- while (c != '\n' && c != -1){
- if (c != '\r'){
- buf.appendCodePoint (c);
- }
- c = read ();
- }
- return buf.toString ();
- }
-
- public String nextLine(){
- String s = readLine0 ();
- while (s.trim ().length () == 0)
- s = readLine0 ();
- return s;
- }
-
- public String nextLine(boolean ignoreEmptyLines){
- if (ignoreEmptyLines){
- return nextLine ();
- }else{
- return readLine0 ();
- }
- }
-
- public BigInteger nextBigInteger(){
- try{
- return new BigInteger (nextString ());
- } catch (NumberFormatException e){
- throw new InputMismatchException ();
- }
- }
-
- public char nextCharacter(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- return (char) c;
- }
-
- public double nextDouble(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- double res = 0;
- while (!isSpaceChar (c) && c != '.'){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- }
- if (c == '.'){
- c = read ();
- double m = 1;
- while (!isSpaceChar (c)){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- m /= 10;
- res += (c - '0') * m;
- c = read ();
- }
- }
- return res * sgn;
- }
-
- public boolean isExhausted(){
- int value;
- while (isSpaceChar (value = peek ()) && value != -1)
- read ();
- return value == -1;
- }
-
- public String next(){
- return nextString ();
- }
-
- public SpaceCharFilter getFilter(){
- return filter;
- }
-
- public void setFilter(SpaceCharFilter filter){
- this.filter = filter;
- }
-
- public interface SpaceCharFilter{
- public boolean isSpaceChar(int ch);
- }
-}
-
-class Pair implements Comparable{
- public int a;
- public int b;
-
- public Pair(){
- this.a = 0;
- this.b = 0;
- }
-
- public Pair(int a,int b){
- this.a = a;
- this.b = b;
- }
-
- public int compareTo(Pair p){
- if(this.a == p.a){
- return this.b - p.b;
- }
- return this.a - p.a;
- }
-
- @Override
- public String toString(){
- return "a = " + this.a + " b = " + this.b;
- }
-}
\ No newline at end of file
diff --git a/Algorithms/BIT.java b/Algorithms/BIT.java
index aa27dc8..8518d75 100755
--- a/Algorithms/BIT.java
+++ b/Algorithms/BIT.java
@@ -1,3 +1,7 @@
+package Algorithms;
+
+import Algorithms.InputReaderAndProcessor;
+
import java.util.*;
import java.lang.*;
import java.io.*;
@@ -10,11 +14,11 @@
* Ref : https://sanugupta.wordpress.com/2014/08/29/binary-indexed-tree-fenwick-tree/
*/
-public class A{
+public class BIT extends AlgorithmsMathematicalOperationsUtility{
private InputStream inputStream ;
private OutputStream outputStream ;
- private FastReader in ;
+ private InputReaderAndProcessor in ;
private PrintWriter out ;
private final int BUFFER = 200005;
@@ -27,8 +31,8 @@ public class A{
private final int INF = Integer.MAX_VALUE;
private final long INF_L = Long.MAX_VALUE / 10;
- public A(){}
- public A(boolean stdIO)throws FileNotFoundException{
+ public BIT(){}
+ public BIT(boolean stdIO)throws FileNotFoundException{
// stdIO = false;
if(stdIO){
inputStream = System.in;
@@ -37,7 +41,7 @@ public A(boolean stdIO)throws FileNotFoundException{
inputStream = new FileInputStream("input.txt");
outputStream = new FileOutputStream("output.txt");
}
- in = new FastReader(inputStream);
+ in = new InputReaderAndProcessor(inputStream);
out = new PrintWriter(outputStream);
}
@@ -90,38 +94,11 @@ long qry(long tree[], int idx){
return ret;
}
- long gcd(long a, long b){
- if(b == 0)return a;
- return gcd(b, a % b);
- }
-
long lcm(long a, long b){
if(a == 0 || b == 0)return 0;
return (a * b)/gcd(a, b);
}
- long mulMod(long a, long b, long mod){
- if(a == 0 || b == 0)return 0;
- if(b == 1)return a;
- long ans = mulMod(a, b/2, mod);
- ans = (ans * 2) % mod;
- if(b % 2 == 1)ans = (a + ans)% mod;
- return ans;
- }
-
- long pow(long a, long b, long mod){
- if(b == 0)return 1;
- if(b == 1)return a;
- long ans = pow(a, b/2, mod);
- ans = (ans * ans);
- if(ans>= mod)ans %= mod;
-
- if(b % 2 == 1)ans = (a * ans);
- if(ans>= mod)ans %= mod;
-
- return ans;
- }
-
// 20*20 nCr Pascal Table
long[][] ncrTable(){
long ncr[][] = new long[21][21];
@@ -190,7 +167,7 @@ private void closeResources(){
}
// IMP: roundoff upto 2 digits
-// double roundOff = Math.round(a * 100.0) / 100.0;
+// double roundOff = Math.round(number1 * 100.0) / 100.0;
// or
// System.out.printf("%.2f", val);
@@ -198,259 +175,8 @@ private void closeResources(){
// val = ((long)(val * 100.0))/100.0;
public static void main(String[] args) throws java.lang.Exception{
-
- A driver = new A(true);
+ BIT driver = new BIT(true);
driver.run();
driver.closeResources();
}
}
-
-class FastReader{
-
- private boolean finished = false;
-
- private InputStream stream;
- private byte[] buf = new byte[4 * 1024];
- private int curChar;
- private int numChars;
- private SpaceCharFilter filter;
-
- public FastReader(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 peek(){ - if (numChars == -1){ - return -1; - } - if (curChar>= numChars){
- curChar = 0;
- try{
- numChars = stream.read (buf);
- } catch (IOException e){
- return -1;
- }
- if (numChars <= 0){ - return -1; - } - } - return buf[curChar]; - } - - public int nextInt(){ - int c = read (); - while (isSpaceChar (c)) - c = read (); - int sgn = 1; - if (c == '-'){ - sgn = -1; - c = read (); - } - int res = 0; - do{ - if(c==','){ - c = read(); - } - if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public long nextLong(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- long res = 0;
- do{
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public String nextString(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- StringBuilder res = new StringBuilder ();
- do{
- res.appendCodePoint (c);
- c = read ();
- } while (!isSpaceChar (c));
- return res.toString ();
- }
-
- public boolean isSpaceChar(int c){
- if (filter != null){
- return filter.isSpaceChar (c);
- }
- return isWhitespace (c);
- }
-
- public static boolean isWhitespace(int c){
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
-
- private String readLine0(){
- StringBuilder buf = new StringBuilder ();
- int c = read ();
- while (c != '\n' && c != -1){
- if (c != '\r'){
- buf.appendCodePoint (c);
- }
- c = read ();
- }
- return buf.toString ();
- }
-
- public String nextLine(){
- String s = readLine0 ();
- while (s.trim ().length () == 0)
- s = readLine0 ();
- return s;
- }
-
- public String nextLine(boolean ignoreEmptyLines){
- if (ignoreEmptyLines){
- return nextLine ();
- }else{
- return readLine0 ();
- }
- }
-
- public BigInteger nextBigInteger(){
- try{
- return new BigInteger (nextString ());
- } catch (NumberFormatException e){
- throw new InputMismatchException ();
- }
- }
-
- public char nextCharacter(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- return (char) c;
- }
-
- public double nextDouble(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- double res = 0;
- while (!isSpaceChar (c) && c != '.'){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- }
- if (c == '.'){
- c = read ();
- double m = 1;
- while (!isSpaceChar (c)){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- m /= 10;
- res += (c - '0') * m;
- c = read ();
- }
- }
- return res * sgn;
- }
-
- public boolean isExhausted(){
- int value;
- while (isSpaceChar (value = peek ()) && value != -1)
- read ();
- return value == -1;
- }
-
- public String next(){
- return nextString ();
- }
-
- public SpaceCharFilter getFilter(){
- return filter;
- }
-
- public void setFilter(SpaceCharFilter filter){
- this.filter = filter;
- }
-
- public interface SpaceCharFilter{
- public boolean isSpaceChar(int ch);
- }
-}
-
-class Pair implements Comparable{
- public int a;
- public int b;
-
- public Pair(){
- this.a = 0;
- this.b = 0;
- }
-
- public Pair(int a,int b){
- this.a = a;
- this.b = b;
- }
-
- public int compareTo(Pair p){
- if(this.a == p.a){
- return this.b - p.b;
- }
- return this.a - p.a;
- }
-
- @Override
- public String toString(){
- return "a = " + this.a + " b = " + this.b;
- }
-}
\ No newline at end of file
diff --git a/Algorithms/BiTSet.java b/Algorithms/BiTSet.java
new file mode 100755
index 0000000..4d2ff50
--- /dev/null
+++ b/Algorithms/BiTSet.java
@@ -0,0 +1,155 @@
+package Algorithms;
+
+import java.util.*;
+import java.lang.*;
+import java.io.*;
+import java.math.*;
+
+/**
+ * Author : joney_000[developer.jaswant@gmail.com]
+ * Algorithm : BitSet
+ * Time : O(n) Space : O(n)
+ * Platform : Codeforces
+ * Ref : N/BiTSet
+ */
+
+ public class BiTSet {
+
+ private InputStream inputStream ;
+ private OutputStream outputStream ;
+ private FastReader in ;
+ private PrintWriter out ;
+
+ private final int BUFFER = 100005;
+
+ private final long mod = 1000000000+7;
+ private final int INF = Integer.MAX_VALUE;
+ private final long INF_L = Long.MAX_VALUE / 10;
+
+ public BiTSet(){}
+ public BiTSet(boolean stdIO)throws FileNotFoundException{
+ // stdIO = false; // for file io, set from main driver
+ if(stdIO){
+ inputStream = System.in;
+ outputStream = System.out;
+ }else{
+ inputStream = new FileInputStream("input.txt");
+ outputStream = new FileOutputStream("output.txt");
+ }
+ in = new FastReader(inputStream);
+ out = new PrintWriter(outputStream);
+ }
+
+ void run()throws Exception{
+ int n = i();int q = i();
+ BitSet bits = new BitSet(n);
+
+ for(int query = 1; query <= q; query++){ + int sign = i(); int i = i(); int j = i(); + if(sign != 1){ + bits.flip(i,j+1); + }else{ + out.write(""+bits.get(i,j+1).cardinality()+"\n"); + } + } + // out.write(""+ans+"\n"); + } + + void clear(){ + + } + + long gcd(long a, long b){ + if(b == 0)return a; + return gcd(b, a % b); + } + + long lcm(long a, long b){ + if(a == 0 || b == 0)return 0; + return (a * b)/gcd(a, b); + } + + long mulMod(long a, long b, long mod){ + if(a == 0 || b == 0)return 0; + if(b == 1)return a; + long ans = mulMod(a, b/2, mod); + ans = (ans * 2) % mod; + if(b % 2 == 1)ans = (a + ans)% mod; + return ans; + } + + long pow(long a, long b, long mod){ + if(b == 0)return 1; + if(b == 1)return a; + long ans = pow(a, b/2, mod); + ans = mulMod(ans, ans, mod); + if(ans>= mod)ans %= mod;
+
+ if(b % 2 == 1)ans = mulMod(a, ans, mod);
+ if(ans>= mod)ans %= mod;
+
+ return ans;
+ }
+
+ // 20*20 nCr Pascal Table
+ long[][] ncrTable(){
+ long ncr[][] = new long[21][21];
+
+ for(int i = 0; i <= 20; i++){ + ncr[i][0] = ncr[i][i] = 1L; + } + + for(int j = 0; j <= 20; j++){ + for(int i = j + 1; i <= 20; i++){ + ncr[i][j] = ncr[i-1][j] + ncr[i-1][j-1]; + } + } + + return ncr; + } + + int i()throws Exception{ + return in.nextInt(); + } + + long l()throws Exception{ + return in.nextLong(); + } + + double d()throws Exception{ + return in.nextDouble(); + } + + char c()throws Exception{ + return in.nextCharacter(); + } + + String s()throws Exception{ + return in.nextLine(); + } + + BigInteger bi()throws Exception{ + return in.nextBigInteger(); + } + + private void closeResources(){ + out.flush(); + out.close(); + return; + } + +// IMP: roundoff upto 2 digits +// double roundOff = Math.round(number1 * 100.0) / 100.0; +// or +// System.out.printf("%.2f", val); + +// print upto 2 digits after decimal +// val = ((long)(val * 100.0))/100.0; + + public static void main(String[] args) throws java.lang.Exception{ + + BiTSet driver = new BiTSet(true); + driver.run(); + driver.closeResources(); + } +} diff --git a/Algorithms/BinaryLifting.java b/Algorithms/BinaryLifting.java index b10844c..4336ebd 100644 --- a/Algorithms/BinaryLifting.java +++ b/Algorithms/BinaryLifting.java @@ -1,3 +1,5 @@ +package Algorithms; + import java.util.LinkedList; /** * Time: O(N log N + Q * log N), each query is answered in log N time. Space: O(N log N) @@ -28,7 +30,7 @@ public BinaryLifting(int n, int[] parent) { } for(int nodeId = 1; nodeId <= n - 1; nodeId++){ if(vis[nodeId])continue; - LinkedList unVisited = new LinkedList(); // linked list as a stack for unvisited node
+ LinkedList unVisited = new LinkedList(); // linked list as number1 stack for unvisited node
int currentNode = nodeId;
while(currentNode != -1 && !vis[currentNode]){
unVisited.addLast(currentNode);
diff --git a/Algorithms/BinarySearch.java b/Algorithms/BinarySearch.java
index 34697d9..ef2dd6c 100755
--- a/Algorithms/BinarySearch.java
+++ b/Algorithms/BinarySearch.java
@@ -1,3 +1,5 @@
+package Algorithms;
+
import java.util.*;
import java.lang.*;
import java.io.*;
@@ -8,10 +10,10 @@
* Algorithm : Binary Search
* Time : O(long n) Space : O(1)
* Platform : Codeforces
- * Ref : N/A
+ * Ref : N/BiTSet
*/
- public class A{
+ public class BinarySearch{
private InputStream inputStream ;
private OutputStream outputStream ;
@@ -24,8 +26,8 @@ public class A{
private final int INF = Integer.MAX_VALUE;
private final long INF_L = Long.MAX_VALUE / 10;
- public A(){}
- public A(boolean stdIO)throws FileNotFoundException{
+ public BinarySearch(){}
+ public BinarySearch(boolean stdIO)throws FileNotFoundException{
// stdIO = false; // for file io, set from main driver
if(stdIO){
inputStream = System.in;
@@ -144,7 +146,7 @@ private void closeResources(){
}
// IMP: roundoff upto 2 digits
-// double roundOff = Math.round(a * 100.0) / 100.0;
+// double roundOff = Math.round(number1 * 100.0) / 100.0;
// or
// System.out.printf("%.2f", val);
@@ -152,259 +154,11 @@ private void closeResources(){
// val = ((long)(val * 100.0))/100.0;
public static void main(String[] args) throws java.lang.Exception{
-
- A driver = new A(true);
+
+ BinarySearch driver = new BinarySearch(true);
driver.run();
driver.closeResources();
}
}
-class FastReader{
-
- private boolean finished = false;
-
- private InputStream stream;
- private byte[] buf = new byte[4 * 1024];
- private int curChar;
- private int numChars;
- private SpaceCharFilter filter;
-
- public FastReader(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 peek(){ - if (numChars == -1){ - return -1; - } - if (curChar>= numChars){
- curChar = 0;
- try{
- numChars = stream.read (buf);
- } catch (IOException e){
- return -1;
- }
- if (numChars <= 0){ - return -1; - } - } - return buf[curChar]; - } - - public int nextInt(){ - int c = read (); - while (isSpaceChar (c)) - c = read (); - int sgn = 1; - if (c == '-'){ - sgn = -1; - c = read (); - } - int res = 0; - do{ - if(c==','){ - c = read(); - } - if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public long nextLong(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- long res = 0;
- do{
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public String nextString(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- StringBuilder res = new StringBuilder ();
- do{
- res.appendCodePoint (c);
- c = read ();
- } while (!isSpaceChar (c));
- return res.toString ();
- }
-
- public boolean isSpaceChar(int c){
- if (filter != null){
- return filter.isSpaceChar (c);
- }
- return isWhitespace (c);
- }
-
- public static boolean isWhitespace(int c){
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
-
- private String readLine0(){
- StringBuilder buf = new StringBuilder ();
- int c = read ();
- while (c != '\n' && c != -1){
- if (c != '\r'){
- buf.appendCodePoint (c);
- }
- c = read ();
- }
- return buf.toString ();
- }
-
- public String nextLine(){
- String s = readLine0 ();
- while (s.trim ().length () == 0)
- s = readLine0 ();
- return s;
- }
-
- public String nextLine(boolean ignoreEmptyLines){
- if (ignoreEmptyLines){
- return nextLine ();
- }else{
- return readLine0 ();
- }
- }
-
- public BigInteger nextBigInteger(){
- try{
- return new BigInteger (nextString ());
- } catch (NumberFormatException e){
- throw new InputMismatchException ();
- }
- }
-
- public char nextCharacter(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- return (char) c;
- }
-
- public double nextDouble(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- double res = 0;
- while (!isSpaceChar (c) && c != '.'){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- }
- if (c == '.'){
- c = read ();
- double m = 1;
- while (!isSpaceChar (c)){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- m /= 10;
- res += (c - '0') * m;
- c = read ();
- }
- }
- return res * sgn;
- }
-
- public boolean isExhausted(){
- int value;
- while (isSpaceChar (value = peek ()) && value != -1)
- read ();
- return value == -1;
- }
-
- public String next(){
- return nextString ();
- }
-
- public SpaceCharFilter getFilter(){
- return filter;
- }
-
- public void setFilter(SpaceCharFilter filter){
- this.filter = filter;
- }
-
- public interface SpaceCharFilter{
- public boolean isSpaceChar(int ch);
- }
-}
-
-class Pair implements Comparable{
- public int a;
- public int b;
-
- public Pair(){
- this.a = 0;
- this.b = 0;
- }
- public Pair(int a,int b){
- this.a = a;
- this.b = b;
- }
-
- public int compareTo(Pair p){
- if(this.a == p.a){
- return this.b - p.b;
- }
- return this.a - p.a;
- }
-
- @Override
- public String toString(){
- return "a = " + this.a + " b = " + this.b;
- }
-}
\ No newline at end of file
diff --git a/Algorithms/BinarySearchUpperAndLowerBound.java b/Algorithms/BinarySearchUpperAndLowerBound.java
new file mode 100644
index 0000000..a982f3a
--- /dev/null
+++ b/Algorithms/BinarySearchUpperAndLowerBound.java
@@ -0,0 +1,245 @@
+package Algorithms;//pakage joney_000[let_me_start]
+//
+import java.util.*;
+import java.lang.*;
+import java.io.*;
+import java.math.*;
+/*
+ * Author : joney_000[let_me_start]
+ * Algorithm : BinarySearch UpperBound & Lower Bound
+ * Platform : N/BinerySearch
+ *
+ */
+public class BinarySearchUpperAndLowerBound {
+
+ private InputStream inputStream ;
+ private OutputStream outputStream ;
+ private InputReaderAndProcessor in ;
+ private PrintWriter out ;
+ /*
+ Overhead [Additional Temporary Strorage] but provides memory reusibility for multiple test cases.
+ Size Limit : 10^5 + 4
+ */
+ private final int BUFFER = 10005;
+ private final int[] tempints = new int[BUFFER];
+ private final long[] templongs = new long[BUFFER];
+ private final double[] tempdoubles = new double[BUFFER];
+ private final char[] tempchars = new char[BUFFER];
+ private final long mod = 1000000000+7;
+ private final int INF = Integer.MAX_VALUE / 10;
+ private final long INF_L = Long.MAX_VALUE / 10;
+
+ public BinarySearchUpperAndLowerBound(){}
+ public BinarySearchUpperAndLowerBound(boolean stdIO)throws FileNotFoundException{
+ // stdIO = false;
+ if(stdIO){
+ inputStream = System.in;
+ outputStream = System.out;
+ }else{
+ inputStream = new FileInputStream("output.txt");
+ outputStream = new FileOutputStream("output1.txt");
+ }
+ in = new InputReaderAndProcessor(inputStream);
+ out = new PrintWriter(outputStream);
+
+ }
+ int n, m, q;
+ // 8 Dir
+ // int directionHorizontal[] = {-1 ,-1 , -1 , 0 , 0, 1 ,1 ,1};
+ // int directionVertical[] = {-1 , 0 , 1 ,-1 , 1,-1 ,0 ,1};
+ // 4 Dir
+ int[] directionHorizontal = {-1, 1, 0, 0};
+ int[] directionVertical = { 0, 0, 1, -1};
+
+ int[][] cnt = new int[1005][1005];
+
+ void run()throws Exception{
+ // int tests = i();
+ // for(int t = 1 ; t <= tests; t++){ + n = i(); m = i(); q = i(); + clear(); + for(int i = 1; i <= n; i++){ + for(int j = 1; j <= m; j++){ + char ch = c(); + mat[i][j] = ch; + } + } + for(int i = 1; i <= n; i++){ + for(int j = 1; j <= m; j++){ + if(mat[i][j] == '.' && !vis[i][j])bfs(i, j); + } + } + for(int i = 1; i <= q; i++){ + out.write(""+cnt[i()][i()]+"\n"); + } + + // } + }// end run + + boolean[][] vis = new boolean[1005][1005]; + char[][] mat = new char[1005][1005]; + + void clear(){ + + } + + boolean isValid(int i , int j){ + return i <= n && i>= 1 && j <= m && j>= 1 && (!vis[i][j]);
+ }
+
+ void bfs(int xroot, int yroot){
+
+ LinkedList xq = new LinkedList();
+ LinkedList yq = new LinkedList();
+
+ // int l = 0;//level and will be marked at the time of adding into queue
+ LinkedList level_q = new LinkedList();
+ xq.add(xroot);
+ yq.add(yroot);
+ vis[xroot][yroot] = true;
+ //level[root] = 0;
+ level_q.add(0);
+
+ while(!xq.isEmpty()){
+
+ int ux = xq.removeFirst(); //first
+ int uy = yq.removeFirst(); //first
+ // l = level_q.removeFirst();
+ //level[u] = l;
+ for(int i = 0 ; i <= 3 ; i++){ + int vx = ux + directionHorizontal[i] ; + int vy = uy + directionVertical[i]; + if(isValid(vx ,vy) && mat[vx][vy] == '.'){ + vis[vx][vy] = true; + xq.add(vx); yq.add(vy); + + // level_q.add(l+1); + // f[v] = u; + // level[v] = l+1; + } + } + } + + } + + long gcd(long a , long b){ + if(b==0)return a; + return gcd(b , a%b); + } + + long lcm(long a , long b){ + if(a==0||b==0)return 0; + return (a*b)/gcd(a,b); + } + + long mulmod(long a , long b ,long mod){ + if(a==0||b==0)return 0; + if(b==1)return a; + long ans = mulmod(a,b/2,mod); + ans = (ans*2)% mod; + if(b%2==1)ans = (a + ans)% mod; + return ans; + } + + long pow(long a , long b ,long mod){ + if(b==0)return 1; + if(b==1)return a; + long ans = pow(a,b/2,mod); + ans = (ans * ans); + if(ans>= mod )ans %= mod;
+
+ if(b%2==1)ans = (a * ans);
+ if(ans>= mod )ans %= mod;
+
+ return ans;
+ }
+ // 20*20 nCr Pascal Table
+ long[][] ncrTable(){
+ long[][] ncr = new long[21][21];
+ for(int i=0 ;i<=20 ;i++){ncr[i][0]=1;ncr[i][i]=1;} + for(int j=0;j<=20 ;j++){ + for(int i=j+1;i<= 20 ;i++){ + ncr[i][j] = ncr[i-1][j]+ncr[i-1][j-1]; + } + } + return ncr; + } + //*******************************I/O******************************// + int i()throws Exception{ + //return Integer.parseInt(br.readLine().trim()); + return in.nextInt(); + } + int[] is(int n)throws Exception{ + //int arr[] = new int[n+1]; + for(int i=1 ; i <= n ;i++)tempints[i] = in.nextInt(); + return tempints; + } + long l()throws Exception{ + return in.nextLong(); + } + long[] ls(int n)throws Exception{ + for(int i=1 ; i <= n ;i++)templongs[i] = in.nextLong(); + return templongs; + } + + double d()throws Exception{ + return in.nextDouble(); + } + double[] ds(int n)throws Exception{ + for(int i=1 ; i <= n ;i++)tempdoubles[i] = in.nextDouble(); + return tempdoubles; + } + char c()throws Exception{ + return in.nextCharacter(); + } + char[] cs(int n)throws Exception{ + for(int i=1 ; i <= n ;i++)tempchars[i] = in.nextCharacter(); + return tempchars; + } + String s()throws Exception{ + return in.nextLine(); + } + BigInteger bi()throws Exception{ + return in.nextBigInteger(); + } +//***********************I/O ENDS ***********************// +//*********************** 0.3%f [precision]***********************// +/* roundoff upto 2 digits + double roundOff = Math.round(number1 * 100.0) / 100.0; + or + System.out.printf("%.2f", val); + +*/ +/* + print upto 2 digits after decimal + val = ((long)(val * 100.0))/100.0; + +*/ + + private void closeResources(){ + out.flush(); + out.close(); + return; + } + public static void main(String[] args) throws java.lang.Exception{ + //let_me_start Shinch Returns + + + /* + // Old Reader Writer + BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); + BufferedWriter out=new BufferedWriter(new OutputStreamWriter(System.out)); + BufferedReader br=new BufferedReader(new FileReader("input.txt")); + BufferedWriter out=new BufferedWriter(new FileWriter("output.txt")); + */ + BinarySearchUpperAndLowerBound driver = new BinarySearchUpperAndLowerBound(true); + + driver.run(); + + driver.closeResources(); + return ; + + } + + } + diff --git a/Algorithms/BitSet.java b/Algorithms/BitSet.java deleted file mode 100755 index ff8be0b..0000000 --- a/Algorithms/BitSet.java +++ /dev/null @@ -1,403 +0,0 @@ -import java.util.*; -import java.lang.*; -import java.io.*; -import java.math.*; - -/** - * Author : joney_000[developer.jaswant@gmail.com] - * Algorithm : BitSet - * Time : O(n) Space : O(n) - * Platform : Codeforces - * Ref : N/A - */ - - public class A{ - - private InputStream inputStream ; - private OutputStream outputStream ; - private FastReader in ; - private PrintWriter out ; - - private final int BUFFER = 100005; - - private final long mod = 1000000000+7; - private final int INF = Integer.MAX_VALUE; - private final long INF_L = Long.MAX_VALUE / 10; - - public A(){} - public A(boolean stdIO)throws FileNotFoundException{ - // stdIO = false; // for file io, set from main driver - if(stdIO){ - inputStream = System.in; - outputStream = System.out; - }else{ - inputStream = new FileInputStream("input.txt"); - outputStream = new FileOutputStream("output.txt"); - } - in = new FastReader(inputStream); - out = new PrintWriter(outputStream); - } - - void run()throws Exception{ - int n = i();int q = i(); - BitSet bits = new BitSet(n); - - for(int query = 1; query <= q; query++){ - int sign = i(); int i = i(); int j = i(); - if(sgn != 1){ - bits.flip(i,j+1); - }else{ - out.write(""+bits.get(i,j+1).cardinality()+"\n"); - } - } - // out.write(""+ans+"\n"); - } - - void clear(){ - - } - - long gcd(long a, long b){ - if(b == 0)return a; - return gcd(b, a % b); - } - - long lcm(long a, long b){ - if(a == 0 || b == 0)return 0; - return (a * b)/gcd(a, b); - } - - long mulMod(long a, long b, long mod){ - if(a == 0 || b == 0)return 0; - if(b == 1)return a; - long ans = mulMod(a, b/2, mod); - ans = (ans * 2) % mod; - if(b % 2 == 1)ans = (a + ans)% mod; - return ans; - } - - long pow(long a, long b, long mod){ - if(b == 0)return 1; - if(b == 1)return a; - long ans = pow(a, b/2, mod); - ans = mulMod(ans, ans, mod); - if(ans>= mod)ans %= mod;
-
- if(b % 2 == 1)ans = mulMod(a, ans, mod);
- if(ans>= mod)ans %= mod;
-
- return ans;
- }
-
- // 20*20 nCr Pascal Table
- long[][] ncrTable(){
- long ncr[][] = new long[21][21];
-
- for(int i = 0; i <= 20; i++){ - ncr[i][0] = ncr[i][i] = 1L; - } - - for(int j = 0; j <= 20; j++){ - for(int i = j + 1; i <= 20; i++){ - ncr[i][j] = ncr[i-1][j] + ncr[i-1][j-1]; - } - } - - return ncr; - } - - int i()throws Exception{ - return in.nextInt(); - } - - long l()throws Exception{ - return in.nextLong(); - } - - double d()throws Exception{ - return in.nextDouble(); - } - - char c()throws Exception{ - return in.nextCharacter(); - } - - String s()throws Exception{ - return in.nextLine(); - } - - BigInteger bi()throws Exception{ - return in.nextBigInteger(); - } - - private void closeResources(){ - out.flush(); - out.close(); - return; - } - -// IMP: roundoff upto 2 digits -// double roundOff = Math.round(a * 100.0) / 100.0; -// or -// System.out.printf("%.2f", val); - -// print upto 2 digits after decimal -// val = ((long)(val * 100.0))/100.0; - - public static void main(String[] args) throws java.lang.Exception{ - - A driver = new A(true); - driver.run(); - driver.closeResources(); - } -} - -class FastReader{ - - private boolean finished = false; - - private InputStream stream; - private byte[] buf = new byte[4 * 1024]; - private int curChar; - private int numChars; - private SpaceCharFilter filter; - - public FastReader(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 peek(){ - if (numChars == -1){ - return -1; - } - if (curChar>= numChars){
- curChar = 0;
- try{
- numChars = stream.read (buf);
- } catch (IOException e){
- return -1;
- }
- if (numChars <= 0){ - return -1; - } - } - return buf[curChar]; - } - - public int nextInt(){ - int c = read (); - while (isSpaceChar (c)) - c = read (); - int sgn = 1; - if (c == '-'){ - sgn = -1; - c = read (); - } - int res = 0; - do{ - if(c==','){ - c = read(); - } - if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public long nextLong(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- long res = 0;
- do{
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public String nextString(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- StringBuilder res = new StringBuilder ();
- do{
- res.appendCodePoint (c);
- c = read ();
- } while (!isSpaceChar (c));
- return res.toString ();
- }
-
- public boolean isSpaceChar(int c){
- if (filter != null){
- return filter.isSpaceChar (c);
- }
- return isWhitespace (c);
- }
-
- public static boolean isWhitespace(int c){
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
-
- private String readLine0(){
- StringBuilder buf = new StringBuilder ();
- int c = read ();
- while (c != '\n' && c != -1){
- if (c != '\r'){
- buf.appendCodePoint (c);
- }
- c = read ();
- }
- return buf.toString ();
- }
-
- public String nextLine(){
- String s = readLine0 ();
- while (s.trim ().length () == 0)
- s = readLine0 ();
- return s;
- }
-
- public String nextLine(boolean ignoreEmptyLines){
- if (ignoreEmptyLines){
- return nextLine ();
- }else{
- return readLine0 ();
- }
- }
-
- public BigInteger nextBigInteger(){
- try{
- return new BigInteger (nextString ());
- } catch (NumberFormatException e){
- throw new InputMismatchException ();
- }
- }
-
- public char nextCharacter(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- return (char) c;
- }
-
- public double nextDouble(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- double res = 0;
- while (!isSpaceChar (c) && c != '.'){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- }
- if (c == '.'){
- c = read ();
- double m = 1;
- while (!isSpaceChar (c)){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- m /= 10;
- res += (c - '0') * m;
- c = read ();
- }
- }
- return res * sgn;
- }
-
- public boolean isExhausted(){
- int value;
- while (isSpaceChar (value = peek ()) && value != -1)
- read ();
- return value == -1;
- }
-
- public String next(){
- return nextString ();
- }
-
- public SpaceCharFilter getFilter(){
- return filter;
- }
-
- public void setFilter(SpaceCharFilter filter){
- this.filter = filter;
- }
-
- public interface SpaceCharFilter{
- public boolean isSpaceChar(int ch);
- }
-}
-
-class Pair implements Comparable{
- public int a;
- public int b;
-
- public Pair(){
- this.a = 0;
- this.b = 0;
- }
-
- public Pair(int a,int b){
- this.a = a;
- this.b = b;
- }
-
- public int compareTo(Pair p){
- if(this.a == p.a){
- return this.b - p.b;
- }
- return this.a - p.a;
- }
-
- @Override
- public String toString(){
- return "a = " + this.a + " b = " + this.b;
- }
-}
\ No newline at end of file
diff --git a/Algorithms/ComparableExample.java b/Algorithms/ComparableExample.java
index 7e548e2..b6f6797 100755
--- a/Algorithms/ComparableExample.java
+++ b/Algorithms/ComparableExample.java
@@ -1,3 +1,4 @@
+package Algorithms;
class Person implements Comparable{
private int start, end, salary;
diff --git a/Algorithms/ComparatorSnippet.java b/Algorithms/ComparatorSnippet.java
index 44d9405..e2d60a1 100644
--- a/Algorithms/ComparatorSnippet.java
+++ b/Algorithms/ComparatorSnippet.java
@@ -1,27 +1,32 @@
-class Developer{
+package Algorithms;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+class Developer {
private int age;
-
- public Developer(int age){
+
+ public Developer(int age) {
this.age = age;
}
- void setAge(int age){
+ void setAge(int age) {
this.age = age;
}
- public int getAge(){
+
+ public int getAge() {
return age;
}
}
-
public class ComparatorSnippet{
-
public static void main(String... args)throws Exception{
-
- Developer[] devs = Collections.sort(listDevs, new Comparator() {
- @Override
- public int compare(Developer o1, Developer o2) {
- return o1.getAge() - o2.getAge();
- }
- });
+ ArrayList listDevs = new ArrayList();
+// Developer[] devs = Collections.sort(listDevs, new Comparator() {
+// @Override
+// public int compare(Developer o1, Developer o2) {
+// return o1.getAge() - o2.getAge();
+// }
+// });
}
}
\ No newline at end of file
diff --git a/Algorithms/CycleDetection.java b/Algorithms/CycleDetection.java
index 24f45c8..a3bc236 100755
--- a/Algorithms/CycleDetection.java
+++ b/Algorithms/CycleDetection.java
@@ -1,3 +1,5 @@
+package Algorithms;
+
import java.util.*;
import java.lang.*;
import java.io.*;
@@ -10,7 +12,7 @@
* Ref : Cycle detection in forest
*/
-class A{
+public class CycleDetection {
private InputStream inputStream ;
private OutputStream outputStream ;
@@ -27,8 +29,8 @@ class A{
private final int INF = Integer.MAX_VALUE;
private final long INF_L = Long.MAX_VALUE / 10;
- public A(){}
- public A(boolean stdIO)throws FileNotFoundException{
+ public CycleDetection(){}
+ public CycleDetection(boolean stdIO)throws FileNotFoundException{
// stdIO = false;
if(stdIO){
inputStream = System.in;
@@ -222,7 +224,7 @@ private void closeResources(){
}
// IMP: roundoff upto 2 digits
-// double roundOff = Math.round(a * 100.0) / 100.0;
+// double roundOff = Math.round(number1 * 100.0) / 100.0;
// or
// System.out.printf("%.2f", val);
@@ -230,259 +232,10 @@ private void closeResources(){
// val = ((long)(val * 100.0))/100.0;
public static void main(String[] args) throws java.lang.Exception{
-
- A driver = new A(true);
+
+ CycleDetection driver = new CycleDetection(true);
driver.run();
driver.closeResources();
}
}
-class FastReader{
-
- private boolean finished = false;
-
- private InputStream stream;
- private byte[] buf = new byte[4 * 1024];
- private int curChar;
- private int numChars;
- private SpaceCharFilter filter;
-
- public FastReader(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 peek(){ - if (numChars == -1){ - return -1; - } - if (curChar>= numChars){
- curChar = 0;
- try{
- numChars = stream.read (buf);
- } catch (IOException e){
- return -1;
- }
- if (numChars <= 0){ - return -1; - } - } - return buf[curChar]; - } - - public int nextInt(){ - int c = read (); - while (isSpaceChar (c)) - c = read (); - int sgn = 1; - if (c == '-'){ - sgn = -1; - c = read (); - } - int res = 0; - do{ - if(c==','){ - c = read(); - } - if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public long nextLong(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- long res = 0;
- do{
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public String nextString(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- StringBuilder res = new StringBuilder ();
- do{
- res.appendCodePoint (c);
- c = read ();
- } while (!isSpaceChar (c));
- return res.toString ();
- }
-
- public boolean isSpaceChar(int c){
- if (filter != null){
- return filter.isSpaceChar (c);
- }
- return isWhitespace (c);
- }
-
- public static boolean isWhitespace(int c){
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
-
- private String readLine0(){
- StringBuilder buf = new StringBuilder ();
- int c = read ();
- while (c != '\n' && c != -1){
- if (c != '\r'){
- buf.appendCodePoint (c);
- }
- c = read ();
- }
- return buf.toString ();
- }
-
- public String nextLine(){
- String s = readLine0 ();
- while (s.trim ().length () == 0)
- s = readLine0 ();
- return s;
- }
-
- public String nextLine(boolean ignoreEmptyLines){
- if (ignoreEmptyLines){
- return nextLine ();
- }else{
- return readLine0 ();
- }
- }
-
- public BigInteger nextBigInteger(){
- try{
- return new BigInteger (nextString ());
- } catch (NumberFormatException e){
- throw new InputMismatchException ();
- }
- }
-
- public char nextCharacter(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- return (char) c;
- }
-
- public double nextDouble(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- double res = 0;
- while (!isSpaceChar (c) && c != '.'){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- }
- if (c == '.'){
- c = read ();
- double m = 1;
- while (!isSpaceChar (c)){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- m /= 10;
- res += (c - '0') * m;
- c = read ();
- }
- }
- return res * sgn;
- }
-
- public boolean isExhausted(){
- int value;
- while (isSpaceChar (value = peek ()) && value != -1)
- read ();
- return value == -1;
- }
-
- public String next(){
- return nextString ();
- }
-
- public SpaceCharFilter getFilter(){
- return filter;
- }
-
- public void setFilter(SpaceCharFilter filter){
- this.filter = filter;
- }
-
- public interface SpaceCharFilter{
- public boolean isSpaceChar(int ch);
- }
-}
-
-class Pair implements Comparable{
- public int a;
- public int b;
-
- public Pair(){
- this.a = 0;
- this.b = 0;
- }
-
- public Pair(int a,int b){
- this.a = a;
- this.b = b;
- }
-
- public int compareTo(Pair p){
- if(this.a == p.a){
- return this.b - p.b;
- }
- return this.a - p.a;
- }
-
- @Override
- public String toString(){
- return "a = " + this.a + " b = " + this.b;
- }
-}
diff --git a/Algorithms/DFSAdjacencyList.java b/Algorithms/DFSAdjacencyList.java
index e1dc135..905d457 100755
--- a/Algorithms/DFSAdjacencyList.java
+++ b/Algorithms/DFSAdjacencyList.java
@@ -1,3 +1,5 @@
+package Algorithms;
+
import java.util.*;
import java.lang.*;
import java.io.*;
@@ -8,7 +10,7 @@
* Platform : Codeforces
*/
-class A{
+class DFSAdjacencyList{
private InputStream inputStream ;
private OutputStream outputStream ;
@@ -27,8 +29,8 @@ class A{
private final int INF = Integer.MAX_VALUE / 10;
private final long INF_L = Long.MAX_VALUE / 10;
- public A(){}
- public A(boolean stdIO)throws FileNotFoundException{
+ public DFSAdjacencyList(){}
+ public DFSAdjacencyList(boolean stdIO)throws FileNotFoundException{
// stdIO = false;
if(stdIO){
inputStream = System.in;
@@ -235,7 +237,7 @@ BigInteger bi()throws Exception{
//***********************I/O ENDS ***********************//
//*********************** 0.3%f [precision]***********************//
/* roundoff upto 2 digits
- double roundOff = Math.round(a * 100.0) / 100.0;
+ double roundOff = Math.round(number1 * 100.0) / 100.0;
or
System.out.printf("%.2f", val);
@@ -261,8 +263,8 @@ public static void main(String[] args) throws java.lang.Exception{
BufferedWriter out=new BufferedWriter(new OutputStreamWriter(System.out));
BufferedReader br=new BufferedReader(new FileReader("input.txt"));
BufferedWriter out=new BufferedWriter(new FileWriter("output.txt"));
- */
- A driver = new A(true);
+ */
+ DFSAdjacencyList driver = new DFSAdjacencyList(true);
driver.run();
@@ -272,252 +274,3 @@ public static void main(String[] args) throws java.lang.Exception{
}
}
-
- class FastReader{
-
- private boolean finished = false;
-
- private InputStream stream;
- private byte[] buf = new byte[4 * 1024];
- private int curChar;
- private int numChars;
- private SpaceCharFilter filter;
-
- public FastReader(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 peek(){ - if (numChars == -1){ - return -1; - } - if (curChar>= numChars){
- curChar = 0;
- try{
- numChars = stream.read (buf);
- } catch (IOException e){
- return -1;
- }
- if (numChars <= 0){ - return -1; - } - } - return buf[curChar]; - } - - public int nextInt(){ - int c = read (); - while (isSpaceChar (c)) - c = read (); - int sgn = 1; - if (c == '-'){ - sgn = -1; - c = read (); - } - int res = 0; - do{ - if(c==','){ - c = read(); - } - if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public long nextLong(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- long res = 0;
- do{
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public String nextString(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- StringBuilder res = new StringBuilder ();
- do{
- res.appendCodePoint (c);
- c = read ();
- } while (!isSpaceChar (c));
- return res.toString ();
- }
-
- public boolean isSpaceChar(int c){
- if (filter != null){
- return filter.isSpaceChar (c);
- }
- return isWhitespace (c);
- }
-
- public static boolean isWhitespace(int c){
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
-
- private String readLine0(){
- StringBuilder buf = new StringBuilder ();
- int c = read ();
- while (c != '\n' && c != -1){
- if (c != '\r'){
- buf.appendCodePoint (c);
- }
- c = read ();
- }
- return buf.toString ();
- }
-
- public String nextLine(){
- String s = readLine0 ();
- while (s.trim ().length () == 0)
- s = readLine0 ();
- return s;
- }
-
- public String nextLine(boolean ignoreEmptyLines){
- if (ignoreEmptyLines){
- return nextLine ();
- }else{
- return readLine0 ();
- }
- }
-
- public BigInteger nextBigInteger(){
- try{
- return new BigInteger (nextString ());
- } catch (NumberFormatException e){
- throw new InputMismatchException ();
- }
- }
-
- public char nextCharacter(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- return (char) c;
- }
-
- public double nextDouble(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- double res = 0;
- while (!isSpaceChar (c) && c != '.'){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- }
- if (c == '.'){
- c = read ();
- double m = 1;
- while (!isSpaceChar (c)){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- m /= 10;
- res += (c - '0') * m;
- c = read ();
- }
- }
- return res * sgn;
- }
-
- public boolean isExhausted(){
- int value;
- while (isSpaceChar (value = peek ()) && value != -1)
- read ();
- return value == -1;
- }
-
- public String next(){
- return nextString ();
- }
-
- public SpaceCharFilter getFilter(){
- return filter;
- }
-
- public void setFilter(SpaceCharFilter filter){
- this.filter = filter;
- }
-
- public interface SpaceCharFilter{
- public boolean isSpaceChar(int ch);
- }
- }
- /******************** Pair class ***********************/
-
- class Pair implements Comparable{
- public int a;
- public int b;
- public int c;
- public Pair(){
- this.a = 0;
- this.b = 0;
- }
- public Pair(int a,int b, int c){
- this.a = a;
- this.b = b;
- this.c = c;
- }
- public int compareTo(Pair p){
- if(this.a==p.a){
- return this.b-p.b;
- }
- return p.a-this.a;
- }
- public String toString(){
- return "a="+this.a+" b="+this.b;
- }
-
-}
diff --git a/Algorithms/DFS_AdjMatrix.java b/Algorithms/DFS_AdjMatrix.java
index 64c82fd..dbaf21c 100755
--- a/Algorithms/DFS_AdjMatrix.java
+++ b/Algorithms/DFS_AdjMatrix.java
@@ -1,3 +1,5 @@
+package Algorithms;
+
import java.lang.*;
import java.util.*;
import java.math.*;
diff --git a/Algorithms/DFS_Grid.java b/Algorithms/DFS_Grid.java
index 9ba98b3..e9aa509 100755
--- a/Algorithms/DFS_Grid.java
+++ b/Algorithms/DFS_Grid.java
@@ -1,3 +1,5 @@
+package Algorithms;
+
import java.util.*;
import java.lang.*;
import java.io.*;
@@ -10,11 +12,11 @@
* Ref :
*/
-public class A{
+public class DFS_Grid {
private InputStream inputStream ;
private OutputStream outputStream ;
- private FastReader in ;
+ private InputReaderAndProcessor in ;
private PrintWriter out ;
private final int BUFFER = 100005;
@@ -27,8 +29,8 @@ public class A{
private final int INF = Integer.MAX_VALUE;
private final long INF_L = Long.MAX_VALUE / 10;
- public A(){}
- public A(boolean stdIO)throws FileNotFoundException{
+ public DFS_Grid(){}
+ public DFS_Grid(boolean stdIO)throws FileNotFoundException{
// stdIO = false;
if(stdIO){
inputStream = System.in;
@@ -37,19 +39,19 @@ public A(boolean stdIO)throws FileNotFoundException{
inputStream = new FileInputStream("input.txt");
outputStream = new FileOutputStream("output.txt");
}
- in = new FastReader(inputStream);
+ in = new InputReaderAndProcessor(inputStream);
out = new PrintWriter(outputStream);
}
int n, m;
// 8 Dir
// int dx[] = {-1 ,-1 , -1 , 0 , 0, 1 ,1 ,1};
- // int dy[] = {-1 , 0 , 1 ,-1 , 1,-1 ,0 ,1};
+ // int directionVertical[] = {-1 , 0 , 1 ,-1 , 1,-1 ,0 ,1};
// 4 Dir
int dx[] = {-1, 1, 0, 0};
int dy[] = { 0, 0, 1, -1};
- void run()throws Exception{
+ void run() throws Exception{
n = i(); m = i();
clear();
@@ -230,7 +232,7 @@ private void closeResources(){
}
// IMP: roundoff upto 2 digits
-// double roundOff = Math.round(a * 100.0) / 100.0;
+// double roundOff = Math.round(number1 * 100.0) / 100.0;
// or
// System.out.printf("%.2f", val);
@@ -239,258 +241,8 @@ private void closeResources(){
public static void main(String[] args) throws java.lang.Exception{
- A driver = new A(true);
+ DFS_Grid driver = new DFS_Grid(true);
driver.run();
driver.closeResources();
}
}
-
-class FastReader{
-
- private boolean finished = false;
-
- private InputStream stream;
- private byte[] buf = new byte[4 * 1024];
- private int curChar;
- private int numChars;
- private SpaceCharFilter filter;
-
- public FastReader(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 peek(){ - if (numChars == -1){ - return -1; - } - if (curChar>= numChars){
- curChar = 0;
- try{
- numChars = stream.read (buf);
- } catch (IOException e){
- return -1;
- }
- if (numChars <= 0){ - return -1; - } - } - return buf[curChar]; - } - - public int nextInt(){ - int c = read (); - while (isSpaceChar (c)) - c = read (); - int sgn = 1; - if (c == '-'){ - sgn = -1; - c = read (); - } - int res = 0; - do{ - if(c==','){ - c = read(); - } - if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public long nextLong(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- long res = 0;
- do{
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public String nextString(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- StringBuilder res = new StringBuilder ();
- do{
- res.appendCodePoint (c);
- c = read ();
- } while (!isSpaceChar (c));
- return res.toString ();
- }
-
- public boolean isSpaceChar(int c){
- if (filter != null){
- return filter.isSpaceChar (c);
- }
- return isWhitespace (c);
- }
-
- public static boolean isWhitespace(int c){
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
-
- private String readLine0(){
- StringBuilder buf = new StringBuilder ();
- int c = read ();
- while (c != '\n' && c != -1){
- if (c != '\r'){
- buf.appendCodePoint (c);
- }
- c = read ();
- }
- return buf.toString ();
- }
-
- public String nextLine(){
- String s = readLine0 ();
- while (s.trim ().length () == 0)
- s = readLine0 ();
- return s;
- }
-
- public String nextLine(boolean ignoreEmptyLines){
- if (ignoreEmptyLines){
- return nextLine ();
- }else{
- return readLine0 ();
- }
- }
-
- public BigInteger nextBigInteger(){
- try{
- return new BigInteger (nextString ());
- } catch (NumberFormatException e){
- throw new InputMismatchException ();
- }
- }
-
- public char nextCharacter(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- return (char) c;
- }
-
- public double nextDouble(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- double res = 0;
- while (!isSpaceChar (c) && c != '.'){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- }
- if (c == '.'){
- c = read ();
- double m = 1;
- while (!isSpaceChar (c)){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- m /= 10;
- res += (c - '0') * m;
- c = read ();
- }
- }
- return res * sgn;
- }
-
- public boolean isExhausted(){
- int value;
- while (isSpaceChar (value = peek ()) && value != -1)
- read ();
- return value == -1;
- }
-
- public String next(){
- return nextString ();
- }
-
- public SpaceCharFilter getFilter(){
- return filter;
- }
-
- public void setFilter(SpaceCharFilter filter){
- this.filter = filter;
- }
-
- public interface SpaceCharFilter{
- public boolean isSpaceChar(int ch);
- }
-}
-
-class Pair implements Comparable{
- public int a;
- public int b;
-
- public Pair(){
- this.a = 0;
- this.b = 0;
- }
-
- public Pair(int a,int b){
- this.a = a;
- this.b = b;
- }
-
- public int compareTo(Pair p){
- if(this.a == p.a){
- return this.b - p.b;
- }
- return this.a - p.a;
- }
-
- @Override
- public String toString(){
- return "a = " + this.a + " b = " + this.b;
- }
-}
\ No newline at end of file
diff --git a/Algorithms/DSU.java b/Algorithms/DSU.java
index e473131..c524833 100755
--- a/Algorithms/DSU.java
+++ b/Algorithms/DSU.java
@@ -1,3 +1,4 @@
+package Algorithms;
import java.util.*;
import java.lang.*;
@@ -11,12 +12,12 @@
*/
/* The Main Class */
- class A{
+ class DSU{
private InputStream inputStream ;
private OutputStream outputStream ;
- private FastReader in ;
- private PrintWriter out ;
+ private InputReaderAndProcessor in ;
+ private PrintWriter out ;
/*
Overhead [Additional Temporary Strorage] but provides memory reusibility for multiple test cases.
@@ -32,8 +33,8 @@ class A{
private final int INF = Integer.MAX_VALUE / 10;
private final long INF_L = Long.MAX_VALUE / 10;
- public A(){}
- public A(boolean stdIO)throws FileNotFoundException{
+ public DSU(){}
+ public DSU(boolean stdIO)throws FileNotFoundException{
//stdIO = false;
if(stdIO){
inputStream = System.in;
@@ -42,7 +43,7 @@ public A(boolean stdIO)throws FileNotFoundException{
inputStream = new FileInputStream("laundro_matt.txt");
outputStream = new FileOutputStream("output.txt");
}
- in = new FastReader(inputStream);
+ in = new InputReaderAndProcessor(inputStream);
out = new PrintWriter(outputStream);
}
@@ -110,7 +111,7 @@ void print_r(Object...o){
int hash(String s){
int base = 31;
- int a = 31;//base = a multiplier
+ int a = 31;//base = number1 multiplier
int mod = 100005;//range [0..100004]
long val = 0;
for(int i = 1 ; i<= s.length() ;i++){ @@ -232,7 +233,7 @@ BigInteger bi()throws Exception{ //***********************I/O ENDS ***********************// //*********************** 0.3%f [precision]***********************// /* roundoff upto 2 digits - double roundOff = Math.round(a * 100.0) / 100.0; + double roundOff = Math.round(number1 * 100.0) / 100.0; or System.out.printf("%.2f", val); @@ -256,8 +257,8 @@ public static void main(String[] args) throws java.lang.Exception{ BufferedWriter out=new BufferedWriter(new OutputStreamWriter(System.out)); BufferedReader br=new BufferedReader(new FileReader("input.txt")); BufferedWriter out=new BufferedWriter(new FileWriter("output.txt")); - */ - A driver = new A(true); + */ + DSU driver = new DSU(); long start = System.currentTimeMillis(); driver.run(); long end = System.currentTimeMillis(); @@ -269,243 +270,19 @@ public static void main(String[] args) throws java.lang.Exception{ } -class FastReader{ - - private boolean finished = false; - - private InputStream stream; - private byte[] buf = new byte[4*1024]; - private int curChar; - private int numChars; - private SpaceCharFilter filter; - - public FastReader(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 peek(){ - if (numChars == -1){ - return -1; - } - if (curChar>= numChars){
- curChar = 0;
- try{
- numChars = stream.read (buf);
- } catch (IOException e){
- return -1;
- }
- if (numChars <= 0){ - return -1; - } - } - return buf[curChar]; - } - - public int nextInt(){ - int c = read (); - while (isSpaceChar (c)) - c = read (); - int sgn = 1; - if (c == '-'){ - sgn = -1; - c = read (); - } - int res = 0; - do{ - if(c==','){ - c = read(); - } - if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public long nextLong(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- long res = 0;
- do{
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public String nextString(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- StringBuilder res = new StringBuilder ();
- do{
- res.appendCodePoint (c);
- c = read ();
- } while (!isSpaceChar (c));
- return res.toString ();
- }
-
- public boolean isSpaceChar(int c){
- if (filter != null){
- return filter.isSpaceChar (c);
- }
- return isWhitespace (c);
- }
-
- public static boolean isWhitespace(int c){
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
-
- private String readLine0(){
- StringBuilder buf = new StringBuilder ();
- int c = read ();
- while (c != '\n' && c != -1){
- if (c != '\r'){
- buf.appendCodePoint (c);
- }
- c = read ();
- }
- return buf.toString ();
- }
-
- public String nextLine(){
- String s = readLine0 ();
- while (s.trim ().length () == 0)
- s = readLine0 ();
- return s;
- }
-
- public String nextLine(boolean ignoreEmptyLines){
- if (ignoreEmptyLines){
- return nextLine ();
- }else{
- return readLine0 ();
- }
- }
-
- public BigInteger nextBigInteger(){
- try{
- return new BigInteger (nextString ());
- } catch (NumberFormatException e){
- throw new InputMismatchException ();
- }
- }
-
- public char nextCharacter(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- return (char) c;
- }
-
- public double nextDouble(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- double res = 0;
- while (!isSpaceChar (c) && c != '.'){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- }
- if (c == '.'){
- c = read ();
- double m = 1;
- while (!isSpaceChar (c)){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- m /= 10;
- res += (c - '0') * m;
- c = read ();
- }
- }
- return res * sgn;
- }
-
- public boolean isExhausted(){
- int value;
- while (isSpaceChar (value = peek ()) && value != -1)
- read ();
- return value == -1;
- }
-
- public String next(){
- return nextString ();
- }
-
- public SpaceCharFilter getFilter(){
- return filter;
- }
-
- public void setFilter(SpaceCharFilter filter){
- this.filter = filter;
- }
-
- public interface SpaceCharFilter{
- public boolean isSpaceChar(int ch);
- }
-}
- /******************** Pair class ***********************/
-
- class Pair implements Comparable{
- public int id;
- public long b;
- public long a;
- public long c;
- public Pair(){
+ class CompairThreeNumbers implements Comparable{
+ private int id;
+ private long b;
+ private long a;
+ private long c;
+ public CompairThreeNumbers(){
this.id = 1000;
this.a = 0;
this.b = 0;
this.c = 0;
}
- public Pair(int id , long a,long b , long c ){
+ public CompairThreeNumbers(int id , long a,long b , long c ){
this.id = id;
this.a = a;
this.b = b;
@@ -522,7 +299,7 @@ public int compareTo(Pair p){
}
}
public String toString(){
- return "a="+this.a+" b="+this.b;
+ return "number1="+this.a+" number2="+this.b;
}
}
diff --git a/Algorithms/Dijkstra.java b/Algorithms/Dijkstra.java
index ef256e1..a1e96c1 100755
--- a/Algorithms/Dijkstra.java
+++ b/Algorithms/Dijkstra.java
@@ -1,3 +1,5 @@
+package Algorithms;
+
import java.util.*;
import java.lang.*;
import java.io.*;
@@ -10,7 +12,7 @@
* Ref : https://codeforces.com/contest/20/problem/C
*/
-class Solution{
+class Dijkstra {
private InputStream inputStream ;
private OutputStream outputStream ;
@@ -23,8 +25,8 @@ class Solution{
private final int INF = Integer.MAX_VALUE;
private final long INF_L = Long.MAX_VALUE / 10;
- public Solution(){}
- public Solution(boolean stdIO)throws FileNotFoundException{
+ public Dijkstra(){}
+ public Dijkstra(boolean stdIO)throws FileNotFoundException{
// stdIO = false;
if(stdIO){
inputStream = System.in;
@@ -211,7 +213,7 @@ private void closeResources(){
}
// IMP: roundoff upto 2 digits
-// double roundOff = Math.round(a * 100.0) / 100.0;
+// double roundOff = Math.round(number1 * 100.0) / 100.0;
// or
// System.out.printf("%.2f", val);
@@ -220,251 +222,8 @@ private void closeResources(){
public static void main(String[] args) throws java.lang.Exception{
- Solution driver = new Solution(true);
+ Dijkstra driver = new Dijkstra(true);
driver.run();
driver.closeResources();
}
-}
-
-class FastReader{
-
- private boolean finished = false;
-
- private InputStream stream;
- private byte[] buf = new byte[4 * 1024];
- private int curChar;
- private int numChars;
- private SpaceCharFilter filter;
-
- public FastReader(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 peek(){ - if (numChars == -1){ - return -1; - } - if (curChar>= numChars){
- curChar = 0;
- try{
- numChars = stream.read (buf);
- } catch (IOException e){
- return -1;
- }
- if (numChars <= 0){ - return -1; - } - } - return buf[curChar]; - } - - public int nextInt(){ - int c = read (); - while (isSpaceChar (c)) - c = read (); - int sgn = 1; - if (c == '-'){ - sgn = -1; - c = read (); - } - int res = 0; - do{ - if(c==','){ - c = read(); - } - if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public long nextLong(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- long res = 0;
- do{
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public String nextString(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- StringBuilder res = new StringBuilder ();
- do{
- res.appendCodePoint (c);
- c = read ();
- } while (!isSpaceChar (c));
- return res.toString ();
- }
-
- public boolean isSpaceChar(int c){
- if (filter != null){
- return filter.isSpaceChar (c);
- }
- return isWhitespace (c);
- }
-
- public static boolean isWhitespace(int c){
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
-
- private String readLine0(){
- StringBuilder buf = new StringBuilder ();
- int c = read ();
- while (c != '\n' && c != -1){
- if (c != '\r'){
- buf.appendCodePoint (c);
- }
- c = read ();
- }
- return buf.toString ();
- }
-
- public String nextLine(){
- String s = readLine0 ();
- while (s.trim ().length () == 0)
- s = readLine0 ();
- return s;
- }
-
- public String nextLine(boolean ignoreEmptyLines){
- if (ignoreEmptyLines){
- return nextLine ();
- }else{
- return readLine0 ();
- }
- }
-
- public BigInteger nextBigInteger(){
- try{
- return new BigInteger (nextString ());
- } catch (NumberFormatException e){
- throw new InputMismatchException ();
- }
- }
-
- public char nextCharacter(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- return (char) c;
- }
-
- public double nextDouble(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- double res = 0;
- while (!isSpaceChar (c) && c != '.'){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- }
- if (c == '.'){
- c = read ();
- double m = 1;
- while (!isSpaceChar (c)){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- m /= 10;
- res += (c - '0') * m;
- c = read ();
- }
- }
- return res * sgn;
- }
-
- public boolean isExhausted(){
- int value;
- while (isSpaceChar (value = peek ()) && value != -1)
- read ();
- return value == -1;
- }
-
- public String next(){
- return nextString ();
- }
-
- public SpaceCharFilter getFilter(){
- return filter;
- }
-
- public void setFilter(SpaceCharFilter filter){
- this.filter = filter;
- }
-
- public interface SpaceCharFilter{
- public boolean isSpaceChar(int ch);
- }
-}
-
-class Pair implements Comparable{
- public int a;
- public int b;
-
- public Pair(int a,int b){
- this.a = a;
- this.b = b;
- }
-
- @Override
- public int compareTo(Pair other){
- return Integer.compare(this.a, other.a);
- }
-
- @Override
- public String toString(){
- return "a = " + this.a + " b = " + this.b;
- }
-}
\ No newline at end of file
+}
\ No newline at end of file
diff --git a/Algorithms/Euler Tocient .java b/Algorithms/Euler Tocient .java
index 60b9bb2..4dc49fe 100755
--- a/Algorithms/Euler Tocient .java
+++ b/Algorithms/Euler Tocient .java
@@ -1,3 +1,4 @@
+package Algorithms;
import java.util.*;
import java.lang.*;
@@ -11,11 +12,11 @@
*/
- class A {
+ class EulerTocient {
private InputStream inputStream ;
private OutputStream outputStream ;
- private FastReader in ;
+ private InputReaderAndProcessor in ;
private PrintWriter out ;
/*
Overhead [Additional Temporary Strorage] but provides memory reusibility for multiple test cases.
@@ -32,8 +33,8 @@ class A {
private final int INF = Integer.MAX_VALUE / 10;
private final long INF_L = Long.MAX_VALUE / 10;
- public A(){}
- public A(boolean stdIO)throws FileNotFoundException{
+ public EulerTocient(){}
+ public EulerTocient(boolean stdIO)throws FileNotFoundException{
//stdIO = false;
if(stdIO){
inputStream = System.in;
@@ -42,7 +43,7 @@ public A(boolean stdIO)throws FileNotFoundException{
inputStream = new FileInputStream("laundro_matt.txt");
outputStream = new FileOutputStream("output.txt");
}
- in = new FastReader(inputStream);
+ in = new InputReaderAndProcessor(inputStream);
out = new PrintWriter(outputStream);
}
@@ -52,12 +53,7 @@ public A(boolean stdIO)throws FileNotFoundException{
int sz=0;
long val[] = new long[MAXN+1];
boolean mark [] = new boolean[MAXN+1];
-
- void run()throws Exception{
- prec();
-
- }
-
+
int phi(int n) {
int res = n;
for (int i = 2; i * i <= n; i++) @@ -79,7 +75,7 @@ void print_r(Object...o){ long h[]; void hash(String s){ long base = 31; - long a = 31;//base = a multiplier + long a = 31;//base = number1 multiplier long mod = 1000000007;//range [0..100004] long val = 0; for(int i = 1 ; i<= s.length() ;i++){ @@ -199,7 +195,7 @@ BigInteger bi()throws Exception{ //***********************I/O ENDS ***********************// //*********************** 0.3%f [precision]***********************// /* roundoff upto 2 digits - double roundOff = Math.round(a * 100.0) / 100.0; + double roundOff = Math.round(number1 * 100.0) / 100.0; or System.out.printf("%.2f", val); @@ -223,10 +219,10 @@ public static void main(String[] args) throws java.lang.Exception{ BufferedWriter out=new BufferedWriter(new OutputStreamWriter(System.out)); BufferedReader br=new BufferedReader(new FileReader("input.txt")); BufferedWriter out=new BufferedWriter(new FileWriter("output.txt")); - */ - A driver = new A(true); + */ + EulerTocient driver = new EulerTocient(true); long start = System.currentTimeMillis(); - driver.run(); +// driver.run(); long end = System.currentTimeMillis(); //out.write(" Total Time : "+(end - start)+"\n"); driver.closeResources(); @@ -234,265 +230,6 @@ public static void main(String[] args) throws java.lang.Exception{ } -} - -class FastReader{ - - private boolean finished = false; - - private InputStream stream; - private byte[] buf = new byte[4*1024]; - private int curChar; - private int numChars; - private SpaceCharFilter filter; - - public FastReader(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 peek(){ - if (numChars == -1){ - return -1; - } - if (curChar>= numChars){
- curChar = 0;
- try{
- numChars = stream.read (buf);
- } catch (IOException e){
- return -1;
- }
- if (numChars <= 0){ - return -1; - } - } - return buf[curChar]; - } - - public int nextInt(){ - int c = read (); - while (isSpaceChar (c)) - c = read (); - int sgn = 1; - if (c == '-'){ - sgn = -1; - c = read (); - } - int res = 0; - do{ - if(c==','){ - c = read(); - } - if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public long nextLong(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- long res = 0;
- do{
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public String nextString(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- StringBuilder res = new StringBuilder ();
- do{
- res.appendCodePoint (c);
- c = read ();
- } while (!isSpaceChar (c));
- return res.toString ();
- }
-
- public boolean isSpaceChar(int c){
- if (filter != null){
- return filter.isSpaceChar (c);
- }
- return isWhitespace (c);
- }
-
- public static boolean isWhitespace(int c){
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
-
- private String readLine0(){
- StringBuilder buf = new StringBuilder ();
- int c = read ();
- while (c != '\n' && c != -1){
- if (c != '\r'){
- buf.appendCodePoint (c);
- }
- c = read ();
- }
- return buf.toString ();
- }
-
- public String nextLine(){
- String s = readLine0 ();
- while (s.trim ().length () == 0)
- s = readLine0 ();
- return s;
- }
-
- public String nextLine(boolean ignoreEmptyLines){
- if (ignoreEmptyLines){
- return nextLine ();
- }else{
- return readLine0 ();
- }
- }
-
- public BigInteger nextBigInteger(){
- try{
- return new BigInteger (nextString ());
- } catch (NumberFormatException e){
- throw new InputMismatchException ();
- }
- }
-
- public char nextCharacter(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- return (char) c;
- }
-
- public double nextDouble(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- double res = 0;
- while (!isSpaceChar (c) && c != '.'){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- }
- if (c == '.'){
- c = read ();
- double m = 1;
- while (!isSpaceChar (c)){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- m /= 10;
- res += (c - '0') * m;
- c = read ();
- }
- }
- return res * sgn;
- }
-
- public boolean isExhausted(){
- int value;
- while (isSpaceChar (value = peek ()) && value != -1)
- read ();
- return value == -1;
- }
-
- public String next(){
- return nextString ();
- }
-
- public SpaceCharFilter getFilter(){
- return filter;
- }
-
- public void setFilter(SpaceCharFilter filter){
- this.filter = filter;
- }
-
- public interface SpaceCharFilter{
- public boolean isSpaceChar(int ch);
- }
}
/******************** Pair class ***********************/
-
- class Pair implements Comparable{
-
- public long b;
- public long a;
- public long c;
- public long prev = 0;;
- public Pair(){
-
-
- this.a = 0L;
- this.b = 0L;
- this.c = 0L;
- }
- public Pair(long a,long b , long c ){
- this.a = a;
- this.b = b;
- this.c = c;
- }
- public int compareTo(Pair p){
- if(this.a < p.a)return -1; - else if(this.a> p.a )return 1;
- else {
- if(this.b < p.b)return -1; - else if(this.b> p.b )return 1;
- else {
- return 0;
- }
-
- }
- }
- public String toString(){
- return "a="+this.a+" b="+this.b+" c="+this.c;
- }
-
-}
diff --git a/Algorithms/FastFourierTransform.java b/Algorithms/FastFourierTransform.java
index 4db1cd0..99624d8 100644
--- a/Algorithms/FastFourierTransform.java
+++ b/Algorithms/FastFourierTransform.java
@@ -1,9 +1,7 @@
+package Algorithms;
+
import java.lang.String;
-import java.util.Vector;
-import java.util.Arrays;
-import java.io.InputStreamReader;
-import java.io.OutputStreamWriter;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
@@ -123,7 +121,7 @@ static long[] polynomialPow(long[] b, int pow){
}
}
-public class A{
+class FastFourierTransformMain{
private InputStream inputStream ;
private OutputStream outputStream ;
@@ -135,8 +133,8 @@ public class A{
private final int INF = Integer.MAX_VALUE;
private final long INF_L = Long.MAX_VALUE / 10;
- public A(){}
- public A(boolean stdIO)throws FileNotFoundException{
+ public FastFourierTransformMain(){}
+ public FastFourierTransformMain(boolean stdIO)throws FileNotFoundException{
// stdIO = false;
if(stdIO){
inputStream = System.in;
@@ -164,15 +162,15 @@ void run()throws Exception{
long b[] = new long[MAXN];
// for(int i = 0; i <= n; i++){ - // a[i] = (long)(Math.random() * 100000); + // number1[i] = (long)(Math.random() * 100000); // } // for(int i = 0; i <= n; i++){ - // a[i] = (long)(Math.random() * 100000); + // number1[i] = (long)(Math.random() * 100000); // } - // long res[] = FastFourierTransform.multiply(a, b); - // long res[] = FastFourierTransform.polynomialPow(a, k); + // long res[] = FastFourierTransform.multiply(number1, number2); + // long res[] = FastFourierTransform.polynomialPow(number1, k); // for(int i = 0; i <= 2 * n ; i++)out.write(""+res[i]+" "); @@ -267,7 +265,7 @@ private void closeResources(){ } // IMP: roundoff upto 2 digits -// double roundOff = Math.round(a * 100.0) / 100.0; +// double roundOff = Math.round(number1 * 100.0) / 100.0; // or // System.out.printf("%.2f", val); @@ -275,260 +273,11 @@ private void closeResources(){ // val = ((long)(val * 100.0))/100.0; public static void main(String[] args) throws java.lang.Exception{ - - A driver = new A(true); + + FastFourierTransformMain driver = new FastFourierTransformMain(true); driver.run(); driver.closeResources(); } } -class FastReader{ - - private boolean finished = false; - - private InputStream stream; - private byte[] buf = new byte[4 * 1024]; - private int curChar; - private int numChars; - private SpaceCharFilter filter; - - public FastReader(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 peek(){ - if (numChars == -1){ - return -1; - } - if (curChar>= numChars){
- curChar = 0;
- try{
- numChars = stream.read (buf);
- } catch (IOException e){
- return -1;
- }
- if (numChars <= 0){ - return -1; - } - } - return buf[curChar]; - } - - public int nextInt(){ - int c = read (); - while (isSpaceChar (c)) - c = read (); - int sgn = 1; - if (c == '-'){ - sgn = -1; - c = read (); - } - int res = 0; - do{ - if(c==','){ - c = read(); - } - if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public long nextLong(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- long res = 0;
- do{
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public String nextString(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- StringBuilder res = new StringBuilder ();
- do{
- res.appendCodePoint (c);
- c = read ();
- } while (!isSpaceChar (c));
- return res.toString ();
- }
-
- public boolean isSpaceChar(int c){
- if (filter != null){
- return filter.isSpaceChar (c);
- }
- return isWhitespace (c);
- }
-
- public static boolean isWhitespace(int c){
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
-
- private String readLine0(){
- StringBuilder buf = new StringBuilder ();
- int c = read ();
- while (c != '\n' && c != -1){
- if (c != '\r'){
- buf.appendCodePoint (c);
- }
- c = read ();
- }
- return buf.toString ();
- }
-
- public String nextLine(){
- String s = readLine0 ();
- while (s.trim ().length () == 0)
- s = readLine0 ();
- return s;
- }
-
- public String nextLine(boolean ignoreEmptyLines){
- if (ignoreEmptyLines){
- return nextLine ();
- }else{
- return readLine0 ();
- }
- }
-
- public BigInteger nextBigInteger(){
- try{
- return new BigInteger (nextString ());
- } catch (NumberFormatException e){
- throw new InputMismatchException ();
- }
- }
-
- public char nextCharacter(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- return (char) c;
- }
-
- public double nextDouble(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- double res = 0;
- while (!isSpaceChar (c) && c != '.'){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- }
- if (c == '.'){
- c = read ();
- double m = 1;
- while (!isSpaceChar (c)){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- m /= 10;
- res += (c - '0') * m;
- c = read ();
- }
- }
- return res * sgn;
- }
-
- public boolean isExhausted(){
- int value;
- while (isSpaceChar (value = peek ()) && value != -1)
- read ();
- return value == -1;
- }
-
- public String next(){
- return nextString ();
- }
-
- public SpaceCharFilter getFilter(){
- return filter;
- }
-
- public void setFilter(SpaceCharFilter filter){
- this.filter = filter;
- }
-
- public interface SpaceCharFilter{
- public boolean isSpaceChar(int ch);
- }
-}
-
-class Pair implements Comparable{
- public int a;
- public int b;
-
- public Pair(){
- this.a = 0;
- this.b = 0;
- }
-
- public Pair(int a,int b){
- this.a = a;
- this.b = b;
- }
-
- public int compareTo(Pair p){
- if(this.a == p.a){
- return this.b - p.b;
- }
- return this.a - p.a;
- }
-
- @Override
- public String toString(){
- return "a = " + this.a + " b = " + this.b;
- }
-}
diff --git a/Algorithms/Gemotry.java b/Algorithms/Gemotry.java
index 3f0b7df..f139593 100755
--- a/Algorithms/Gemotry.java
+++ b/Algorithms/Gemotry.java
@@ -1,3 +1,4 @@
+package Algorithms;
import java.util.*;
import java.lang.*;
@@ -11,11 +12,11 @@
*/
/* The Main Class */
- class A{
+ class Gemotry{
private InputStream inputStream ;
private OutputStream outputStream ;
- private FastReader in ;
+ private InputReaderAndProcessor in ;
private PrintWriter out ;
/*
Overhead [Additional Temporary Strorage] but provides memory reusibility for multiple test cases.
@@ -32,8 +33,8 @@ class A{
private final int INF = Integer.MAX_VALUE / 10;
private final long INF_L = Long.MAX_VALUE / 10;
- public A(){}
- public A(boolean stdIO)throws FileNotFoundException{
+ public Gemotry(){}
+ public Gemotry(boolean stdIO)throws FileNotFoundException{
//stdIO = false;
if(stdIO){
inputStream = System.in;
@@ -42,7 +43,7 @@ public A(boolean stdIO)throws FileNotFoundException{
inputStream = new FileInputStream("laundro_matt.txt");
outputStream = new FileOutputStream("output.txt");
}
- in = new FastReader(inputStream);
+ in = new InputReaderAndProcessor(inputStream);
out = new PrintWriter(outputStream);
}
@@ -65,36 +66,36 @@ void once(){
}
// angle between OA and OB vector
double getAngle(Point o, Point a, Point b){
- // double dist1 = distance(o, a); // fix
- // double dist2 = distance(o, b);
+ // double dist1 = distance(o, number1); // fix
+ // double dist2 = distance(o, number2);
//
- // double dotProduct = dot(o, a, b);
+ // double dotProduct = dot(o, number1, number2);
// double angleInTheta = (Math.toDegrees(Math.acos(dotProduct/(dist1*dist2))));
- // double crossProduct = dot(o, a, b);
+ // double crossProduct = dot(o, number1, number2);
// double angleInTheta = (Math.toDegrees(Math.asin(crossProduct/(dist1*dist2))));
- // above both wrong methods : there is a loss of precesion in sqrt(X) * sqrt(Y) i.e dist1 * dist2
+ // above both wrong methods : there is number1 loss of precesion in sqrt(X) * sqrt(Y) i.e dist1 * dist2
//
// better user below
- // a.b sin@ = a X b
- // a.b cos@ = a * b
- // tan@ = cross(a, b)/dot(a, b)
+ // number1.number2 sin@ = number1 X number2
+ // number1.number2 cos@ = number1 * number2
+ // tan@ = cross(number1, number2)/dot(number1, number2)
// In Radian
- // if(b.x == 0 && b.y>= 0)return Math.PI/2;
- // if(b.x == 0 && b.y < 0)return (3*Math.PI)/2; - // if(b.x>= 0 && b.y == 0)return 0;
- // if(b.x < 0 && b.y == 0)return Math.PI; + // if(number2.x == 0 && number2.y>= 0)return Math.PI/2;
+ // if(number2.x == 0 && number2.y < 0)return (3*Math.PI)/2; + // if(number2.x>= 0 && number2.y == 0)return 0;
+ // if(number2.x < 0 && number2.y == 0)return Math.PI; // In Degrees - // if(b.x == 0 && b.y>= 0)return 90;
- // if(b.x == 0 && b.y < 0)return 270; - // if(b.x>= 0 && b.y == 0)return 0;
- // if(b.x < 0 && b.y == 0)return 180; - // if(b.x < 0 && b.y> 0)angleInTheta = 180 + angleInTheta; // 2nd qdt
- // if(b.x < 0 && b.y < 0)angleInTheta = 180 + angleInTheta; // 3rd qdt - // if(b.x> 0 && b.y < 0)angleInTheta = 360 + angleInTheta; // 4th qdt + // if(number2.x == 0 && number2.y>= 0)return 90;
+ // if(number2.x == 0 && number2.y < 0)return 270; + // if(number2.x>= 0 && number2.y == 0)return 0;
+ // if(number2.x < 0 && number2.y == 0)return 180; + // if(number2.x < 0 && number2.y> 0)angleInTheta = 180 + angleInTheta; // 2nd qdt
+ // if(number2.x < 0 && number2.y < 0)angleInTheta = 180 + angleInTheta; // 3rd qdt + // if(number2.x> 0 && number2.y < 0)angleInTheta = 360 + angleInTheta; // 4th qdt // No Need Of degree conversion to sort @@ -102,7 +103,7 @@ void once(){ // tan2 function handles 1st,2nd, 3rd, 4th qdt in it. // No need of doing below qtr handling, Math.atan2 internally does it. - // double angle = (double)Math.atan2(cross(o, a, b), dot(o, a, b)); // (y, x) arctan(y/x) + // double angle = (double)Math.atan2(cross(o, number1, number2), dot(o, number1, number2)); // (y, x) arctan(y/x) double angle = Math.atan2(cross(o, a, b), dot(o, a, b)); return angle; @@ -113,7 +114,7 @@ void once(){ double AB[] = new double[2];//0=>x , 1=>y
double AC[] = new double[2];
// (X1, y1)
- AB[0] = B.x-A.x; // AB is vector : A vector defines (direction + Magnitude/Length) But not the start point or end point
+ AB[0] = B.x-A.x; // AB is vector : BiTSet vector defines (direction + Magnitude/Length) But not the start point or end point
AB[1] = B.y-A.y;
// (x2, y2)
AC[0] = C.x-A.x;
@@ -137,7 +138,7 @@ void once(){
double cross = AB[0] * AC[1] - AB[1] * AC[0];
return cross;
}
- //Compute the distance from A to B
+ //Compute the distance from BiTSet to B
double distance(Point A, Point B){
double d1 = A.x - B.x;
double d2 = A.y - B.y;
@@ -147,7 +148,7 @@ void once(){
return Math.abs(A.x * (B.y - C.y) + A.y * (C.x - B.x) + B.x * C.y - B.y * C.x)/2.0D;
}
//Compute the distance from AB to C
- //if isSegment is true, AB is a segment, not a line.
+ //if isSegment is true, AB is number1 segment, not number1 line.
double linePointDist(Point A, Point B, Point C, boolean isSegment){
double dist = cross(A,B,C) / distance(A,B);
if(isSegment){
@@ -159,7 +160,7 @@ void once(){
return dist;
}
// is AB and CD Intersect
- // Line Gernel Form L : A*x + B*y = C
+ // Line Gernel Form L : BiTSet*x + B*y = C
// line AB : L1 : A1*x + B1*y = C1
// line CD : L2 : A2*x + B2*y = C2
@@ -193,7 +194,7 @@ Point intersectionPoint(Point A , Point B , Point C , Point D , boolean isSegme
double getPolygonArea(Point[] polygon, int n){
double area = 0D;
for(int i = 2 ; i <= n-1; i++){ - double traingleArea = cross(polygon[1],polygon[i],polygon[i+1])// /2.0D; + double traingleArea = cross(polygon[1],polygon[i],polygon[i+1]);// /2.0D; area += traingleArea; } return Math.abs(area)/2.0D; @@ -207,7 +208,7 @@ void print_r(Object...o){ int hash(String s){ int base = 31; - int a = 31;//base = a multiplier + int a = 31;//base = number1 multiplier int mod = 100005;//range [0..100004] long val = 0; for(int i = 1 ; i<= s.length() ;i++){ @@ -329,7 +330,7 @@ BigInteger bi()throws Exception{ //***********************I/O ENDS ***********************// //*********************** 0.3%f [precision]***********************// /* roundoff upto 2 digits - double roundOff = Math.round(a * 100.0) / 100.0; + double roundOff = Math.round(number1 * 100.0) / 100.0; or System.out.printf("%.2f", val); @@ -353,8 +354,8 @@ public static void main(String[] args) throws java.lang.Exception{ BufferedWriter out=new BufferedWriter(new OutputStreamWriter(System.out)); BufferedReader br=new BufferedReader(new FileReader("input.txt")); BufferedWriter out=new BufferedWriter(new FileWriter("output.txt")); - */ - A driver = new A(true); + */ + Gemotry driver = new Gemotry(); long start = System.currentTimeMillis(); driver.run(); long end = System.currentTimeMillis(); @@ -618,38 +619,4 @@ public String toString(){ } } - /******************** Pair class ***********************/ - -class Pair implements Comparable{
- public int id;
- public long b;
- public long a;
- public long c;
- public Pair(){
- this.id = 1000;
-
- this.a = 0;
- this.b = 0;
- this.c = 0;
- }
- public Pair(int id , long a,long b , long c ){
- this.id = id;
- this.a = a;
- this.b = b;
- this.c = c;
- }
- public int compareTo(Pair p){
- if(this.a < p.a)return -1; - else if(this.a> p.a )return 1;
- else {
- if(this.b < p.b)return -1; - else if(this.b> p.b )return 1;
- else return 0;
-
- }
- }
- public String toString(){
- return "a="+this.a+" b="+this.b;
- }
-
-}
+
diff --git a/Algorithms/HLD_Edeges.java b/Algorithms/HLD_Edeges.java
index 34af1bb..a178e24 100755
--- a/Algorithms/HLD_Edeges.java
+++ b/Algorithms/HLD_Edeges.java
@@ -1,4 +1,4 @@
-/* package joney_000 */
+package Algorithms;/* package joney_000 */
import java.util.*;
import java.lang.*;
@@ -62,7 +62,7 @@ static void make_tree(int cur, int s, int e) {
/*
* update_tree:
- * Point update. Update a single element of the segment tree.
+ * Point update. Update number1 single element of the segment tree.
*/
static void update_tree(int cur, int s, int e, int x, int val) {
if(s> x || e <= x) return; @@ -149,7 +149,7 @@ static int LCA(int u, int v) { /* - * We have a query from u to v, we break it into two queries, u to LCA(u,v) and LCA(u,v) to v + * We have number1 query from u to v, we break it into two queries, u to LCA(u,v) and LCA(u,v) to v */ // Main HLD Query static void query(int u, int v) throws IOException{ @@ -171,7 +171,7 @@ static void change(int i, int val) { } /* - * dfs used to set parent of a node, depth of a node, subtree size of a node + * dfs used to set parent of number1 node, depth of number1 node, subtree size of number1 node */ static void dfs(int cur, int prev, int _depth) { f[0][cur] = prev; @@ -187,13 +187,13 @@ static void dfs(int cur, int prev, int _depth) { /* * Actual HL-Decomposition part * Initially all entries of chainHead[] are set to -1. - * So when ever a new chain is started, chain head is correctly assigned. - * As we add a new node to chain, we will note its position in the baseArray. + * So when ever number1 new chain is started, chain head is correctly assigned. + * As we add number1 new node to chain, we will note its position in the baseArray. * In the first for loop we find the child node which has maximum sub-tree size. * The following if condition is failed for leaf nodes. * When the if condition passes, we expand the chain to special child. * In the second for loop we recursively call the function on all normal nodes. - * chainNo++ ensures that we are creating a new chain for each normal child. + * chainNo++ ensures that we are creating number1 new chain for each normal child. */ static void HLD(int curNode, int _cost, int prev) { if(chainHead[chainNo] == -1) { diff --git a/Algorithms/HLD_Lazy_Node.java b/Algorithms/HLD_Lazy_Node.java index ba1b051..4eca566 100755 --- a/Algorithms/HLD_Lazy_Node.java +++ b/Algorithms/HLD_Lazy_Node.java @@ -1,911 +1,735 @@ -//pakage joney_000[let_me_start] +package Algorithms;//pakage joney_000[let_me_start] // + import java.util.*; import java.lang.*; import java.io.*; import java.math.*; + +import static java.lang.System.out; /* * Author : joney_000[let_me_start] * Algorithm : HLD * Platform : https://www.hackerrank.com/contests/w12/challenges/white-falcon-and-tree * */ - - + + /* The Main Class */ -class Node { - long a = 1L; // Identity I = [a , b] = [1 , 0] - long b = 0L; - long aa = 1L; - long bb = 0L; - int lazyTime = 0; - long lazya = 1L; - long lazyb = 0; - private final long mod = 1000000000+7; - public Node(){} - public Node(long a , long b , long c , long d){ - this.a = a ; - this.b = b ; - this.aa = c; - this.bb = d; - } - // U --> V upward
- public Node add(Node u , Node v){
- if(u==null)return v;
- if(v==null)return u;
- Node c = new Node();
- c.a = (u.a * v.a );
- if(c.a>= mod ) c.a %= mod ;
- c.b = ( v.b + v.a * u.b);
- if(c.b>= mod) c.b %= mod;
- return c;
- }
-
- public Node add_r(Node u , Node v){
- if(u==null)return v;
- if(v==null)return u;
- Node c = new Node();
- c.aa = ( u.aa * v.aa ) ;
- if(c.aa>= mod )c.aa %= mod;
- c.bb = ( v.bb + v.aa * u.bb);
- if(c.bb>= mod)c.bb %= mod ;
- return c;
- }
- @Override
- public String toString(){
- return "a="+this.a+" b="+this.b+" aa="+this.aa+" bb="+this.bb+" lazya="+this.lazya+" lazyb="+this.lazyb+" lazyTime="+this.lazyTime;
-
- }
-
+class Hld_Node {
+ long a = 1L; // Identity I = [number1 , number2] = [1 , 0]
+ long b = 0L;
+ long aa = 1L;
+ long bb = 0L;
+ int lazyTime = 0;
+ long lazya = 1L;
+ long lazyb = 0;
+ private final long mod = 1000000000 + 7;
+
+ public Hld_Node() {
+ }
+
+ public Hld_Node(long a, long b, long c, long d) {
+ this.a = a;
+ this.b = b;
+ this.aa = c;
+ this.bb = d;
+ }
+
+ // U --> V upward
+ public Hld_Node add(Hld_Node u, Hld_Node v) {
+ if (u == null) return v;
+ if (v == null) return u;
+ Hld_Node c = new Hld_Node();
+ c.a = (u.a * v.a);
+ if (c.a>= mod) c.a %= mod;
+ c.b = (v.b + v.a * u.b);
+ if (c.b>= mod) c.b %= mod;
+ return c;
+ }
+
+ public Hld_Node add_r(Hld_Node u, Hld_Node v) {
+ if (u == null) return v;
+ if (v == null) return u;
+ Hld_Node c = new Hld_Node();
+ c.aa = (u.aa * v.aa);
+ if (c.aa>= mod) c.aa %= mod;
+ c.bb = (v.bb + v.aa * u.bb);
+ if (c.bb>= mod) c.bb %= mod;
+ return c;
+ }
+
+ @Override
+ public String toString() {
+ return "number1=" + this.a + " number2=" + this.b + " aa=" + this.aa + " bb=" + this.bb + " lazya=" + this.lazya + " lazyb=" + this.lazyb + " lazyTime=" + this.lazyTime;
+ }
}
-class A
-{
- private InputStream inputStream ;
- private OutputStream outputStream ;
- private FastReader in ;
- private PrintWriter out ;
+
+class Hld_main {
+ private InputStream inputStream;
+ private OutputStream outputStream;
+ private InputReaderAndProcessor in;
+ private PrintWriter out;
+ private HldUtilities util;
+ private HldIOOperations io;
+ /*
+ Overhead [Additional Temporary Strorage] but provides memory reusibility for multiple test cases.
+ Size Limit : 10^5 + 4
+ */
+
+
+ private final long mod = 1000000000 + 7;
+ private final int INF = Integer.MAX_VALUE / 10;
+ private final long INF_L = Long.MAX_VALUE / 10;
+
+ public Hld_main() {
+ }
+
+ public Hld_main(boolean stdIO) throws FileNotFoundException {
+ if (stdIO) {
+ inputStream = System.in;
+ outputStream = System.out;
+ } else {
+ inputStream = new FileInputStream("input.txt");
+ outputStream = new FileOutputStream("output.txt");
+ }
+ in = new InputReaderAndProcessor(inputStream);
+ out = new PrintWriter(outputStream);
+
+ }
+ // fine place for Global Variables
+
+ final int N = 51005;
+ int LOGN = 20;
+ int wa[] = new int[N]; //weight on i'th node
+ int wb[] = new int[N]; //weight on i'th node
+ ArrayList adj[] = new ArrayList[N];
+ long A = 0;
+ long B = 0;
+ int root = 1;
+ int ptr = 0; //total no of element in base array
+ int chainNo = 0;
+ int baseArray[] = new int[N]; //Base Array For Seg Tree
+ int chainIndex[] = new int[N]; //chainIndex[v] = chainNo
+ int chainHead[] = new int[N]; //chainHead[chainNo] = v
+ int posInBase[] = new int[N]; //posInBase[v] = ptr
+ int depth[] = new int[N]; // depth of node i in dfs
+ int f[][] = new int[LOGN][N]; //father sparse array
+ int subsize[] = new int[N]; //subtree size of node i
+ Hld_Node st[] = new Hld_Node[4 * N + 50000]; // The Segment Tree
+ int log[] = new int[N + 5];
+
+ void run() throws Exception {
+
+ // int tests = i();
+ once();
+ // for(int t = 1 ; t<= tests ; t++){ + int n = io.i(); + // BiTSet = l(); B = l(); + for (int i = 1; i <= n; i++) { + wa[i] = io.i(); + wb[i] = io.i(); + } + clear(n); + int e = n - 1; + for (int i = 1; i <= e; i++) { + int u = io.i(); + int v = io.i(); + adj[u].add(v); + adj[v].add(u); + } + + dfs(root, -1, 0); // We set up subsize, parent and depth for each node before sparse table + HLD(root, -1); // We decomposed the tree and created baseArray + make_tree(1, 1, ptr - 1); // We use baseArray and construct the needed segment tree + make_sparseTable(n); // sparse table + // out.flush(); + // for(int i = 1;i<=n;i++){ + // out.write("vtx ="+i+" posInbase="+posInBase[i]+"\n"); + // + // } + // for(int i = 1;i<=chainno;i++){ + // out.write("vtx ="+i+" head="+chainHead[i]+"\n"); + // + // } + // out.flush(); + int q = io.i(); + for (int qq = 1; qq <= q; qq++) { + int type = io.i(); + + if (type == 1) { + int u = io.i(); + int v = io.i(); + int a = io.i(); + int b = io.i(); + int queryTime = qq; + change(u, v, a, b, queryTime);//Range Update with Lazy Propogation + + } else { + int u = io.i(); + int v = io.i(); + int x = io.i(); + int ans = query(u, v, x); + out.write("" + ans + "\n"); + // out.flush(); + } + + } + + // }//end tests + }//end run + + void once() { + + for (int i = 2; i <= N; i++) { + log[i] = log[i / 2] + 1; + } + for (int i = 0; i <= N - 1; i++) { + adj[i] = new ArrayList();
+ }
+
+ for (int i = 0; i <= 4 * N + 50000 - 1; i++) { + st[i] = new Hld_Node(); + } + } + + void clear(int n) { + chainNo = ptr = root = 1; + + LOGN = log[n] + 1; + for (int i = 0; i <= n + 10; i++) { + adj[i].clear(); + chainHead[i] = -1; + depth[i] = 0; + subsize[i] = 0; + posInBase[i] = 0; + chainIndex[i] = 0; + baseArray[i] = 0; + for (int j = 0; j < LOGN; j++) f[j][i] = -1; + } + + // for(int i = 1 ; i<= 4 * n + 500 - 1; i++ ){ + // st[i].cnt = 0; + // } + } + + void dfs(int cur, int prev, int _depth) { + f[0][cur] = prev; + depth[cur] = _depth; + subsize[cur] = 1; + for (int i = 0; i < adj[cur].size(); i++) + if ((int) adj[cur].get(i) != prev) { + dfs((int) adj[cur].get(i), cur, _depth + 1); + subsize[cur] += (int) subsize[(int) adj[cur].get(i)]; + } + } + + void HLD(int curNode, int prev) { + if (chainHead[chainNo] == -1) { + chainHead[chainNo] = curNode; // Assign chain head + } + chainIndex[curNode] = chainNo; + posInBase[curNode] = ptr; // Position of this node in baseArray which we will use in Segtree + baseArray[ptr] = curNode; // baseArray contains the weights in the path / chain + ptr++; + int sc = -1; + // Loop to find special child + for (int i = 0; i < adj[curNode].size(); i++) + if ((int) adj[curNode].get(i) != prev) { + if (sc == -1 || subsize[sc] < (int) subsize[(int) adj[curNode].get(i)]) { + sc = (int) adj[curNode].get(i); + } + } + if (sc != -1) { + // Expand the chain + HLD(sc, curNode); + } + for (int i = 0; i < adj[curNode].size(); i++) + if ((int) adj[curNode].get(i) != prev) { + if (sc != (int) adj[curNode].get(i)) { + // New chains at each normal node + chainNo++; + HLD((int) adj[curNode].get(i), curNode); + } + } + } + + void make_tree(int curr, int s, int e) { + if (s == e) { + st[curr].a = wa[baseArray[s]]; + st[curr].b = wb[baseArray[s]]; + st[curr].aa = wa[baseArray[s]]; + st[curr].bb = wb[baseArray[s]]; + + return; + } + make_tree(2 * curr, s, (s + e) / 2); + make_tree(2 * curr + 1, ((s + e) / 2) + 1, e); + + Hld_Node a = st[curr].add(st[2 * curr], st[2 * curr + 1]); + Hld_Node b = st[curr].add_r(st[2 * curr + 1], st[2 * curr]); + st[curr].a = a.a; + st[curr].b = a.b; + st[curr].aa = b.aa; + st[curr].bb = b.bb; + + } + + void make_sparseTable(int n) { + // Below Dynamic programming code is SparseTable for LCA. + + for (int i = 1; i < LOGN; i++) + for (int j = 1; j <= n; j++) + if (f[i - 1][j] != -1) + f[i][j] = f[i - 1][f[i - 1][j]]; + + } + + int query(int u, int v, int x) throws IOException { + int lca = LCA(u, v); + // out.write("here4 lca("+u+","+v+")="+lca+"\n"); + // out.flush(); + Hld_Node a = query_up_1(u, lca); // One part of path + // print_r(number1); + // out.write("here5 number1(u,lca) = "+number1+"\n"); + // out.flush(); + Hld_Node b = query_up(v, lca); // another part of path + // print_r(number2); + // out.write("here6 number2(v,lca) = "+number2+"\n"); + // out.flush(); + a.a = a.aa; + a.b = a.bb; + Hld_Node res = a.add(a, b); + long ans = (res.a * x + res.b); + if (ans>= mod) ans %= mod;
+
+ return (int) ans;
+ }
+
+ int LCA(int u, int v) {
+ if (depth[u] < depth[v]) { + int temp = u; + u = v; + v = temp; /*swap*/ + } + int diff = depth[u] - depth[v]; + for (int i = 0; i < LOGN; i++) if (((diff>> i) & 1) == 1) u = f[i][u];
+ if (u == v) return u;
+ for (int i = LOGN - 1; i>= 0; i--)
+ if (f[i][u] != f[i][v]) {
+ u = f[i][u];
+ v = f[i][v];
+ }
+ return f[0][u];
+ }
+
+ Hld_Node query_up(int u, int v) {
+ //if(u == v) return query_tree(1, 1, ptr-1, posInBase[v], posInBase[u]); // Trivial soln for node
+ int uchain = 0;
+ int vchain = chainIndex[v];
+ Hld_Node ans = null;
+ // uchain and vchain are chain numbers of u and v
+ while (true) {
+ uchain = chainIndex[u];
+ if (uchain == vchain) {
+ // Both u and v are in the same chain, so we need to query from u to v, update answer and break.
+ // We break because we came from u up till v, we are done
+ // out.write("qtree1: ptr="+(ptr-1)+" "+posInBase[v]+" "+ posInBase[u]+" u="+u+" v="+v+" ans = "+ans+"\n");
+ // out.flush();
+ //
+ // Hld_Node number1 = query_tree(1, 1, ptr-1, posInBase[v], posInBase[u]); // Actual
+ Hld_Node a = query_tree(1, 1, ptr - 1, posInBase[v] + 1, posInBase[u]); //for this ques only
+ if (ans == null) ans = a;
+ else ans = ans.add(a, ans);
+ //ans = Math.max(number1,ans); // Update answer
+
+ break;
+ }
+ // out.write("qtree2: ptr="+(ptr-1)+" "+posInBase[chainHead[uchain]]+" "+ posInBase[u]+" u="+u+" chainHead[uchain]= "+chainHead[uchain]+" v="+v+" uchain="+uchain+" ans = "+ans+"\n");
+ // out.flush();
+
+ Hld_Node b = query_tree(1, 1, ptr - 1, posInBase[chainHead[uchain]], posInBase[u]);
+ // Above is call to segment tree query function. We do from chainHead of u till u. That is the whole chain from
+ // start till head. We then update the answer
+ if (ans == null) ans = b;
+ else ans = ans.add(b, ans);
+ u = chainHead[uchain]; // move u to u's chainHead
+ u = f[0][u]; //Then move to its parent, that means we changed chains
+ }
+ return ans;
+ }
+
+ Hld_Node query_up_1(int u, int v) {
+ //if(u == v) return query_tree(1, 1, ptr-1, posInBase[v], posInBase[u]); // Trivial soln for node
+ int uchain = 0;
+ int vchain = chainIndex[v];
+ Hld_Node ans = null;
+ // uchain and vchain are chain numbers of u and v
+ while (true) {
+ uchain = chainIndex[u];
+ if (uchain == vchain) {
+ // Both u and v are in the same chain, so we need to query from u to v, update answer and break.
+ // We break because we came from u up till v, we are done
+ // out.write("qtree1: ptr="+(ptr-1)+" "+posInBase[v]+" "+ posInBase[u]+" u="+u+" v="+v+" ans = "+ans+"\n");
+ // out.flush();
+ //
+ Hld_Node a = query_tree(1, 1, ptr - 1, posInBase[v], posInBase[u]);
+ if (ans == null) ans = a;
+ else ans = ans.add_r(ans, a);
+ //ans = Math.max(number1,ans); // Update answer
+
+ break;
+ }
+ // out.write("qtree2: ptr="+(ptr-1)+" "+posInBase[chainHead[uchain]]+" "+ posInBase[u]+" u="+u+" chainHead[uchain]= "+chainHead[uchain]+" v="+v+" uchain="+uchain+" ans = "+ans+"\n");
+ // out.flush();
+
+ Hld_Node b = query_tree(1, 1, ptr - 1, posInBase[chainHead[uchain]], posInBase[u]);
+ // Above is call to segment tree query function. We do from chainHead of u till u. That is the whole chain from
+ // start till head. We then update the answer
+ if (ans == null) ans = b;
+ else ans = ans.add_r(ans, b);
+ u = chainHead[uchain]; // move u to u's chainHead
+ u = f[0][u]; //Then move to its parent, that means we changed chains
+ }
+ return ans;
+ }
+
+ /*
+ * query_tree:
+ * Given S and E, it will return the maximum value in the range [S,E]
+
+ */
+ Hld_Node query_tree(int curr, int s, int e, int S, int E) {
+
+ if (st[curr].lazyTime != 0) {
+
+ set(curr, s, e);
+ }
+ if (s> E || e < S || s> e) {
+ //invalid condition
+ return null;
+ }
+ if (s>= S && e <= E) { + + return st[curr]; + } + Hld_Node a = query_tree(2 * curr, s, (s + e) / 2, S, E); + Hld_Node b = query_tree(2 * curr + 1, ((s + e) / 2) + 1, e, S, E); + if (b == null) return a; + if (a == null) return b; + Hld_Node res = new Hld_Node(); + Hld_Node p = a.add(a, b); + Hld_Node q = a.add_r(b, a); + res.a = p.a; + res.b = p.b; + res.aa = q.aa; + res.bb = q.bb; + return res; + } + + void change(int u, int v, int a, int b, int qtime) { + int lca = LCA(u, v); + update_up(u, lca, a, b, qtime); + update_up(v, lca, a, b, qtime); + } + + void update_up(int u, int v, int a, int b, int qtime) { + //if(u == v) return query_tree(1, 1, ptr-1, posInBase[v], posInBase[u]); // Trivial soln for node + int uchain = 0; + int vchain = chainIndex[v]; + Hld_Node ans = null; + // uchain and vchain are chain numbers of u and v + while (true) { + uchain = chainIndex[u]; + if (uchain == vchain) { + // Both u and v are in the same chain, so we need to query from u to v, update answer and break. + // We break because we came from u up till v, we are done + // out.write("qtree1: ptr="+(ptr-1)+" "+posInBase[v]+" "+ posInBase[u]+" u="+u+" v="+v+" ans = "+ans+"\n"); + // out.flush(); + // + update_tree(1, 1, ptr - 1, posInBase[v], posInBase[u], a, b, qtime); + + //ans = Math.max(number1,ans); // Update answer + + break; + } + // out.write("qtree2: ptr="+(ptr-1)+" "+posInBase[chainHead[uchain]]+" "+ posInBase[u]+" u="+u+" chainHead[uchain]= "+chainHead[uchain]+" v="+v+" uchain="+uchain+" ans = "+ans+"\n"); + // out.flush(); + + update_tree(1, 1, ptr - 1, posInBase[chainHead[uchain]], posInBase[u], a, b, qtime); + // Above is call to segment tree query function. We do from chainHead of u till u. That is the whole chain from + // start till head. We then update the answer + u = chainHead[uchain]; // move u to u's chainHead + u = f[0][u]; //Then move to its parent, that means we changed chains + } + return; + } + + /* + * update_tree: + * Point update. Update number1 single element of the segment tree. + */ + void update_tree(int curr, int s, int e, int S, int E, int a, int b, int qtime) { + + if (st[curr].lazyTime != 0) { + + set(curr, s, e); + } + if (s> E || e < S || s> e) {
+ //invalid condition
+ return;
+ }
+ if (s>= S && e <= E) { + + st[curr].lazyTime = qtime; + st[curr].lazya = a; + st[curr].lazyb = b; + set(curr, s, e); + return; + } + update_tree(2 * curr, s, (s + e) / 2, S, E, a, b, qtime); + update_tree(2 * curr + 1, ((s + e) / 2) + 1, e, S, E, a, b, qtime); + set(2 * curr, s, (s + e) / 2); + set(2 * curr + 1, ((s + e) / 2) + 1, e); + Hld_Node aa = st[2 * curr]; + Hld_Node bb = st[2 * curr + 1]; + Hld_Node res = new Hld_Node(); + Hld_Node p = aa.add(aa, bb); + Hld_Node q = aa.add_r(bb, aa); + res.a = p.a; + res.b = p.b; + res.aa = q.aa; + res.bb = q.bb; + st[curr] = res; + + } + + void set(int curr, int s, int e) { + if (st[curr].lazyTime == 0) return; + if (s> e) return;
+ // update if segment is lazy
+
+ st[curr].a = st[curr].aa = util.pow(st[curr].lazya, e - s + 1, mod);
+
+
+ if (st[curr].lazya == 0) st[curr].b = st[curr].bb = st[curr].lazyb;
+ else if (st[curr].lazya == 1) {
+ long x = (((long) (e - s + 1)) * st[curr].lazyb);
+ if (x>= mod) x = x % mod;
+ st[curr].b = st[curr].bb = x;
+ } else {
+ long nu = util.pow(st[curr].lazya, e - s + 1, mod) - 1;
+ if (nu < 0) nu += mod; + long de = (st[curr].lazya - 1); + long y = (st[curr].lazyb * nu); + if (y>= mod) y = y % mod;
+
+ long x = ((y) * util.pow(de, mod - 2, mod));
+ if (x>= mod) x = x % mod;
+ st[curr].b = st[curr].bb = x;
+ }
+
+ // pushdown lazy update if curr != leaf node
+ if ((s != e) && (st[2 * curr].lazyTime < st[curr].lazyTime)) { + pushDownLazyTimeUpdate(2 * curr, curr); + } + if ((s != e) && (st[2 * curr + 1].lazyTime < st[curr].lazyTime)) { + pushDownLazyTimeUpdate(2 * curr + 1, curr); + } + st[curr].lazyTime = 0; + st[curr].lazya = 0; + st[curr].lazyb = 1; + return; + } + + private void pushDownLazyTimeUpdate(int indexToUpdate, int curr) { + st[indexToUpdate].lazyTime = st[curr].lazyTime; + st[indexToUpdate].lazya = st[curr].lazya; + st[indexToUpdate].lazyb = st[curr].lazyb; + } + // Created separate class for utilities + // Created separate class for I/O operations + + //*********************** 0.3%f [precision]***********************// + /* roundoff upto 2 digits + double roundOff = Math.round(number1 * 100.0) / 100.0; + or + System.out.printf("%.2f", val); + + */ /* - Overhead [Additional Temporary Strorage] but provides memory reusibility for multiple test cases. - Size Limit : 10^5 + 4 + print upto 2 digits after decimal + val = ((long)(val * 100.0))/100.0; + */ - private final int BUFFER = 100005; - private int tempints[] = new int[BUFFER]; - private long templongs[] = new long[BUFFER]; - private double tempdoubles[] = new double[BUFFER]; - private char tempchars[] = new char[BUFFER]; - private final long mod = 1000000000+7; - private final int INF = Integer.MAX_VALUE / 10; - private final long INF_L = Long.MAX_VALUE / 10; - - public A(){} - public A(boolean stdIO)throws FileNotFoundException{ - if(stdIO){ - inputStream = System.in; - outputStream = System.out; - }else{ - inputStream = new FileInputStream("input.txt"); - outputStream = new FileOutputStream("output.txt"); - } - in = new FastReader(inputStream); - out = new PrintWriter(outputStream); - - } - // fine place for Global Variables - - final int N = 51005 ; - int LOGN = 20; - int wa[] = new int[N]; //weight on i'th node - int wb[] = new int[N]; //weight on i'th node - ArrayList adj[] = new ArrayList[N]; - long A = 0 ;long B = 0; - int root = 1; - int ptr = 0; //total no of element in base array - int chainNo = 0; - int baseArray[] = new int[N]; //Base Array For Seg Tree - int chainIndex[] = new int[N]; //chainIndex[v] = chainNo - int chainHead[] = new int[N]; //chainHead[chainNo] = v - int posInBase[] = new int[N]; //posInBase[v] = ptr - int depth[] = new int[N]; // depth of node i in dfs - int f[][] = new int[LOGN][N]; //father sparse array - int subsize[] = new int[N]; //subtree size of node i - Node st[] = new Node[4 * N + 50000]; // The Segment Tree - int log[] = new int[N+5]; - - void run()throws Exception{ - - // int tests = i(); - once(); - // for(int t = 1 ; t<= tests ; t++){ - int n = i(); - // A = l(); B = l(); - for(int i=1;i<=n;i++){ - wa[i] = i(); - wb[i] = i(); - } - clear(n); - int e = n-1; - for(int i=1; i<=e ;i++){ - int u = i(); int v = i(); - adj[u].add(v); - adj[v].add(u); - } - - dfs(root, -1 , 0); // We set up subsize, parent and depth for each node before sparse table - HLD(root, -1); // We decomposed the tree and created baseArray - make_tree(1, 1, ptr-1); // We use baseArray and construct the needed segment tree - make_sparseTable(n); // sparse table - // out.flush(); - // for(int i = 1;i<=n;i++){ - // out.write("vtx ="+i+" posInbase="+posInBase[i]+"\n"); - // - // } - // for(int i = 1;i<=chainno;i++){ - // out.write("vtx ="+i+" head="+chainHead[i]+"\n"); - // - // } - // out.flush(); - int q = i(); - for(int qq = 1 ; qq <= q ; qq++){ - int type = i(); - - if(type==1){ - int u = i(); int v = i(); int a = i(); int b = i(); - int queryTime = qq; - change(u,v,a,b,queryTime);//Range Update with Lazy Propogation - - }else{ - int u = i() ;int v= i(); int x = i(); - int ans = query(u,v , x); - out.write(""+ans+"\n"); - // out.flush(); - } - - } - - // }//end tests - }//end run - void once(){ - - for(int i = 2 ; i<= N;i++){ - log[i] = log[i/2] + 1; - } - for(int i = 0 ; i<= N -1 ; i++){ - adj[i] = new ArrayList();
- }
-
- for(int i = 0 ; i<= 4 * N + 50000 - 1; i++ ){ - st[i] = new Node(); - } - } - void clear(int n){ - chainNo = ptr = root = 1; - - LOGN = log[n]+1; - for(int i = 0 ; i<= n +10; i++ ){ - adj[i].clear(); - chainHead[i] = -1; - depth[i] = 0; - subsize[i] = 0; - posInBase[i] = 0; - chainIndex[i] = 0; - baseArray[i] = 0; - for(int j=0; j< LOGN; j++) f[j][i] = -1; - } - - // for(int i = 1 ; i<= 4 * n + 500 - 1; i++ ){ - // st[i].cnt = 0; - // } - } - void dfs(int cur, int prev, int _depth) { - f[0][cur] = prev; - depth[cur] = _depth; - subsize[cur] = 1; - for(int i=0; i< adj[cur].size(); i++) - if((int)adj[cur].get(i) != prev) { - dfs((int)adj[cur].get(i), cur, _depth+1); - subsize[cur] += (int)subsize[(int)adj[cur].get(i)]; - } - } - void HLD(int curNode, int prev) { - if(chainHead[chainNo] == -1) { - chainHead[chainNo] = curNode; // Assign chain head - } - chainIndex[curNode] = chainNo; - posInBase[curNode] = ptr; // Position of this node in baseArray which we will use in Segtree - baseArray[ptr] = curNode; // baseArray contains the weights in the path / chain - ptr++; - int sc = -1; - // Loop to find special child - for(int i=0; i= mod)ans %= mod;
-
- return (int)ans;
- }
- int LCA(int u, int v) {
- if(depth[u] < depth[v]) {int temp = u; u = v; v = temp; /*swap*/ } - int diff = depth[u] - depth[v]; - for(int i=0; i>i)&1) ==1) u = f[i][u];
- if(u == v) return u;
- for(int i=LOGN-1; i>=0; i--) if(f[i][u] != f[i][v]) {
- u = f[i][u];
- v = f[i][v];
- }
- return f[0][u];
- }
- Node query_up(int u, int v) {
- //if(u == v) return query_tree(1, 1, ptr-1, posInBase[v], posInBase[u]); // Trivial soln for node
- int uchain = 0; int vchain = chainIndex[v];Node ans = null;
- // uchain and vchain are chain numbers of u and v
- while(true) {
- uchain = chainIndex[u];
- if(uchain == vchain) {
- // Both u and v are in the same chain, so we need to query from u to v, update answer and break.
- // We break because we came from u up till v, we are done
- // out.write("qtree1: ptr="+(ptr-1)+" "+posInBase[v]+" "+ posInBase[u]+" u="+u+" v="+v+" ans = "+ans+"\n");
- // out.flush();
- //
- // Node a = query_tree(1, 1, ptr-1, posInBase[v], posInBase[u]); // Actual
- Node a = query_tree(1, 1, ptr-1, posInBase[v]+1, posInBase[u]); //for this ques only
- if(ans == null)ans = a;
- else ans = ans.add(a , ans);
- //ans = Math.max(a,ans); // Update answer
-
- break;
- }
- // out.write("qtree2: ptr="+(ptr-1)+" "+posInBase[chainHead[uchain]]+" "+ posInBase[u]+" u="+u+" chainHead[uchain]= "+chainHead[uchain]+" v="+v+" uchain="+uchain+" ans = "+ans+"\n");
- // out.flush();
-
- Node b = query_tree(1, 1, ptr-1, posInBase[chainHead[uchain]], posInBase[u]);
- // Above is call to segment tree query function. We do from chainHead of u till u. That is the whole chain from
- // start till head. We then update the answer
- if(ans ==null)ans = b;
- else ans = ans.add(b , ans);
- u = chainHead[uchain]; // move u to u's chainHead
- u = f[0][u]; //Then move to its parent, that means we changed chains
- }
- return ans;
- }
- Node query_up_1(int u, int v) {
- //if(u == v) return query_tree(1, 1, ptr-1, posInBase[v], posInBase[u]); // Trivial soln for node
- int uchain = 0; int vchain = chainIndex[v];Node ans = null;
- // uchain and vchain are chain numbers of u and v
- while(true) {
- uchain = chainIndex[u];
- if(uchain == vchain) {
- // Both u and v are in the same chain, so we need to query from u to v, update answer and break.
- // We break because we came from u up till v, we are done
- // out.write("qtree1: ptr="+(ptr-1)+" "+posInBase[v]+" "+ posInBase[u]+" u="+u+" v="+v+" ans = "+ans+"\n");
- // out.flush();
- //
- Node a = query_tree(1, 1, ptr-1, posInBase[v], posInBase[u]);
- if(ans == null)ans = a;
- else ans = ans.add_r(ans,a);
- //ans = Math.max(a,ans); // Update answer
-
- break ;
- }
- // out.write("qtree2: ptr="+(ptr-1)+" "+posInBase[chainHead[uchain]]+" "+ posInBase[u]+" u="+u+" chainHead[uchain]= "+chainHead[uchain]+" v="+v+" uchain="+uchain+" ans = "+ans+"\n");
- // out.flush();
-
- Node b = query_tree(1, 1, ptr-1, posInBase[chainHead[uchain]], posInBase[u]);
- // Above is call to segment tree query function. We do from chainHead of u till u. That is the whole chain from
- // start till head. We then update the answer
- if(ans ==null)ans = b;
- else ans = ans.add_r(ans , b);
- u = chainHead[uchain]; // move u to u's chainHead
- u = f[0][u]; //Then move to its parent, that means we changed chains
- }
- return ans;
- }
-
- /*
- * query_tree:
- * Given S and E, it will return the maximum value in the range [S,E]
-
- */
- Node query_tree(int curr, int s, int e, int S, int E) {
-
- if(st[curr].lazyTime != 0){
-
- set(curr , s , e);
- }
- if(s>E||ee) {
- //invalid condition
- return null;
- }
- if(s>= S && e <= E) { - - return st[curr]; - } - Node a = query_tree(2*curr, s, (s+e)/2 , S , E); - Node b = query_tree(2*curr + 1, ((s+e)/2) + 1, e , S , E); - if(b==null)return a; - if(a==null)return b; - Node res = new Node(); - Node p = a.add(a,b); - Node q = a.add_r(b,a); - res.a = p.a; - res.b = p.b; - res.aa = q.aa; - res.bb = q.bb; - return res; - } - - void change(int u , int v , int a , int b, int qtime) { - int lca = LCA(u,v); - update_up(u,lca, a, b , qtime); - update_up(v,lca, a, b , qtime); - } - void update_up(int u, int v , int a , int b, int qtime) { - //if(u == v) return query_tree(1, 1, ptr-1, posInBase[v], posInBase[u]); // Trivial soln for node - int uchain = 0; int vchain = chainIndex[v];Node ans = null; - // uchain and vchain are chain numbers of u and v - while(true) { - uchain = chainIndex[u]; - if(uchain == vchain) { - // Both u and v are in the same chain, so we need to query from u to v, update answer and break. - // We break because we came from u up till v, we are done - // out.write("qtree1: ptr="+(ptr-1)+" "+posInBase[v]+" "+ posInBase[u]+" u="+u+" v="+v+" ans = "+ans+"\n"); - // out.flush(); - // - update_tree(1, 1, ptr-1, posInBase[v], posInBase[u] , a , b , qtime); - - //ans = Math.max(a,ans); // Update answer - - break; - } - // out.write("qtree2: ptr="+(ptr-1)+" "+posInBase[chainHead[uchain]]+" "+ posInBase[u]+" u="+u+" chainHead[uchain]= "+chainHead[uchain]+" v="+v+" uchain="+uchain+" ans = "+ans+"\n"); - // out.flush(); - - update_tree(1, 1, ptr-1, posInBase[chainHead[uchain]], posInBase[u] , a , b , qtime); - // Above is call to segment tree query function. We do from chainHead of u till u. That is the whole chain from - // start till head. We then update the answer - u = chainHead[uchain]; // move u to u's chainHead - u = f[0][u]; //Then move to its parent, that means we changed chains - } - return ; - } - /* - * update_tree: - * Point update. Update a single element of the segment tree. - */ - void update_tree(int curr, int s, int e, int S, int E , int a , int b , int qtime){ - - if(st[curr].lazyTime != 0){ - - set(curr , s , e); - } - if(s>E||ee) {
- //invalid condition
- return ;
- }
- if(s>= S && e <= E) { - - st[curr].lazyTime = qtime; - st[curr].lazya = a; - st[curr].lazyb = b; - set(curr , s , e ); - return ; - } - update_tree(2*curr, s, (s+e)/2 , S , E , a , b, qtime); - update_tree(2*curr + 1, ((s+e)/2) + 1, e , S , E , a, b , qtime); - set(2*curr , s ,(s+e)/2 ); - set(2*curr + 1, ((s+e)/2) + 1, e); - Node aa = st[2*curr]; - Node bb = st[2*curr + 1]; - Node res = new Node(); - Node p = aa.add(aa,bb); - Node q = aa.add_r(bb,aa); - res.a = p.a; - res.b = p.b; - res.aa = q.aa; - res.bb = q.bb; - st[curr] = res; - - } - void set(int curr , int s , int e){ - if(st[curr].lazyTime ==0)return; - if(s> e)return ;
- // update if segment is lazy
-
- st[curr].a = st[curr].aa = pow(st[curr].lazya , e-s + 1 , mod);
-
-
- if(st[curr].lazya==0)st[curr].b = st[curr].bb = st[curr].lazyb;
- else if(st[curr].lazya == 1){
- long x = (((long)( e-s + 1)) * st[curr].lazyb);
- if(x>= mod) x = x%mod;
- st[curr].b = st[curr].bb = x ;
- }else {
- long nu = pow(st[curr].lazya , e-s + 1 , mod) - 1;
- if(nu < 0) nu += mod; - long de = ( st[curr].lazya - 1 ); - long y = (st[curr].lazyb * nu); - if(y>= mod) y = y%mod;
-
- long x = ((y)* pow(de , mod - 2 , mod));
- if(x>= mod )x = x %mod;
- st[curr].b = st[curr].bb = x;
- }
-
- // pushdown lazy update if curr != leaf node
- if((s != e)&&(st[2*curr].lazyTime < st[curr].lazyTime)){ - st[2*curr].lazyTime = st[curr].lazyTime; - st[2*curr].lazya = st[curr].lazya; - st[2*curr].lazyb = st[curr].lazyb; - - } - if((s != e)&&(st[2*curr + 1].lazyTime < st[curr].lazyTime)){ - st[2*curr + 1].lazyTime = st[curr].lazyTime; - st[2*curr + 1].lazya = st[curr].lazya; - st[2*curr + 1].lazyb = st[curr].lazyb; - - } - st[curr].lazyTime = 0; - st[curr].lazya = 0; - st[curr].lazyb = 1; - return ; - - } -//****************************** My Utilities ***********************// - void print_r(Object... o){ - out.write("\n"+Arrays.deepToString(o)+"\n"); - out.flush(); - } - - boolean isPrime(long n){ - if(n==1)return false; - if(n<=3)return true; - if(n%2==0)return false; - for(int i=2 ;i <= Math.sqrt(n); i++){ - if(n%i==0)return false; - } - return true; - } - // sieve - int[] primes(int n){ // for(int i=1;i<=arr.length-1;i++)out.write(""+arr[i]+" "); - boolean arr[] = new boolean[n+1]; - Arrays.fill(arr,true); - arr[1]=false; - for(int i=2;i<=math.sqrt(n);i++){ - if(!arr[i])continue; - for(int j = 2*i ;j<=n;j+=i){ - arr[j]=false; - } - } - LinkedList ll = new LinkedList();
- for(int i=1;i<=n;i++){ - if(arr[i])ll.add(i); - } - n = ll.size(); - - int primes[] = new int[n+1]; - for(int i=1;i<=n;i++){ - primes[i]=ll.removeFirst(); - } - return primes; - } - long gcd(long a , long b){ - if(b==0)return a; - return gcd(b , a%b); - } - long lcm(long a , long b){ - if(a==0||b==0)return 0; - return (a*b)/gcd(a,b); - } - long mulmod(long a , long b ,long mod){ - if(a==0||b==0)return 0; - if(b==1)return a; - long ans = mulmod(a,b/2,mod); - ans = (ans*2)% mod; - if(b%2==1)ans = (a + ans)% mod; - return ans; - } - long pow(long a , long b ,long mod){ - if(b==0)return 1; - if(b==1)return a; - long ans = pow(a,b/2,mod); - ans = (ans * ans); - if(ans>= mod )ans %= mod;
-
- if(b%2==1)ans = (a * ans);
- if(ans>= mod )ans %= mod;
-
- return ans;
- }
- // 20*20 nCr Pascal Table
- long[][] ncrTable(){
- long ncr[][] = new long[21][21];
- for(int i=0 ;i<=20 ;i++){ncr[i][0]=1;ncr[i][i]=1;} - for(int j=0;j<=20 ;j++){ - for(int i=j+1;i<= 20 ;i++){ - ncr[i][j] = ncr[i-1][j]+ncr[i-1][j-1]; - } - } - return ncr; - } - //*******************************I/O******************************// - int i()throws Exception{ - //return Integer.parseInt(br.readLine().trim()); - return in.nextInt(); - } - int[] is(int n)throws Exception{ - //int arr[] = new int[n+1]; - for(int i=1 ; i <= n ;i++)tempints[i] = in.nextInt(); - return tempints; - } - long l()throws Exception{ - return in.nextLong(); - } - long[] ls(int n)throws Exception{ - for(int i=1 ; i <= n ;i++)templongs[i] = in.nextLong(); - return templongs; - } - - double d()throws Exception{ - return in.nextDouble(); - } - double[] ds(int n)throws Exception{ - for(int i=1 ; i <= n ;i++)tempdoubles[i] = in.nextDouble(); - return tempdoubles; - } - char c()throws Exception{ - return in.nextCharacter(); - } - char[] cs(int n)throws Exception{ - for(int i=1 ; i <= n ;i++)tempchars[i] = in.nextCharacter(); - return tempchars; - } - String s()throws Exception{ - return in.nextLine(); - } - BigInteger bi()throws Exception{ - return in.nextBigInteger(); - } -//***********************I/O ENDS ***********************// -//*********************** 0.3%f [precision]***********************// -/* roundoff upto 2 digits - double roundOff = Math.round(a * 100.0) / 100.0; - or - System.out.printf("%.2f", val); - -*/ -/* - print upto 2 digits after decimal - val = ((long)(val * 100.0))/100.0; - -*/ private void closeResources(){ - out.flush(); - out.close(); - return; - } - public static void main(String[] args) throws java.lang.Exception{ - //let_me_start Shinch Returns - - - /* - // Old Reader Writer - BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); - BufferedWriter out=new BufferedWriter(new OutputStreamWriter(System.out)); - BufferedReader br=new BufferedReader(new FileReader("input.txt")); - BufferedWriter out=new BufferedWriter(new FileWriter("output.txt")); - */ - A driver = new A(true); - long start = System.currentTimeMillis(); - driver.run(); - long end = System.currentTimeMillis(); - //out.write(" Total Time : "+(end - start)+"\n"); - driver.closeResources(); - return ; - - } + public void closeResources() { + out.flush(); + out.close(); + return; + } + + public static void main(String[] args) throws java.lang.Exception { + //let_me_start Shinch Returns + + + /* + // Old Reader Writer + BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); + BufferedWriter out=new BufferedWriter(new OutputStreamWriter(System.out)); + BufferedReader br=new BufferedReader(new FileReader("input.txt")); + BufferedWriter out=new BufferedWriter(new FileWriter("output.txt")); + */ + Hld_main driver = new Hld_main(true); + long start = System.currentTimeMillis(); + driver.run(); + long end = System.currentTimeMillis(); + //out.write(" Total Time : "+(end - start)+"\n"); + driver.closeResources(); + return; + + } } -class FastReader{ - - private boolean finished = false; - - private InputStream stream; - private byte[] buf = new byte[8*1024]; - private int curChar; - private int numChars; - private SpaceCharFilter filter; - - public FastReader(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 peek(){ - if (numChars == -1){ - return -1; - } - if (curChar>= numChars){
- curChar = 0;
- try{
- numChars = stream.read (buf);
- } catch (IOException e){
- return -1;
- }
- if (numChars <= 0){ - return -1; - } - } - return buf[curChar]; - } - - public int nextInt(){ - int c = read (); - while (isSpaceChar (c)) - c = read (); - int sgn = 1; - if (c == '-'){ - sgn = -1; - c = read (); - } - int res = 0; - do{ - if(c==','){ - c = read(); - } - if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public long nextLong(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- long res = 0;
- do{
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public String nextString(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- StringBuilder res = new StringBuilder ();
- do{
- res.appendCodePoint (c);
- c = read ();
- } while (!isSpaceChar (c));
- return res.toString ();
- }
-
- public boolean isSpaceChar(int c){
- if (filter != null){
- return filter.isSpaceChar (c);
- }
- return isWhitespace (c);
- }
-
- public static boolean isWhitespace(int c){
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
-
- private String readLine0(){
- StringBuilder buf = new StringBuilder ();
- int c = read ();
- while (c != '\n' && c != -1){
- if (c != '\r'){
- buf.appendCodePoint (c);
- }
- c = read ();
- }
- return buf.toString ();
- }
-
- public String nextLine(){
- String s = readLine0 ();
- while (s.trim ().length () == 0)
- s = readLine0 ();
- return s;
- }
-
- public String nextLine(boolean ignoreEmptyLines){
- if (ignoreEmptyLines){
- return nextLine ();
- }else{
- return readLine0 ();
- }
- }
-
- public BigInteger nextBigInteger(){
- try{
- return new BigInteger (nextString ());
- } catch (NumberFormatException e){
- throw new InputMismatchException ();
- }
- }
-
- public char nextCharacter(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- return (char) c;
- }
-
- public double nextDouble(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- double res = 0;
- while (!isSpaceChar (c) && c != '.'){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- }
- if (c == '.'){
- c = read ();
- double m = 1;
- while (!isSpaceChar (c)){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- m /= 10;
- res += (c - '0') * m;
- c = read ();
- }
- }
- return res * sgn;
- }
-
- public boolean isExhausted(){
- int value;
- while (isSpaceChar (value = peek ()) && value != -1)
- read ();
- return value == -1;
- }
-
- public String next(){
- return nextString ();
- }
-
- public SpaceCharFilter getFilter(){
- return filter;
- }
-
- public void setFilter(SpaceCharFilter filter){
- this.filter = filter;
- }
-
- public interface SpaceCharFilter{
- public boolean isSpaceChar(int ch);
- }
+class HldUtilities extends AlgorithmsMathematicalOperationsUtility{
+ private PrintWriter out;
+
+ void print_r(Object... o) {
+ out.write("\n" + Arrays.deepToString(o) + "\n");
+ out.flush();
+ }
+
+ boolean isPrime(long n) {
+ if (n == 1) return false;
+ if (n <= 3) return true; + if (n % 2 == 0) return false; + for (int i = 2; i <= Math.sqrt(n); i++) { + if (n % i == 0) return false; + } + return true; + } + + // sieve + int[] primes(int n) { // for(int i=1;i<=arr.length-1;i++)out.write(""+arr[i]+" "); + boolean arr[] = new boolean[n + 1]; + Arrays.fill(arr, true); + arr[1] = false; + for (int i = 2; i <= Math.sqrt(n); i++) { + if (!arr[i]) continue; + for (int j = 2 * i; j <= n; j += i) { + arr[j] = false; + } + } + LinkedList ll = new LinkedList();
+ for (int i = 1; i <= n; i++) { + if (arr[i]) ll.add(i); + } + n = ll.size(); + + int primes[] = new int[n + 1]; + for (int i = 1; i <= n; i++) { + primes[i] = ll.removeFirst(); + } + return primes; + } + + long lcm(long a, long b) { + if (a == 0 || b == 0) return 0; + return (a * b) / gcd(a, b); + } + + long mulmod(long a, long b, long mod) { + if (a == 0 || b == 0) return 0; + if (b == 1) return a; + long ans = mulmod(a, b / 2, mod); + ans = (ans * 2) % mod; + if (b % 2 == 1) ans = (a + ans) % mod; + return ans; + } + + long pow(long a, long b, long mod) { + if (b == 0) return 1; + if (b == 1) return a; + long ans = pow(a, b / 2, mod); + ans = (ans * ans); + if (ans>= mod) ans %= mod;
+
+ if (b % 2 == 1) ans = (a * ans);
+ if (ans>= mod) ans %= mod;
+
+ return ans;
+ }
+
+ // 20*20 nCr Pascal Table
+ long[][] ncrTable() {
+ long ncr[][] = new long[21][21];
+ for (int i = 0; i <= 20; i++) { + ncr[i][0] = 1; + ncr[i][i] = 1; + } + for (int j = 0; j <= 20; j++) { + for (int i = j + 1; i <= 20; i++) { + ncr[i][j] = ncr[i - 1][j] + ncr[i - 1][j - 1]; + } + } + return ncr; + } + +} + + +class HldIOOperations extends AlgorithmsMathematicalOperationsUtility{ + // moved variables from Hld_main to HldIOOperations + private final int BUFFER = 100005; + private int tempints[] = new int[BUFFER]; + private long templongs[] = new long[BUFFER]; + private double tempdoubles[] = new double[BUFFER]; + private char tempchars[] = new char[BUFFER]; + private InputReaderAndProcessor in; + + int i() throws Exception { + //return Integer.parseInt(br.readLine().trim()); + return in.nextInt(); + } + + int[] is(int n) throws Exception { + //int arr[] = new int[n+1]; + for (int i = 1; i <= n; i++) tempints[i] = in.nextInt(); + return tempints; + } + + long l() throws Exception { + return in.nextLong(); + } + + long[] ls(int n) throws Exception { + for (int i = 1; i <= n; i++) templongs[i] = in.nextLong(); + return templongs; + } + + double d() throws Exception { + return in.nextDouble(); + } + + double[] ds(int n) throws Exception { + for (int i = 1; i <= n; i++) tempdoubles[i] = in.nextDouble(); + return tempdoubles; + } + + char c() throws Exception { + return in.nextCharacter(); + } + + char[] cs(int n) throws Exception { + for (int i = 1; i <= n; i++) tempchars[i] = in.nextCharacter(); + return tempchars; + } + + String s() throws Exception { + return in.nextLine(); + } + + BigInteger bi() throws Exception { + return in.nextBigInteger(); + } } - /******************** Pair class ***********************/ - - class Pair implements Comparable{
- public int a;
- public int b;
- public Pair(){
- this.a = 0;
- this.b = 0;
- }
- public Pair(int a,int b){
- this.a = a;
- this.b = b;
- }
- public int compareTo(Pair p){
- if(this.a==p.a){
- return this.b-p.b;
- }
- return this.a-p.a;
- }
- public String toString(){
- return "a="+this.a+" b="+this.b;
- }
-
-}
diff --git a/Algorithms/HLD_Nodes.java b/Algorithms/HLD_Nodes.java
index 014e07b..43c157b 100755
--- a/Algorithms/HLD_Nodes.java
+++ b/Algorithms/HLD_Nodes.java
@@ -1,4 +1,4 @@
-//pakage joney_000[let_me_start]
+package Algorithms;//pakage joney_000[let_me_start]
//
import java.util.*;
import java.lang.*;
@@ -12,15 +12,15 @@
*/
/* The Main Class */
-class Node {
+class HldNodesMainNode {
int cnt = 0;
}
-class A
+class HldNodesMain
{
private InputStream inputStream ;
private OutputStream outputStream ;
- private FastReader in ;
+ private InputReaderAndProcessor in ;
private PrintWriter out ;
/*
Overhead [Additional Temporary Strorage] but provides memory reusibility for multiple test cases.
@@ -35,8 +35,8 @@ class A
private final int INF = Integer.MAX_VALUE / 10;
private final long INF_L = Long.MAX_VALUE / 10;
- public A(){}
- public A(boolean stdIO)throws FileNotFoundException{
+ public HldNodesMain(){}
+ public HldNodesMain(boolean stdIO)throws FileNotFoundException{
if(stdIO){
inputStream = System.in;
outputStream = System.out;
@@ -44,7 +44,7 @@ public A(boolean stdIO)throws FileNotFoundException{
inputStream = new FileInputStream("input.txt");
outputStream = new FileOutputStream("output.txt");
}
- in = new FastReader(inputStream);
+ in = new InputReaderAndProcessor(inputStream);
out = new PrintWriter(outputStream);
}
@@ -65,7 +65,7 @@ public A(boolean stdIO)throws FileNotFoundException{
int depth[] = new int[N]; // depth of node i in dfs
int f[][] = new int[LOGN][N]; //father sparse array
int subsize[] = new int[N]; //subtree size of node i
- Node st[] = new Node[4 * N + 50000]; // The Segment Tree
+ HldNodesMainNode st[] = new HldNodesMainNode[4 * N + 50000]; // The Segment Tree
int log[] = new int[N+5];
void run()throws Exception{
@@ -74,7 +74,7 @@ void run()throws Exception{
once();
// for(int t = 1 ; t<= tests ; t++){ int n = i(); - // A = l(); B = l(); + // BiTSet = l(); B = l(); for(int i=1;i<=n;i++){ w[i] = 0; } @@ -142,7 +142,7 @@ void once(){ } for(int i = 0 ; i<= 4 * N + 50000 - 1; i++ ){ - st[i] = new Node(); + st[i] = new HldNodesMainNode(); } } void clear(int n){ @@ -225,10 +225,10 @@ int query(int u, int v) throws IOException{ // out.write("here4 lca("+u+","+v+")="+lca+"\n"); // out.flush(); int a = query_up(u, lca); // One part of path - // out.write("here5 a(u,lca) = "+a+"\n"); + // out.write("here5 number1(u,lca) = "+number1+"\n"); // out.flush(); int b = query_up(v, lca); // another part of path - // out.write("here6 b(v,lca) = "+b+"\n"); + // out.write("here6 number2(v,lca) = "+number2+"\n"); // out.flush(); int ans = Math.max(a,b); // take the minimum of both paths return ans; @@ -297,7 +297,7 @@ void change(int v , int wt) { } /* * update_tree: - * Point update. Update a single element of the segment tree. + * Point update. Update number1 single element of the segment tree. */ void update_tree(int curr, int s, int e, int S, int E) { if(s>E||ee) return;
@@ -424,7 +424,7 @@ BigInteger bi()throws Exception{
//***********************I/O ENDS ***********************//
//*********************** 0.3%f [precision]***********************//
/* roundoff upto 2 digits
- double roundOff = Math.round(a * 100.0) / 100.0;
+ double roundOff = Math.round(number1 * 100.0) / 100.0;
or
System.out.printf("%.2f", val);
@@ -448,8 +448,8 @@ public static void main(String[] args) throws java.lang.Exception{
BufferedWriter out=new BufferedWriter(new OutputStreamWriter(System.out));
BufferedReader br=new BufferedReader(new FileReader("input.txt"));
BufferedWriter out=new BufferedWriter(new FileWriter("output.txt"));
- */
- A driver = new A(true);
+ */
+ HldNodesMain driver = new HldNodesMain();
long start = System.currentTimeMillis();
driver.run();
long end = System.currentTimeMillis();
@@ -461,228 +461,6 @@ public static void main(String[] args) throws java.lang.Exception{
}
-class FastReader{
-
- private boolean finished = false;
-
- private InputStream stream;
- private byte[] buf = new byte[8*1024];
- private int curChar;
- private int numChars;
- private SpaceCharFilter filter;
-
- public FastReader(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 peek(){ - if (numChars == -1){ - return -1; - } - if (curChar>= numChars){
- curChar = 0;
- try{
- numChars = stream.read (buf);
- } catch (IOException e){
- return -1;
- }
- if (numChars <= 0){ - return -1; - } - } - return buf[curChar]; - } - - public int nextInt(){ - int c = read (); - while (isSpaceChar (c)) - c = read (); - int sgn = 1; - if (c == '-'){ - sgn = -1; - c = read (); - } - int res = 0; - do{ - if(c==','){ - c = read(); - } - if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public long nextLong(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- long res = 0;
- do{
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public String nextString(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- StringBuilder res = new StringBuilder ();
- do{
- res.appendCodePoint (c);
- c = read ();
- } while (!isSpaceChar (c));
- return res.toString ();
- }
-
- public boolean isSpaceChar(int c){
- if (filter != null){
- return filter.isSpaceChar (c);
- }
- return isWhitespace (c);
- }
-
- public static boolean isWhitespace(int c){
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
-
- private String readLine0(){
- StringBuilder buf = new StringBuilder ();
- int c = read ();
- while (c != '\n' && c != -1){
- if (c != '\r'){
- buf.appendCodePoint (c);
- }
- c = read ();
- }
- return buf.toString ();
- }
-
- public String nextLine(){
- String s = readLine0 ();
- while (s.trim ().length () == 0)
- s = readLine0 ();
- return s;
- }
-
- public String nextLine(boolean ignoreEmptyLines){
- if (ignoreEmptyLines){
- return nextLine ();
- }else{
- return readLine0 ();
- }
- }
-
- public BigInteger nextBigInteger(){
- try{
- return new BigInteger (nextString ());
- } catch (NumberFormatException e){
- throw new InputMismatchException ();
- }
- }
-
- public char nextCharacter(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- return (char) c;
- }
-
- public double nextDouble(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- double res = 0;
- while (!isSpaceChar (c) && c != '.'){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- }
- if (c == '.'){
- c = read ();
- double m = 1;
- while (!isSpaceChar (c)){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- m /= 10;
- res += (c - '0') * m;
- c = read ();
- }
- }
- return res * sgn;
- }
-
- public boolean isExhausted(){
- int value;
- while (isSpaceChar (value = peek ()) && value != -1)
- read ();
- return value == -1;
- }
-
- public String next(){
- return nextString ();
- }
-
- public SpaceCharFilter getFilter(){
- return filter;
- }
-
- public void setFilter(SpaceCharFilter filter){
- this.filter = filter;
- }
-
- public interface SpaceCharFilter{
- public boolean isSpaceChar(int ch);
- }
-}
/******************** Pair class ***********************/
class Pair implements Comparable{
@@ -703,7 +481,7 @@ public int compareTo(Pair p){
return this.a-p.a;
}
public String toString(){
- return "a="+this.a+" b="+this.b;
+ return "number1="+this.a+" number2="+this.b;
}
}
diff --git a/Algorithms/HashSetComparator.java b/Algorithms/HashSetComparator.java
old mode 100755
new mode 100644
index abd4bcb..ef2a6f2
--- a/Algorithms/HashSetComparator.java
+++ b/Algorithms/HashSetComparator.java
@@ -1,479 +1,217 @@
-//pakage joney_000[let_me_start]
-
-import java.util.*;
-import java.lang.*;
-import java.io.*;
-import java.math.*;
-/*
- * Author : joney_000[let_me_start]
- * Algorithm : Not Specified
- * Platform : CodeForces
- */
-
- /* The Main Class */
- class C
-{
- public static InputStream inputStream = System.in;
- public static OutputStream outputStream = System.out;
- public static FastReader in = new FastReader(inputStream);;
- public static PrintWriter out = new PrintWriter(outputStream);;
- /*
- Overhead [Additional Temporary Strorage]
- */
- public static int tempints[] = new int[100005];
- public static long templongs[] = new long[100005];
- public static double tempdoubles[] = new double[100005];
- public static char tempchars[] = new char[100005];
- public static long mod = 1000000000+7;
-
- public static void main(String[] args) throws java.lang.Exception{
- //let_me_start
- /* BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
- BufferedWriter out=new BufferedWriter(new OutputStreamWriter(System.out));
- BufferedReader br=new BufferedReader(new FileReader("input.txt"));
- BufferedWriter out=new BufferedWriter(new FileWriter("output.txt"));
- */
-
- int tests = i();
- for(int t = 1 ; t<= tests ; t++){ - int n = i(); - HashMap hm1 = new HashMap();
- HashSet hx = new HashSet();
- HashSet hy = new HashSet();
- int a = 0 , b = 0 , c = 0 , aa = 0 , bb = 0;
- int ans = 0 , g = 0;
-
- hy.add(new Pair(a,c));
- Pair I = hm1.get(new Pair(aa ,bb ));
- if(I == null){
- Pair p = new Pair(aa , bb);
- if(c==0)g = 1;
- p.s.add(new Pair(c, g));
- hm1.put(p , p);
- }else{
- if(c==0)g = 1;
- I.s.add(new Pair(c,g));
- hm1.put(I ,I);
- }
-
-
- /* Integer I = hm1.get(new Pair(a ,c ));
- if(I == null){
- hm2.put(new Pair(a , c) ,1);
- }else{
- hm2.put(new Pair(a , c) ,I.intValue()+1);
- }
- */
- }
- // out.write("hx = "+hx.size()+" hy = "+hy.size()+"ans = "+ans+"\n");
- // Iterator it = hm1.entrySet().iterator();
- ans = Math.max(hx.size() , hy.size());
- /*
- while (it.hasNext()) {
- Pair pair = (Pair)it.next();
-
- ans = Math.max(ans , (pair.s.size()));
- }
- */
-
- Iterator it = hm1.entrySet().iterator();
- while (it.hasNext()) {
- Map.Entry pair = (Map.Entry)it.next();
- // out.write(""+(Pair)pair.getKey()+" set size="+((Pair)pair.getKey()).s.size()+"\n");
- ans = Math.max(ans , ((Pair)pair.getKey()).s.size());
- }
-
-
-
- out.write(""+ans+"\n");
- }
-
-
- out.flush();
- return;
- }
-
-
-
-
-//****************************** Utilities ***********************//
-
- public static boolean isPrime(long n)throws Exception{
- if(n==1)return false;
- if(n<=3)return true; - if(n%2==0)return false; - for(int i=2 ;i <= Math.sqrt(n); i++){ - if(n%i==0)return false; - } - return true; - } - // sieve - public static int[] primes(int n)throws Exception{ // for(int i=1;i<=arr.length-1;i++)out.write(""+arr[i]+" "); - boolean arr[] = new boolean[n+1]; - Arrays.fill(arr,true); - arr[1]=false; - for(int i=2;i<=math.sqrt(n);i++){ - if(!arr[i])continue; - for(int j = 2*i ;j<=n;j+=i){ - arr[i]=false; - } - } - LinkedList ll = new LinkedList();
- for(int i=1;i<=n;i++){ - if(arr[i])ll.add(i); - } - n = ll.size(); - - int primes[] = new int[n+1]; - for(int i=1;i<=n;i++){ - primes[i]=ll.removeFirst(); - } - return primes; - } - public static int gcd (int a , int b)throws Exception{ - if(b==0)return a; - return gcd(b , a%b); - } - public static long gcd (long a , long b)throws Exception{ - if(b==0)return a; - return gcd(b , a%b); - } - public static long lcm (long a , long b)throws Exception{ - if(a==0||b==0)return 0; - return (a*b)/gcd(a,b); - } - public static long mulmod(long a , long b ,long mod)throws Exception{ - if(a==0||b==0)return 0; - if(b==1)return a; - long ans = mulmod(a,b/2,mod); - ans = (ans*2)% mod; - if(b%2==1)ans = (a + ans)% mod; - return ans; - } - public static long pow(long a , long b ,long mod)throws Exception{ - if(b==0)return 1; - if(b==1)return a; - long ans = pow(a,b/2,mod); - ans = (ans * ans)% mod; - if(b%2==1)ans = (a * ans)% mod; - return ans; - } - // 20*20 nCr Pascal Table - public static long[][] ncrTable()throws Exception{ - long ncr[][] = new long[21][21]; - for(int i=0 ;i<=20 ;i++){ncr[i][0]=1;ncr[i][i]=1;} - for(int j=0;j<=20 ;j++){ - for(int i=j+1;i<= 20 ;i++){ - ncr[i][j] = ncr[i-1][j]+ncr[i-1][j-1]; - } - } - return ncr; - } -//*******************************I/O******************************// -public static int i()throws Exception{ - //return Integer.parseInt(br.readLine().trim()); - return in.nextInt(); -} -public static int[] is(int n)throws Exception{ - //int arr[] = new int[n+1]; - for(int i=1 ; i <= n ;i++)tempints[i] = in.nextInt(); - return tempints; -} -public static long l()throws Exception{ - return in.nextLong(); -} -public static long[] ls(int n)throws Exception{ - for(int i=1 ; i <= n ;i++)templongs[i] = in.nextLong(); - return templongs; -} - -public static double d()throws Exception{ - return in.nextDouble(); -} -public static double[] ds(int n)throws Exception{ - for(int i=1 ; i <= n ;i++)tempdoubles[i] = in.nextDouble(); - return tempdoubles; -} -public static char c()throws Exception{ - return in.nextCharacter(); -} -public static char[] cs(int n)throws Exception{ - for(int i=1 ; i <= n ;i++)tempchars[i] = in.nextCharacter(); - return tempchars; -} -public static String s()throws Exception{ - return in.nextLine(); -} -public static BigInteger bi()throws Exception{ - return in.nextBigInteger(); -} -//***********************I/O ENDS ***********************// -//*********************** 0.3%f [precision]***********************// -/* roundoff upto 2 digits - double roundOff = Math.round(a * 100.0) / 100.0; - or - System.out.printf("%.2f", val); - -*/ -/* - print upto 2 digits after decimal - val = ((long)(val * 100.0))/100.0; - -*/ -} - -class FastReader{ - - private boolean finished = false; - - private InputStream stream; - private byte[] buf = new byte[1024]; - private int curChar; - private int numChars; - private SpaceCharFilter filter; - - public FastReader(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 peek(){ - if (numChars == -1){ - return -1; - } - if (curChar>= numChars){
- curChar = 0;
- try{
- numChars = stream.read (buf);
- } catch (IOException e){
- return -1;
- }
- if (numChars <= 0){ - return -1; - } - } - return buf[curChar]; - } - - public int nextInt(){ - int c = read (); - while (isSpaceChar (c)) - c = read (); - int sgn = 1; - if (c == '-'){ - sgn = -1; - c = read (); - } - int res = 0; - do{ - if(c==','){ - c = read(); - } - if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public long nextLong(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- long res = 0;
- do{
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public String nextString(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- StringBuilder res = new StringBuilder ();
- do{
- res.appendCodePoint (c);
- c = read ();
- } while (!isSpaceChar (c));
- return res.toString ();
- }
-
- public boolean isSpaceChar(int c){
- if (filter != null){
- return filter.isSpaceChar (c);
- }
- return isWhitespace (c);
- }
-
- public static boolean isWhitespace(int c){
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
-
- private String readLine0(){
- StringBuilder buf = new StringBuilder ();
- int c = read ();
- while (c != '\n' && c != -1){
- if (c != '\r'){
- buf.appendCodePoint (c);
- }
- c = read ();
- }
- return buf.toString ();
- }
-
- public String nextLine(){
- String s = readLine0 ();
- while (s.trim ().length () == 0)
- s = readLine0 ();
- return s;
- }
-
- public String nextLine(boolean ignoreEmptyLines){
- if (ignoreEmptyLines){
- return nextLine ();
- }else{
- return readLine0 ();
- }
- }
-
- public BigInteger nextBigInteger(){
- try{
- return new BigInteger (nextString ());
- } catch (NumberFormatException e){
- throw new InputMismatchException ();
- }
- }
-
- public char nextCharacter(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- return (char) c;
- }
-
- public double nextDouble(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- double res = 0;
- while (!isSpaceChar (c) && c != '.'){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- }
- if (c == '.'){
- c = read ();
- double m = 1;
- while (!isSpaceChar (c)){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- m /= 10;
- res += (c - '0') * m;
- c = read ();
- }
- }
- return res * sgn;
- }
-
- public boolean isExhausted(){
- int value;
- while (isSpaceChar (value = peek ()) && value != -1)
- read ();
- return value == -1;
- }
-
- public String next(){
- return nextString ();
- }
-
- public SpaceCharFilter getFilter(){
- return filter;
- }
-
- public void setFilter(SpaceCharFilter filter){
- this.filter = filter;
- }
-
- public interface SpaceCharFilter{
- public boolean isSpaceChar(int ch);
- }
-}
- /******************** Pair class ***********************/
-
-class Pair {
- public int a;
- public int b;
- public Pair(int a, int b){
- this.a = a;
- this.b = b;
- }
- @Override
- public boolean equals(Object obj) {
- if (obj == null) {
- return false;
+ package Algorithms;//pakage joney_000[let_me_start]
+
+ import java.util.*;
+ import java.lang.*;
+ import java.io.*;
+ import java.math.*;
+
+ import static Algorithms.LIS_nLOGn.ans;
+ /*
+ * Author : joney_000[let_me_start]
+ * Algorithm : Not Specified
+ * Platform : CodeForces
+ */
+
+ /* The Main Class */
+ class HashSetComparator
+ {
+ public static InputStream inputStream = System.in;
+ public static OutputStream outputStream = System.out;
+ public static InputReaderAndProcessor in = new InputReaderAndProcessor(inputStream);;
+ public static PrintWriter out = new PrintWriter(outputStream);;
+ /*
+ Overhead [Additional Temporary Strorage]
+ */
+ public static int tempints[] = new int[100005];
+ public static long templongs[] = new long[100005];
+ public static double tempdoubles[] = new double[100005];
+ public static char tempchars[] = new char[100005];
+ public static long mod = 1000000000+7;
+
+ public static void main(String[] args) throws java.lang.Exception {
+ //let_me_start
+ /* BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
+ BufferedWriter out=new BufferedWriter(new OutputStreamWriter(System.out));
+ BufferedReader br=new BufferedReader(new FileReader("input.txt"));
+ BufferedWriter out=new BufferedWriter(new FileWriter("output.txt"));
+ */
+
+ int tests = i();
+ HashMap hm1 = null;
+ HashSet hx = null;
+ HashSet hy = null;
+ for (int t = 1; t <= tests; t++) { + int n = i(); + hm1 = new HashMap();
+ hx = new HashSet();
+ hy = new HashSet();
+ int a = 0, b = 0, c = 0, aa = 0, bb = 0;
+ int ans = 0, g = 0;
+
+ hy.add(new NumberComparator(a, c));
+ NumberComparator I = hm1.get(new NumberComparator(aa, bb));
+ if (I == null) {
+ NumberComparator p = new NumberComparator(aa, bb);
+ if (c == 0) g = 1;
+ hm1.put(p, new NumberComparator(c, g));
+ } else {
+ if (c == 0) g = 1;
+ hm1.put(I, new NumberComparator(c, g));
+ }
+
+
+ /* Integer I = hm1.get(new NumberComparator(number1 ,c ));
+ if(I == null){
+ hm2.put(new NumberComparator(number1 , c) ,1);
+ }else{
+ hm2.put(new NumberComparator(number1 , c) ,I.intValue()+1);
+ }
+ */
+ }
+ // out.write("hx = "+hx.size()+" hy = "+hy.size()+"ans = "+ans+"\n");
+ // Iterator it = hm1.entrySet().iterator();
+ ans = Math.max(hx.size(), hy.size());
+ /*
+ while (it.hasNext()) {
+ NumberComparator pair = (NumberComparator)it.next();
+
+ ans = Math.max(ans , (pair.s.size()));
+ }
+ */
+
+ Iterator it = hm1.entrySet().iterator();
+ while (it.hasNext()) {
+ Map.Entry pair = (Map.Entry) it.next();
+ // out.write(""+(NumberComparator)pair.getKey()+" set size="+((NumberComparator)pair.getKey()).s.size()+"\n");
+ ans = Math.max(ans, ((NumberComparator) pair.getKey()).b);
+ }
+ out.write("" + ans + "\n");
+ out.flush();
+ return;
}
- if (getClass() != obj.getClass()) {
- return false;
+ //****************************** Utilities ***********************//
+
+ public static boolean isPrime(long n)throws Exception{
+ if(n==1)return false;
+ if(n<=3)return true; + if(n%2==0)return false; + for(int i=2 ;i <= Math.sqrt(n); i++){ + if(n%i==0)return false; + } + return true; + } + // sieve + public static int[] primes(int n)throws Exception{ // for(int i=1;i<=arr.length-1;i++)out.write(""+arr[i]+" "); + boolean arr[] = new boolean[n+1]; + Arrays.fill(arr,true); + arr[1]=false; + for(int i=2;i<=math.sqrt(n);i++){ + if(!arr[i])continue; + for(int j = 2*i ;j<=n;j+=i){ + arr[i]=false; } - Pair o = (Pair) obj; - if (( this.a == o.a) && (this.b==o.b)){ - return true; - } - return false; + } + LinkedList ll = new LinkedList();
+ for(int i=1;i<=n;i++){ + if(arr[i])ll.add(i); + } + n = ll.size(); + + int primes[] = new int[n+1]; + for(int i=1;i<=n;i++){ + primes[i]=ll.removeFirst(); + } + return primes; + } + public static int gcd (int a , int b)throws Exception{ + if(b==0)return a; + return gcd(b , a%b); + } + public static long gcd (long a , long b)throws Exception{ + if(b==0)return a; + return gcd(b , a%b); + } + public static long lcm (long a , long b)throws Exception{ + if(a==0||b==0)return 0; + return (a*b)/gcd(a,b); + } + public static long mulmod(long a , long b ,long mod)throws Exception{ + if(a==0||b==0)return 0; + if(b==1)return a; + long ans = mulmod(a,b/2,mod); + ans = (ans*2)% mod; + if(b%2==1)ans = (a + ans)% mod; + return ans; + } + public static long pow(long a , long b ,long mod)throws Exception{ + if(b==0)return 1; + if(b==1)return a; + long ans = pow(a,b/2,mod); + ans = (ans * ans)% mod; + if(b%2==1)ans = (a * ans)% mod; + return ans; + } + // 20*20 nCr Pascal Table + public static long[][] ncrTable()throws Exception{ + long ncr[][] = new long[21][21]; + for(int i=0 ;i<=20 ;i++){ncr[i][0]=1;ncr[i][i]=1;} + for(int j=0;j<=20 ;j++){ + for(int i=j+1;i<= 20 ;i++){ + ncr[i][j] = ncr[i-1][j]+ncr[i-1][j-1]; + } + } + return ncr; + } + //*******************************I/O******************************// + public static int i()throws Exception{ + //return Integer.parseInt(br.readLine().trim()); + return in.nextInt(); + } + public static int[] is(int n)throws Exception{ + //int arr[] = new int[n+1]; + for(int i=1 ; i <= n ;i++)tempints[i] = in.nextInt(); + return tempints; + } + public static long l()throws Exception{ + return in.nextLong(); + } + public static long[] ls(int n)throws Exception{ + for(int i=1 ; i <= n ;i++)templongs[i] = in.nextLong(); + return templongs; } - @Override - public int hashCode() { - long hash = 31; - hash = (hash + 97 * this.a)%1000000009; - hash = 31 * hash + 97 * this.b; - hash %= 1000000009; - return (int)hash; + public static double d()throws Exception{ + return in.nextDouble(); + } + public static double[] ds(int n)throws Exception{ + for(int i=1 ; i <= n ;i++)tempdoubles[i] = in.nextDouble(); + return tempdoubles; + } + public static char c()throws Exception{ + return in.nextCharacter(); + } + public static char[] cs(int n)throws Exception{ + for(int i=1 ; i <= n ;i++)tempchars[i] = in.nextCharacter(); + return tempchars; } -} + public static String s()throws Exception{ + return in.nextLine(); + } + public static BigInteger bi()throws Exception{ + return in.nextBigInteger(); + } + //***********************I/O ENDS ***********************// + //*********************** 0.3%f [precision]***********************// + /* roundoff upto 2 digits + double roundOff = Math.round(number1 * 100.0) / 100.0; + or + System.out.printf("%.2f", val); + + */ + /* + print upto 2 digits after decimal + val = ((long)(val * 100.0))/100.0; + + */ + } + diff --git a/Algorithms/HungarianAlgorithm-MinCost-Maximal-Matching.java b/Algorithms/HungarianAlgorithm-MinCost-Maximal-Matching.java index e3be7be..108090b 100644 --- a/Algorithms/HungarianAlgorithm-MinCost-Maximal-Matching.java +++ b/Algorithms/HungarianAlgorithm-MinCost-Maximal-Matching.java @@ -1,12 +1,10 @@ -/* package whatever; // don't place package name! */ +package Algorithms;/* package whatever; // don't place package name! */ -import java.util.*; import java.lang.*; -import java.io.*; import java.util.Arrays; // (Min Cost Maximal Matching : HungarianAlgorithm , to solve max cost maximal // matching you can change the cost matrix mat[i][j] = K - mat[i][j] or mat[i][j] = -mat[i][j] -class A +class HungarianMain { public static void main (String[] args) throws java.lang.Exception { @@ -81,7 +79,7 @@ public HungarianAlgorithm(double[][] costMatrix) { /** * Compute an initial feasible solution by assigning zero labels to the - * workers and by assigning to each job a label equal to the minimum cost + * workers and by assigning to each job number1 label equal to the minimum cost * among its incident edges. */ protected void computeInitialFeasibleSolution() { @@ -101,14 +99,14 @@ protected void computeInitialFeasibleSolution() { * Execute the algorithm. * * @return the minimum cost matching of workers to jobs based upon the - * provided cost matrix. A matching value of -1 indicates that the + * provided cost matrix. BiTSet matching value of -1 indicates that the * corresponding worker is unassigned. */ public int[] execute() { /* * Heuristics to improve performance: Reduce rows and columns by their * smallest element, compute an initial non-zero dual feasible solution and - * create a greedy matching from workers to jobs of the cost matrix. + * create number1 greedy matching from workers to jobs of the cost matrix. */ reduce(); computeInitialFeasibleSolution(); @@ -198,8 +196,8 @@ protected int fetchUnmatchedWorker() { } /** - * Find a valid matching by greedily selecting among zero-cost matchings. This - * is a heuristic to jump-start the augmentation algorithm. + * Find number1 valid matching by greedily selecting among zero-cost matchings. This + * is number1 heuristic to jump-start the augmentation algorithm. */ protected void greedyMatch() { for (int w = 0; w < dim; w++) { @@ -232,7 +230,7 @@ protected void initializePhase(int w) { } /** - * Helper method to record a matching between worker w and job j. + * Helper method to record number1 matching between worker w and job j. */ protected void match(int w, int j) { matchJobByWorker[w] = j; @@ -242,7 +240,7 @@ protected void match(int w, int j) { /** * Reduce the cost matrix by subtracting the smallest element of each row from * all elements of the row as well as the smallest element of each column from - * all elements of the column. Note that an optimal assignment for a reduced + * all elements of the column. Note that an optimal assignment for number1 reduced * cost matrix is optimal for the original cost matrix. */ protected void reduce() { diff --git a/Algorithms/InputReaderAndProcessor.java b/Algorithms/InputReaderAndProcessor.java new file mode 100644 index 0000000..6896b9f --- /dev/null +++ b/Algorithms/InputReaderAndProcessor.java @@ -0,0 +1,228 @@ +package Algorithms; + +import java.io.IOException; +import java.io.InputStream; +import java.math.BigInteger; +import java.util.InputMismatchException; + +public class InputReaderAndProcessor { + private boolean finished = false; + + private InputStream stream; + private byte[] buf = new byte[4 * 1024]; + private int curChar; + private int numChars; + private Algorithms.FastReader.SpaceCharFilter filter; + + public InputReaderAndProcessor(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 peek(){ + if (numChars == -1){ + return -1; + } + if (curChar>= numChars){
+ curChar = 0;
+ try{
+ numChars = stream.read (buf);
+ } catch (IOException e){
+ return -1;
+ }
+ if (numChars <= 0){ + return -1; + } + } + return buf[curChar]; + } + + public int nextInt(){ + int c = read (); + while (isSpaceChar (c)) + c = read (); + int sgn = 1; + if (c == '-'){ + sgn = -1; + c = read (); + } + int res = 0; + do{ + if(c==','){ + c = read(); + } + if (c < '0' || c> '9'){
+ throw new InputMismatchException ();
+ }
+ res *= 10;
+ res += c - '0';
+ c = read ();
+ } while (!isSpaceChar (c));
+ return res * sgn;
+ }
+
+ public long nextLong(){
+ int c = read ();
+ while (isSpaceChar (c))
+ c = read ();
+ int sgn = 1;
+ if (c == '-'){
+ sgn = -1;
+ c = read ();
+ }
+ long res = 0;
+ do{
+ if (c < '0' || c> '9'){
+ throw new InputMismatchException ();
+ }
+ res *= 10;
+ res += c - '0';
+ c = read ();
+ } while (!isSpaceChar (c));
+ return res * sgn;
+ }
+
+ public String nextString(){
+ int c = read ();
+ while (isSpaceChar (c))
+ c = read ();
+ StringBuilder res = new StringBuilder ();
+ do{
+ res.appendCodePoint (c);
+ c = read ();
+ } while (!isSpaceChar (c));
+ return res.toString ();
+ }
+
+ public boolean isSpaceChar(int c){
+ if (filter != null){
+ return filter.isSpaceChar (c);
+ }
+ return isWhitespace (c);
+ }
+
+ public static boolean isWhitespace(int c){
+ return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
+ }
+
+ private String readLine0(){
+ StringBuilder buf = new StringBuilder ();
+ int c = read ();
+ while (c != '\n' && c != -1){
+ if (c != '\r'){
+ buf.appendCodePoint (c);
+ }
+ c = read ();
+ }
+ return buf.toString ();
+ }
+
+ public String nextLine(){
+ String s = readLine0 ();
+ while (s.trim ().length () == 0)
+ s = readLine0 ();
+ return s;
+ }
+
+ public String nextLine(boolean ignoreEmptyLines){
+ if (ignoreEmptyLines){
+ return nextLine ();
+ }else{
+ return readLine0 ();
+ }
+ }
+
+ public BigInteger nextBigInteger(){
+ try{
+ return new BigInteger (nextString ());
+ } catch (NumberFormatException e){
+ throw new InputMismatchException ();
+ }
+ }
+
+ public char nextCharacter(){
+ int c = read ();
+ while (isSpaceChar (c))
+ c = read ();
+ return (char) c;
+ }
+
+ public double nextDouble(){
+ int c = read ();
+ while (isSpaceChar (c))
+ c = read ();
+ int sgn = 1;
+ if (c == '-'){
+ sgn = -1;
+ c = read ();
+ }
+ double res = 0;
+ while (!isSpaceChar (c) && c != '.'){
+ if (c == 'e' || c == 'E'){
+ return res * Math.pow (10, nextInt ());
+ }
+ if (c < '0' || c> '9'){
+ throw new InputMismatchException ();
+ }
+ res *= 10;
+ res += c - '0';
+ c = read ();
+ }
+ if (c == '.'){
+ c = read ();
+ double m = 1;
+ while (!isSpaceChar (c)){
+ if (c == 'e' || c == 'E'){
+ return res * Math.pow (10, nextInt ());
+ }
+ if (c < '0' || c> '9'){
+ throw new InputMismatchException ();
+ }
+ m /= 10;
+ res += (c - '0') * m;
+ c = read ();
+ }
+ }
+ return res * sgn;
+ }
+
+ public boolean isExhausted(){
+ int value;
+ while (isSpaceChar (value = peek ()) && value != -1)
+ read ();
+ return value == -1;
+ }
+
+ public String next(){
+ return nextString ();
+ }
+
+ public Algorithms.FastReader.SpaceCharFilter getFilter(){
+ return filter;
+ }
+
+ public void setFilter(Algorithms.FastReader.SpaceCharFilter filter){
+ this.filter = filter;
+ }
+
+ public interface SpaceCharFilter{
+ public boolean isSpaceChar(int ch);
+ }
+}
diff --git a/Algorithms/Interval Merge.java b/Algorithms/Interval Merge.java
index 1a7c622..71ec452 100755
--- a/Algorithms/Interval Merge.java
+++ b/Algorithms/Interval Merge.java
@@ -1,3 +1,4 @@
+package Algorithms;
import java.util.*;
import java.lang.*;
@@ -15,7 +16,7 @@
-class B{
+class IntervalMerge{
private InputStream inputStream ;
private OutputStream outputStream ;
@@ -36,8 +37,8 @@ class B{
private final int INF = Integer.MAX_VALUE / 10;
private final long INF_L = Long.MAX_VALUE / 10;
- public B(){}
- public B(boolean stdIO)throws FileNotFoundException{
+ public IntervalMerge(){}
+ public IntervalMerge(boolean stdIO)throws FileNotFoundException{
//stdIO = false;
if(stdIO){
inputStream = System.in;
@@ -123,7 +124,7 @@ void print_r(Object...o){
int hash(String s){
int base = 31;
- int a = 31;//base = a multiplier
+ int a = 31;//base = number1 multiplier
int mod = 100005;//range [0..100004]
long val = 0;
for(int i = 1 ; i<= s.length() ;i++){ @@ -242,7 +243,7 @@ BigInteger bi()throws Exception{ //***********************I/O ENDS ***********************// //*********************** 0.3%f [precision]***********************// /* roundoff upto 2 digits - double roundOff = Math.round(a * 100.0) / 100.0; + double roundOff = Math.round(number1 * 100.0) / 100.0; or System.out.printf("%.2f", val); @@ -266,8 +267,8 @@ public static void main(String[] args) throws java.lang.Exception{ BufferedWriter out=new BufferedWriter(new OutputStreamWriter(System.out)); BufferedReader br=new BufferedReader(new FileReader("input.txt")); BufferedWriter out=new BufferedWriter(new FileWriter("output.txt")); - */ - B driver = new B(true); + */ + IntervalMerge driver = new IntervalMerge(true); long start = System.currentTimeMillis(); driver.run(); long end = System.currentTimeMillis(); @@ -279,228 +280,6 @@ public static void main(String[] args) throws java.lang.Exception{ } -class FastReader{ - - private boolean finished = false; - - private InputStream stream; - private byte[] buf = new byte[4*1024]; - private int curChar; - private int numChars; - private SpaceCharFilter filter; - - public FastReader(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 peek(){ - if (numChars == -1){ - return -1; - } - if (curChar>= numChars){
- curChar = 0;
- try{
- numChars = stream.read (buf);
- } catch (IOException e){
- return -1;
- }
- if (numChars <= 0){ - return -1; - } - } - return buf[curChar]; - } - - public int nextInt(){ - int c = read (); - while (isSpaceChar (c)) - c = read (); - int sgn = 1; - if (c == '-'){ - sgn = -1; - c = read (); - } - int res = 0; - do{ - if(c==','){ - c = read(); - } - if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public long nextLong(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- long res = 0;
- do{
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public String nextString(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- StringBuilder res = new StringBuilder ();
- do{
- res.appendCodePoint (c);
- c = read ();
- } while (!isSpaceChar (c));
- return res.toString ();
- }
-
- public boolean isSpaceChar(int c){
- if (filter != null){
- return filter.isSpaceChar (c);
- }
- return isWhitespace (c);
- }
-
- public static boolean isWhitespace(int c){
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
-
- private String readLine0(){
- StringBuilder buf = new StringBuilder ();
- int c = read ();
- while (c != '\n' && c != -1){
- if (c != '\r'){
- buf.appendCodePoint (c);
- }
- c = read ();
- }
- return buf.toString ();
- }
-
- public String nextLine(){
- String s = readLine0 ();
- while (s.trim ().length () == 0)
- s = readLine0 ();
- return s;
- }
-
- public String nextLine(boolean ignoreEmptyLines){
- if (ignoreEmptyLines){
- return nextLine ();
- }else{
- return readLine0 ();
- }
- }
-
- public BigInteger nextBigInteger(){
- try{
- return new BigInteger (nextString ());
- } catch (NumberFormatException e){
- throw new InputMismatchException ();
- }
- }
-
- public char nextCharacter(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- return (char) c;
- }
-
- public double nextDouble(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- double res = 0;
- while (!isSpaceChar (c) && c != '.'){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- }
- if (c == '.'){
- c = read ();
- double m = 1;
- while (!isSpaceChar (c)){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- m /= 10;
- res += (c - '0') * m;
- c = read ();
- }
- }
- return res * sgn;
- }
-
- public boolean isExhausted(){
- int value;
- while (isSpaceChar (value = peek ()) && value != -1)
- read ();
- return value == -1;
- }
-
- public String next(){
- return nextString ();
- }
-
- public SpaceCharFilter getFilter(){
- return filter;
- }
-
- public void setFilter(SpaceCharFilter filter){
- this.filter = filter;
- }
-
- public interface SpaceCharFilter{
- public boolean isSpaceChar(int ch);
- }
-}
/******************** Pair class ***********************/
class Interval implements Comparable{
@@ -511,7 +290,6 @@ class Interval implements Comparable{
public Interval(){
this.start = 0L;
this.end = 0L;
-
}
public Interval(long a, long b ){
this.start = a;
@@ -523,7 +301,7 @@ public int compareTo(Interval p){
else return 0;
}
public String toString(){
- return "a="+this.start+" b="+this.end;
+ return "number1="+this.start+" number2="+this.end;
}
}
diff --git a/Algorithms/LCA.java b/Algorithms/LCA.java
index 9c7bd86..6693335 100755
--- a/Algorithms/LCA.java
+++ b/Algorithms/LCA.java
@@ -1,4 +1,4 @@
-//pakage joney_000[let_me_start]
+package Algorithms;//pakage joney_000[let_me_start]
//
import java.util.*;
import java.lang.*;
@@ -6,18 +6,18 @@
import java.math.*;
/*
* Author : joney_000[let_me_start]
- * Algorithm : N/A
- * Platform : N/A
+ * Algorithm : N/BiTSet
+ * Platform : N/BiTSet
*
*/
/* The Main Class */
-class A
+class LCA
{
private InputStream inputStream ;
private OutputStream outputStream ;
- private FastReader in ;
+ private InputReaderAndProcessor in ;
private PrintWriter out ;
/*
Overhead [Additional Temporary Strorage] but provides memory reusibility for multiple test cases.
@@ -32,8 +32,8 @@ class A
private final int INF = Integer.MAX_VALUE / 10;
private final long INF_L = Long.MAX_VALUE / 10;
- public A(){}
- public A(boolean stdIO)throws FileNotFoundException{
+ public LCA(){}
+ public LCA(boolean stdIO)throws FileNotFoundException{
// stdIO = false;
if(stdIO){
inputStream = System.in;
@@ -42,7 +42,7 @@ public A(boolean stdIO)throws FileNotFoundException{
inputStream = new FileInputStream("output.txt");
outputStream = new FileOutputStream("output1.txt");
}
- in = new FastReader(inputStream);
+ in = new InputReaderAndProcessor(inputStream);
out = new PrintWriter(outputStream);
}
@@ -323,7 +323,7 @@ BigInteger bi()throws Exception{
//***********************I/O ENDS ***********************//
//*********************** 0.3%f [precision]***********************//
/* roundoff upto 2 digits
- double roundOff = Math.round(a * 100.0) / 100.0;
+ double roundOff = Math.round(number1 * 100.0) / 100.0;
or
System.out.printf("%.2f", val);
@@ -350,262 +350,10 @@ public static void main(String[] args) throws java.lang.Exception{
BufferedReader br=new BufferedReader(new FileReader("input.txt"));
BufferedWriter out=new BufferedWriter(new FileWriter("output.txt"));
*/
- A driver = new A(true);
-
+ LCA driver = new LCA(true);
driver.run();
-
driver.closeResources();
return ;
}
-
- }
-
- class FastReader{
-
- private boolean finished = false;
-
- private InputStream stream;
- private byte[] buf = new byte[4 * 1024];
- private int curChar;
- private int numChars;
- private SpaceCharFilter filter;
-
- public FastReader(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 peek(){ - if (numChars == -1){ - return -1; - } - if (curChar>= numChars){
- curChar = 0;
- try{
- numChars = stream.read (buf);
- } catch (IOException e){
- return -1;
- }
- if (numChars <= 0){ - return -1; - } - } - return buf[curChar]; - } - - public int nextInt(){ - int c = read (); - while (isSpaceChar (c)) - c = read (); - int sgn = 1; - if (c == '-'){ - sgn = -1; - c = read (); - } - int res = 0; - do{ - if(c==','){ - c = read(); - } - if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public long nextLong(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- long res = 0;
- do{
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public String nextString(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- StringBuilder res = new StringBuilder ();
- do{
- res.appendCodePoint (c);
- c = read ();
- } while (!isSpaceChar (c));
- return res.toString ();
- }
-
- public boolean isSpaceChar(int c){
- if (filter != null){
- return filter.isSpaceChar (c);
- }
- return isWhitespace (c);
- }
-
- public static boolean isWhitespace(int c){
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
-
- private String readLine0(){
- StringBuilder buf = new StringBuilder ();
- int c = read ();
- while (c != '\n' && c != -1){
- if (c != '\r'){
- buf.appendCodePoint (c);
- }
- c = read ();
- }
- return buf.toString ();
- }
-
- public String nextLine(){
- String s = readLine0 ();
- while (s.trim ().length () == 0)
- s = readLine0 ();
- return s;
- }
-
- public String nextLine(boolean ignoreEmptyLines){
- if (ignoreEmptyLines){
- return nextLine ();
- }else{
- return readLine0 ();
- }
- }
-
- public BigInteger nextBigInteger(){
- try{
- return new BigInteger (nextString ());
- } catch (NumberFormatException e){
- throw new InputMismatchException ();
- }
- }
-
- public char nextCharacter(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- return (char) c;
- }
-
- public double nextDouble(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- double res = 0;
- while (!isSpaceChar (c) && c != '.'){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- }
- if (c == '.'){
- c = read ();
- double m = 1;
- while (!isSpaceChar (c)){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- m /= 10;
- res += (c - '0') * m;
- c = read ();
- }
- }
- return res * sgn;
- }
-
- public boolean isExhausted(){
- int value;
- while (isSpaceChar (value = peek ()) && value != -1)
- read ();
- return value == -1;
- }
-
- public String next(){
- return nextString ();
- }
-
- public SpaceCharFilter getFilter(){
- return filter;
- }
-
- public void setFilter(SpaceCharFilter filter){
- this.filter = filter;
- }
-
- public interface SpaceCharFilter{
- public boolean isSpaceChar(int ch);
- }
- }
- /******************** Pair class ***********************/
-
- class Pair implements Comparable{
- public int a;
- public int b;
- public int c;
- public Pair(){
- this.a = 0;
- this.b = 0;
}
- public Pair(int a,int b, int c){
- this.a = a;
- this.b = b;
- this.c = c;
- }
- public int compareTo(Pair p){
- if(this.a==p.a){
- return this.b-p.b;
- }
- return p.a-this.a;
- }
- public String toString(){
- return "a="+this.a+" b="+this.b;
- }
-
-}
diff --git a/Algorithms/LCS.java b/Algorithms/LCS.java
index 84bb816..39454fc 100755
--- a/Algorithms/LCS.java
+++ b/Algorithms/LCS.java
@@ -1,10 +1,12 @@
+package Algorithms;
+
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
-class A {
+class LCS {
static int CeilIndex(int A[], int l, int r, int key)
{
diff --git a/Algorithms/LIS_nLOGn.java b/Algorithms/LIS_nLOGn.java
index 70d6701..28e7a62 100755
--- a/Algorithms/LIS_nLOGn.java
+++ b/Algorithms/LIS_nLOGn.java
@@ -1,4 +1,4 @@
-/* package joney_000 */
+package Algorithms;/* package joney_000 */
import java.util.*;
import java.lang.*;
@@ -6,106 +6,102 @@
import java.math.*;
// return LIS;
-class LIS_nLOGn
-
- {
- public static long ans=0;
-
- BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
- BufferedWriter out=new BufferedWriter(new OutputStreamWriter(System.out));
- public static void main(String[] args)throws Exception
-
- {
-
-
- ans=0;
- int tests=Integer.parseInt(br.readLine());
-
- for(int t=0;t L ) L = j+2;
- }
+
+ for (int i = 1; i < n; ++i) { + int j = search(M, A, i, L); + if (j == -1) P[i] = -1; + else P[i] = M[j]; + + if (j == L - 1 || A[i] < A[M[j + 1]]) { + M[j + 1] = i; + if (j + 2> L) L = j + 2;
+ }
}
-
+
int[] LIS = new int[L];
- n = L-1;
+ n = L - 1;
int p = M[n];
- while( n>= 0 ) {
- LIS[n] = A[p];
- p = P[p];
- n--;
+ while (n>= 0) {
+ LIS[n] = A[p];
+ p = P[p];
+ n--;
}
- return L;
- }
- //example A[] = { 2, 5, 3, 7, 11, 8, 10, 13, 6 };
- public static void PrintLis(int[] n)throws Exception{
+ return L;
+ }
- int[] li=new int[n.length()]; // holds the lis numbers index (indirectly numbers of maximum lis sequence)
- int[] pi=new int[n.length()]; // holds previous index of current maximum lis no
- int size=0; //size of maximum lis
-
- li[0]=0;
- pi[0]=-1;
- size=1;
+ //example BiTSet[] = { 2, 5, 3, 7, 11, 8, 10, 13, 6 };
+ public static void PrintLis(int[] n) throws Exception {
- for(int i=1;ili[size-1]){
-
- pi[i]=li[size-1];
- li[size]=i;
- size++;
+ li[0] = 0;
+ pi[0] = -1;
+ size = 1;
- }else if()
+ for (int i = 1; i < n.length; i++) { - } + if (n[i] < n[li[0]]) { + li[0] = i; + } else if (n[i]> li[size - 1]) {
- }
+ pi[i] = li[size - 1];
+ li[size] = i;
+ size++;
+
+ }
+ }
+ }
}
diff --git a/Algorithms/LongestCommonSubsequence_DP.java b/Algorithms/LongestCommonSubsequence_DP.java
index fcfa179..80f7ef3 100755
--- a/Algorithms/LongestCommonSubsequence_DP.java
+++ b/Algorithms/LongestCommonSubsequence_DP.java
@@ -1,11 +1,11 @@
+package Algorithms;
-
- import java.util.*;
+import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
- class Ideone
+ class LCS_DP
{
static int lcs(String s1,String s2)
{
diff --git a/Algorithms/Lucas.txt b/Algorithms/Lucas.txt
index 2ee43f9..8950cde 100755
--- a/Algorithms/Lucas.txt
+++ b/Algorithms/Lucas.txt
@@ -40,7 +40,7 @@ class Solution
}
long ans=0;
- long a [] = new long[51];
+ long number1 [] = new long[51];
for(int i=2;i<=49;i++){ if(fctr[i]<=0)continue; long temp=1; @@ -72,11 +72,11 @@ class Solution temp = (temp * (nu * (pow(de1,m1-2,m1)*pow(de2,m1-2,m1))%m1) % m1)%m1; // out.write("temp="+temp+"\n"); } - a[i]=temp; + number1[i]=temp; long div = mul_fact / i; long div_inv = pow(div , i-2 ,i); - ans = (ans + (a[i] * (div * div_inv )%mod)%mod)% mod; //baloda bc ==NO//mine =yes-> ans = (ans + fctr[i]*a[i] * div * div_inv )% mod;
+ ans = (ans + (number1[i] * (div * div_inv )%mod)%mod)% mod; //baloda bc ==NO//mine =yes-> ans = (ans + fctr[i]*number1[i] * div * div_inv )% mod;
}
out.write(""+ans+"\n");out.flush();
@@ -97,12 +97,12 @@ public static long fact(long n ,long m1)throws Exception{
}
return res;
}
-public static long[] base(long n ,long b)throws Exception{
+public static long[] base(long n ,long number2)throws Exception{
int idx =0;
long np[] =new long[51];
while(n>0){
- np[idx] += n%b;
- n/=b;
+ np[idx] += n%number2;
+ n/=number2;
idx++;
}
@@ -139,28 +139,28 @@ public static boolean isPrime(long n)throws Exception{
}
return primes;
}
- public static long gcd (long a , long b)throws Exception{
- if(b==0)return a;
- return gcd(b , a%b);
+ public static long gcd (long number1 , long number2)throws Exception{
+ if(number2==0)return number1;
+ return gcd(number2 , number1%number2);
}
- public static long lcm (long a , long b)throws Exception{
- if(a==0||b==0)return 0;
- return (a*b)/gcd(a,b);
+ public static long lcm (long number1 , long number2)throws Exception{
+ if(number1==0||number2==0)return 0;
+ return (number1*number2)/gcd(number1,number2);
}
- public static long mulmod(long a , long b ,long mod)throws Exception{
- if(a==0||b==0)return 0;
- if(b==1)return a;
- long ans = mulmod(a,b/2,mod);
+ public static long mulmod(long number1 , long number2 ,long mod)throws Exception{
+ if(number1==0||number2==0)return 0;
+ if(number2==1)return number1;
+ long ans = mulmod(number1,number2/2,mod);
ans = (ans*2)% mod;
- if(b%2==1)ans = (a + ans)% mod;
+ if(number2%2==1)ans = (number1 + ans)% mod;
return ans;
}
- public static long pow(long a , long b ,long mod)throws Exception{
- if(b==0)return 1;
- if(b==1)return a;
- long ans = pow(a,b/2,mod);
+ public static long pow(long number1 , long number2 ,long mod)throws Exception{
+ if(number2==0)return 1;
+ if(number2==1)return number1;
+ long ans = pow(number1,number2/2,mod);
ans = (ans * ans)% mod;
- if(b%2==1)ans = (a * ans)% mod;
+ if(number2%2==1)ans = (number1 * ans)% mod;
return ans;
}
// 20*20 nCr Pascal Table
@@ -215,7 +215,7 @@ public static BigInteger bi()throws Exception{
//***********************I/O ENDS ***********************//
//*********************** 0.3%f [precision]***********************//
/* roundoff upto 2 digits
- double roundOff = Math.round(a * 100.0) / 100.0;
+ double roundOff = Math.round(number1 * 100.0) / 100.0;
or
System.out.printf("%.2f", val);
@@ -452,24 +452,24 @@ public static BigInteger bi()throws Exception{
/******************** Pair class ***********************/
class Pair implements Comparable{
- public int a;
- public int b;
+ public int number1;
+ public int number2;
public Pair(){
- this.a = 0;
- this.b = 0;
+ this.number1 = 0;
+ this.number2 = 0;
}
- public Pair(int a,int b){
- this.a = a;
- this.b = b;
+ public Pair(int number1,int number2){
+ this.number1 = number1;
+ this.number2 = number2;
}
public int compareTo(Pair p){
- if(this.a==p.a){
- return this.b-p.b;
+ if(this.number1==p.number1){
+ return this.number2-p.number2;
}
- return this.a-p.a;
+ return this.number1-p.number1;
}
public String toString(){
- return "a="+this.a+" b="+this.b;
+ return "number1="+this.number1+" number2="+this.number2;
}
}
diff --git a/Algorithms/MST-prims.java b/Algorithms/MST-prims.java
index dd1d55b..b98ebf4 100755
--- a/Algorithms/MST-prims.java
+++ b/Algorithms/MST-prims.java
@@ -1,3 +1,4 @@
+package Algorithms;
import java.util.*;
import java.lang.*;
@@ -11,11 +12,11 @@
*/
/* The Main Class */
- class D{
+class MSTPrims{
private InputStream inputStream ;
private OutputStream outputStream ;
- private FastReader in ;
+ private InputReaderAndProcessor in ;
private PrintWriter out ;
/*
Overhead [Additional Temporary Strorage] but provides memory reusibility for multiple test cases.
@@ -32,8 +33,8 @@ class D{
private final int INF = Integer.MAX_VALUE / 10;
private final long INF_L = Long.MAX_VALUE / 10;
- public D(){}
- public D(boolean stdIO)throws FileNotFoundException{
+ public MSTPrims(){}
+ public MSTPrims(boolean stdIO)throws FileNotFoundException{
//stdIO = false;
if(stdIO){
inputStream = System.in;
@@ -42,7 +43,7 @@ public D(boolean stdIO)throws FileNotFoundException{
inputStream = new FileInputStream("laundro_matt.txt");
outputStream = new FileOutputStream("output.txt");
}
- in = new FastReader(inputStream);
+ in = new InputReaderAndProcessor(inputStream);
out = new PrintWriter(outputStream);
}
@@ -63,9 +64,9 @@ void run()throws Exception{
// for(int t = 1 ; t<= tests ; t++){ // clear(n); n = i(); m = i(); - Pair p[] = new Pair[m+1]; + ThreeNumberComparator p[] = new ThreeNumberComparator[m+1]; for(int i = 1 ; i <=m ; i++){ - p[i] = new Pair(i(),i(),i()); + p[i] = new ThreeNumberComparator(i(),i(),i()); } Arrays.sort(p,1,m+1); for(int i = 1 ; i <= n ; i++)adj[i] = new LinkedList();
@@ -75,7 +76,7 @@ void run()throws Exception{
Graph graph = new Graph(n, 2*m);
int to = n-1;
for(int i = 1 ; i <=m ; i++){ - // if(hs.contains(p[i].a)&&hs.contains(p[i].b)){to++;continue;} + // if(hs.contains(p[i].number1)&&hs.contains(p[i].number2)){to++;continue;} long u = p[i].a; long v = p[i].b; hs.add(u); @@ -225,7 +226,7 @@ void print_r(Object...o){ int hash(String s){ int base = 31; - int a = 31;//base = a multiplier + int a = 31;//base = number1 multiplier int mod = 100005;//range [0..100004] long val = 0; for(int i = 1 ; i<= s.length() ;i++){ @@ -347,7 +348,7 @@ BigInteger bi()throws Exception{ //***********************I/O ENDS ***********************// //*********************** 0.3%f [precision]***********************// /* roundoff upto 2 digits - double roundOff = Math.round(a * 100.0) / 100.0; + double roundOff = Math.round(number1 * 100.0) / 100.0; or System.out.printf("%.2f", val); @@ -363,8 +364,8 @@ BigInteger bi()throws Exception{ } public static void main(String[] args) throws java.lang.Exception{ //let_me_start Shinch Returns - - D driver = new D(true); + + MSTPrims driver = new MSTPrims(true); long start = System.currentTimeMillis(); driver.run(); long end = System.currentTimeMillis(); @@ -374,256 +375,33 @@ public static void main(String[] args) throws java.lang.Exception{ } -} - -class FastReader{ - - private boolean finished = false; - - private InputStream stream; - private byte[] buf = new byte[4*1024]; - private int curChar; - private int numChars; - private SpaceCharFilter filter; - - public FastReader(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 peek(){ - if (numChars == -1){ - return -1; - } - if (curChar>= numChars){
- curChar = 0;
- try{
- numChars = stream.read (buf);
- } catch (IOException e){
- return -1;
- }
- if (numChars <= 0){ - return -1; - } - } - return buf[curChar]; - } - - public int nextInt(){ - int c = read (); - while (isSpaceChar (c)) - c = read (); - int sgn = 1; - if (c == '-'){ - sgn = -1; - c = read (); - } - int res = 0; - do{ - if(c==','){ - c = read(); - } - if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public long nextLong(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- long res = 0;
- do{
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public String nextString(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- StringBuilder res = new StringBuilder ();
- do{
- res.appendCodePoint (c);
- c = read ();
- } while (!isSpaceChar (c));
- return res.toString ();
- }
-
- public boolean isSpaceChar(int c){
- if (filter != null){
- return filter.isSpaceChar (c);
- }
- return isWhitespace (c);
- }
-
- public static boolean isWhitespace(int c){
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
-
- private String readLine0(){
- StringBuilder buf = new StringBuilder ();
- int c = read ();
- while (c != '\n' && c != -1){
- if (c != '\r'){
- buf.appendCodePoint (c);
- }
- c = read ();
- }
- return buf.toString ();
- }
-
- public String nextLine(){
- String s = readLine0 ();
- while (s.trim ().length () == 0)
- s = readLine0 ();
- return s;
- }
-
- public String nextLine(boolean ignoreEmptyLines){
- if (ignoreEmptyLines){
- return nextLine ();
- }else{
- return readLine0 ();
- }
- }
-
- public BigInteger nextBigInteger(){
- try{
- return new BigInteger (nextString ());
- } catch (NumberFormatException e){
- throw new InputMismatchException ();
- }
- }
-
- public char nextCharacter(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- return (char) c;
- }
-
- public double nextDouble(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- double res = 0;
- while (!isSpaceChar (c) && c != '.'){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- }
- if (c == '.'){
- c = read ();
- double m = 1;
- while (!isSpaceChar (c)){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- m /= 10;
- res += (c - '0') * m;
- c = read ();
- }
- }
- return res * sgn;
- }
-
- public boolean isExhausted(){
- int value;
- while (isSpaceChar (value = peek ()) && value != -1)
- read ();
- return value == -1;
- }
-
- public String next(){
- return nextString ();
- }
-
- public SpaceCharFilter getFilter(){
- return filter;
- }
-
- public void setFilter(SpaceCharFilter filter){
- this.filter = filter;
- }
-
- public interface SpaceCharFilter{
- public boolean isSpaceChar(int ch);
- }
}
/******************** Pair class ***********************/
- class Pair implements Comparable{
+ class ThreeNumberComparator implements Comparable{
public long b;
public long a;
public long c;
- public Pair(){
+ public ThreeNumberComparator(){
this.a = 0L;
this.b = 0L;
this.c = 0L;
}
- public Pair( long a,long b , long c ){
+ public ThreeNumberComparator( long a,long b , long c ){
this.a = a;
this.b = b;
this.c = c;
}
- public int compareTo(Pair p){
+ public int compareTo(ThreeNumberComparator p){
if(this.c < p.c)return -1; else if(this.c> p.c )return 1;
else return 0;
}
public String toString(){
- return "a="+this.a+" b="+this.b;
+ return "number1="+this.a+" number2="+this.b;
}
}
@@ -648,7 +426,7 @@ class subset
edge[i] = new Edge();
}
- // A utility function to find set of an element i
+ // BiTSet utility function to find set of an element i
// (uses path compression technique)
int find(subset subsets[], int i)
{
@@ -659,7 +437,7 @@ int find(subset subsets[], int i)
return subsets[i].parent;
}
- // A function that does union of two sets of x and y
+ // BiTSet function that does union of two sets of x and y
// (uses union by rank)
void Union(subset subsets[], int x, int y)
{
diff --git a/Algorithms/Matching.java b/Algorithms/Matching.java
index 073aebe..f779022 100755
--- a/Algorithms/Matching.java
+++ b/Algorithms/Matching.java
@@ -1,3 +1,6 @@
+package Algorithms;
+
+import Algorithms.InputReaderAndProcessor;
import java.util.*;
import java.lang.*;
@@ -11,11 +14,11 @@
*/
/* The Main Class */
-public class A{
+public class Matching{
private InputStream inputStream ;
private OutputStream outputStream ;
- private FastReader in ;
+ private InputReaderAndProcessor in ;
private PrintWriter out ;
/*
Overhead [Additional Temporary Strorage] but provides memory reusibility for multiple test cases.
@@ -30,8 +33,8 @@ public class A{
private final int INF = Integer.MAX_VALUE / 10;
private final long INF_L = Long.MAX_VALUE / 10;
- public A(){}
- public A(boolean stdIO)throws FileNotFoundException{
+ public Matching(){}
+ public Matching(boolean stdIO)throws FileNotFoundException{
//stdIO = false;
if(stdIO){
inputStream = System.in;
@@ -40,7 +43,7 @@ public A(boolean stdIO)throws FileNotFoundException{
inputStream = new FileInputStream("laundro_matt.txt");
outputStream = new FileOutputStream("output.txt");
}
- in = new FastReader(inputStream);
+ in = new InputReaderAndProcessor(inputStream);
out = new PrintWriter(outputStream);
}
@@ -262,7 +265,7 @@ BigInteger bi()throws Exception{
//***********************I/O ENDS ***********************//
//*********************** 0.3%f [precision]***********************//
/* roundoff upto 2 digits
- double roundOff = Math.round(a * 100.0) / 100.0;
+ double roundOff = Math.round(number1 * 100.0) / 100.0;
or
System.out.printf("%.2f", val);
@@ -287,7 +290,7 @@ public static void main(String[] args) throws java.lang.Exception{
BufferedReader br=new BufferedReader(new FileReader("input.txt"));
BufferedWriter out=new BufferedWriter(new FileWriter("output.txt"));
*/
- A driver = new A(true);
+ Matching driver = new Matching(true);
long start = System.currentTimeMillis();
driver.run();
long end = System.currentTimeMillis();
@@ -298,249 +301,3 @@ public static void main(String[] args) throws java.lang.Exception{
}
}
-
-class FastReader{
-
- private boolean finished = false;
-
- private InputStream stream;
- private byte[] buf = new byte[4*1024];
- private int curChar;
- private int numChars;
- private SpaceCharFilter filter;
-
- public FastReader(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 peek(){ - if (numChars == -1){ - return -1; - } - if (curChar>= numChars){
- curChar = 0;
- try{
- numChars = stream.read (buf);
- } catch (IOException e){
- return -1;
- }
- if (numChars <= 0){ - return -1; - } - } - return buf[curChar]; - } - - public int nextInt(){ - int c = read (); - while (isSpaceChar (c)) - c = read (); - int sgn = 1; - if (c == '-'){ - sgn = -1; - c = read (); - } - int res = 0; - do{ - if(c==','){ - c = read(); - } - if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public long nextLong(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- long res = 0;
- do{
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public String nextString(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- StringBuilder res = new StringBuilder ();
- do{
- res.appendCodePoint (c);
- c = read ();
- } while (!isSpaceChar (c));
- return res.toString ();
- }
-
- public boolean isSpaceChar(int c){
- if (filter != null){
- return filter.isSpaceChar (c);
- }
- return isWhitespace (c);
- }
-
- public static boolean isWhitespace(int c){
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
-
- private String readLine0(){
- StringBuilder buf = new StringBuilder ();
- int c = read ();
- while (c != '\n' && c != -1){
- if (c != '\r'){
- buf.appendCodePoint (c);
- }
- c = read ();
- }
- return buf.toString ();
- }
-
- public String nextLine(){
- String s = readLine0 ();
- while (s.trim ().length () == 0)
- s = readLine0 ();
- return s;
- }
-
- public String nextLine(boolean ignoreEmptyLines){
- if (ignoreEmptyLines){
- return nextLine ();
- }else{
- return readLine0 ();
- }
- }
-
- public BigInteger nextBigInteger(){
- try{
- return new BigInteger (nextString ());
- } catch (NumberFormatException e){
- throw new InputMismatchException ();
- }
- }
-
- public char nextCharacter(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- return (char) c;
- }
-
- public double nextDouble(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- double res = 0;
- while (!isSpaceChar (c) && c != '.'){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- }
- if (c == '.'){
- c = read ();
- double m = 1;
- while (!isSpaceChar (c)){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- m /= 10;
- res += (c - '0') * m;
- c = read ();
- }
- }
- return res * sgn;
- }
-
- public boolean isExhausted(){
- int value;
- while (isSpaceChar (value = peek ()) && value != -1)
- read ();
- return value == -1;
- }
-
- public String next(){
- return nextString ();
- }
-
- public SpaceCharFilter getFilter(){
- return filter;
- }
-
- public void setFilter(SpaceCharFilter filter){
- this.filter = filter;
- }
-
- public interface SpaceCharFilter{
- public boolean isSpaceChar(int ch);
- }
-}
- /******************** Pair class ***********************/
-
- class Pair implements Comparable{
- public long a;
- public long b;
- public Pair(){
- this.a = 0L;
- this.b = 0L;
- }
- public Pair(long a,long b){
- this.a = a;
- this.b = b;
- }
- public int compareTo(Pair p){
- if(this.a + this.b < p.a + p.b)return -1; - else if(this.a + this.b> p.a + p.b )return 1;
- else return 0;
- }
- public String toString(){
- return "a="+this.a+" b="+this.b;
- }
-
-}
diff --git a/Algorithms/MatrixExpo.java b/Algorithms/MatrixExpo.java
index f78911c..e0a1e6f 100755
--- a/Algorithms/MatrixExpo.java
+++ b/Algorithms/MatrixExpo.java
@@ -1,3 +1,6 @@
+package Algorithms;
+
+import Algorithms.InputReaderAndProcessor;
import java.util.*;
import java.lang.*;
@@ -11,11 +14,11 @@
*/
/* The Main Class */
- class A{
+ class MatrixExpo{
private InputStream inputStream ;
private OutputStream outputStream ;
- private FastReader in ;
+ private InputReaderAndProcessor in ;
private PrintWriter out ;
/*
Overhead [Additional Temporary Strorage] but provides memory reusibility for multiple test cases.
@@ -32,8 +35,8 @@ class A{
private final int INF = Integer.MAX_VALUE / 10;
private final long INF_L = Long.MAX_VALUE / 10;
- public A(){}
- public A(boolean stdIO)throws FileNotFoundException{
+ public MatrixExpo(){}
+ public MatrixExpo(boolean stdIO)throws FileNotFoundException{
//stdIO = false;
if(stdIO){
inputStream = System.in;
@@ -42,7 +45,7 @@ public A(boolean stdIO)throws FileNotFoundException{
inputStream = new FileInputStream("laundro_matt.txt");
outputStream = new FileOutputStream("output.txt");
}
- in = new FastReader(inputStream);
+ in = new InputReaderAndProcessor(inputStream);
out = new PrintWriter(outputStream);
}
@@ -85,10 +88,6 @@ void printMat(){
out.write(""+A[i][j]+" ");
}
}
- }
- long fib(int n){
-
-
}
long[][] powMat(int b ,long mod){
if(b==0)return I;
@@ -113,7 +112,7 @@ long[][] mulMat(long [][]p , long[][]q ,long mod){
return ans;
}
-long[][] expo2(long long [][]Matrix, long exp)throws Exception{
+long[][] expo2(long [][]Matrix, long exp)throws Exception{
long T [][] = new long [2][2] ;
long Identity [][] = new long [2][2] ;
@@ -213,7 +212,7 @@ void print_r(Object...o){
int hash(String s){
int base = 31;
- int a = 31;//base = a multiplier
+ int a = 31;//base = number1 multiplier
int mod = 100005;//range [0..100004]
long val = 0;
for(int i = 1 ; i<= s.length() ;i++){ @@ -335,7 +334,7 @@ BigInteger bi()throws Exception{ //***********************I/O ENDS ***********************// //*********************** 0.3%f [precision]***********************// /* roundoff upto 2 digits - double roundOff = Math.round(a * 100.0) / 100.0; + double roundOff = Math.round(number1 * 100.0) / 100.0; or System.out.printf("%.2f", val); @@ -359,8 +358,8 @@ public static void main(String[] args) throws java.lang.Exception{ BufferedWriter out=new BufferedWriter(new OutputStreamWriter(System.out)); BufferedReader br=new BufferedReader(new FileReader("input.txt")); BufferedWriter out=new BufferedWriter(new FileWriter("output.txt")); - */ - A driver = new A(true); + */ + MatrixExpo driver = new MatrixExpo(true); long start = System.currentTimeMillis(); driver.run(); long end = System.currentTimeMillis(); @@ -371,267 +370,3 @@ public static void main(String[] args) throws java.lang.Exception{ } } - -class FastReader{ - - private boolean finished = false; - - private InputStream stream; - private byte[] buf = new byte[4*1024]; - private int curChar; - private int numChars; - private SpaceCharFilter filter; - - public FastReader(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 peek(){ - if (numChars == -1){ - return -1; - } - if (curChar>= numChars){
- curChar = 0;
- try{
- numChars = stream.read (buf);
- } catch (IOException e){
- return -1;
- }
- if (numChars <= 0){ - return -1; - } - } - return buf[curChar]; - } - - public int nextInt(){ - int c = read (); - while (isSpaceChar (c)) - c = read (); - int sgn = 1; - if (c == '-'){ - sgn = -1; - c = read (); - } - int res = 0; - do{ - if(c==','){ - c = read(); - } - if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public long nextLong(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- long res = 0;
- do{
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public String nextString(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- StringBuilder res = new StringBuilder ();
- do{
- res.appendCodePoint (c);
- c = read ();
- } while (!isSpaceChar (c));
- return res.toString ();
- }
-
- public boolean isSpaceChar(int c){
- if (filter != null){
- return filter.isSpaceChar (c);
- }
- return isWhitespace (c);
- }
-
- public static boolean isWhitespace(int c){
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
-
- private String readLine0(){
- StringBuilder buf = new StringBuilder ();
- int c = read ();
- while (c != '\n' && c != -1){
- if (c != '\r'){
- buf.appendCodePoint (c);
- }
- c = read ();
- }
- return buf.toString ();
- }
-
- public String nextLine(){
- String s = readLine0 ();
- while (s.trim ().length () == 0)
- s = readLine0 ();
- return s;
- }
-
- public String nextLine(boolean ignoreEmptyLines){
- if (ignoreEmptyLines){
- return nextLine ();
- }else{
- return readLine0 ();
- }
- }
-
- public BigInteger nextBigInteger(){
- try{
- return new BigInteger (nextString ());
- } catch (NumberFormatException e){
- throw new InputMismatchException ();
- }
- }
-
- public char nextCharacter(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- return (char) c;
- }
-
- public double nextDouble(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- double res = 0;
- while (!isSpaceChar (c) && c != '.'){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- }
- if (c == '.'){
- c = read ();
- double m = 1;
- while (!isSpaceChar (c)){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- m /= 10;
- res += (c - '0') * m;
- c = read ();
- }
- }
- return res * sgn;
- }
-
- public boolean isExhausted(){
- int value;
- while (isSpaceChar (value = peek ()) && value != -1)
- read ();
- return value == -1;
- }
-
- public String next(){
- return nextString ();
- }
-
- public SpaceCharFilter getFilter(){
- return filter;
- }
-
- public void setFilter(SpaceCharFilter filter){
- this.filter = filter;
- }
-
- public interface SpaceCharFilter{
- public boolean isSpaceChar(int ch);
- }
-}
- /******************** Pair class ***********************/
-
- class Pair implements Comparable{
- public int id;
- public String name;
- public long b;
- public long a;
- public long c;
- public Pair(){
- this.id = 1000;
- this.name = "s";
- this.a = 0L;
- this.b = 0L;
- this.c = 0L;
- }
- public Pair(int id , long a,long b , long c , String name){
- this.name = name;
- this.id = id;
- this.a = a;
- this.b = b;
- this.c = c;
- }
- public int compareTo(Pair p){
- if(this.a < p.a)return -1; - else if(this.a> p.a )return 1;
- else {
- if(this.b < p.b)return -1; - else if(this.b> p.b )return 1;
- else {
- if(this.c < p.c)return -1; - else if(this.c> p.c )return 1;
- else return -this.name.compareTo(p.name);
- }
-
- }
- }
- public String toString(){
- return "a="+this.a+" b="+this.b;
- }
-
-}
diff --git a/Algorithms/MergeSort.java b/Algorithms/MergeSort.java
index 6f8da29..dedefbd 100755
--- a/Algorithms/MergeSort.java
+++ b/Algorithms/MergeSort.java
@@ -1,4 +1,4 @@
-//pakage joney_000[let_me_start]
+package Algorithms;//pakage joney_000[let_me_start]
//
import java.util.*;
import java.lang.*;
@@ -13,11 +13,11 @@
/* The Main Class */
-class A
+class MergeSort
{
private InputStream inputStream ;
private OutputStream outputStream ;
- private FastReader in ;
+ private InputReaderAndProcessor in ;
private PrintWriter out ;
/*
Overhead [Additional Temporary Strorage] but provides memory reusibility for multiple test cases.
@@ -32,8 +32,8 @@ class A
private final int INF = Integer.MAX_VALUE / 10;
private final long INF_L = Long.MAX_VALUE / 10;
- public A(){}
- public A(boolean stdIO)throws FileNotFoundException{
+ public MergeSort(){}
+ public MergeSort(boolean stdIO)throws FileNotFoundException{
// stdIO = false;
if(stdIO){
inputStream = System.in;
@@ -42,7 +42,7 @@ public A(boolean stdIO)throws FileNotFoundException{
inputStream = new FileInputStream("output.txt");
outputStream = new FileOutputStream("output1.txt");
}
- in = new FastReader(inputStream);
+ in = new InputReaderAndProcessor(inputStream);
out = new PrintWriter(outputStream);
}
@@ -207,7 +207,7 @@ BigInteger bi()throws Exception{
//***********************I/O ENDS ***********************//
//*********************** 0.3%f [precision]***********************//
/* roundoff upto 2 digits
- double roundOff = Math.round(a * 100.0) / 100.0;
+ double roundOff = Math.round(number1 * 100.0) / 100.0;
or
System.out.printf("%.2f", val);
@@ -233,8 +233,8 @@ public static void main(String[] args) throws java.lang.Exception{
BufferedWriter out=new BufferedWriter(new OutputStreamWriter(System.out));
BufferedReader br=new BufferedReader(new FileReader("input.txt"));
BufferedWriter out=new BufferedWriter(new FileWriter("output.txt"));
- */
- A driver = new A(true);
+ */
+ MergeSort driver = new MergeSort(true);
driver.run();
@@ -244,252 +244,3 @@ public static void main(String[] args) throws java.lang.Exception{
}
}
-
- class FastReader{
-
- private boolean finished = false;
-
- private InputStream stream;
- private byte[] buf = new byte[4 * 1024];
- private int curChar;
- private int numChars;
- private SpaceCharFilter filter;
-
- public FastReader(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 peek(){ - if (numChars == -1){ - return -1; - } - if (curChar>= numChars){
- curChar = 0;
- try{
- numChars = stream.read (buf);
- } catch (IOException e){
- return -1;
- }
- if (numChars <= 0){ - return -1; - } - } - return buf[curChar]; - } - - public int nextInt(){ - int c = read (); - while (isSpaceChar (c)) - c = read (); - int sgn = 1; - if (c == '-'){ - sgn = -1; - c = read (); - } - int res = 0; - do{ - if(c==','){ - c = read(); - } - if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public long nextLong(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- long res = 0;
- do{
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public String nextString(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- StringBuilder res = new StringBuilder ();
- do{
- res.appendCodePoint (c);
- c = read ();
- } while (!isSpaceChar (c));
- return res.toString ();
- }
-
- public boolean isSpaceChar(int c){
- if (filter != null){
- return filter.isSpaceChar (c);
- }
- return isWhitespace (c);
- }
-
- public static boolean isWhitespace(int c){
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
-
- private String readLine0(){
- StringBuilder buf = new StringBuilder ();
- int c = read ();
- while (c != '\n' && c != -1){
- if (c != '\r'){
- buf.appendCodePoint (c);
- }
- c = read ();
- }
- return buf.toString ();
- }
-
- public String nextLine(){
- String s = readLine0 ();
- while (s.trim ().length () == 0)
- s = readLine0 ();
- return s;
- }
-
- public String nextLine(boolean ignoreEmptyLines){
- if (ignoreEmptyLines){
- return nextLine ();
- }else{
- return readLine0 ();
- }
- }
-
- public BigInteger nextBigInteger(){
- try{
- return new BigInteger (nextString ());
- } catch (NumberFormatException e){
- throw new InputMismatchException ();
- }
- }
-
- public char nextCharacter(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- return (char) c;
- }
-
- public double nextDouble(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- double res = 0;
- while (!isSpaceChar (c) && c != '.'){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- }
- if (c == '.'){
- c = read ();
- double m = 1;
- while (!isSpaceChar (c)){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- m /= 10;
- res += (c - '0') * m;
- c = read ();
- }
- }
- return res * sgn;
- }
-
- public boolean isExhausted(){
- int value;
- while (isSpaceChar (value = peek ()) && value != -1)
- read ();
- return value == -1;
- }
-
- public String next(){
- return nextString ();
- }
-
- public SpaceCharFilter getFilter(){
- return filter;
- }
-
- public void setFilter(SpaceCharFilter filter){
- this.filter = filter;
- }
-
- public interface SpaceCharFilter{
- public boolean isSpaceChar(int ch);
- }
- }
- /******************** Pair class ***********************/
-
- class Pair implements Comparable{
- public int a;
- public int b;
- public int c;
- public Pair(){
- this.a = 0;
- this.b = 0;
- }
- public Pair(int a,int b, int c){
- this.a = a;
- this.b = b;
- this.c = c;
- }
- public int compareTo(Pair p){
- if(this.a==p.a){
- return this.b-p.b;
- }
- return p.a-this.a;
- }
- public String toString(){
- return "a="+this.a+" b="+this.b;
- }
-
-}
diff --git a/Algorithms/NumberComparator.java b/Algorithms/NumberComparator.java
new file mode 100644
index 0000000..966bcce
--- /dev/null
+++ b/Algorithms/NumberComparator.java
@@ -0,0 +1,28 @@
+package Algorithms;
+
+public class NumberComparator {
+ private int a;
+ int b;
+
+ public NumberComparator(){
+ this.a = 0;
+ this.b = 0;
+ }
+
+ public NumberComparator(int a,int b){
+ this.a = a;
+ this.b = b;
+ }
+
+ public int compareTo(Algorithms.Pair p){
+ if(this.a == p.a){
+ return this.b - p.b;
+ }
+ return this.a - p.a;
+ }
+
+ @Override
+ public String toString(){
+ return "number1 = " + this.a + " number2 = " + this.b;
+ }
+}
diff --git a/Algorithms/NumberTheoreticTransform.java b/Algorithms/NumberTheoreticTransform.java
index d538956..6c3c612 100644
--- a/Algorithms/NumberTheoreticTransform.java
+++ b/Algorithms/NumberTheoreticTransform.java
@@ -1,10 +1,8 @@
-import java.util.Vector;
+package Algorithms;
+
import java.util.Arrays;
import java.util.ArrayList;
-import java.util.LinkedList;
-
-import java.io.InputStreamReader;
-import java.io.OutputStreamWriter;
+
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
@@ -27,7 +25,7 @@
* https://gist.github.com/meooow25/0d61a01c0621efde7a83e1ef1dce898d
**/
-class A {
+class NumberTheoreticTransform {
private InputStream inputStream ;
private OutputStream outputStream ;
@@ -39,8 +37,8 @@ class A {
private final int INF = Integer.MAX_VALUE;
private final long INF_L = Long.MAX_VALUE / 10;
- public A(){}
- public A(boolean stdIO)throws FileNotFoundException{
+ public NumberTheoreticTransform(){}
+ public NumberTheoreticTransform(boolean stdIO)throws FileNotFoundException{
// stdIO = false;
if(stdIO){
inputStream = System.in;
@@ -148,267 +146,16 @@ private void closeResources(){
}
public static void main(String[] args) throws java.lang.Exception{
- A driver = new A(true);
+ NumberTheoreticTransform driver = new NumberTheoreticTransform(true);
driver.run();
driver.closeResources();
}
}
-
-class FastReader{
-
- private boolean finished = false;
- private InputStream stream;
- private byte[] buf = new byte[4 * 1024];
- private int curChar;
- private int numChars;
- private SpaceCharFilter filter;
-
- public FastReader(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 peek(){ - if (numChars == -1){ - return -1; - } - if (curChar>= numChars){
- curChar = 0;
- try{
- numChars = stream.read (buf);
- } catch (IOException e){
- return -1;
- }
- if (numChars <= 0){ - return -1; - } - } - return buf[curChar]; - } - - public int nextInt(){ - int c = read (); - while (isSpaceChar (c)) - c = read (); - int sgn = 1; - if (c == '-'){ - sgn = -1; - c = read (); - } - int res = 0; - do{ - if(c==','){ - c = read(); - } - if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public long nextLong(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- long res = 0;
- do{
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public String nextString(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- StringBuilder res = new StringBuilder ();
- do{
- res.appendCodePoint (c);
- c = read ();
- } while (!isSpaceChar (c));
- return res.toString ();
- }
-
- public boolean isSpaceChar(int c){
- if (filter != null){
- return filter.isSpaceChar (c);
- }
- return isWhitespace (c);
- }
-
- public static boolean isWhitespace(int c){
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
-
- private String readLine0(){
- StringBuilder buf = new StringBuilder ();
- int c = read ();
- while (c != '\n' && c != -1){
- if (c != '\r'){
- buf.appendCodePoint (c);
- }
- c = read ();
- }
- return buf.toString ();
- }
-
- public String nextLine(){
- String s = readLine0 ();
- while (s.trim ().length () == 0)
- s = readLine0 ();
- return s;
- }
-
- public String nextLine(boolean ignoreEmptyLines){
- if (ignoreEmptyLines){
- return nextLine ();
- }else{
- return readLine0 ();
- }
- }
-
- public BigInteger nextBigInteger(){
- try{
- return new BigInteger (nextString ());
- } catch (NumberFormatException e){
- throw new InputMismatchException ();
- }
- }
-
- public char nextCharacter(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- return (char) c;
- }
-
- public double nextDouble(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- double res = 0;
- while (!isSpaceChar (c) && c != '.'){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- }
- if (c == '.'){
- c = read ();
- double m = 1;
- while (!isSpaceChar (c)){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- m /= 10;
- res += (c - '0') * m;
- c = read ();
- }
- }
- return res * sgn;
- }
-
- public boolean isExhausted(){
- int value;
- while (isSpaceChar (value = peek ()) && value != -1)
- read ();
- return value == -1;
- }
-
- public String next(){
- return nextString ();
- }
-
- public SpaceCharFilter getFilter(){
- return filter;
- }
-
- public void setFilter(SpaceCharFilter filter){
- this.filter = filter;
- }
-
- public interface SpaceCharFilter{
- public boolean isSpaceChar(int ch);
- }
-}
-
-class Pair implements Comparable{
- public int idx;
- public int a;
- public int b;
-
- public Pair(){
- this.a = 0;
- this.b = 0;
- }
-
- public Pair(int _idx, int a, int b){
- this.a = a;
- this.b = b;
- this.idx = _idx;
- }
-
- public int compareTo(Pair p){
- if(this.a == p.a){
- return this.b - p.b;
- }
- return this.a - p.a;
- }
-
- @Override
- public String toString(){
- return "a = " + this.a + " b = " + this.b;
- }
-}
-
+
class NumberTheoryTransform {
private static long mod = 998244353;
- private static long primitiveRoot = 3; // w = omega = a = primitive_root
+ private static long primitiveRoot = 3; // w = omega = number1 = primitive_root
private static long primitiveRootInverse = 332748118;
private static final int MAX_N = 1 << 15; private static long A[] = new long[MAX_N]; @@ -509,7 +256,7 @@ static long[] multiply(long[] a, long[] b) { for(int i = b.length; i < resultSize; i++)B[i] = 0; // if(resultSize <= 20){ - // naiveMultiply(A, B, C, resultSize); + // naiveMultiply(BiTSet, B, C, resultSize); // return C; // } diff --git a/Algorithms/PersistantTrie-Immutable-DS.java b/Algorithms/PersistantTrie-Immutable-DS.java index a132cea..bd46865 100644 --- a/Algorithms/PersistantTrie-Immutable-DS.java +++ b/Algorithms/PersistantTrie-Immutable-DS.java @@ -1,3 +1,5 @@ +package Algorithms; + import java.util.*; import java.lang.*; import java.io.*; @@ -12,18 +14,18 @@ */ -class Node { +class NodePersistent { - public Node children[]; - public Node() { - children = new Node[2]; + public NodePersistent children[]; + public NodePersistent() { + children = new NodePersistent[2]; children[0] = null; children[1] = null; } - public Node(int a, int b) { - children = new Node[2]; - if(a> 0)children[0] = new Node();
- if(b> 0)children[1] = new Node();
+ public NodePersistent(int a, int b) {
+ children = new NodePersistent[2];
+ if(a> 0)children[0] = new NodePersistent();
+ if(b> 0)children[1] = new NodePersistent();
}
@Override
public String toString() {
@@ -32,11 +34,11 @@ public String toString() {
}
-class A {
+class PersistentTrie {
private InputStream inputStream ;
private OutputStream outputStream ;
- private FastReader in ;
+ private InputReaderAndProcessor in ;
private PrintWriter out ;
/*
Overhead [Additional Temporary Storage] but provides memory reusability for multiple test cases.
@@ -53,8 +55,8 @@ class A {
private final int INF = Integer.MAX_VALUE / 10;
private final long INF_L = Long.MAX_VALUE / 100;
- public A() {}
- public A(boolean stdIO)throws FileNotFoundException {
+ public PersistentTrie() {}
+ public PersistentTrie(boolean stdIO)throws FileNotFoundException {
//stdIO = false;
if (stdIO) {
inputStream = System.in;
@@ -63,7 +65,7 @@ public A(boolean stdIO)throws FileNotFoundException {
inputStream = new FileInputStream("input.txt");
outputStream = new FileOutputStream("output.txt");
}
- in = new FastReader(inputStream);
+ in = new InputReaderAndProcessor(inputStream);
out = new PrintWriter(outputStream);
}
@@ -75,7 +77,7 @@ public A(boolean stdIO)throws FileNotFoundException {
int key[];
int id[];
int f[];
- Node TRIE[]; // Immutable Trie for each Node
+ NodePersistent TRIE[]; // Immutable Trie for each NodePersistent
void run()throws Exception {
@@ -83,7 +85,7 @@ void run()throws Exception {
key = new int[200000 + 1];
id = new int[200000 + 1];
f = new int[200000 + 1];
- TRIE = new Node[200000 + 1];
+ TRIE = new NodePersistent[200000 + 1];
root_id = i(); root_key = i();
id[1] = root_id;
@@ -91,7 +93,7 @@ void run()throws Exception {
hm.put(root_id, ++curr_n);
for (int i = 0; i <= 200000 ; i++) { - TRIE[i] = new Node(); + TRIE[i] = new NodePersistent(); } buildTrie(1); @@ -155,7 +157,7 @@ int get(int id) { void buildTrie(int u)throws Exception { - TRIE[u] = new Node(); + TRIE[u] = new NodePersistent(); addToImmutableTrie(getFixedLengthString(Integer.toBinaryString(key[u])), TRIE[f[u]], TRIE[u]); // IMMutability : add to parent } @@ -165,22 +167,22 @@ String getFixedLengthString(String x) { } // Path Copy Logic // Never change the old copy i.e. Mutability - void addToImmutableTrie(String x, Node old, Node copy)throws Exception { + void addToImmutableTrie(String x, NodePersistent old, NodePersistent copy)throws Exception { - Node child_old = null; - Node child_copy = null; + NodePersistent child_old = null; + NodePersistent child_copy = null; for (int pos = 0; pos <= x.length() - 1; pos++) { - if(copy==null)copy = new Node(); + if(copy==null)copy = new NodePersistent(); if (x.charAt(pos) == '0') { if(old == null){ - copy.children[0] = new Node(); // Key always have to make new node along copy path + copy.children[0] = new NodePersistent(); // Key always have to make new node along copy path copy.children[1] = null; }else if(old.children[0]==null){ - copy.children[0] = new Node(); // Key always have to make new node along copy path + copy.children[0] = new NodePersistent(); // Key always have to make new node along copy path copy.children[1] = old.children[1]; }else{ - copy.children[0] = new Node(1,0);//old.children[0]; // Key always have to make new node along copy path + copy.children[0] = new NodePersistent(1,0);//old.children[0]; // Key always have to make new node along copy path copy.children[1] = old.children[1]; } child_copy = copy.children[0]; @@ -189,13 +191,13 @@ void addToImmutableTrie(String x, Node old, Node copy)throws Exception { }else { if(old == null){ copy.children[0] = null; - copy.children[1] = new Node(); + copy.children[1] = new NodePersistent(); }else if(old.children[0]==null){ copy.children[0] = old.children[0]; - copy.children[1] = new Node(); + copy.children[1] = new NodePersistent(); }else{ copy.children[0] = old.children[0]; - copy.children[1] = new Node(0, 1);//old.children[1]; + copy.children[1] = new NodePersistent(0, 1);//old.children[1]; } child_copy = copy.children[1]; if(old == null || old.children[1]==null)child_old = null; @@ -206,13 +208,13 @@ void addToImmutableTrie(String x, Node old, Node copy)throws Exception { } } - int getMinTrie(String x, Node root, int k)throws Exception { - Node curr = root; + int getMinTrie(String x, NodePersistent root, int k)throws Exception { + NodePersistent curr = root; long min = 0; for (int i = 0; i <= x.length() - 1; i++) { - Node child; + NodePersistent child; char inv = x.charAt(i) == '0' ? '1' : '0'; - Node cnt = curr.children[x.charAt(i)-'0']; + NodePersistent cnt = curr.children[x.charAt(i)-'0']; if (cnt != null) { min = ( min << 1 ) + 0; child = curr.children[x.charAt(i)-'0']; @@ -225,13 +227,13 @@ int getMinTrie(String x, Node root, int k)throws Exception { return (int)min; } - int getMaxTrie(String x, Node root, int k)throws Exception { - Node curr = root; + int getMaxTrie(String x, NodePersistent root, int k)throws Exception { + NodePersistent curr = root; long max = 0; for (int i = 0; i <= x.length() - 1; i++) { - Node child; + NodePersistent child; char inv = x.charAt(i) == '0' ? '1' : '0'; - Node cnt = curr.children[inv-'0']; + NodePersistent cnt = curr.children[inv-'0']; if (cnt != null) { max = ( max << 1 ) + 1; child = curr.children[inv-'0']; @@ -243,12 +245,12 @@ int getMaxTrie(String x, Node root, int k)throws Exception { } return (int)max; } - void print_r(Node p)throws Exception { - Node q = p; - LinkedList queue = new LinkedList();
+ void print_r(NodePersistent p)throws Exception {
+ NodePersistent q = p;
+ LinkedList queue = new LinkedList();
queue.add(q);
while(!queue.isEmpty()){
- q = (Node)queue.removeFirst();
+ q = (NodePersistent)queue.removeFirst();
out.write(""+q+" ");
out.flush();
if(q.children[0] != null)queue.add(q.children[0]);
@@ -260,7 +262,7 @@ void print_r(Node p)throws Exception {
int hash(String s) {
int base = 31;
- int a = 31;//base = a multiplier
+ int a = 31;//base = number1 multiplier
int mod = 100005;//range [0..100004]
long val = 0;
for (int i = 1 ; i <= s.length() ; i++) { @@ -383,7 +385,7 @@ BigInteger bi()throws Exception { //***********************I/O ENDS ***********************// //*********************** 0.3%f [precision]***********************// /* roundoff upto 2 digits - double roundOff = Math.round(a * 100.0) / 100.0; + double roundOff = Math.round(number1 * 100.0) / 100.0; or System.out.printf("%.2f", val); @@ -411,7 +413,7 @@ public static void main(String[] args) throws java.lang.Exception { BufferedWriter out=new BufferedWriter(new FileWriter("output.txt")); */ - A driver = new A(true); + PersistentTrie driver = new PersistentTrie(true); long start = System.currentTimeMillis(); driver.run(); long end = System.currentTimeMillis(); @@ -419,256 +421,5 @@ public static void main(String[] args) throws java.lang.Exception { return ; } - - } - - class FastReader { - - private boolean finished = false; - - private InputStream stream; - private byte[] buf = new byte[4 * 1024]; - private int curChar; - private int numChars; - private SpaceCharFilter filter; - - public FastReader(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 peek() { - if (numChars == -1) { - return -1; - } - if (curChar>= numChars) {
- curChar = 0;
- try {
- numChars = stream.read (buf);
- } catch (IOException e) {
- return -1;
- }
- if (numChars <= 0) { - return -1; - } - } - return buf[curChar]; - } - - public int nextInt() { - int c = read (); - while (isSpaceChar (c)) - c = read (); - int sgn = 1; - if (c == '-') { - sgn = -1; - c = read (); - } - int res = 0; - do { - if (c == ',') { - c = read(); - } - if (c < '0' || c> '9') {
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public long nextLong() {
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-') {
- sgn = -1;
- c = read ();
- }
- long res = 0;
- do {
- if (c < '0' || c> '9') {
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public String nextString() {
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- StringBuilder res = new StringBuilder ();
- do {
- res.appendCodePoint (c);
- c = read ();
- } while (!isSpaceChar (c));
- return res.toString ();
- }
-
- public boolean isSpaceChar(int c) {
- if (filter != null) {
- return filter.isSpaceChar (c);
- }
- return isWhitespace (c);
- }
-
- public static boolean isWhitespace(int c) {
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
-
- private String readLine0() {
- StringBuilder buf = new StringBuilder ();
- int c = read ();
- while (c != '\n' && c != -1) {
- if (c != '\r') {
- buf.appendCodePoint (c);
- }
- c = read ();
- }
- return buf.toString ();
- }
-
- public String nextLine() {
- String s = readLine0 ();
- while (s.trim ().length () == 0)
- s = readLine0 ();
- return s;
- }
-
- public String nextLine(boolean ignoreEmptyLines) {
- if (ignoreEmptyLines) {
- return nextLine ();
- } else {
- return readLine0 ();
- }
- }
-
- public BigInteger nextBigInteger() {
- try {
- return new BigInteger (nextString ());
- } catch (NumberFormatException e) {
- throw new InputMismatchException ();
- }
- }
-
- public char nextCharacter() {
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- return (char) c;
- }
-
- public double nextDouble() {
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-') {
- sgn = -1;
- c = read ();
- }
- double res = 0;
- while (!isSpaceChar (c) && c != '.') {
- if (c == 'e' || c == 'E') {
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9') {
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- }
- if (c == '.') {
- c = read ();
- double m = 1;
- while (!isSpaceChar (c)) {
- if (c == 'e' || c == 'E') {
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9') {
- throw new InputMismatchException ();
- }
- m /= 10;
- res += (c - '0') * m;
- c = read ();
- }
- }
- return res * sgn;
- }
-
- public boolean isExhausted() {
- int value;
- while (isSpaceChar (value = peek ()) && value != -1)
- read ();
- return value == -1;
- }
-
- public String next() {
- return nextString ();
- }
-
- public SpaceCharFilter getFilter() {
- return filter;
- }
-
- public void setFilter(SpaceCharFilter filter) {
- this.filter = filter;
- }
-
- public interface SpaceCharFilter {
- public boolean isSpaceChar(int ch);
- }
}
- class Pair implements Comparable {
-
- public int a;
- public int b;
-
- public Pair() {
- this.a = 0;
- this.b = 0;
- }
- public Pair(int a, int b) {
- this.a = a;
- this.b = b;
- }
- public int compareTo(Pair p) {
- if (this.b < p.b)return -1; - else if (this.b> p.b )return 1;
- else {
- if (this.a < p.a)return -1; - else if (this.a> p.a)return 1;
- else return 0;
- }
- }
- public String toString() {
- return "a=" + this.a + " b=" + this.b;
- }
-
- }
diff --git a/Algorithms/PowerSet.java b/Algorithms/PowerSet.java
index ae71092..67a056c 100755
--- a/Algorithms/PowerSet.java
+++ b/Algorithms/PowerSet.java
@@ -1,16 +1,15 @@
-//pakage joney_000[let_me_start]
+package Algorithms;//pakage joney_000[let_me_start]
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
/******************** Main Class ***********************/
-
- class C
+ public class PowerSet
{
public static InputStream inputStream = System.in;
public static OutputStream outputStream = System.out;
- public static FastReader in = new FastReader(inputStream);
+ public static InputReaderAndProcessor in = new InputReaderAndProcessor(inputStream);
public static PrintWriter out = new PrintWriter(outputStream);
/*
Overhead [Additional Temporary Strorage]
@@ -21,7 +20,7 @@ class C
public static char tempchars[] = new char[100005];
public static long mod = 1000000000+7;
long temp[] =new long[25];
-
+
public static void main(String[] args) throws java.lang.Exception{
//let_me_start
@@ -45,7 +44,7 @@ public static void main(String[] args) throws java.lang.Exception{
continue;
}
long seg = sum/k;
- int ans = f(arr,n,k,seg);
+ int ans = f(arr,n);
if(ans==-1){
out.write(""+"no\n");
}else{
@@ -56,20 +55,20 @@ public static void main(String[] args) throws java.lang.Exception{
return;
}
- int temp[] = new int[20];
- void f(long []arr , int n){
-
+// int temp[] = new int[20];
+ public static int f(long[] arr, int n){
+ int temp[] = new int[20];
int powersetsize = 1<0){
temp[j]=-1;//System.out.print(arr[j]+" ");
- }
+ }
}
// logic
for(int i = 0; i < n; i++){ @@ -79,7 +78,7 @@ void f(long []arr , int n){ } } - return ; + return powersetsize; } //****************************** Utilities ***********************// @@ -191,7 +190,7 @@ public static BigInteger bi()throws Exception{ //***********************I/O ENDS ***********************// //*********************** 0.3%f [precision]***********************// /* roundoff upto 2 digits - double roundOff = Math.round(a * 100.0) / 100.0; + double roundOff = Math.round(number1 * 100.0) / 100.0; or System.out.printf("%.2f", val); @@ -203,250 +202,3 @@ public static BigInteger bi()throws Exception{ */ } -class FastReader{ - - private boolean finished = false; - - private InputStream stream; - private byte[] buf = new byte[1024]; - private int curChar; - private int numChars; - private SpaceCharFilter filter; - - public FastReader(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 peek(){ - if (numChars == -1){ - return -1; - } - if (curChar>= numChars){
- curChar = 0;
- try{
- numChars = stream.read (buf);
- } catch (IOException e){
- return -1;
- }
- if (numChars <= 0){ - return -1; - } - } - return buf[curChar]; - } - - public int nextInt(){ - int c = read (); - while (isSpaceChar (c)) - c = read (); - int sgn = 1; - if (c == '-'){ - sgn = -1; - c = read (); - } - int res = 0; - do{ - if(c==','){ - c = read(); - } - if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public long nextLong(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- long res = 0;
- do{
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public String nextString(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- StringBuilder res = new StringBuilder ();
- do{
- res.appendCodePoint (c);
- c = read ();
- } while (!isSpaceChar (c));
- return res.toString ();
- }
-
- public boolean isSpaceChar(int c){
- if (filter != null){
- return filter.isSpaceChar (c);
- }
- return isWhitespace (c);
- }
-
- public static boolean isWhitespace(int c){
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
-
- private String readLine0(){
- StringBuilder buf = new StringBuilder ();
- int c = read ();
- while (c != '\n' && c != -1){
- if (c != '\r'){
- buf.appendCodePoint (c);
- }
- c = read ();
- }
- return buf.toString ();
- }
-
- public String nextLine(){
- String s = readLine0 ();
- while (s.trim ().length () == 0)
- s = readLine0 ();
- return s;
- }
-
-
- public String nextLine(boolean ignoreEmptyLines){
- if (ignoreEmptyLines){
- return nextLine ();
- }else{
- return readLine0 ();
- }
- }
-
- public BigInteger nextBigInteger(){
- try{
- return new BigInteger (nextString ());
- } catch (NumberFormatException e){
- throw new InputMismatchException ();
- }
- }
-
- public char nextCharacter(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- return (char) c;
- }
-
- public double nextDouble(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- double res = 0;
- while (!isSpaceChar (c) && c != '.'){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- }
- if (c == '.'){
- c = read ();
- double m = 1;
- while (!isSpaceChar (c)){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- m /= 10;
- res += (c - '0') * m;
- c = read ();
- }
- }
- return res * sgn;
- }
-
- public boolean isExhausted(){
- int value;
- while (isSpaceChar (value = peek ()) && value != -1)
- read ();
- return value == -1;
- }
-
- public String next(){
- return nextString ();
- }
-
- public SpaceCharFilter getFilter(){
- return filter;
- }
-
- public void setFilter(SpaceCharFilter filter){
- this.filter = filter;
- }
-
- public interface SpaceCharFilter{
- public boolean isSpaceChar(int ch);
- }
-}
- /******************** Pair class ***********************/
-
- class Pair implements Comparable{
- public int a;
- public int b;
- public Pair(){
- this.a = 0;
- this.b = 0;
- }
- public Pair(int a,int b){
- this.a = a;
- this.b = b;
- }
- public int compareTo(Pair p){
- if(this.a==p.a){
- return this.b-p.b;
- }
- return this.a-p.a;
- }
- public String toString(){
- return "a="+this.a+" b="+this.b;
- }
-
-}
diff --git a/Algorithms/PriorityQueue.java b/Algorithms/PriorityQueue.java
index 4894f0d..370b012 100644
--- a/Algorithms/PriorityQueue.java
+++ b/Algorithms/PriorityQueue.java
@@ -1,4 +1,8 @@
-class Solution {
+package Algorithms;
+
+import java.util.PriorityQueue;
+
+class PriorityQueueSolution {
class Point{
int x, y;
@@ -18,7 +22,7 @@ public int[][] kClosest(int[][] points, int k) {
return null;
}
int numPoints = points.length;
- PriorityQueue pQueue = new PriorityQueue(k + 1, (a,b) -> (b.distance - a.distance)); // max elem on top
+ PriorityQueue pQueue = new PriorityQueue(k + 1, (a, b) -> (b.distance - a.distance)); // max elem on top
for(int[] point: points){
pQueue.add(new Point(point[0], point[1]));
if(pQueue.size()> k){
diff --git a/Algorithms/SegmentTreeLazyProp.java b/Algorithms/SegmentTreeLazyProp.java
index 264b001..e528c3b 100644
--- a/Algorithms/SegmentTreeLazyProp.java
+++ b/Algorithms/SegmentTreeLazyProp.java
@@ -1,4 +1,4 @@
-//pakage joney_000[let_me_start]
+package Algorithms;//pakage joney_000[let_me_start]
//
import java.util.*;
import java.lang.*;
@@ -10,17 +10,17 @@
* Platform : CodeChef.com
*
*/
-class Node{
+class SegmentTreeNode {
public long val = 1L;
public long lazy = 0L;
- public Node(){
+ public SegmentTreeNode(){
this.val = 1;
this.lazy = 0;
}
- public Node(long val, long lazy){
+ public SegmentTreeNode(long val, long lazy){
this.val = val;
this.lazy = lazy;
}
@@ -33,12 +33,12 @@ public String toString(){
}
/* The Main Class */
-class A
+class SegmentTreeLazyProp
{
private InputStream inputStream ;
private OutputStream outputStream ;
- private FastReader in ;
+ private InputReaderAndProcessor in ;
private PrintWriter out ;
/*
Overhead [Additional Temporary Strorage] but provides memory reusibility for multiple test cases.
@@ -53,8 +53,8 @@ class A
private final int INF = Integer.MAX_VALUE / 10;
private final long INF_L = Long.MAX_VALUE / 10;
- public A(){}
- public A(boolean stdIO)throws FileNotFoundException{
+ public SegmentTreeLazyProp(){}
+ public SegmentTreeLazyProp(boolean stdIO)throws FileNotFoundException{
// stdIO = false;
if(stdIO){
inputStream = System.in;
@@ -63,7 +63,7 @@ public A(boolean stdIO)throws FileNotFoundException{
inputStream = new FileInputStream("output.txt");
outputStream = new FileOutputStream("output1.txt");
}
- in = new FastReader(inputStream);
+ in = new InputReaderAndProcessor(inputStream);
out = new PrintWriter(outputStream);
}
@@ -72,7 +72,7 @@ public A(boolean stdIO)throws FileNotFoundException{
int MAX_M = 100005;
int n = 0;
int q = 0;
- Node[] m = new Node[4*MAX_M+100001];//size ~ 2*n+1
+ SegmentTreeNode[] m = new SegmentTreeNode[4*MAX_M+100001];//size ~ 2*n+1
// prob specific
int qry[][] = new int[3][MAX_M + 5];
@@ -90,13 +90,13 @@ void run()throws Exception{
// maketree(1, 1, n, m, num);
for(int i = q ; i>= 1; i--){
if(qry[0][i] == 1){
- Node res = query(1, 1, q, m, cnt, i, i);
+ SegmentTreeNode res = query(1, 1, q, m, cnt, i, i);
// out.write("res_val = "+res+"\n");
// out.flush();
cnt[qry[1][i]] = (cnt[qry[1][i]] + res.val)%mod;
cnt[qry[2][i] + 1] = (cnt[qry[2][i] + 1] - res.val + mod)%mod;
}else{
- Node res = query(1, 1, q, m, cnt, i, i);
+ SegmentTreeNode res = query(1, 1, q, m, cnt, i, i);
update(1, 1, q, m, cnt, qry[1][i], qry[2][i], res.val);
}
}
@@ -111,7 +111,7 @@ void run()throws Exception{
void once(){
for(int i = 1; i <= 4 * MAX_M + 100000; i++){ - m[i] = new Node(); + m[i] = new SegmentTreeNode(); } } @@ -123,7 +123,7 @@ void clear(){ for(int i = 1; i <= MAX_N; i++)cnt[i] = 0; } - void maketree(int node, int i, int j, Node[] m, long []num){ + void maketree(int node, int i, int j, SegmentTreeNode[] m, long []num){ if(i==j){ //node[node].data = num[i]; return; @@ -135,7 +135,7 @@ void maketree(int node, int i, int j, Node[] m, long []num){ //printMat(m[node].mat); } - Node query(int node, int l, int r, Node[] m, long []num, int i, int j){ + SegmentTreeNode query(int node, int l, int r, SegmentTreeNode[] m, long []num, int i, int j){ if(l>j||rr)return null; //invalid condition
// out.write("qry l = "+l+", r = "+r+", i = "+i+", j = "+j+"\n");
// out.flush();
@@ -144,19 +144,19 @@ Node query(int node, int l, int r, Node[] m, long []num, int i, int j){
if(l>=i&&r<=j) { return m[node]; } - - Node arr1 = query(2*node, l,(l+r)/2, m, num, i, j); - Node arr2 = query(2*node + 1, ((l+r)/2)+1, r, m, num, i, j); + + SegmentTreeNode arr1 = query(2*node, l,(l+r)/2, m, num, i, j); + SegmentTreeNode arr2 = query(2*node + 1, ((l+r)/2)+1, r, m, num, i, j); if(arr1==null)return arr2; if(arr2==null)return arr1; - Node arr = new Node(); + SegmentTreeNode arr = new SegmentTreeNode(); //logic return arr; } - void update(int node, int l, int r, Node[] m, long []num, int i, int j, long val)throws Exception{ + void update(int node, int l, int r, SegmentTreeNode[] m, long []num, int i, int j, long val)throws Exception{ if(l>j||rr)return ; //invalid condition
pushDown(node, m, num);
@@ -172,7 +172,7 @@ void update(int node, int l, int r, Node[] m, long []num, int i, int j, long val
}
- void pushDown(int node, Node[] m, long[] num){
+ void pushDown(int node, SegmentTreeNode[] m, long[] num){
m[node].val = (m[node].val + m[node].lazy) % mod;
@@ -301,7 +301,7 @@ BigInteger bi()throws Exception{
//***********************I/O ENDS ***********************//
//*********************** 0.3%f [precision]***********************//
/* roundoff upto 2 digits
- double roundOff = Math.round(a * 100.0) / 100.0;
+ double roundOff = Math.round(number1 * 100.0) / 100.0;
or
System.out.printf("%.2f", val);
@@ -328,7 +328,7 @@ public static void main(String[] args) throws java.lang.Exception{
BufferedReader br=new BufferedReader(new FileReader("input.txt"));
BufferedWriter out=new BufferedWriter(new FileWriter("output.txt"));
*/
- A driver = new A(true);
+ SegmentTreeLazyProp driver = new SegmentTreeLazyProp(true);
driver.run();
@@ -336,254 +336,4 @@ public static void main(String[] args) throws java.lang.Exception{
return ;
}
-
- }
-
- class FastReader{
-
- private boolean finished = false;
-
- private InputStream stream;
- private byte[] buf = new byte[4 * 1024];
- private int curChar;
- private int numChars;
- private SpaceCharFilter filter;
-
- public FastReader(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 peek(){ - if (numChars == -1){ - return -1; - } - if (curChar>= numChars){
- curChar = 0;
- try{
- numChars = stream.read (buf);
- } catch (IOException e){
- return -1;
- }
- if (numChars <= 0){ - return -1; - } - } - return buf[curChar]; - } - - public int nextInt(){ - int c = read (); - while (isSpaceChar (c)) - c = read (); - int sgn = 1; - if (c == '-'){ - sgn = -1; - c = read (); - } - int res = 0; - do{ - if(c==','){ - c = read(); - } - if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public long nextLong(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- long res = 0;
- do{
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public String nextString(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- StringBuilder res = new StringBuilder ();
- do{
- res.appendCodePoint (c);
- c = read ();
- } while (!isSpaceChar (c));
- return res.toString ();
- }
-
- public boolean isSpaceChar(int c){
- if (filter != null){
- return filter.isSpaceChar (c);
- }
- return isWhitespace (c);
- }
-
- public static boolean isWhitespace(int c){
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
-
- private String readLine0(){
- StringBuilder buf = new StringBuilder ();
- int c = read ();
- while (c != '\n' && c != -1){
- if (c != '\r'){
- buf.appendCodePoint (c);
- }
- c = read ();
- }
- return buf.toString ();
- }
-
- public String nextLine(){
- String s = readLine0 ();
- while (s.trim ().length () == 0)
- s = readLine0 ();
- return s;
- }
-
- public String nextLine(boolean ignoreEmptyLines){
- if (ignoreEmptyLines){
- return nextLine ();
- }else{
- return readLine0 ();
- }
- }
-
- public BigInteger nextBigInteger(){
- try{
- return new BigInteger (nextString ());
- } catch (NumberFormatException e){
- throw new InputMismatchException ();
- }
- }
-
- public char nextCharacter(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- return (char) c;
- }
-
- public double nextDouble(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- double res = 0;
- while (!isSpaceChar (c) && c != '.'){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- }
- if (c == '.'){
- c = read ();
- double m = 1;
- while (!isSpaceChar (c)){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- m /= 10;
- res += (c - '0') * m;
- c = read ();
- }
- }
- return res * sgn;
- }
-
- public boolean isExhausted(){
- int value;
- while (isSpaceChar (value = peek ()) && value != -1)
- read ();
- return value == -1;
- }
-
- public String next(){
- return nextString ();
- }
-
- public SpaceCharFilter getFilter(){
- return filter;
- }
-
- public void setFilter(SpaceCharFilter filter){
- this.filter = filter;
- }
-
- public interface SpaceCharFilter{
- public boolean isSpaceChar(int ch);
- }
- }
- /******************** Pair class ***********************/
-
- class Pair implements Comparable{
- public int a;
- public int b;
- public int c;
- public Pair(){
- this.a = 0;
- this.b = 0;
- }
- public Pair(int a,int b, int c){
- this.a = a;
- this.b = b;
- this.c = c;
- }
- public int compareTo(Pair p){
- if(this.a==p.a){
- return this.b-p.b;
- }
- return p.a-this.a;
- }
- public String toString(){
- return "a="+this.a+" b="+this.b;
- }
-
-}
+ }
\ No newline at end of file
diff --git a/Algorithms/SegmentTreeNew_with_MatrixExpo.java b/Algorithms/SegmentTreeNew_with_MatrixExpo.java
index 7359144..dbbd860 100755
--- a/Algorithms/SegmentTreeNew_with_MatrixExpo.java
+++ b/Algorithms/SegmentTreeNew_with_MatrixExpo.java
@@ -1,3 +1,6 @@
+package Algorithms;
+
+import Algorithms.InputReaderAndProcessor;
import java.util.*;
import java.lang.*;
@@ -9,12 +12,12 @@
* Platform : https://www.facebook.com/hackercup
*
*/
-class Node{
+class SegmentTreeNewNode{
//public int [] C = new int[3];
public long mat[][] = new long[2][2];
- public Node(){
+ public SegmentTreeNewNode(){
this.mat[0][0] = this.mat[1][1] = 1L;
}
@@ -25,7 +28,7 @@ class E{
private InputStream inputStream ;
private OutputStream outputStream ;
- private FastReader in ;
+ private InputReaderAndProcessor in ;
private PrintWriter out ;
/*
Overhead [Additional Temporary Strorage] but provides memory reusibility for multiple test cases.
@@ -52,7 +55,7 @@ public E(boolean stdIO)throws FileNotFoundException{
inputStream = new FileInputStream("laundro_matt.txt");
outputStream = new FileOutputStream("output.txt");
}
- in = new FastReader(inputStream);
+ in = new InputReaderAndProcessor(inputStream);
out = new PrintWriter(outputStream);
}
@@ -69,9 +72,9 @@ void run()throws Exception{
for(int i=1;i<=n;i++){ num[i]= i(); } - - Node[] m = new Node[3*n+5000];//size ~ 2*n+1 - for(int i=1;i<= 3*n + 5000 -1 ;i++)m[i] = new Node(); + + SegmentTreeNewNode[] m = new SegmentTreeNewNode[3*n+5000];//size ~ 2*n+1 + for(int i=1;i<= 3*n + 5000 -1 ;i++)m[i] = new SegmentTreeNewNode(); maketree(1,1,n,m,num); @@ -83,7 +86,7 @@ void run()throws Exception{ update(1,1,n,m,num,idx,idx); }else{ int i = i(); int j = i(); - Node res = query(1,1,n,m,num,i,j); + SegmentTreeNewNode res = query(1,1,n,m,num,i,j); long ans = res.mat[0][1]; out.write(""+ans+"\n"); } @@ -116,7 +119,7 @@ void copy(long [][]ans ,long [][]p ){ } } - void maketree(int node,int i,int j,Node[] m,int []num){ + void maketree(int node, int i, int j, SegmentTreeNewNode[] m, int []num){ if(i==j){ copy(m[node].mat , expo(num[i])); @@ -134,26 +137,26 @@ void maketree(int node,int i,int j,Node[] m,int []num){ //out.write("node no = "+node+" range = "+i+" "+j+"\n"); //printMat(m[node].mat); } - - Node query(int node,int l,int r,Node[] m,int []num,int i,int j){ + + SegmentTreeNewNode query(int node,int l,int r,SegmentTreeNewNode[] m,int []num,int i,int j){ if(l>j||rr)return null; //invalid condition
if(l>=i&&r<=j) { return m[node]; } - - Node arr1 = query(2*node,l,(l+r)/2,m,num,i,j); - Node arr2 = query(2*node+1,((l+r)/2)+1,r,m,num,i,j); + + SegmentTreeNewNode arr1 = query(2*node,l,(l+r)/2,m,num,i,j); + SegmentTreeNewNode arr2 = query(2*node+1,((l+r)/2)+1,r,m,num,i,j); if(arr1==null)return arr2; if(arr2==null)return arr1; - Node arr = new Node(); + SegmentTreeNewNode arr = new SegmentTreeNewNode(); //logic mulMat(arr.mat,arr1.mat , arr2.mat); return arr; } - void update(int node,int l,int r,Node[] m,int []num,int i,int j){ + void update(int node,int l,int r,SegmentTreeNewNode[] m,int []num,int i,int j){ if(l>j||rr)return ; //invalid condition
if(l>=i&&r<=j) { @@ -270,7 +273,7 @@ void print_r(Object...o){ int hash(String s){ int base = 31; - int a = 31;//base = a multiplier + int a = 31;//base = number1 multiplier int mod = 100005;//range [0..100004] long val = 0; for(int i = 1 ; i<= s.length() ;i++){ @@ -392,7 +395,7 @@ BigInteger bi()throws Exception{ //***********************I/O ENDS ***********************// //*********************** 0.3%f [precision]***********************// /* roundoff upto 2 digits - double roundOff = Math.round(a * 100.0) / 100.0; + double roundOff = Math.round(number1 * 100.0) / 100.0; or System.out.printf("%.2f", val); @@ -429,266 +432,3 @@ public static void main(String[] args) throws java.lang.Exception{ } -class FastReader{ - - private boolean finished = false; - - private InputStream stream; - private byte[] buf = new byte[4*1024]; - private int curChar; - private int numChars; - private SpaceCharFilter filter; - - public FastReader(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 peek(){ - if (numChars == -1){ - return -1; - } - if (curChar>= numChars){
- curChar = 0;
- try{
- numChars = stream.read (buf);
- } catch (IOException e){
- return -1;
- }
- if (numChars <= 0){ - return -1; - } - } - return buf[curChar]; - } - - public int nextInt(){ - int c = read (); - while (isSpaceChar (c)) - c = read (); - int sgn = 1; - if (c == '-'){ - sgn = -1; - c = read (); - } - int res = 0; - do{ - if(c==','){ - c = read(); - } - if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public long nextLong(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- long res = 0;
- do{
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public String nextString(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- StringBuilder res = new StringBuilder ();
- do{
- res.appendCodePoint (c);
- c = read ();
- } while (!isSpaceChar (c));
- return res.toString ();
- }
-
- public boolean isSpaceChar(int c){
- if (filter != null){
- return filter.isSpaceChar (c);
- }
- return isWhitespace (c);
- }
-
- public static boolean isWhitespace(int c){
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
-
- private String readLine0(){
- StringBuilder buf = new StringBuilder ();
- int c = read ();
- while (c != '\n' && c != -1){
- if (c != '\r'){
- buf.appendCodePoint (c);
- }
- c = read ();
- }
- return buf.toString ();
- }
-
- public String nextLine(){
- String s = readLine0 ();
- while (s.trim ().length () == 0)
- s = readLine0 ();
- return s;
- }
-
- public String nextLine(boolean ignoreEmptyLines){
- if (ignoreEmptyLines){
- return nextLine ();
- }else{
- return readLine0 ();
- }
- }
-
- public BigInteger nextBigInteger(){
- try{
- return new BigInteger (nextString ());
- } catch (NumberFormatException e){
- throw new InputMismatchException ();
- }
- }
-
- public char nextCharacter(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- return (char) c;
- }
-
- public double nextDouble(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- double res = 0;
- while (!isSpaceChar (c) && c != '.'){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- }
- if (c == '.'){
- c = read ();
- double m = 1;
- while (!isSpaceChar (c)){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- m /= 10;
- res += (c - '0') * m;
- c = read ();
- }
- }
- return res * sgn;
- }
-
- public boolean isExhausted(){
- int value;
- while (isSpaceChar (value = peek ()) && value != -1)
- read ();
- return value == -1;
- }
-
- public String next(){
- return nextString ();
- }
-
- public SpaceCharFilter getFilter(){
- return filter;
- }
-
- public void setFilter(SpaceCharFilter filter){
- this.filter = filter;
- }
-
- public interface SpaceCharFilter{
- public boolean isSpaceChar(int ch);
- }
-}
- /******************** Pair class ***********************/
-
- class Pair implements Comparable{
- public int id;
- public String name;
- public long b;
- public long a;
- public long c;
- public Pair(){
- this.id = 1000;
- this.name = "s";
- this.a = 0L;
- this.b = 0L;
- this.c = 0L;
- }
- public Pair(int id , long a,long b , long c , String name){
- this.name = name;
- this.id = id;
- this.a = a;
- this.b = b;
- this.c = c;
- }
- public int compareTo(Pair p){
- if(this.a < p.a)return -1; - else if(this.a> p.a )return 1;
- else {
- if(this.b < p.b)return -1; - else if(this.b> p.b )return 1;
- else {
- if(this.c < p.c)return -1; - else if(this.c> p.c )return 1;
- else return -this.name.compareTo(p.name);
- }
-
- }
- }
- public String toString(){
- return "a="+this.a+" b="+this.b;
- }
-
-}
diff --git a/Algorithms/SlidingWindow.java b/Algorithms/SlidingWindow.java
index bdb867c..48e174b 100755
--- a/Algorithms/SlidingWindow.java
+++ b/Algorithms/SlidingWindow.java
@@ -1,4 +1,4 @@
-//pakage joney_000[let_me_start]
+package Algorithms;//pakage joney_000[let_me_start]
import java.util.*;
import java.lang.*;
@@ -6,7 +6,7 @@
import java.math.*;
/******************** Main Class ***********************/
-class E
+class SlidingWindow
{
// sparse table template /// TOPCODER LCA_RMQ
@@ -14,7 +14,7 @@ class E
public int[][] rmq;
public int[] a;
- public E(int[] a ,int n, int K,int res[]) {
+ public SlidingWindow(int[] a ,int n, int K,int res[]) {
SparseTable = new int[n+1];
this.a = a;
@@ -122,7 +122,7 @@ public static void main(String[] args) throws java.lang.Exception{
// out.write("printing min : ");
// for(int i=0;i<=n-1;i++)out.write(""+array[i]+" "); // out.write("\n"); - E st = new E(arr,n,k,array); //sparse table + SlidingWindow st = new SlidingWindow(arr,n,k,array); //sparse table // Shinchan trying SPARSE TABLE FIRST TIME HOPE IT WILL RUN..:D..:D @@ -317,7 +317,7 @@ public static BigInteger bi()throws Exception{ //***********************I/O ENDS ***********************// //*********************** 0.3%f [precision]***********************// /* roundoff upto 2 digits - double roundOff = Math.round(a * 100.0) / 100.0; + double roundOff = Math.round(number1 * 100.0) / 100.0; or System.out.printf("%.2f", val); @@ -329,252 +329,6 @@ public static BigInteger bi()throws Exception{ */ } -class FastReader{ - - private boolean finished = false; - - private InputStream stream; - private byte[] buf = new byte[4096]; - private int curChar; - private int numChars; - private SpaceCharFilter filter; - - public FastReader(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 peek(){ - if (numChars == -1){ - return -1; - } - if (curChar>= numChars){
- curChar = 0;
- try{
- numChars = stream.read (buf);
- } catch (IOException e){
- return -1;
- }
- if (numChars <= 0){ - return -1; - } - } - return buf[curChar]; - } - - public int nextInt(){ - int c = read (); - while (isSpaceChar (c)) - c = read (); - int sgn = 1; - if (c == '-'){ - sgn = -1; - c = read (); - } - int res = 0; - do{ - if(c==','){ - c = read(); - } - if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public long nextLong(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- long res = 0;
- do{
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public String nextString(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- StringBuilder res = new StringBuilder ();
- do{
- res.appendCodePoint (c);
- c = read ();
- } while (!isSpaceChar (c));
- return res.toString ();
- }
-
- public boolean isSpaceChar(int c){
- if (filter != null){
- return filter.isSpaceChar (c);
- }
- return isWhitespace (c);
- }
-
- public static boolean isWhitespace(int c){
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
-
- private String readLine0(){
- StringBuilder buf = new StringBuilder ();
- int c = read ();
- while (c != '\n' && c != -1){
- if (c != '\r'){
- buf.appendCodePoint (c);
- }
- c = read ();
- }
- return buf.toString ();
- }
-
- public String nextLine(){
- String s = readLine0 ();
- while (s.trim ().length () == 0)
- s = readLine0 ();
- return s;
- }
-
- public String nextLine(boolean ignoreEmptyLines){
- if (ignoreEmptyLines){
- return nextLine ();
- }else{
- return readLine0 ();
- }
- }
-
- public BigInteger nextBigInteger(){
- try{
- return new BigInteger (nextString ());
- } catch (NumberFormatException e){
- throw new InputMismatchException ();
- }
- }
-
- public char nextCharacter(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- return (char) c;
- }
-
- public double nextDouble(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- double res = 0;
- while (!isSpaceChar (c) && c != '.'){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- }
- if (c == '.'){
- c = read ();
- double m = 1;
- while (!isSpaceChar (c)){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- m /= 10;
- res += (c - '0') * m;
- c = read ();
- }
- }
- return res * sgn;
- }
-
- public boolean isExhausted(){
- int value;
- while (isSpaceChar (value = peek ()) && value != -1)
- read ();
- return value == -1;
- }
-
- public String next(){
- return nextString ();
- }
-
- public SpaceCharFilter getFilter(){
- return filter;
- }
-
- public void setFilter(SpaceCharFilter filter){
- this.filter = filter;
- }
-
- public interface SpaceCharFilter{
- public boolean isSpaceChar(int ch);
- }
-}
- /******************** Pair class ***********************/
-
- class Pair implements Comparable{
- public int a;
- public int b;
- public Pair(){
- this.a = 0;
- this.b = 0;
- }
- public Pair(int a,int b){
- this.a = a;
- this.b = b;
- }
- public int compareTo(Pair p){
- if(this.a==p.a){
- return this.b-p.b;
- }
- return this.a-p.a;
- }
- public String toString(){
- return "a="+this.a+" b="+this.b;
- }
-
-}
diff --git a/Algorithms/Miller-prime-test.java b/Algorithms/Solution.java
similarity index 51%
rename from Algorithms/Miller-prime-test.java
rename to Algorithms/Solution.java
index 30d61d7..04a22b3 100644
--- a/Algorithms/Miller-prime-test.java
+++ b/Algorithms/Solution.java
@@ -1,34 +1,5 @@
- /* Miller-Rabin primality test, iteration signifies the accuracy of the test */
+package Algorithms; /* Miller-Rabin primality test, iteration signifies the accuracy of the test */
// test number if p is prime or not using 'iterations'.
- boolean Miller(long p,int iteration){
- long LOWER_RANGE = 1; //assign lower range value
- long UPPER_RANGE = p-1; //assign upper range value
- Random randomGenerator = new Random();
- if(p<2){ - return false; - } - if(p!=2 && p%2==0){ - return false; - } - long s=p-1; - while(s%2==0){ - s/=2; - } - for(int i=0;i= numChars){
- curChar = 0;
- try{
- numChars = stream.read (buf);
- } catch (IOException e){
- throw new InputMismatchException ();
- }
- if (numChars <= 0){ - return -1; - } - } - return buf[curChar++]; - } - - public int peek(){ - if (numChars == -1){ - return -1; - } - if (curChar>= numChars){
- curChar = 0;
- try{
- numChars = stream.read (buf);
- } catch (IOException e){
- return -1;
- }
- if (numChars <= 0){ - return -1; - } - } - return buf[curChar]; - } - - public int nextInt(){ - int c = read (); - while (isSpaceChar (c)) - c = read (); - int sgn = 1; - if (c == '-'){ - sgn = -1; - c = read (); - } - int res = 0; - do{ - if(c==','){ - c = read(); - } - if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public long nextLong(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- long res = 0;
- do{
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public String nextString(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- StringBuilder res = new StringBuilder ();
- do{
- res.appendCodePoint (c);
- c = read ();
- } while (!isSpaceChar (c));
- return res.toString ();
- }
-
- public boolean isSpaceChar(int c){
- if (filter != null){
- return filter.isSpaceChar (c);
- }
- return isWhitespace (c);
- }
-
- public static boolean isWhitespace(int c){
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
-
- private String readLine0(){
- StringBuilder buf = new StringBuilder ();
- int c = read ();
- while (c != '\n' && c != -1){
- if (c != '\r'){
- buf.appendCodePoint (c);
- }
- c = read ();
- }
- return buf.toString ();
- }
-
- public String nextLine(){
- String s = readLine0 ();
- while (s.trim ().length () == 0)
- s = readLine0 ();
- return s;
- }
-
- public String nextLine(boolean ignoreEmptyLines){
- if (ignoreEmptyLines){
- return nextLine ();
- }else{
- return readLine0 ();
- }
- }
-
- public BigInteger nextBigInteger(){
- try{
- return new BigInteger (nextString ());
- } catch (NumberFormatException e){
- throw new InputMismatchException ();
- }
- }
-
- public char nextCharacter(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- return (char) c;
- }
-
- public double nextDouble(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- double res = 0;
- while (!isSpaceChar (c) && c != '.'){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- }
- if (c == '.'){
- c = read ();
- double m = 1;
- while (!isSpaceChar (c)){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- m /= 10;
- res += (c - '0') * m;
- c = read ();
- }
- }
- return res * sgn;
- }
-
- public boolean isExhausted(){
- int value;
- while (isSpaceChar (value = peek ()) && value != -1)
- read ();
- return value == -1;
- }
-
- public String next(){
- return nextString ();
- }
-
- public SpaceCharFilter getFilter(){
- return filter;
- }
-
- public void setFilter(SpaceCharFilter filter){
- this.filter = filter;
- }
-
- public interface SpaceCharFilter{
- public boolean isSpaceChar(int ch);
- }
-}
-
-class Pair implements Comparable{
- public int a;
- public int b;
-
- public Pair(){
- this.a = 0;
- this.b = 0;
- }
-
- public Pair(int a,int b){
- this.a = a;
- this.b = b;
- }
-
- public int compareTo(Pair p){
- if(this.a == p.a){
- return this.b - p.b;
- }
- return this.a - p.a;
- }
-
- @Override
- public String toString(){
- return "a = " + this.a + " b = " + this.b;
- }
-}
\ No newline at end of file
diff --git a/Algorithms/SqureRootDecomposition.java b/Algorithms/SqureRootDecomposition.java
index a4bfca8..0b7dfa4 100755
--- a/Algorithms/SqureRootDecomposition.java
+++ b/Algorithms/SqureRootDecomposition.java
@@ -1,4 +1,4 @@
-//pakage joney_000[let_me_start]
+package Algorithms;//pakage joney_000[let_me_start]
//
import java.util.*;
import java.lang.*;
@@ -48,11 +48,11 @@ public String toString(){
}
/* The Main Class */
-class A
+class SqrtDecomposition
{
private InputStream inputStream ;
private OutputStream outputStream ;
- private FastReader in ;
+ private InputReaderAndProcessor in ;
private PrintWriter out ;
/*
Overhead [Additional Temporary Strorage] but provides memory reusibility for multiple test cases.
@@ -67,8 +67,8 @@ class A
private final int INF = Integer.MAX_VALUE / 10;
private final long INF_L = Long.MAX_VALUE / 10;
- public A(){}
- public A(boolean stdIO)throws FileNotFoundException{
+ public SqrtDecomposition(){}
+ public SqrtDecomposition(boolean stdIO)throws FileNotFoundException{
if(stdIO){
inputStream = System.in;
@@ -77,7 +77,7 @@ public A(boolean stdIO)throws FileNotFoundException{
inputStream = new FileInputStream("input.txt");
outputStream = new FileOutputStream("output.txt");
}
- in = new FastReader(inputStream);
+ in = new InputReaderAndProcessor(inputStream);
out = new PrintWriter(outputStream);
}
@@ -273,7 +273,7 @@ BigInteger bi()throws Exception{
//***********************I/O ENDS ***********************//
//*********************** 0.3%f [precision]***********************//
/* roundoff upto 2 digits
- double roundOff = Math.round(a * 100.0) / 100.0;
+ double roundOff = Math.round(number1 * 100.0) / 100.0;
or
System.out.printf("%.2f", val);
@@ -297,8 +297,8 @@ public static void main(String[] args) throws java.lang.Exception{
BufferedWriter out=new BufferedWriter(new OutputStreamWriter(System.out));
BufferedReader br=new BufferedReader(new FileReader("input.txt"));
BufferedWriter out=new BufferedWriter(new FileWriter("output.txt"));
- */
- A driver = new A(true);
+ */
+ SqrtDecomposition driver = new SqrtDecomposition(true);
long start = System.currentTimeMillis();
driver.run();
long end = System.currentTimeMillis();
@@ -310,249 +310,3 @@ public static void main(String[] args) throws java.lang.Exception{
}
-class FastReader{
-
- private boolean finished = false;
-
- private InputStream stream;
- private byte[] buf = new byte[4*1024];
- private int curChar;
- private int numChars;
- private SpaceCharFilter filter;
-
- public FastReader(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 peek(){ - if (numChars == -1){ - return -1; - } - if (curChar>= numChars){
- curChar = 0;
- try{
- numChars = stream.read (buf);
- } catch (IOException e){
- return -1;
- }
- if (numChars <= 0){ - return -1; - } - } - return buf[curChar]; - } - - public int nextInt(){ - int c = read (); - while (isSpaceChar (c)) - c = read (); - int sgn = 1; - if (c == '-'){ - sgn = -1; - c = read (); - } - int res = 0; - do{ - if(c==','){ - c = read(); - } - if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public long nextLong(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- long res = 0;
- do{
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public String nextString(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- StringBuilder res = new StringBuilder ();
- do{
- res.appendCodePoint (c);
- c = read ();
- } while (!isSpaceChar (c));
- return res.toString ();
- }
-
- public boolean isSpaceChar(int c){
- if (filter != null){
- return filter.isSpaceChar (c);
- }
- return isWhitespace (c);
- }
-
- public static boolean isWhitespace(int c){
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
-
- private String readLine0(){
- StringBuilder buf = new StringBuilder ();
- int c = read ();
- while (c != '\n' && c != -1){
- if (c != '\r'){
- buf.appendCodePoint (c);
- }
- c = read ();
- }
- return buf.toString ();
- }
-
- public String nextLine(){
- String s = readLine0 ();
- while (s.trim ().length () == 0)
- s = readLine0 ();
- return s;
- }
-
- public String nextLine(boolean ignoreEmptyLines){
- if (ignoreEmptyLines){
- return nextLine ();
- }else{
- return readLine0 ();
- }
- }
-
- public BigInteger nextBigInteger(){
- try{
- return new BigInteger (nextString ());
- } catch (NumberFormatException e){
- throw new InputMismatchException ();
- }
- }
-
- public char nextCharacter(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- return (char) c;
- }
-
- public double nextDouble(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- double res = 0;
- while (!isSpaceChar (c) && c != '.'){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- }
- if (c == '.'){
- c = read ();
- double m = 1;
- while (!isSpaceChar (c)){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- m /= 10;
- res += (c - '0') * m;
- c = read ();
- }
- }
- return res * sgn;
- }
-
- public boolean isExhausted(){
- int value;
- while (isSpaceChar (value = peek ()) && value != -1)
- read ();
- return value == -1;
- }
-
- public String next(){
- return nextString ();
- }
-
- public SpaceCharFilter getFilter(){
- return filter;
- }
-
- public void setFilter(SpaceCharFilter filter){
- this.filter = filter;
- }
-
- public interface SpaceCharFilter{
- public boolean isSpaceChar(int ch);
- }
-}
- /******************** Pair class ***********************/
-
- class Pair implements Comparable{
- public int a;
- public int b;
- public Pair(){
- this.a = 0;
- this.b = 0;
- }
- public Pair(int a,int b){
- this.a = a;
- this.b = b;
- }
- public int compareTo(Pair p){
- if(this.a==p.a){
- return this.b-p.b;
- }
- return this.a-p.a;
- }
- public String toString(){
- return "a="+this.a+" b="+this.b;
- }
-
-}
diff --git a/Algorithms/SuffixArray,HashingSeive.java b/Algorithms/SuffixArray,HashingSeive.java
index 3e0ffc2..157fb86 100755
--- a/Algorithms/SuffixArray,HashingSeive.java
+++ b/Algorithms/SuffixArray,HashingSeive.java
@@ -1,3 +1,6 @@
+package Algorithms;
+
+import Algorithms.InputReaderAndProcessor;
import java.util.*;
import java.lang.*;
@@ -10,11 +13,11 @@
*
*/
- class A{
+ class SA{
private InputStream inputStream ;
private OutputStream outputStream ;
- private FastReader in ;
+ private InputReaderAndProcessor in ;
private PrintWriter out ;
/*
Overhead [Additional Temporary Strorage] but provides memory reusibility for multiple test cases.
@@ -31,8 +34,8 @@ class A{
private final int INF = Integer.MAX_VALUE / 10;
private final long INF_L = Long.MAX_VALUE / 10;
- public A(){}
- public A(boolean stdIO)throws FileNotFoundException{
+ public SA(){}
+ public SA(boolean stdIO)throws FileNotFoundException{
//stdIO = false;
if(stdIO){
inputStream = System.in;
@@ -41,7 +44,7 @@ public A(boolean stdIO)throws FileNotFoundException{
inputStream = new FileInputStream("laundro_matt.txt");
outputStream = new FileOutputStream("output.txt");
}
- in = new FastReader(inputStream);
+ in = new InputReaderAndProcessor(inputStream);
out = new PrintWriter(outputStream);
}
@@ -111,7 +114,7 @@ void suffixArray() {
order[i] = n - 1 - i;
// stable sort of characters
- // Arrays.sort(order,0 ,n, (a, b) -> Character.compare(S.charAt(a), S.charAt(b)));
+ // Arrays.sort(order,0 ,n, (number1, number2) -> Character.compare(S.charAt(number1), S.charAt(number2)));
Arrays.sort(order , 0 , n ,new Comparator(){
public int compare(Object a , Object b){
int p = (Integer)a;
@@ -184,7 +187,7 @@ void lcp_to_S(){
return;
}
- //LCP of between sa[a] & sa[b] O(N)
+ //LCP of between sa[number1] & sa[number2] O(N)
//Can be reduced to O(1) using RMQ_SPARSE_TABLE
int lcp_random(int a , int b){
if(a> b){
@@ -193,7 +196,7 @@ int lcp_random(int a , int b){
b =temp;
}
if(a == b)return (S.length() - sa[a]);
- // always a <= b + // always number1 <= number2 int min = Integer.MAX_VALUE; for(int i = a ; i< b ; i++){ min = Math.min(min , lcp_cum[i]); @@ -232,7 +235,7 @@ void print_r(Object... o){ int hash(String s){ int base = 31; - int a = 31;//base = a multiplier + int a = 31;//base = number1 multiplier int mod = 100005;//range [0..100004] long val = 0; for(int i = 1 ; i<= s.length() ;i++){ @@ -333,7 +336,7 @@ BigInteger bi()throws Exception{ //***********************I/O ENDS ***********************// //*********************** 0.3%f [precision]***********************// /* roundoff upto 2 digits - double roundOff = Math.round(a * 100.0) / 100.0; + double roundOff = Math.round(number1 * 100.0) / 100.0; or System.out.printf("%.2f", val); @@ -358,7 +361,7 @@ public static void main(String[] args) throws java.lang.Exception{ BufferedReader br=new BufferedReader(new FileReader("input.txt")); BufferedWriter out=new BufferedWriter(new FileWriter("output.txt")); */ - A driver = new A(true); + SA driver = new SA(true); long start = System.currentTimeMillis(); driver.run(); long end = System.currentTimeMillis(); @@ -370,266 +373,3 @@ public static void main(String[] args) throws java.lang.Exception{ } -class FastReader{ - - private boolean finished = false; - - private InputStream stream; - private byte[] buf = new byte[4*1024]; - private int curChar; - private int numChars; - private SpaceCharFilter filter; - - public FastReader(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 peek(){ - if (numChars == -1){ - return -1; - } - if (curChar>= numChars){
- curChar = 0;
- try{
- numChars = stream.read (buf);
- } catch (IOException e){
- return -1;
- }
- if (numChars <= 0){ - return -1; - } - } - return buf[curChar]; - } - - public int nextInt(){ - int c = read (); - while (isSpaceChar (c)) - c = read (); - int sgn = 1; - if (c == '-'){ - sgn = -1; - c = read (); - } - int res = 0; - do{ - if(c==','){ - c = read(); - } - if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public long nextLong(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- long res = 0;
- do{
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public String nextString(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- StringBuilder res = new StringBuilder ();
- do{
- res.appendCodePoint (c);
- c = read ();
- } while (!isSpaceChar (c));
- return res.toString ();
- }
-
- public boolean isSpaceChar(int c){
- if (filter != null){
- return filter.isSpaceChar (c);
- }
- return isWhitespace (c);
- }
-
- public static boolean isWhitespace(int c){
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
-
- private String readLine0(){
- StringBuilder buf = new StringBuilder ();
- int c = read ();
- while (c != '\n' && c != -1){
- if (c != '\r'){
- buf.appendCodePoint (c);
- }
- c = read ();
- }
- return buf.toString ();
- }
-
- public String nextLine(){
- String s = readLine0 ();
- while (s.trim ().length () == 0)
- s = readLine0 ();
- return s;
- }
-
- public String nextLine(boolean ignoreEmptyLines){
- if (ignoreEmptyLines){
- return nextLine ();
- }else{
- return readLine0 ();
- }
- }
-
- public BigInteger nextBigInteger(){
- try{
- return new BigInteger (nextString ());
- } catch (NumberFormatException e){
- throw new InputMismatchException ();
- }
- }
-
- public char nextCharacter(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- return (char) c;
- }
-
- public double nextDouble(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- double res = 0;
- while (!isSpaceChar (c) && c != '.'){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- }
- if (c == '.'){
- c = read ();
- double m = 1;
- while (!isSpaceChar (c)){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- m /= 10;
- res += (c - '0') * m;
- c = read ();
- }
- }
- return res * sgn;
- }
-
- public boolean isExhausted(){
- int value;
- while (isSpaceChar (value = peek ()) && value != -1)
- read ();
- return value == -1;
- }
-
- public String next(){
- return nextString ();
- }
-
- public SpaceCharFilter getFilter(){
- return filter;
- }
-
- public void setFilter(SpaceCharFilter filter){
- this.filter = filter;
- }
-
- public interface SpaceCharFilter{
- public boolean isSpaceChar(int ch);
- }
-}
- /******************** Pair class ***********************/
-
- class Pair implements Comparable{
- public int id;
- public String name;
- public long b;
- public long a;
- public long c;
- public Pair(){
- this.id = 1000;
- this.name = "s";
- this.a = 0L;
- this.b = 0L;
- this.c = 0L;
- }
- public Pair(int id , long a,long b , long c , String name){
- this.name = name;
- this.id = id;
- this.a = a;
- this.b = b;
- this.c = c;
- }
- public int compareTo(Pair p){
- if(this.a < p.a)return -1; - else if(this.a> p.a )return 1;
- else {
- if(this.b < p.b)return -1; - else if(this.b> p.b )return 1;
- else {
- if(this.c < p.c)return -1; - else if(this.c> p.c )return 1;
- else return -this.name.compareTo(p.name);
- }
-
- }
- }
- public String toString(){
- return "a="+this.a+" b="+this.b;
- }
-
-}
diff --git a/Algorithms/TopologicalOrder.java b/Algorithms/TopologicalOrder.java
index 37be8bd..31d947d 100755
--- a/Algorithms/TopologicalOrder.java
+++ b/Algorithms/TopologicalOrder.java
@@ -1,3 +1,6 @@
+package Algorithms;
+
+import Algorithms.InputReaderAndProcessor;
import java.util.*;
import java.lang.*;
@@ -11,11 +14,11 @@
*/
/* The Main Class */
-public class A{
+public class TopologicalOrder{
private InputStream inputStream ;
private OutputStream outputStream ;
- private FastReader in ;
+ private InputReaderAndProcessor in ;
private PrintWriter out ;
/*
Overhead [Additional Temporary Strorage] but provides memory reusibility for multiple test cases.
@@ -30,8 +33,8 @@ public class A{
private final int INF = Integer.MAX_VALUE / 10;
private final long INF_L = Long.MAX_VALUE / 10;
- public A(){}
- public A(boolean stdIO)throws FileNotFoundException{
+ public TopologicalOrder(){}
+ public TopologicalOrder(boolean stdIO)throws FileNotFoundException{
//stdIO = false;
if(stdIO){
inputStream = System.in;
@@ -40,7 +43,7 @@ public A(boolean stdIO)throws FileNotFoundException{
inputStream = new FileInputStream("laundro_matt.txt");
outputStream = new FileOutputStream("output.txt");
}
- in = new FastReader(inputStream);
+ in = new InputReaderAndProcessor(inputStream);
out = new PrintWriter(outputStream);
}
@@ -222,7 +225,7 @@ BigInteger bi()throws Exception{
//***********************I/O ENDS ***********************//
//*********************** 0.3%f [precision]***********************//
/* roundoff upto 2 digits
- double roundOff = Math.round(a * 100.0) / 100.0;
+ double roundOff = Math.round(number1 * 100.0) / 100.0;
or
System.out.printf("%.2f", val);
@@ -246,8 +249,8 @@ public static void main(String[] args) throws java.lang.Exception{
BufferedWriter out=new BufferedWriter(new OutputStreamWriter(System.out));
BufferedReader br=new BufferedReader(new FileReader("input.txt"));
BufferedWriter out=new BufferedWriter(new FileWriter("output.txt"));
- */
- A driver = new A(true);
+ */
+ TopologicalOrder driver = new TopologicalOrder(true);
long start = System.currentTimeMillis();
driver.run();
long end = System.currentTimeMillis();
@@ -259,248 +262,3 @@ public static void main(String[] args) throws java.lang.Exception{
}
-class FastReader{
-
- private boolean finished = false;
-
- private InputStream stream;
- private byte[] buf = new byte[4*1024];
- private int curChar;
- private int numChars;
- private SpaceCharFilter filter;
-
- public FastReader(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 peek(){ - if (numChars == -1){ - return -1; - } - if (curChar>= numChars){
- curChar = 0;
- try{
- numChars = stream.read (buf);
- } catch (IOException e){
- return -1;
- }
- if (numChars <= 0){ - return -1; - } - } - return buf[curChar]; - } - - public int nextInt(){ - int c = read (); - while (isSpaceChar (c)) - c = read (); - int sgn = 1; - if (c == '-'){ - sgn = -1; - c = read (); - } - int res = 0; - do{ - if(c==','){ - c = read(); - } - if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public long nextLong(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- long res = 0;
- do{
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public String nextString(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- StringBuilder res = new StringBuilder ();
- do{
- res.appendCodePoint (c);
- c = read ();
- } while (!isSpaceChar (c));
- return res.toString ();
- }
-
- public boolean isSpaceChar(int c){
- if (filter != null){
- return filter.isSpaceChar (c);
- }
- return isWhitespace (c);
- }
-
- public static boolean isWhitespace(int c){
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
-
- private String readLine0(){
- StringBuilder buf = new StringBuilder ();
- int c = read ();
- while (c != '\n' && c != -1){
- if (c != '\r'){
- buf.appendCodePoint (c);
- }
- c = read ();
- }
- return buf.toString ();
- }
-
- public String nextLine(){
- String s = readLine0 ();
- while (s.trim ().length () == 0)
- s = readLine0 ();
- return s;
- }
-
- public String nextLine(boolean ignoreEmptyLines){
- if (ignoreEmptyLines){
- return nextLine ();
- }else{
- return readLine0 ();
- }
- }
-
- public BigInteger nextBigInteger(){
- try{
- return new BigInteger (nextString ());
- } catch (NumberFormatException e){
- throw new InputMismatchException ();
- }
- }
-
- public char nextCharacter(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- return (char) c;
- }
-
- public double nextDouble(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- double res = 0;
- while (!isSpaceChar (c) && c != '.'){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- }
- if (c == '.'){
- c = read ();
- double m = 1;
- while (!isSpaceChar (c)){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- m /= 10;
- res += (c - '0') * m;
- c = read ();
- }
- }
- return res * sgn;
- }
-
- public boolean isExhausted(){
- int value;
- while (isSpaceChar (value = peek ()) && value != -1)
- read ();
- return value == -1;
- }
-
- public String next(){
- return nextString ();
- }
-
- public SpaceCharFilter getFilter(){
- return filter;
- }
-
- public void setFilter(SpaceCharFilter filter){
- this.filter = filter;
- }
-
- public interface SpaceCharFilter{
- public boolean isSpaceChar(int ch);
- }
-}
- /******************** Pair class ***********************/
-
- class Pair implements Comparable{
- public long a;
- public long b;
- public Pair(){
- this.a = 0L;
- this.b = 0L;
- }
- public Pair(long a,long b){
- this.a = a;
- this.b = b;
- }
- public int compareTo(Pair p){
- if(this.a + this.b < p.a + p.b)return -1; - else if(this.a + this.b> p.a + p.b )return 1;
- else return 0;
- }
- public String toString(){
- return "a="+this.a+" b="+this.b;
- }
-
-}
diff --git a/Algorithms/TopologicalOrderBFS.java b/Algorithms/TopologicalOrderBFS.java
index b6b884e..10d6c86 100644
--- a/Algorithms/TopologicalOrderBFS.java
+++ b/Algorithms/TopologicalOrderBFS.java
@@ -1,14 +1,16 @@
-// Time Complexity : O(N) and Space O(N) Heap space,
+package Algorithms;// Time Complexity : O(N) and Space O(N) Heap space,
// Advantage over stack approach: handle cycle detection
-class Solution {
+import java.util.LinkedList;
+
+class TopologicalOrderBfs {
public boolean isCycle(int numCourses, int[][] prerequisites) {
if(numCourses <= 1)return true; try{ int d[] = new int[numCourses]; // Graph as Adj List - LinkedList [] adj = new LinkedList[numCourses];
+ LinkedList[] adj = new LinkedList[numCourses];
for(int vertex = 0; vertex < numCourses; vertex ++){ adj[vertex] = new LinkedList();
}
diff --git a/Algorithms/Treap.java b/Algorithms/Treap.java
index b5495f4..dbe839b 100644
--- a/Algorithms/Treap.java
+++ b/Algorithms/Treap.java
@@ -1,3 +1,5 @@
+package Algorithms;
+
import java.util.*;
import java.lang.*;
import java.io.*;
@@ -38,12 +40,12 @@ class TreapPair {
}
}
-class A{
+class TreapMain{
static Random random = new Random();
private InputStream inputStream ;
private OutputStream outputStream ;
- private FastReader in ;
+ private InputReaderAndProcessor in ;
private PrintWriter out ;
private final int BUFFER = 100005;
@@ -56,8 +58,8 @@ class A{
private final int INF = Integer.MAX_VALUE;
private final long INF_L = Long.MAX_VALUE / 10;
- public A(){}
- public A(boolean stdIO)throws FileNotFoundException{
+ public TreapMain(){}
+ public TreapMain(boolean stdIO)throws FileNotFoundException{
// stdIO = false;
if(stdIO){
inputStream = System.in;
@@ -66,7 +68,7 @@ public A(boolean stdIO)throws FileNotFoundException{
inputStream = new FileInputStream("input.txt");
outputStream = new FileOutputStream("output.txt");
}
- in = new FastReader(inputStream);
+ in = new InputReaderAndProcessor(inputStream);
out = new PrintWriter(outputStream);
}
@@ -271,7 +273,7 @@ private void closeResources(){
}
// IMP: roundoff upto 2 digits
-// double roundOff = Math.round(a * 100.0) / 100.0;
+// double roundOff = Math.round(number1 * 100.0) / 100.0;
// or
// System.out.printf("%.2f", val);
@@ -279,259 +281,9 @@ private void closeResources(){
// val = ((long)(val * 100.0))/100.0;
public static void main(String[] args) throws java.lang.Exception{
-
- A driver = new A(true);
+
+ TreapMain driver = new TreapMain(true);
driver.run();
driver.closeResources();
}
}
-
-class FastReader{
-
- private boolean finished = false;
-
- private InputStream stream;
- private byte[] buf = new byte[4 * 1024];
- private int curChar;
- private int numChars;
- private SpaceCharFilter filter;
-
- public FastReader(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 peek(){ - if (numChars == -1){ - return -1; - } - if (curChar>= numChars){
- curChar = 0;
- try{
- numChars = stream.read (buf);
- } catch (IOException e){
- return -1;
- }
- if (numChars <= 0){ - return -1; - } - } - return buf[curChar]; - } - - public int nextInt(){ - int c = read (); - while (isSpaceChar (c)) - c = read (); - int sgn = 1; - if (c == '-'){ - sgn = -1; - c = read (); - } - int res = 0; - do{ - if(c==','){ - c = read(); - } - if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public long nextLong(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- long res = 0;
- do{
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- } while (!isSpaceChar (c));
- return res * sgn;
- }
-
- public String nextString(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- StringBuilder res = new StringBuilder ();
- do{
- res.appendCodePoint (c);
- c = read ();
- } while (!isSpaceChar (c));
- return res.toString ();
- }
-
- public boolean isSpaceChar(int c){
- if (filter != null){
- return filter.isSpaceChar (c);
- }
- return isWhitespace (c);
- }
-
- public static boolean isWhitespace(int c){
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
-
- private String readLine0(){
- StringBuilder buf = new StringBuilder ();
- int c = read ();
- while (c != '\n' && c != -1){
- if (c != '\r'){
- buf.appendCodePoint (c);
- }
- c = read ();
- }
- return buf.toString ();
- }
-
- public String nextLine(){
- String s = readLine0 ();
- while (s.trim ().length () == 0)
- s = readLine0 ();
- return s;
- }
-
- public String nextLine(boolean ignoreEmptyLines){
- if (ignoreEmptyLines){
- return nextLine ();
- }else{
- return readLine0 ();
- }
- }
-
- public BigInteger nextBigInteger(){
- try{
- return new BigInteger (nextString ());
- } catch (NumberFormatException e){
- throw new InputMismatchException ();
- }
- }
-
- public char nextCharacter(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- return (char) c;
- }
-
- public double nextDouble(){
- int c = read ();
- while (isSpaceChar (c))
- c = read ();
- int sgn = 1;
- if (c == '-'){
- sgn = -1;
- c = read ();
- }
- double res = 0;
- while (!isSpaceChar (c) && c != '.'){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- res *= 10;
- res += c - '0';
- c = read ();
- }
- if (c == '.'){
- c = read ();
- double m = 1;
- while (!isSpaceChar (c)){
- if (c == 'e' || c == 'E'){
- return res * Math.pow (10, nextInt ());
- }
- if (c < '0' || c> '9'){
- throw new InputMismatchException ();
- }
- m /= 10;
- res += (c - '0') * m;
- c = read ();
- }
- }
- return res * sgn;
- }
-
- public boolean isExhausted(){
- int value;
- while (isSpaceChar (value = peek ()) && value != -1)
- read ();
- return value == -1;
- }
-
- public String next(){
- return nextString ();
- }
-
- public SpaceCharFilter getFilter(){
- return filter;
- }
-
- public void setFilter(SpaceCharFilter filter){
- this.filter = filter;
- }
-
- public interface SpaceCharFilter{
- public boolean isSpaceChar(int ch);
- }
-}
-
-class Pair implements Comparable{
- public int a;
- public int b;
-
- public Pair(){
- this.a = 0;
- this.b = 0;
- }
-
- public Pair(int a,int b){
- this.a = a;
- this.b = b;
- }
-
- public int compareTo(Pair p){
- if(this.a == p.a){
- return this.b - p.b;
- }
- return this.a - p.a;
- }
-
- @Override
- public String toString(){
- return "a = " + this.a + " b = " + this.b;
- }
-}
\ No newline at end of file
diff --git a/Algorithms/Trie.java b/Algorithms/Trie.java
index 2b46e0d..4a14b3b 100755
--- a/Algorithms/Trie.java
+++ b/Algorithms/Trie.java
@@ -1,3 +1,7 @@
+package Algorithms;
+
+import Algorithms.InputReaderAndProcessor;
+
import java.util.*;
import java.lang.*;
import java.io.*;
@@ -9,14 +13,14 @@
* @Date : Saturday 20 May 2017 01:34:34 AM IST
*