题目链接

传送门

题意

给你\(n\)堆石子,每堆有\(a_i\)堆石子,\(q\)次操作:

  • 在\([L,R]\)内有多少个子区间使得\(Alice\)(先手)在\(Nim\)博弈中获胜;
  • 交换\(a_{pos},a_{pos+1}\)的值。

思路

这题和cf617E差不多。

首先我们知道以下性质:

  • \(Nim\)博弈只有当所有石子数异或为\(0\)才会导致先手必败;
  • 在预处理前缀异或和后,交换相邻两堆石子的石子数只会影响\(pos\)处的值。

因此我们在预处理出前缀异或和后就可以用待修改莫队来解决本题,我们用\(cnt\)数组来处理出区间内\(x\)出现次数,\(sum\)表示区间内有多少个子区间异或和为\(0\),那么最后答案为\(\frac{(R-L+1)(R-L)}{2}-sum\)。

代码实现如下

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL; #define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("D://Code//in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0) const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 1e5 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL; int n, q, op, l, r, block;
LL sum;
int a[maxn], b[maxn];
LL cnt[1<<20+5]; struct que {
int l, r, id, t;
LL ans;
bool operator < (const que& x) const {
if((l - 1) / block != (x.l - 1) / block) {
return l < x.l;
}
if((r - 1) / block != (x.r - 1) / block) {
return r < x.r;
}
return t < x.t;
}
}ask[maxn]; struct modify {
int pre, val, pos;
}mfy[maxn]; void del(int x) {
--cnt[x];
sum -= cnt[x];
} void add(int x) {
sum += cnt[x];
++cnt[x];
} int main() {
#ifndef ONLINE_JUDGE
FIN;
#endif // ONLINE_JUDGE
while(~scanf("%d%d", &n, &q)) {
sum = 0;
for(int i = 0; i <= 1024 * 1024; ++i) cnt[i] = 0;
for(int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
b[i] = b[i-1] ^ a[i];
}
block = (int)pow(n, 2.0 / 3);
int nw = 0, idq = 0, idc = 0;
for(int i = 1; i <= q; ++i) {
scanf("%d", &op);
if(op == 1) {
idq++;
scanf("%d%d", &ask[idq].l, &ask[idq].r);
ask[idq].id = idq;
--ask[idq].l;
ask[idq].t = nw;
} else {
idc++;
++nw;
scanf("%d", &mfy[idc].pos);
int pos = mfy[idc].pos;
mfy[idc].pre = b[pos];
mfy[idc].val = b[pos+1] ^ a[pos];
b[pos] = b[pos+1] ^ a[pos];
swap(a[pos], a[pos+1]);
}
}
sort(ask + 1, ask + idq + 1);
int tmp = nw;
for(int i = 1, l = 1, r = 0; i <= idq; ++i) {
while (r < ask[i].r) add(b[++r]);
while (l > ask[i].l) add(b[--l]);
while (r > ask[i].r) del(b[r--]);
while (l < ask[i].l) del(b[l++]);
while (tmp < ask[i].t) {
tmp++;
if (mfy[tmp].pos >= ask[i].l && mfy[tmp].pos <= ask[i].r) {
del(mfy[tmp].pre);
add(mfy[tmp].val);
}
b[mfy[tmp].pos] = mfy[tmp].val;
}
while (tmp > ask[i].t) {
if (mfy[tmp].pos >= ask[i].l && mfy[tmp].pos <= ask[i].r) {
del(mfy[tmp].val);
add(mfy[tmp].pre);
}
b[mfy[tmp].pos] = mfy[tmp].pre;
tmp--;
}
ask[ask[i].id].ans = 1LL * (ask[i].r - ask[i].l) * (ask[i].r - ask[i].l + 1) / 2 - sum;
}
for(int i = 1; i <= idq; ++i) {
printf("%lld\n", ask[i].ans);
}
}
return 0;
}

