Skip to content

Instantly share code, notes, and snippets.

@mastersobg
Created October 13, 2013 16:15
Show Gist options
  • Save mastersobg/6963992 to your computer and use it in GitHub Desktop.
Save mastersobg/6963992 to your computer and use it in GitHub Desktop.
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class Solution {
static final boolean DEBUG = false;
BufferedReader in;
StringTokenizer st;
PrintWriter out;
String problem = "c";
void solve() throws IOException {
Read r = new Read(in);
int n = r.r();
int m = r.r();
int [][]v = new int[n][m];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
v[i][j] = r.r();
}
}
while(true) {
try {
int le = r.r();
int ri = r.r();
int ret = 0;
for (int i = 0; i < n; ++i) {
int lower = count(v[i], le - 1);
int upper = count(v[i], ri);
ret += upper - lower;
}
out.println(ret);
} catch (IOException e) {
break;
}
}
}
int count(int []v, int a) {
int l = -1, r = v.length;
while (l + 1 < r) {
int m = (l + r) >> 1;
if (v[m] <= a)
l = m;
else r = m;
}
return l + 1;
}
class Read {
BufferedReader r;
char []buf = new char[4];
Read(BufferedReader br) {
r = br;
}
int r() throws IOException {
int read = r.read(buf);
if (read == -1) {
throw new IOException();
}
if (read != 4) {
throw new RuntimeException();
}
int ret = 0;
for (int i = 0; i < 4; ++i) {
ret |= (buf[i] & 0xFF) << (8 * i);
}
return ret;
}
}
class Write {
PrintWriter w;
Write(PrintWriter w) {
this.w = w;
}
void w(int a) throws IOException {
for (int i = 0; i < 4; ++i) {
int x = a & 0xFF;
w.write(x);
a >>= 8;
}
w.flush();
}
}
void createFile(BufferedReader in, Write out) throws IOException {
String s;
while ((s = in.readLine()) != null) {
StringTokenizer st = new StringTokenizer(s);
while (st.hasMoreTokens()) {
int value = Integer.valueOf(st.nextToken());
out.w(value);
}
}
}
void dbg(Object ... objs) {
if (!DEBUG) {
return ;
}
for (Object o : objs) {
String printLine;
if (o.getClass().isArray()) {
printLine = arrayToString(o);
} else {
printLine = o.toString();
}
System.err.print(printLine + " ");
}
System.err.println();
}
String arrayToString(Object o) {
if (o instanceof long[])
return Arrays.toString((long[]) o);
if (o instanceof int[])
return Arrays.toString((int[]) o);
if (o instanceof short[])
return Arrays.toString((short[]) o);
if (o instanceof char[])
return Arrays.toString((char[]) o);
if (o instanceof byte[])
return Arrays.toString((byte[]) o);
if (o instanceof double[])
return Arrays.toString((double[]) o);
if (o instanceof float[])
return Arrays.toString((float[]) o);
if (o instanceof boolean[])
return Arrays.toString((boolean[]) o);
if (o instanceof Object[])
return Arrays.deepToString((Object[]) o);
throw new IllegalStateException();
}
public void run() throws IOException {
Locale.setDefault(Locale.US);
in = new BufferedReader(new FileReader(problem + ".in"));
out = new PrintWriter(System.out);
// createFile(new BufferedReader(new FileReader("a")), new Write(new PrintWriter(problem + ".in")));
solve();
in.close();
out.close();
}
String ns() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(in.readLine());
return st.nextToken();
}
int ni() throws IOException {
return Integer.valueOf(ns());
}
long nl() throws IOException {
return Long.valueOf(ns());
}
double nd() throws IOException {
return Double.valueOf(ns());
}
public static void main(String[] args) throws IOException {
new Solution().run();
}
class Timer {
long time;
void start() {
time = System.currentTimeMillis();
}
long time() {
return System.currentTimeMillis() - time;
}
void print() {
print("Time spent = ");
}
void print(String message) {
dbg(message, time());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment