publicstaticvoidmain(String[] args){ init(); Scanner sc = new Scanner(System.in); int m = sc.nextInt(); while (m-- != 0) { String op = sc.next(); if (op.charAt(0) == 'H') { int x = sc.nextInt(); add_to_head(x); } elseif (op.charAt(0) == 'D') { int k = sc.nextInt(); if (k == 0) head = ne[head]; else remove(k - 1); } else { int k = sc.nextInt(); int x = sc.nextInt(); add(k - 1, x); } } for (int i = head; i != -1; i = ne[i]) { System.out.printf("%d ", e[i]); } } }
publicclasschain_list_2{ staticint N = 100010; //e[i]表示节点i的值 staticint[] e = newint[N]; //l[i]表示节点i左边指向点的下标 staticint[] l = newint[N]; //r[i]表示节点i右边指向点的下标 staticint[] r = newint[N];
publicclassMain{ staticint N = 1000010; staticint[] a = newint[N];
staticint[] q = newint[N];
publicstaticvoidmain(String[] args)throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] str = br.readLine().split(" "); int n = Integer.parseInt(str[0]); int k = Integer.parseInt(str[1]); str = br.readLine().split(" "); for (int i = 0; i < n; i++) a[i] = Integer.parseInt(str[i]); int hh = 0, tt = -1; for (int i = 0; i < n; i++) { if (hh <= tt && i - k + 1 > q[hh]) hh++;
while (hh <= tt && a[q[tt]] >= a[i]) tt--;//单增队列
q[++tt] = i;
if (i >= k - 1) System.out.print(a[q[hh]] + " "); } System.out.println(); hh = 0; tt = -1; for (int i = 0; i < n; i++) { if (hh <= tt && i - k + 1 > q[hh]) hh++; while (hh <= tt && a[q[tt]] <= a[i]) tt--;//单减队列 q[++tt] = i; if (i >= k - 1) System.out.print(a[q[hh]] + " "); } System.out.println(); } }
staticvoidinsert(String str){ int p = 0; for (int i = 0; i < str.length(); i++) { int u = str.charAt(i) - 'a'; if (son[p][u] == 0) son[p][u] = ++idx; p = son[p][u]; } cnt[p]++; }
staticintquery(String str){ int p = 0; for (int i = 0; i < str.length(); i++) { int u = str.charAt(i) - 'a'; if (son[p][u] == 0) return0; p = son[p][u]; } return cnt[p]; }
publicstaticvoidmain(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for (int i = 0; i < n; i++) { String op = sc.next(); if (op.equals("I")) insert(sc.next()); else System.out.println(query(sc.next())); } } }
好栗子:
异或数列:
在给定的 N 个整数 A1,A2……AN 中选出两个进行 xor(异或)运算,得到的结果最大是多少?
publicclasstest{ staticint N = 31 * 100010; staticint[][] son = newint[N][2];
staticint idx = 0;
staticint[] a = newint[N];
staticvoidinsert(int x){ int p = 0; for (int i = 30; i >= 0; i--) { int k = x >> i & 1;//位运算取第k位 if (son[p][k] == 0) son[p][k] = ++idx; p = son[p][k]; } }
staticintquery(int x){ int p = 0; int res = 0; for (int i = 30; i >= 0; i--) { int k = x >> i & 1; int kp = k == 0 ? 1:0; if (son[p][kp] != 0) { p = son[p][kp]; res = res * 2 + kp; } else { p = son[p][k]; res = res * 2 + k; } } return res; }
publicstaticvoidmain(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int max = 0; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); insert(a[i]); int res = query(a[i]); max = Math.max(max, res ^ a[i]); } System.out.println(max); } }
publicclassheap_sort{ staticint N = 100010; staticint[] h = newint[N]; staticint size = 0;
staticvoiddown(int u){ int k = u; if (u * 2 <= size && h[u * 2] < h[k]) k = u * 2; if (u * 2 + 1 <= size && h[u * 2 + 1] < h[k]) k = u * 2 + 1; if (k != u) { int t = h[k]; h[k] = h[u]; h[u] = t; down(k); } }
staticvoidup(int u){ while (u / 2 >= 1 && h[u / 2] > h[u]) { int t = h[u]; h[u] = h[u / 2]; h[u / 2] = t; u /= 2; } }
publicstaticvoidmain(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt();
for (int i = 1; i <= n; i++) {//习惯堆下标从1开始 h[i] = sc.nextInt(); } size = n; for (int i = n / 2; i > 0; i--) down(i);//初始化堆 复杂度O(n)
Scanner sc = new Scanner(System.in); int n = sc.nextInt(); while (n-- != 0) { String s = sc.next(); int x = sc.nextInt(); int k = find(x); if(s.equals("I"))h[k] = x; else{ if(h[k] != C)System.out.println("Yes"); else System.out.println("No"); } } } }
publicstaticvoidmain(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); String s = sc.next(); p[0] = 1; for(int i = 1; i <= n; i++){ p[i] = p[i-1] * P; h[i] = h[i-1] * P + s.charAt(i-1); }
while(m-- != 0){ int l1 = sc.nextInt(); int r1 = sc.nextInt(); int l2 = sc.nextInt(); int r2 = sc.nextInt();
staticintquery(int u, int l, int r){ if(tr[u].l >= l && tr[u].r <= r)return tr[u].w; int mid = tr[u].l + tr[u].r >> 1; int sum = 0; if(l <= mid)sum += query(u << 1, l, r); if(r > mid)sum += query(u << 1 | 1, l, r); return sum; }
staticvoidmodify(int u, int x, int v){ if(tr[u].l == x && tr[u].r == x)tr[u].w += v; else { int mid = tr[u].l + tr[u].r >> 1; if(x <= mid)modify(u << 1, x, v); else modify(u << 1 | 1, x, v); pushup(u); } }
publicstaticvoidmain(String[] args)throws IOException { String[] str = br.readLine().split(" "); int n = Integer.parseInt(str[0]); int m = Integer.parseInt(str[1]); str = br.readLine().split(" "); build(1, 1, n); for(int i = 1; i <= n; i++){ a[i] = Integer.parseInt(str[i - 1]); modify(1, i, a[i]); } while(m-- != 0){ str = br.readLine().split(" "); int k = Integer.parseInt(str[0]); int a = Integer.parseInt(str[1]); int b = Integer.parseInt(str[2]); if(k == 0){ bw.write(query(1, a, b) + "\n"); } else { modify(1, a, b); } } bw.flush(); } }