JZOJ 7392. 【2021.11.17NOIP提高组联考】数 (ds)
\(\text{Problem}\)
元素带类型与权值,每次修改权值或类型,求区间每种类型和的 \(k\) 次方和
强制在线
\(\text{Solution}\)
显然简单分块,根据询问需要发现要
维护任意两块之间的答案,每种类型的权值在块中的前缀和
询问就很简单,考虑枚举散块出现的类型先去除其在整块中的贡献再加上散块与整块一起的贡献即可
考虑修改,涉及一个元素,权值和的前缀和只要暴力更新这个元素所在块与其后的所有块即可
任意两块间的答案的更新要枚举覆盖它的连续块,整体考虑这个连续块,也只涉及一个点的修改,可以 \(O(1)\) 更新
设块长为 \(d\)
则询问复杂度为 \(O(md)\),修改为 \(O(m\frac{n^2}{d^2})\)
简单计算出当 \(d = n^{\frac 2 3}\) 是最优
还要那类型的动态离散化,哈希表处理即可
本人代码挂了一个测试点,原因未知
\(\text{Code}\)
#include <cstdio>
#include <cmath>
#include <iostream>
#include <cstring>
#include <tr1/unordered_map>
#define re register
using namespace std;
typedef unsigned int uint;
tr1::unordered_map<uint, int> vis;
const int N = 1e5 + 5;
int n, m, k, ty, c[N], st[N], ed[N], id[N], size, num, buc[N], col[N];
uint v[N], f[41][N], sf[N][41], g[41][41], t[N], sum[N];
inline uint fpow(uint x, int y){uint s = 1; for(; y; y >>= 1, x = x * x) if (y & 1) s = s * x; return s;}
inline uint SF(int c, int l, int r){return sf[c][r] - sf[c][l - 1];}
void prepare()
{
memset(f, 0, sizeof f), memset(g, 0, sizeof g), memset(sf, 0, sizeof sf);
num = pow(n, 1.0 / 3);
for(re int i = 1; i <= num; i++)
{
st[i] = ed[i - 1] + 1, ed[i] = (i == num ? n : ed[i - 1] + n / num);
for(re int j = st[i]; j <= ed[i]; j++) id[j] = i, f[i][c[j]] += v[j];
}
for(re int i = 1; i <= size; i++)
for(re int j = 1; j <= num; j++) sf[i][j] = sf[i][j - 1] + f[j][i];
for(re int i = 1; i <= num; i++)
for(re int j = i; j <= num; j++)
{
int cnt = 0;
for(re int l = st[j]; l <= ed[j]; sum[c[l]] += v[l], l++) if (!buc[c[l]]) buc[c[l]] = 1, col[++cnt] = c[l];
if (i == j) for(re int l = 1; l <= cnt; l++) g[i][i] += fpow(sum[col[l]], k), buc[col[l]] = sum[col[l]] = 0;
else{
g[i][j] = g[i][j - 1];
for(re int l = 1; l <= cnt; l++)
g[i][j] = g[i][j] - fpow(SF(col[l], i, j - 1), k) + fpow(sum[col[l]] + SF(col[l], i, j - 1), k), buc[col[l]] = sum[col[l]] = 0;
}
}
}
inline uint Query(int l, int r)
{
int x = id[l], y = id[r], cnt = 0; uint res = 0;
if (x == y || n <= 5000)
{
for(re int i = l; i <= r; sum[c[i]] += v[i], i++) if (!buc[c[i]]) buc[c[i]] = 1, col[++cnt] = c[i];
for(re int i = 1; i <= cnt; i++) res += fpow(sum[col[i]], k), sum[col[i]] = buc[col[i]] = 0;
return res;
}
for(re int i = l; i <= ed[x]; sum[c[i]] += v[i], i++) if (!buc[c[i]]) buc[c[i]] = 1, col[++cnt] = c[i];
for(re int i = st[y]; i <= r; sum[c[i]] += v[i], i++) if (!buc[c[i]]) buc[c[i]] = 1, col[++cnt] = c[i];
res = g[x + 1][y - 1];
for(re int i = 1; i <= cnt; i++)
res = res - fpow(SF(col[i], x + 1, y - 1), k) + fpow(sum[col[i]] + SF(col[i], x + 1, y - 1), k), buc[col[i]] = sum[col[i]] = 0;
return res;
}
inline void modify1(int x, uint y)
{
int p = id[x];
f[p][c[x]] += y - v[x];
for(re int i = 1; i <= p; i++)
for(re int j = p; j <= num; j++) g[i][j] = g[i][j] - fpow(SF(c[x], i, j), k) + fpow(SF(c[x], i, j) - v[x] + y, k);
for(re int i = p; i <= num; i++) sf[c[x]][i] = sf[c[x]][i - 1] + f[i][c[x]]; v[x] = y;
}
inline void modify2(int x, uint y)
{
int p = id[x]; uint cl = (vis[y] ? vis[y] : (vis[y] = ++size));
for(re int i = 1; i <= p; i++)
for(re int j = p; j <= num; j++)
g[i][j] = g[i][j] - fpow(SF(c[x], i, j), k) + fpow(SF(c[x], i, j) - v[x], k) - fpow(SF(cl, i, j), k) + fpow(SF(cl, i, j) + v[x], k);
f[p][c[x]] -= v[x], f[p][cl] += v[x];
for(re int i = p; i <= num; i++) sf[c[x]][i] = sf[c[x]][i - 1] + f[i][c[x]], sf[cl][i] = sf[cl][i - 1] + f[i][cl];
c[x] = cl;
}
int main()
{
freopen("ds.in", "r", stdin), freopen("ds.out", "w", stdout);
scanf("%d%d%d%d", &n, &m, &k, &ty);
for(re int i = 1; i <= n; i++) scanf("%u", &v[i]);
for(re int i = 1; i <= n; c[i] = vis[t[i]], i++) scanf("%u", &t[i]), vis[t[i]] = (vis[t[i]] ? vis[t[i]] : ++size);
prepare(); char op[5]; uint x, y, lst = 0;
for(; m; --m)
{
scanf("%s%u%u", op, &x, &y);
if (ty) x ^= lst, y ^= lst;
if (op[0] == 'Q') printf("%u\n", lst = Query(x, y));
else if (op[0] == 'C') modify1(x, y);
else modify2(x, y);
}
}
JZOJ 7392. 【2021.11.17NOIP提高组联考】数 (ds)的更多相关文章
- JZOJ2020年8月11日提高组T4 景点中心
JZOJ2020年8月11日提高组T4 景点中心 题目 Description 话说宁波市的中小学生在镇海中学参加计算机程序设计比赛,比赛之余,他们在镇海中学的各个景点参观.镇海中学共有n个景点,每个 ...
- JZOJ2020年8月11日提高组T3 页
JZOJ2020年8月11日提高组T3 页 题目 Description 战神阿瑞斯听说2008年在中华大地上,将举行一届规模盛大的奥林匹克运动会,心中顿觉异常兴奋,他想让天马在广阔的天空上,举行一场 ...
- JZOJ2020年8月11日提高组T2 宝石
JZOJ2020年8月11日提高组T2 宝石 题目 Description 见上帝动了恻隐之心,天后也想显示一下慈悲之怀,随即从口袋中取出一块魔术方巾,让身边的美神维纳斯拿到后堂的屏风上去试试,屏风是 ...
- JZOJ2020年8月11日提高组T1 密码
JZOJ2020年8月11日提高组T1 密码 题目 Description 在浩浩茫茫的苍穹深处,住着上帝和他的神仆们,他们闲谈着下界的凡人俗事,对人世间表现的聪明智慧,大加赞赏.今天他们正在观赏大地 ...
- JZOJ2020年8月11日提高组反思
JZOJ2020年8月11日提高组反思 T1 看到题 啊这?! 我看错了吗??? 我理解错题了吗?? 好吧没有-- 高精度模板题,不用多说 T2 看到这种矩阵的问题 以为是前缀和搞事情 结果呢 扫描线 ...
- 【2020.11.28提高组模拟】T1染色(color)
[2020.11.28提高组模拟]T1染色(color) 题目 题目描述 给定 \(n\),你现在需要给整数 \(1\) 到 \(n\) 进行染色,使得对于所有的 \(1\leq i<j\leq ...
- 【2020.11.28提高组模拟】T2 序列(array)
序列(array) 题目描述 给定一个长为 \(m\) 的序列 \(a\). 有一个长为 \(m\) 的序列 \(b\),需满足 \(0\leq b_i \leq n\),\(\sum_{i=1}^ ...
- 11.5NOIP2018提高组模拟题
书信(letter) Description 有 n 个小朋友, 编号为 1 到 n, 他们每人写了一封信, 放到了一个信箱里, 接下来每个人从中抽取一封书信. 显然, 这样一共有 n!种拿到书信的情 ...
- 求hack or 证明(【JZOJ 4923】 【NOIP2017提高组模拟12.17】巧克力狂欢)
前言 本人在此题有一种不是题解的方法,但无法证明也找不到反例. 如果各位大神有反例或证明请发至 邮箱:qq1350742779@163.com Description Alice和Bob有一棵树(无根 ...
- 【2020.11.30提高组模拟】剪辣椒(chilli)
剪辣椒(chilli) 题目描述 在花园里劳累了一上午之后,你决定用自己种的干辣椒奖励自己. 你有n个辣椒,这些辣椒用n-1条绳子连接在一起,任意两个辣椒通过用若干个绳子相连,即形成一棵树. 你决定分 ...
随机推荐
- 关于pip3 ImportError: cannot import name 'main'的报错的原因及解决办法
这个问题的出现大多数都是因为你用错误的方法去升级pip3导致的 先来说一下正确的升级方法: python3 -m pip install --upgrade pip 我发现升级后版本变为了 19.x, ...
- BFS和DFS学习笔记
1 算法介绍 1.1 BFS Breadth First Search(广度优先搜索),将相邻的节点一层层查找,找到最多的 以上图为例,首先确定一个根节点,然后依次在剩下的节点中找已找出的节点的相邻节 ...
- DHorse的链路追踪
目前,DHorse的链路追踪功能是通过SkyWalking来实现.实现原理是DHorse在部署应用时,通过指定SkyWalking的Agent来收集服务的调用链路信息.下面就来具体看一下DHorse如 ...
- ORM数据库查询优化only与defer(select_related与prefetch_related)
目录 一:数据库查询优化 1.ORM语句特点 2.only 3.defer 4.only与defer区别 5.select_related与prefetch_related 6.select_rela ...
- TIE: A Framework for Embedding-based Incremental Temporal Knowledge Graph Completion 增量时序知识图谱补全论文解读
论文网址:https://dl.acm.org/doi/10.1145/3404835.3462961 论文提出一种用增量学习思想做时序知识图谱补全(Temporal Knowledge Graph ...
- 【深入浅出SpringCloud原理及实战】「SpringCloud-Alibaba系列」微服务模式搭建系统基础架构实战指南及版本规划踩坑分析
Spring Cloud Alibaba Nacos Discovery Spring Boot 应用程序在服务注册与发现方面提供和 Nacos 的无缝集成. 通过一些简单的注解,您可以快速来注册一个 ...
- Base58算法加密解密(Python实现)
def b58encode(tmp:str) -> str: tmp = list(map(ord,tmp)) temp = tmp[0] base58 = "123456789ABC ...
- [OpenCV实战]45 基于OpenCV实现图像哈希算法
目前有许多算法来衡量两幅图像的相似性,本文主要介绍在工程领域最常用的图像相似性算法评价算法:图像哈希算法(img hash).图像哈希算法通过获取图像的哈希值并比较两幅图像的哈希值的汉明距离来衡量两幅 ...
- [深度学习] 经典深度学习模型及其微调(Caffe)总结
目录 经典模型 Caffe预训练模型 经典模型 LeNet https://blog.csdn.net/kaido0/article/details/53161684 AlexNet https:// ...
- react 高效高质量搭建后台系统 系列 —— 请求数据
其他章节请看: react 高效高质量搭建后台系统 系列 请求数据 后续要做登录模块(主页),需要先和后端约定JSON数据格式,将 axios 进行封装,实现本地的数据模拟 mockjs. Tip:s ...