TYVJ 1512 异或密码 解题报告
可以用朴素算法,在TYVJ上2000ms,别的评测机可能超时,所以修改成了线段树的,代码如下:
#include <stdio.h> #include <stdlib.h> struct tree{ int l, r; int ll, rr; int xor; }tree[100001]; int end; int num[50000]; int maketree(int s, int t) { int l = end++; tree[l].l = s; tree[l].r = t; if(s == t){ tree[l].ll = tree[l].rr = -1; tree[l].xor = num[s]; return num[s]; } tree[l].ll = end; tree[l].xor = maketree(s, (s + t) / 2); tree[l].rr = end; tree[l].xor ^= maketree((s + t) / 2 + 1, t); return tree[l].xor; } int deal(int r, int s, int t) { int mid; if(tree[r].l == s && tree[r].r == t){ return tree[r].xor; } mid = (tree[r].l + tree[r].r) / 2; if(t <= mid){ return deal(tree[r].ll, s, t); }else if(s > mid){ return deal(tree[r].rr, s, t); } return deal(tree[r].ll, s, mid) ^ deal(tree[r].rr, mid + 1, t); } void fix(int r, int a, int b) { int mid = (tree[r].l + tree[r].r) / 2; if(tree[r].l == a && tree[r].r == a){ tree[r].xor = b; return; } if(a <= mid){ fix(tree[r].ll, a, b); }else{ fix(tree[r].rr, a, b); } tree[r].xor = tree[tree[r].ll].xor ^ tree[tree[r].rr].xor; } int main(int argc, char **argv) { int i; int n, m; int a, b, c; scanf("%d%d", &n, &m); for(i = 0; i < n; i++){ scanf("%d", &num[i]); } maketree(0, n - 1); for(i = 0; i < m; i++){ scanf("%d%d%d", &a, &b, &c); if(a){ fix(0, b - 1, c); }else{ printf("%d\n", deal(0, b - 1, c - 1)); } } return 0; }