• [codeforces 339]D. Xenia and Bit Operations


    [codeforces 339]D. Xenia and Bit Operations

    试题描述

    Xenia the beginner programmer has a sequence a, consisting of 2n non-negative integers: a1, a2, ..., a2n. Xenia is currently studying bit operations. To better understand how they work, Xenia decided to calculate some value v for a.

    Namely, it takes several iterations to calculate value v. At the first iteration, Xenia writes a new sequence a1 or a2, a3 or a4, ..., a2n - 1 or a2n, consisting of 2n - 1 elements. In other words, she writes down the bit-wise OR of adjacent elements of sequence a. At the second iteration, Xenia writes the bitwise exclusive OR of adjacent elements of the sequence obtained after the first iteration. At the third iteration Xenia writes the bitwise OR of the adjacent elements of the sequence obtained after the second iteration. And so on; the operations of bitwise exclusive OR and bitwise OR alternate. In the end, she obtains a sequence consisting of one element, and that element is v.

    Let's consider an example. Suppose that sequence a = (1, 2, 3, 4). Then let's write down all the transformations (1, 2, 3, 4)  → (1 or 2 = 3, 3 or 4 = 7)  →  (3 xor 7 = 4). The result is v = 4.

    You are given Xenia's initial sequence. But to calculate value v for a given sequence would be too easy, so you are given additional mqueries. Each query is a pair of integers p, b. Query p, b means that you need to perform the assignment ap = b. After each query, you need to print the new value v for the new sequence a.

    输入

    The first line contains two integers n and m (1 ≤ n ≤ 17, 1 ≤ m ≤ 105). The next line contains 2n integers a1, a2, ..., a2n (0 ≤ ai < 230). Each of the next m lines contains queries. The i-th line contains integers pi, bi (1 ≤ pi ≤ 2n, 0 ≤ bi < 230) — the i-th query.

    输出

    Print m integers — the i-th integer denotes value v for sequence a after the i-th query.

    输入示例

    2 4
    1 6 3 5
    1 4
    3 4
    1 2
    1 2

    输出示例

    1
    3
    3
    3

    数据规模及约定

    见“输入

    题解

    我们发现“运算之后得到新序列”这一过程只会进行 n 次,事实上,这整个过程就像一个倒过来的完全二叉树,我们不妨将初始序列得到的这颗完全二叉树建出来;以后每改动一个数字即改变了一个叶节点的权值,受到影响需要改动的节点为该叶节点到根节点(最终答案)的路径,于是每次修改时间复杂度也是 O(n) 的。

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <stack>
    #include <vector>
    #include <queue>
    #include <cstring>
    #include <string>
    #include <map>
    #include <set>
    using namespace std;
    
    const int BufferSize = 1 << 16;
    char buffer[BufferSize], *Head, *Tail;
    inline char Getchar() {
        if(Head == Tail) {
            int l = fread(buffer, 1, BufferSize, stdin);
            Tail = (Head = buffer) + l;
        }
        return *Head++;
    }
    int read() {
        int x = 0, f = 1; char c = getchar();
        while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
        while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
        return x * f;
    }
    
    #define maxn 20
    #define maxm 1048576
    int n, q, ToT, A[maxm], fa[maxm];
    
    int main() {
    	n = read(); q = read();
    	for(int i = 0; i < (1 << n); i++) A[i] = read();
    	
    	memset(fa, -1, sizeof(fa));
    	ToT = 1 << n; bool cur = 0;
    	int nl = 0, nr = (1 << n) - 1;
    	for(int i = 1; i <= n; i++, cur ^= 1) {
    		int nnl = -1, nnr = -1;
    		for(int j = nl; j < nr; j += 2) {
    			if(cur) A[ToT++] = A[j] ^ A[j^1], fa[j] = fa[j^1] = ToT - 1;
    			else A[ToT++] = A[j] | A[j^1], fa[j] = fa[j^1] = ToT - 1;
    			if(nnl < 0) nnl = ToT;
    			nnr = ToT;
    		}
    		nl = nnl; nr = nnr;
    	}
    //	printf("%d
    ", ToT);
    	
    	while(q--) {
    		int p = read() - 1; A[p] = read();
    		cur = 0;
    		while(fa[p] >= 0) {
    			if(cur) A[fa[p]] = A[p] ^ A[p^1];
    			else A[fa[p]] = A[p] | A[p^1];
    			p = fa[p]; cur ^= 1;
    		}
    		printf("%d
    ", A[ToT-1]);
    	}
    	
    	return 0;
    }
    
  • 相关阅读:
    jdbc调用存储过程和存储函数
    jdbc测试
    JDBC -JSP
    eclipse连数据库(sql server)的注意事项
    九大内置对象(新)
    application 全局对象
    编码:session
    建库和建表
    db vs dbm
    Source Insight异常退出,错误代码C0000005
  • 原文地址:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/5833913.html
Copyright © 2020-2023  润新知