noip模拟赛 街灯
分析:对于前30%的数据直接暴力模拟即可,对于另外30%的数据,因为每次的p是一样的,所以可以用莫队来维护,先离散化一下,再用一个桶统计次数.
100%的做法和之前做过的一道模拟赛题很像,当模数很小的时候分块,否则就暴力修改.其实看到区间操作第一感觉是线段树,但是线段树并不能维护这个,分块维护的信息多一些,所以分块.在模数较小的时候记录一下第i个块,模p等于v的有多少个,即g[i][p][v],利用前缀和统计1~i个块的个数.在模数较大的时候因为只有v,v+p,v+2p对答案有影响,所以记录第i个块值为v的有多少个,即f[i][v],同样也可以用前缀和处理一下.查询的时候还是分模数的大小来进行,先统计完整包含在块里面的,在暴力统计在块外面的就可以了.
当有些信息线段树维护不了的时候可以考虑一下分块,分块的时候可以根据范围来决定什么时候分块,什么时候用其它的方法,前缀和可以加速区间查询操作.
60分暴力:
#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; int n, m, a[], b[], cnt, tot, c[], sizee, L = , R = , tong[], ans[];
bool flag = true; struct node
{
int l, r, p, v,id;
}e[]; bool cmp(node a, node b)
{
if (a.l / sizee == b.l / sizee)
return a.r < b.r;
return a.l < b.l;
} void add(int x)
{
tong[a[x]]++;
} void del(int x)
{
tong[a[x]]--;
} int main()
{
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++)
scanf("%d", &a[i]);
cnt = ;
for (int i = ; i <= m; i++)
{
scanf("%d%d%d%d", &e[i].l, &e[i].r, &e[i].p, &e[i].v);
e[i].id = i;
if (i != && e[i].p != e[i - ].p)
flag = false;
b[++cnt] = e[i].v;
}
if (flag)
{
for (int i = ; i <= n; i++)
{
a[i] %= e[].p;
b[++cnt] = a[i];
}
memcpy(c, b, sizeof(b));
sort(c + , c + + cnt);
tot = unique(c + , c + + cnt) - c - ;
for (int i = ; i <= n; i++)
a[i] = lower_bound(c + , c + + tot, a[i]) - c - ;
for (int i = ; i <= m; i++)
e[i].v = lower_bound(c + , c + + tot, e[i].v) - c - ;
sizee = (int)sqrt(n);
sort(e + , e + + m, cmp);
for (int i = ; i <= m; i++)
{
int l = e[i].l, r = e[i].r;
while (R < r)
add(++R);
while (R > r)
del(R--);
while (L > l)
add(--L);
while (L < l)
del(L++);
ans[e[i].id] = tong[e[i].v];
}
for (int i = ; i <= m; i++)
printf("%d\n", ans[i]);
}
else
{
for (int i = ; i <= m; i++)
{
int cnt = ;
for (int j = e[i].l; j <= e[i].r; j++)
if (a[j] % e[i].p == e[i].v)
cnt++;
printf("%d\n", cnt);
}
} return ;
}
100分正解:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; int n, m, a[], maxn, block, cnt, maxx, l[], r[], ans;
int f[][], g[][][], kuai[]; int main()
{
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++)
{
scanf("%d", &a[i]);
maxn = max(maxn, a[i]);
}
block = ;
cnt = (n - ) / block + ;
maxx = ;
for (int i = ; i <= cnt; i++)
{
l[i] = (i - ) * block + , r[i] = min(n, i * block);
for (int j = ; j <= maxn; j++)
f[i][j] = f[i - ][j];
for (int j = ; j <= maxx; j++)
for (int k = ; k < maxx; k++)
g[i][j][k] = g[i - ][j][k];
for (int j = l[i]; j <= r[i]; j++)
{
kuai[j] = i;
f[i][a[j]]++;
for (int k = ; k <= maxx; k++)
g[i][k][a[j] % k]++;
}
}
for (int i = ; i <= m; i++)
{
int ll, rr, p, v;
scanf("%d%d%d%d", &ll, &rr, &p, &v);
ans = ;
v %= p;
int L = kuai[ll], R = kuai[rr];
if (L < R)
{
if (p <= )
ans += g[R - ][p][v] - g[L][p][v];
else
for (int i = v; i <= maxn; i += p)
ans += f[R - ][i] - f[L][i];
for (int j = ll; j <= r[L]; j++)
if (a[j] % p == v)
ans++;
for (int j = l[R]; j <= rr; j++)
if (a[j] % p == v)
ans++;
}
else
for (int j = ll; j <= rr; j++)
if (a[j] % p == v)
ans++;
printf("%d\n", ans);
} return ;
}
noip模拟赛 街灯的更多相关文章
- NOIP模拟赛20161022
NOIP模拟赛2016-10-22 题目名 东风谷早苗 西行寺幽幽子 琪露诺 上白泽慧音 源文件 robot.cpp/c/pas spring.cpp/c/pas iceroad.cpp/c/pas ...
- contesthunter暑假NOIP模拟赛第一场题解
contesthunter暑假NOIP模拟赛#1题解: 第一题:杯具大派送 水题.枚举A,B的公约数即可. #include <algorithm> #include <cmath& ...
- NOIP模拟赛 by hzwer
2015年10月04日NOIP模拟赛 by hzwer (这是小奇=> 小奇挖矿2(mining) [题目背景] 小奇飞船的钻头开启了无限耐久+精准采集模式!这次它要将原矿运到泛光之源的矿 ...
- 大家AK杯 灰天飞雁NOIP模拟赛题解/数据/标程
数据 http://files.cnblogs.com/htfy/data.zip 简要题解 桌球碰撞 纯模拟,注意一开始就在袋口和v=0的情况.v和坐标可以是小数.为保险起见最好用extended/ ...
- 队爷的讲学计划 CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的讲学计划 题解:刚开始理解题意理解了好半天,然后发 ...
- 队爷的Au Plan CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的Au%20Plan 题解:看了题之后觉得肯定是DP ...
- 队爷的新书 CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的新书 题解:看到这题就想到了 poetize 的封 ...
- CH Round #58 - OrzCC杯noip模拟赛day2
A:颜色问题 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2358%20-%20OrzCC杯noip模拟赛day2/颜色问题 题解:算一下每个仆人到它的目的地 ...
- CH Round #52 - Thinking Bear #1 (NOIP模拟赛)
A.拆地毯 题目:http://www.contesthunter.org/contest/CH%20Round%20%2352%20-%20Thinking%20Bear%20%231%20(NOI ...
随机推荐
- boxworld开发日记2019-6-8
打算做一个类似RimWorld的游戏,这里记录一下历程.首先,简单回顾一下. 2018年12月23日 场景管理,打算使用四叉树,后来发现四叉树在空间组织和内存占用方面并不占优势,之后计划使用地图分块 ...
- spring cloud config搭建说明例子(四)-补充配置文件
服务端 ConfigServer pom.xml <dependency> <groupId>org.springframework.cloud</groupId> ...
- Androidstudio的安装与使用调试
1安装与基本使用 1.1androidstudio的安装 1.到android-studio\bin文件夹里面,根据自己的电脑配置,打开studio.exe或者studio64.exe 2.按照向导默 ...
- 【BZOJ4009_洛谷3242】[HNOI2015] 接水果(整体二分)
题目: 洛谷 3242 分析: 明确题意:在一棵树上给定若干权值为 \(w\) 的路径 \((u,v)\) (盘子),每次给定 \((a,b)\) (水果),询问所有满足 \((u,v)\) 被 \( ...
- 离散化+BFS HDOJ 4444 Walk
题目传送门 /* 题意:问一个点到另一个点的最少转向次数. 坐标离散化+BFS:因为数据很大,先对坐标离散化后,三维(有方向的)BFS 关键理解坐标离散化,BFS部分可参考HDOJ_1728 */ # ...
- Python安装第三方包(setup.py)
在github上下载了records文件到本地. 解压文件 cmd切换到文件setup.py的目录下 先执行 python setup.py build 再执行python setup.py inst ...
- 【转】mysql中select用法
转自:http://blog.sina.com.cn/s/blog_a74f39a201013c3b.html 1.选择所有的记录 select * from table_name; 其中*表示表中的 ...
- Xml学习笔记(2)
不同的xml文档构可能要用到不同的方法进行解析这里用到的是例如<student name="张三" id="1" sex="男"/&g ...
- reduce的特殊用法
//计算数组中每个元素出现的次数var arr = ["apple","orange","apple","orange" ...
- JProfiler 9.1.1部署及使用
软件准备: 官网下载地址:http://www.ej-technologies.com/download/jprofiler/files 软件部署: windows安装双击即可. 注册号: L-Lar ...