2019年杭电多校第三场 1008题Game(HDU6610+带修改莫队+Nim博弈)的更多相关文章

  1. 2019年杭电多校第三场 1011题Squrirrel(HDU6613+树DP)

    题目链接 传送门 题意 给你一棵无根树,要你寻找一个根节点使得在将一条边权变为\(0\)后,离树根最远的点到根节点的距离最小. 思路 本题和求树的直径很像,不过要记得的东西有点多,且状态也很多. \( ...

  2. [2019杭电多校第三场][hdu6609]Find the answer(线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6609 大致题意是求出每个位置i最小需要将几个位置j变为0(j<i),使得$\sum_{j=1}^ ...

  3. [2019杭电多校第三场][hdu6608]Fansblog

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6608 大致题意是比p小的最大素数q,求q!%p的值. 由威尔逊定理开始推: $(p-1)!\equiv ...

  4. [2019杭电多校第三场][hdu6606]Distribution of books(线段树&&dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6606 题意为在n个数中选m(自选)个数,然后把m个数分成k块,使得每块数字之和最大的最小. 求数字和最 ...

  5. 2019杭电多校第三场hdu6608 Fansblog(威尔逊定理)

    Fansblog 题目传送门 解题思路 Q! % P = (P-1)!/(P-1)...(Q-1) % P. 因为P是质数,根据威尔逊定理,(P-1)!%P=P-1.所以答案就是(P-1)((P-1) ...

  6. 2019杭电多校第三场hdu6609 Find the answer(线段树)

    Find the answer 题目传送门 解题思路 要想变0的个数最少,显然是优先把大的变成0.所以离散化,建立一颗权值线段树,维护区间和与区间元素数量,假设至少减去k才能满足条件,查询大于等于k的 ...

  7. 2019杭电多校第三场hdu6606 Distribution of books(二分答案+dp+权值线段树)

    Distribution of books 题目传送门 解题思路 求最大值的最小值,可以想到用二分答案. 对于二分出的每个mid,要找到是否存在前缀可以份为小于等于mid的k份.先求出这n个数的前缀和 ...

  8. 2018 Multi-University Training Contest 3 杭电多校第三场

    躺了几天 终于记得来填坑了 1001 Ascending Rating   (hdoj 6319) 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6319 ...

  9. 杭电多校第三场 A Ascending Rating

    Problem Description Before the start of contest, there are n ICPC contestants waiting in a long queu ...

随机推荐

  1. Spring Boot 2.2.2 发布,新增 2 个新特性!

    Spring Boot 2.2.2 发布咯! Spring Boot 2.2.1 发布,一个有点坑的版本! 2.2.1 发布没过一个月,2.2.2 就来了. Maven依赖给大家奉上: <dep ...

  2. 华为鸿蒙OS发布!方舟支持混合编译,终将可替换安卓?

    前言 有关于鸿蒙的消息之前也有说过,就在昨天下午,华为举行了2019开发大会,正式推出了鸿蒙os系统(Harmony).其相关负责人表示,也是基于微软内核的全场景分布式OS   鸿蒙凭借微内核的优势, ...

  3. 【技术博客】利用Python将markdown文档转为html文档

    利用Python将markdown文档转为html文档 v1.0 作者:FZK 元素简单的md文件 Python中自带有一个markdown库,你可以直接这样使用 md_file = open(&qu ...

  4. VUE:页面跳转时传递参数,及参数获取

    https://www.cnblogs.com/zhongchao666/p/9679807.html https://blog.csdn.net/mf_717714/article/details/ ...

  5. Excel VBA业余爱好者心得及总结

    我不是IT专业人士,而是一位医务工作者,当初学习Excel VBA时,是因为读研究生时的课题需要:实验仪器每天产生4个Word文件,每个文件有9个表格,总计近百个数据.为了对这些数据进行统计分析,则需 ...

  6. ps 简单使用 ----- 将图片静态图片制作成动图

    1. 准备好你需要静态图片  放到文件夹中 2.打开ps 软件    将右上角切换为   动感 3.观察下方时间轴   点击加号   将图片导入 按住shift  选择图片 4.切换为帧格式 选择循环 ...

  7. SpringBoot扩展点之三:SpringBootServletInitializer扩展

    SpringBootServletInitializer 熟悉了SpringApplication的原理之后,我们再来了解SpringBootServletInitializer的原理就比较容易了. ...

  8. 乘法器——基于Wallace树的4位乘法器实现

    博主最近在学习加法器乘法等等相关知识,在学习乘法器booth编码加Wallace树压缩时,发现在压缩部分积的时候用到了进位保留加法器(Carry Save Adder),博主对这种加法器不是很理解,而 ...

  9. Android开发DDMS找不到Emulator Control的方法

    1.右键DDMS,点击reset. 2.window->show view->other->android->Emulator Control

  10. session中删除数组中的某一个值 - 购物车例子 - jsp

    这篇随笔简单的讲一下在session中移除数组中的某一项内容,比如这里有一个购物车其中有两件商品,需要移除其中洗发水这一件商品. 其实在这个session对象中存储了一个数组,在订购页面时选择商品加入 ...