The 2019 ICPC China Nanchang National Invitational and International Silk-Road Programming Contest - F.Sequence(打表+线段树)

题意:给你一个长度为$n$的数组,定义函数$f(l,r)=a_{l} \oplus a_{l+1} \oplus...\oplus a_{r}$,$F(l,r)=f(l,l)\oplus f(l,l+1)\oplus ...\oplus f(l,r)\oplus f(l+1,l+1)\oplus ...f(l+1,r)\oplus ...\oplus f(r,r)$,有两种操作,第一种将数组中某个元素$a[x]$变为$y$,第二种计算$F(l,r)$的值。

思路:打表后发现只有当$l$和$r$同时为奇数或者偶数时$F(l,r)$才不为$0$,其他情况下$F(l,r)$都为$0$,并且$F(l,r)=a[l]\oplus a[l+2]\oplus ...\oplus a[r-2]\oplus a[r]$,由于涉及到修改和查询两种操作,所以用线段树来维护,每个结点维护两个值:$w$表示区间内奇数项异或的结果,$ww$表示区间内偶数项异或的结果。

初始建树时

  • 当前项为奇数项,则输入$tree[k].w$的值,同时令$tree[k].ww=0$
  • 当前项为偶数项,则输入$tree[k].ww$的值,同时令$tree[k].w=0$

修改操作时

  • 需要修改的项为奇数项,则对$tree[k].w$进行修改
  • 需要修改的项为偶数项,则对$tree[k].ww$进行修改

查询操作时

  • 如果$l$为奇数,则用$ans=ans\oplus tree[k].w$来更新答案
  • 如果$l$为偶数,则用$ans=ans\oplus tree[k].ww$来更新答案
#include <iostream>
#include <cstdio>

using namespace std;

const int N = 100010;

struct node {
    int l, r, w, ww;
};

int a, b, x, y, ans;
node tree[4 * N];

void build(int k, int lef, int rig)
{
    tree[k].l = lef, tree[k].r = rig;
    if (tree[k].l == tree[k].r) {
        if (0 == tree[k].l % 2) {
            tree[k].w = 0, scanf("%d", &tree[k].ww);
        }
        else {
            tree[k].ww = 0, scanf("%d", &tree[k].w);
        }
        return;
    }
    int mid = (lef + rig) / 2;
    build(k * 2, lef, mid); build(k * 2 + 1, mid + 1, rig);
    tree[k].w = tree[k * 2].w ^ tree[k * 2 + 1].w;
    tree[k].ww = tree[k * 2].ww ^ tree[k * 2 + 1].ww;
}

void change_point(int k)
{
    if (tree[k].l == tree[k].r) {
        if (tree[k].l % 2 == 0) tree[k].ww = y;
        else tree[k].w = y;
        return;
    }
    int mid = (tree[k].l + tree[k].r) / 2;
    if (x <= mid) change_point(2 * k);
    else change_point(2 * k + 1);
    tree[k].w = tree[2 * k].w ^ tree[2 * k + 1].w;
    tree[k].ww = tree[2 * k].ww ^ tree[2 * k + 1].ww;
}

void ask_interval(int k)
{
    if (tree[k].l >= a && tree[k].r <= b) {
        if (0 == a % 2) ans ^= tree[k].ww;
        else ans ^= tree[k].w;
        return;
    }
    int mid = (tree[k].l + tree[k].r) / 2;
    if (a <= mid) ask_interval(2 * k);
    if (b > mid) ask_interval(2 * k + 1);
}

