Created
November 11, 2017 21:46
-
-
Save chermehdi/0eeb41aee00ff42178f46f9d795b7871 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.OutputStream; | |
import java.io.IOException; | |
import java.io.FileInputStream; | |
import java.io.InputStream; | |
import java.io.PrintWriter; | |
import java.util.StringTokenizer; | |
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
import java.io.InputStream; | |
/** | |
* Built using CHelper plug-in | |
* Actual solution is at the top | |
* | |
* @author MaxHeap | |
*/ | |
public class Main { | |
public static void main(String[] args) { | |
InputStream inputStream; | |
try { | |
inputStream = new FileInputStream("wifi.in"); | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
OutputStream outputStream = System.out; | |
FastReader in = new FastReader(inputStream); | |
PrintWriter out = new PrintWriter(outputStream); | |
WifiPassword solver = new WifiPassword(); | |
solver.solve(1, in, out); | |
out.close(); | |
} | |
static class WifiPassword { | |
int[] arr; | |
int[] tree; | |
int n; | |
int v; | |
public void solve(int testNumber, FastReader in, PrintWriter out) { | |
int t = in.nextInt(); | |
while (t-- > 0) { | |
n = in.nextInt(); | |
v = in.nextInt(); | |
arr = new int[n]; | |
tree = new int[4 * n + 2]; | |
for (int i = 0; i < n; i++) { | |
arr[i] = in.nextInt(); | |
} | |
build(0, 0, n - 1); | |
int lo = 0, hi = n; | |
int ans = 0; | |
while (lo <= hi) { | |
int mid = (lo + hi) >> 1; | |
if (can(mid)) { | |
ans = mid; | |
lo = mid + 1; | |
} else { | |
hi = mid - 1; | |
} | |
} | |
out.println(ans); | |
} | |
} | |
private boolean can(int val) { | |
for (int i = 0; i + val <= n; i++) { | |
if (query(0, 0, n - 1, i, i + val - 1) <= v) { | |
return true; | |
} | |
} | |
return false; | |
} | |
private void build(int root, int left, int right) { | |
if (left == right) { | |
tree[root] = arr[left]; | |
return; | |
} | |
int mid = (left + right) >> 1; | |
build(root * 2 + 1, left, mid); | |
build(root * 2 + 2, mid + 1, right); | |
tree[root] = tree[root * 2 + 1] | tree[root * 2 + 2]; | |
} | |
private int query(int root, int lo, int hi, int i, int j) { | |
if (lo > j || hi < i) | |
return 0; | |
if (i <= lo && j >= hi) | |
return tree[root]; | |
int mid = lo + (hi - lo) / 2; | |
if (i > mid) | |
return query(2 * root + 2, mid + 1, hi, i, j); | |
else if (j <= mid) | |
return query(2 * root + 1, lo, mid, i, j); | |
int leftQuery = query(2 * root + 1, lo, mid, i, mid); | |
int rightQuery = query(2 * root + 2, mid + 1, hi, mid + 1, j); | |
return leftQuery | rightQuery; | |
} | |
} | |
static class FastReader { | |
BufferedReader reader; | |
StringTokenizer st; | |
public FastReader(InputStream stream) { | |
reader = new BufferedReader(new InputStreamReader(stream)); | |
st = null; | |
} | |
public String next() { | |
while (st == null || !st.hasMoreTokens()) { | |
try { | |
String line = reader.readLine(); | |
if (line == null) { | |
return null; | |
} | |
st = new StringTokenizer(line); | |
} catch (Exception e) { | |
throw new RuntimeException(); | |
} | |
} | |
return st.nextToken(); | |
} | |
public int nextInt() { | |
return Integer.parseInt(next()); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment