可持久化数据结构
256. 最大异或和
给定的简单样例都要2800ms…Java过不了了
import java.io.*;
public class Main {
static int N = 600010;
static int M = N * 25;
static int n;
static int m;
static int[] s = new int[N];
static int[][] tr = new int[M][2];
static int[] max_id = new int[M];
static int[] root = new int[N];
static int idx;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String[] s1 = br.readLine().split(" ");
n = Integer.parseInt(s1[0]);
m = Integer.parseInt(s1[1]);
max_id[0] = -1;
root[0] = ++idx;
insert(0, 23, 0, root[0]);
String[] s2 = br.readLine().split(" ");
for (int i = 1; i <= n; i++) {
int x = Integer.parseInt(s2[i - 1]);
s[i] = s[i - 1] ^ x;
root[i] = ++idx;
insert(i, 23, root[i - 1], root[i]);
}
String op = null;
int l, r, x;
while (m-- > 0) {
String[] s3 = br.readLine().split(" ");
op = s3[0];
if (op.equals("A")) {
x = Integer.parseInt(s3[1]);
n++;
s[n] = s[n - 1] ^ x;
root[n] = ++idx;
insert(n, 23, root[n - 1], root[n]);
} else {
l = Integer.parseInt(s3[1]);
r = Integer.parseInt(s3[2]);
x = Integer.parseInt(s3[3]);
int res = query(root[r - 1], s[n] ^ x, l - 1);
bw.write("" + res);
bw.newLine();
}
bw.flush();
}
}
private static int query(int root, int C, int L) {
int p = root;
for (int i = 23; i >= 0; i--) {
int v = C >> i & 1;
if (max_id[tr[p][v ^ 1]] >= L) {
p = tr[p][v ^ 1];
} else {
p = tr[p][v];
}
}
return C ^ s[max_id[p]];
}
private static void insert(int i, int k, int p, int q) {
if (k < 0) {
max_id[q] = i;
return;
}
int v = s[i] >> k & 1;
if (p != 0) tr[q][v ^ 1] = tr[p][v ^ 1];
tr[q][v] = ++idx;
insert(i, k - 1, tr[p][v], tr[q][v]);
max_id[q] = Math.max(max_id[tr[q][0]], max_id[tr[q][1]]);
}
}
255. 第K小数
【未完】
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
public class Main {
static int N = 100010;
static int M = 10010;
static int n;
static int m;
static int[] a = new int[N];
static Node[] tr = new Node[N * 4 + N * 17];
static ArrayList<Integer> nums = new ArrayList<>();
static int[] root = new int[N];
static int idx;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String[] s1 = br.readLine().split(" ");
n = Integer.parseInt(s1[0]);
m = Integer.parseInt(s1[1]);
String[] s2 = br.readLine().split(" ");
for (int i = 1; i <= n; i++) {
a[i] = Integer.parseInt(s2[i - 1]);
nums.add(a[i]);
}
Collections.sort(nums);
//去掉重复元素 unique
}
private static int find(int x) {
return low_bound();//这个也是C++自带的
}
}
class Node {
int l, r, cnt;
public Node(int l, int r, int cnt) {
this.l = l;
this.r = r;
this.cnt = cnt;
}
}