int main()
{
    int T, t, n, q, icas = 0;
    scanf("%d", &T);
    while (T--) {
        printf("Case #%d:\n", ++icas);
        scanf("%d%d", &n, &q), build(1, 1, n);
        while (q--) {
            scanf("%d", &t);
            if (0 == t) {
                scanf("%d%d", &x, &y);
                change_point(1);
            }
            else {
                ans = 0, scanf("%d%d", &a, &b);
                if (a % 2 == b % 2) ask_interval(1);
                printf("%d\n", ans);
            }
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/zzzzzzy/p/11706173.html

时间: 2024-07-29 22:56:40

The 2019 ICPC China Nanchang National Invitational and International Silk-Road Programming Contest - F.Sequence(打表+线段树)的相关文章

The 2019 ICPC China Nanchang National Invitational and International Silk-Road Programming Contest C. Xyjj’s sequence(动态规划+欧拉降幂)

题目链接:https://nanti.jisuanke.com/t/40255 中文题面: 解题思路:先用欧拉降幂求出A,B两个序列,定义dp[0][i][j]为取A的前i个元素,B的前j个元素,且C的最后一个元素为B[j],同理dp[1][i][j]为取A的前i个元素,B的前j个元素,且C的最后一个元素为A[i],那么就很容易得到状态转移方程.那么最后答案即为max(dp[0][n][n],dp[1][n][n]).还有值得注意的是:该题需要使用滚动数组,不然会超内存. 在此贴两个关于欧拉降幂

The 2019 ICPC China Nanchang National Invitational and International Silk-Road Programming Contest E. Interesting Trip 长链剖分

题库链接 考虑莫比乌斯, 套上去之后就是变成了统计长度为d的一共有多少路径, 直接长链剖分, 在计蒜客上极度卡常, 卡了一万年才卡过去, 现场好像还有用点分治过去的, 这都能过?? #include<bits/stdc++.h> #define LL long long using namespace std; const int N = (int)5e5 + 7; const int M = 30000; int n, d, a[N], vis[N], miu[M + 1]; int now

The Preliminary Contest for ICPC China Nanchang National Invitational and International Silk-Road Programming Contest

打网络赛 比赛前的准备工作要做好 确保 c++/java/python的编译器能用 打好模板,放在桌面 A. PERFECT NUMBER PROBLEM 1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <string> 6 #include <algorithm> 7 #include <se

The Preliminary Contest for ICPC China Nanchang National Invitational I题

Alice has a magic array. She suggests that the value of a interval is equal to the sum of the values in the interval, multiplied by the smallest value in the interval. Now she is planning to find the max value of the intervals in her array. Can you h

The Preliminary Contest for ICPC China Nanchang National Invitational I.Max answer单调栈

题面 题意:一个5e5的数组,定义一个区间的值为 这个区间的和*这个区间的最小值,注意数组值有负数有正数,求所有区间中最大的值 题解:如果全是正数,那就是原题 POJ2796 单调栈做一下就ok 我们现在有负数,考虑这段区间,他的和必须是负数,由于导致和为负数,最小值一定也是负数, 那对于这样一个和为负的区间进行扩展的时候,遇见下一个数,是负数,我们一定会扩展,无论这个负数大小 遇见下一个是正数,如果和没有变正,那就可以继续扩展下去(不更新答案罢了) 所以我们对于那些和为负的区间,单独统计一下答

2019 ICPC Malaysia National G(拓扑排序)

2019 ICPC Malaysia National G 有点绕,两层拓扑排序. 有空再补详细. 甚至有点丑,因为绕,为了区分,当时变量名写得很长. #include<cstdio> #include<algorithm> #include<cstring> #include<queue> #include<vector> #define debug printf("!") using namespace std; type

2019 ICPC Malaysia National F(状态压缩)

2019 ICPC Malaysia National F 赛后补题.看了这个题解,说是状态压缩. 以第一行的士兵为主,第二行士兵为次,即,第二行被第一行士兵匹配,更新第一行士兵的状态. 用当前第i个士兵的状态更新第i+1个士兵的状态. f[i][j]:i为士兵的下标,j为第i个士兵的状态.(1<j<(1<<(e*2+1))). 比如e=3,二进制 j=1000011,表示第i个士兵之前包括第i个士兵,在[i-3,i+3]范围内,第二行的士兵已被匹配了下标为i-3,i+2,i+3的

2019 ICPC 南昌网络赛

2019 ICPC 南昌网络赛 比赛时间:2019.9.8 比赛链接:The 2019 Asia Nanchang First Round Online Programming Contest 总结 // 史上排名最高一次,开场不到两小时队友各A一题加水题共四题,排名瞬间升至三四十名 // 然后后三小时就自闭了,一题都没有突破...最后排名211 hhhh ? ? B. Fire-Fighting Hero 题意 队友做的,待补. ? AC代码 #include<cstdio> #includ

《ACM/ICPC 算法训练教程》读书笔记 之 数据结构(线段树详解)

依然延续第一篇读书笔记,这一篇是基于<ACM/ICPC 算法训练教程>上关于线段树的讲解的总结和修改(这本书在线段树这里Error非常多),但是总体来说这本书关于具体算法的讲解和案例都是不错的. 线段树简介 这是一种二叉搜索树,类似于区间树,是一种描述线段的树形数据结构,也是ACMer必学的一种数据结构,主要用于查询对一段数据的处理和存储查询,对时间度的优化也是较为明显的,优化后的时间复杂为O(logN).此外,线段树还可以拓展为点树,ZWK线段树等等,与此类似的还有树状数组等等. 例如:要将