感觉做法上应该没什么问题了,但MLE了,求dalao能否点拨一下如何能再次优化空间?
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
public class Main {
static InputReader in;
static PrintWriter out;
static int n, m;
static int[] r, d;
static int[] s, t;
static long[] dd;
static boolean judge(int x) {
dd = new long[1000001];
long sum = 0;
for(int i = 1; i <= x; i++) {
dd[s[i]] += d[i];
dd[t[i] + 1] -= d[i];
}
for(int i = 1; i <= n; i++) {
sum += dd[i];
if(sum > r[i]) {
dd = null;
return false;
}
}
dd = null;
return true;
}
public static void main(String[] args) throws IOException {
in = new InputReader(System.in);
out = new PrintWriter(System.out);
n = in.nextInt();
m = in.nextInt();
r = new int[1000001];
d = new int[1000001];
s = new int[1000001];
t = new int[1000001];
for(int i = 1; i <= n; i++) {
r[i] = in.nextInt();
}
for(int i = 1; i <= m; i++) {
d[i] = in.nextInt();
s[i] = in.nextInt();
t[i] = in.nextInt();
}
if(judge(m)) {
out.print(0);
} else {
int left = 1, right = m;
while(left <= right) {
int mid = (left + right) / 2;
if(judge(mid)) {
left = mid + 1;
} else {
right = mid - 1;
}
}
out.println("-1");
out.print(left);
}
out.close();
}
static class InputReader {
BufferedReader br;
public InputReader(InputStream stream) {
br = new BufferedReader(new InputStreamReader(stream));
}
public int nextInt() throws IOException {
int c = br.read();
while (c <= 32) {
c = br.read();
}
boolean negative = false;
if (c == '-') {
negative = true;
c = br.read();
}
int x = 0;
while (c > 32) {
x = x * 10 + c - '0';
c = br.read();
}
return negative ? -x : x;
}
public long nextLong() throws IOException {
int c = br.read();
while (c <= 32) {
c = br.read();
}
boolean negative = false;
if (c == '-') {
negative = true;
c = br.read();
}
long x = 0;
while (c > 32) {
x = x * 10 + c - '0';
c = br.read();
}
return negative ? -x : x;
}
public String next() throws IOException {
int c = br.read();
while (c <= 32) {
c = br.read();
}
StringBuilder sb = new StringBuilder();
while (c > 32) {
sb.append((char) c);
c = br.read();
}
return sb.toString();
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
}