题目链接

传送门

题意

给你\(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. GitHub for mobile 来了,码农苦逼了!

    北京时间 2019 年 11 月 14 日 GitHub Universe 2019 大会上,GitHub 正式发布了 GitHub for mobile,即 GitHub 的移动版本,支持 iOS ...

  2. 微信小程序云开发不完全指北

    微信小程序云开发不完全指北 首先必须说明云开发的"云"并不是类似云玩家里的云的意思,而是微信小程序真的提供了云开发的接口以及一个简单的提供存储.数据库服务的虚拟后台(对于一些轻量小 ...

  3. 模型区分度衡量指标-KS值

    1.KS值--学习器将正例和反例分开的能力,确定最好的“截断点” KS曲线和ROC曲线都用到了TPR,FPR.KS曲线是把TPR和FPR都作为纵坐标,而样本数作为横坐标.但是AUC只评价了模型的整体训 ...

  4. nginx负载均衡+keepalived高可用

    nginx负载均衡+keepalived高可用 环境准备 192.168.88.111:nginx + keepalived   MASTER 192.168.88.112:nginx + keepa ...

  5. Kafka支持单集群20万分区

    Kafka支持单集群20万分区 之前网上关于确定Kafka分区数的博客多多少少都源自于饶军大神的文章,如今他带来了这方面的第二篇文章,特此翻译一下,记录一下其中的要点. 原贴地址: https://w ...

  6. Linux内核klist链表分析

    1.前言 在Linux内核的源码中,除了简洁的list链表外,内核还有klist链表,它是list链表的线程安全版本,在结构体中提供了整个链表的自旋锁,对链表节点查找.插入和删除等操作,都需要先获得这 ...

  7. 常用Java API之Scanner:功能与使用方法

    Scanner 常用Java API之Scanner:功能与使用方法 Scanner类的功能:可以实现键盘输入数据到程序当中. 引用类型的一般使用步骤:(Scanner是引用类型的) 1.导包 imp ...

  8. C# vb .NET读取识别条形码线性条码ean-8

    ean-8是比较常见的条形码编码规则类型的一种.如何在C#,vb等.NET平台语言里实现快速准确读取该类型条形码呢?答案是使用SharpBarcode! SharpBarcode是C#快速高效.准确的 ...

  9. The underlying connection was closed: An unexpected error occurred on a receive

    解决方法 webRequest.KeepAlive = false; ServicePointManager.ServerCertificateValidationCallback += (s, ce ...

  10. 灰度共生矩阵(Gray-level Co-occurrence Matrix,GLCM),矩阵的特征量

    又叫做灰度共现矩阵 Prerequisites 概念 计算方式 对于精度要求高且纹理细密的纹理分布,我们取像素间距为d=1d=1,以下是方向的说明: 我们来看,matlab内置工具箱中的灰度共生矩阵的 ...