洛谷 P3396 哈希冲突

Problem

P3396 哈希冲突

Solution

本文摘自基础数据结构学习笔记

先想两种极端的做法

  • 对于每个询问,暴力计算\(k,k+p,k+2\times p\dots\)的\(value\)之和。
  • 预处理出\(ans[p][k]\)表示在\(\bmod p\)的意义下,余数为\(k\)的\(value\)之和。

第一种方法时间\(\rm O(n^2)\),空间\(\rm O(1)\);第二种方法时间\(\rm O(1)\),空间\(O(n^2)\)。

考虑根号分治

对于\(p\le \sqrt n\),我们预处理,保存在数组\(ans[p][k]\)中。空间\(\rm O(\sqrt n\times\sqrt n)=\rm O(n)\),时间上预处理\(\rm O(n\sqrt n)\),修改\(\rm O(\sqrt n)\),查询\(\rm O(1)\).

对于\(p>\sqrt n\),我们不预处理,每一次询问暴力统计。每次统计的数量不会超过\(\dfrac np\le \sqrt n\).

\(\color{white}我做了那些Ynoi我都白做了啊啊啊,这么简单的一个根号分治我都想不出来……我太菜了……\color{gray}awa\)

Code

/**************************************************************
* Problem: 3396
* Author: Vanilla_chan
* Date: 20210402
* E-Mail: Vanilla_chan@outlook.com
**************************************************************/
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<limits.h>
#define IL inline
#define re register
#define LL long long
#define ULL unsigned long long
#ifdef TH
#define debug printf("Now is %d\n",__LINE__);
#else
#define debug
#endif
#ifdef ONLINE_JUDGE
char buf[1<<23],* p1=buf,* p2=buf,obuf[1<<23],* O=obuf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
#endif
using namespace std; namespace oi
{
inline bool isdigit(const char& ch)
{
return ch<='9'&&ch>='0';
}
inline bool isalnum(const char& ch)
{
return (ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')||(ch>='0'&&ch<='9');
}
struct istream
{
char ch;
bool fu;
template<class T>inline istream& operator>>(T& d)
{
fu=(d=0);
while(!isdigit(ch)&&ch!='-') ch=getchar();
if(ch=='-') fu=1,ch=getchar();
d=ch-'0';ch=getchar();
while(isdigit(ch))
d=(d<<3)+(d<<1)+(ch^'0'),ch=getchar();
if(fu) d=-d;
return *this;
}
inline istream& operator>>(char& ch)
{
ch=getchar();
for(;!isalnum(ch);ch=getchar());
return *this;
}
inline istream& operator>>(string& str)
{
str.clear();
for(;!isalnum(ch);ch=getchar());
while(isalnum(ch))
str+=ch,ch=getchar();
return *this;
}
}cin;
inline int read()
{
int x=0,fu=1;
char ch=getchar();
while(!isdigit(ch)&&ch!='-') ch=getchar();
if(ch=='-') fu=-1,ch=getchar();
x=ch-'0';ch=getchar();
while(isdigit(ch)) { x=x*10+ch-'0';ch=getchar(); }
return x*fu;
}
int G[55];
template<class T>inline void write(T x)
{
int g=0;
if(x<0) x=-x,putchar('-');
do { G[++g]=x%10;x/=10; } while(x);
for(int i=g;i>=1;--i)putchar('0'+G[i]);putchar('\n');
}
};
using namespace oi;
#define N 150010
#define S 500
int n,m;
int val[N];
int sq;
int ans[S][S];
int main()
{
freopen("3396.in","r",stdin);
//freopen(".out","w",stdout);
n=read();
m=read();
sq=sqrt(n);
for(int i=1;i<=n;i++) val[i]=read();
for(int i=1;i<=sq;i++)
{
for(int j=1;j<=n;j++)
{
ans[i][j%i]+=val[j];
}
}
char op;
int x,y;
while(m--)
{
oi::cin>>op>>x>>y;
if(op=='A')
{
if(x<=sq)
{
write(ans[x][y]);
}
else
{
int ans=0;
for(int i=y;i<=n;i+=x) ans+=val[i];
write(ans);
}
}
else
{
for(int i=1;i<=sq;i++)
{
ans[i][x%i]+=y-val[x];
}
val[x]=y;
}
} return 0;
}

洛谷 P3396 哈希冲突的更多相关文章

  1. 洛谷P3396 哈希冲突 (分块)

    洛谷P3396 哈希冲突 题目背景 此题约为NOIP提高组Day2T2难度. 题目描述 众所周知,模数的hash会产生冲突.例如,如果模的数p=7,那么4和11便冲突了. B君对hash冲突很感兴趣. ...

  2. 洛谷 P3396 哈希冲突 解题报告

    P3396 哈希冲突 题目背景 此题约为NOIP提高组Day2T2难度. 题目描述 众所周知,模数的hash会产生冲突.例如,如果模的数p=7,那么4和11便冲突了. B君对hash冲突很感兴趣.他会 ...

  3. 洛谷P3396 哈希冲突

    分块还真是应用广泛啊...... 题意:求 解:以n0.5为界. 当p小于n0.5的时候,直接用p²大小的数组储存答案. 预处理n1.5,修改n0.5. 当p大于n0.5的时候,直接按照定义计算,复杂 ...

  4. 洛谷P3396哈希冲突

    传送门啦 非常神奇的分块大法. 这个题一看数据范围,觉得不小,但是如果我们以 $ \sqrt(x) $ 为界限,数据范围就降到了 $ x < 400 $ 我们设数组 $ f[i][j] $ 表示 ...

  5. 洛谷P3396 哈希冲突(分块)

    传送门 题解在此,讲的蛮清楚的->这里 我就贴个代码 //minamoto #include<iostream> #include<cstdio> #include< ...

  6. P3396 哈希冲突(思维+方块)

    题目 P3396 哈希冲突 做法 预处理模数\([1,\sqrt{n}]\)的内存池,\(O(n\sqrt{n})\) 查询模数在范围里则直接输出,否则模拟\(O(m\sqrt{n})\) 修改则遍历 ...

  7. 题解 洛谷 P3396 【哈希冲突】(根号分治)

    根号分治 前言 本题是一道讲解根号分治思想的论文题(然鹅我并没有找到论文),正 如论文中所说,根号算法--不仅是分块,根号分治利用的思想和分块像 似却又不同,某一篇洛谷日报中说过,分块算法实质上是一种 ...

  8. P3396 哈希冲突

    很好的根号算法(这种思想好像叫根号分治?) 首先,暴力是Ο(n2)的 考虑预处理: for(p=1;p<=n;p++) //枚举模数 ans[p][i%p]+=value[i]; 看似很好但还是 ...

  9. p3396 哈希冲突(暴力)

    想了好久,没想到优秀的解法,结果是个暴力大吃一静.jpg 分类讨论,预处理\(p\le \sqrt{n}\) 的情况,其他直接暴力,复杂度\(O(n \sqrt{n} )\) #include < ...

  10. 【洛谷3950】部落冲突(LCT维护连通性)

    点此看题面 大致题意: 给你一棵树,\(3\)种操作:连一条边,删一条边,询问两点是否联通. \(LCT\)维护连通性 有一道类似的题目:[BZOJ2049][SDOI2008] Cave 洞穴勘测. ...

随机推荐

  1. nuclei安装使用

    go环境安装 go 下载路径:https://golang.google.cn/dl/ 1.双击 go1.20.7.windows-amd64.msi 2.点击下一步 3.我同意,然后下一步. 4.选 ...

  2. 华为云windows server 2008 迁机遇到字符串问题

    问题 使用主机迁移服务迁移windows server 2008出现问题 2.按照教程安装Windows Agent(Python2)下载后,在源主机上运行agent-start.exe,输入ak后, ...

  3. 获取当前电脑屏幕的dpi

    <div id="test" style="width:1in;height:1in;overflow:hidden;"></div> ...

  4. Oracle删除用户及用户下的全部数据

      1.查看用户 select * from all_users select * from user_users select * from dba_users 2.查看用户的连接状况 select ...

  5. linux服务器通过X11实现图形化界面显示 1 背景描述

    有些LINUX服务器出于性能和效率的考虑,通常都是没有安装图形化界面的,那么图形化程序在服务器上压根儿就跑不起来,或者无法直接显示出来,这就很尴尬了!那么如何解决这个问题呢?可以基于X11 Forwa ...

  6. Git提交历史优化指南:两步合并本地Commit,代码审查更高效!

    在开发过程中,频繁的本地Commit可能导致提交历史冗杂,增加代码审查和维护的复杂度.通过合并连续的Commit,不仅能简化历史记录,还能提升代码可读性和团队协作效率,以下是合并两次本地Commit的 ...

  7. Windows 提权-手工枚举

    本文通过 Google 翻译 Manual Enumeration – Windows Privilege Escalation 这篇文章所产生,本人仅是对机器翻译中部分表达别扭的字词进行了校正及个别 ...

  8. Tampermonkey 油猴脚本中文手册(出处:https://www.itblogcn.com/article/2233.html)

    文章目录 @name @namespace @copyright @version @description @icon, @iconURL, @defaulticon @icon64, @icon6 ...

  9. 行为识别TSM训练ucf101数据集

    序言 最近有个行为检测的需求,打算用行为识别做,纯小白入这个方向,啃了两周的TSM原理和源码,训练好自己的数据集后,发现好像没法应用到自己的需求场景??玛德!算了,还是要记录一下.原理就没别要讲了,网 ...

  10. python,获取当前日期且以当前日期为名称创建文件名

    爬虫爬取信息时,需要把爬取的内容存到txt文档中,且爬虫是每天执行,以日期命名能避免出现名称重复等问题,解决方法如下 import time import os import sys path = o ...