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 ...
随机推荐
- ACM_Appleman and Card Game(简单贪心)
Appleman and Card Game Time Limit: 2000/1000ms (Java/Others) Problem Description: Appleman has n car ...
- 377 Combination Sum IV 组合之和 IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- cloudera-agent启动File not found : /usr/sbin/cmf-agent解决办法(图文详解)
不多说,直接上干货! 问题详情 bigdata@nssa-sensor1:~$ sudo service cloudera-scm-agent startFile not found : /usr/s ...
- SuperSocket学习笔记(一)-一个完整的例子
一.什么是SuperSocket 以下是作者的介绍 执行以下命令,获取SuperSocket项目 $ git clone https://github.com/kerryjiang/SuperSock ...
- 利用eclipse调试JDK源码
先看效果图 综合网上各种教程,总结如下 新建 D:/jdk/src .D:/jdk/debug 目录 src存放源码 debug存放编译结果 将 %JAVA_HOME%/src.zip 解压到 D:/ ...
- Python学习日记之字典深复制与浅复制
Python中通过copy模块有两种复制(深复制与浅复制) copy 浅复制 复制时只会复制父对象,而不会复制对象的内部的子对象. deepcopy 深复制 复制对象及其子对象 因此,复制后对原dic ...
- HP M177打印机驱动安装问题与解决
问题描述 采购HP Color LaserJet Pro MFP M177 PCLmS一台,采用局域网WIFI网络与办公室各电脑连接.安装打印机自带光盘驱动,发现有些电脑安装驱动失败,提示“出现严重错 ...
- (转)淘淘商城系列——使用solrj来测试索引库
http://blog.csdn.net/yerenyuan_pku/article/details/72892280 我们使用solrj来操作索引库,一般习惯先建一个单元测试类测试下增删改查方法是否 ...
- HDU多校Round 8
Solved:2 rank:141 D. Parentheses Matrix n,m有一个小于6的时候是一种构造方法 答案是n + (m - 2) / 2 (n > m) 都大于6的时候 可以 ...
- SAS,SATA普及文档
目前所能见到的硬盘接口类型主要有IDE.SATA.SCSI.SAS.FC等等. IDE是俗称的并口,SATA是俗称的串口,这两种硬盘是个人电脑和低端服务器常见的硬盘.SCSI是"小型计算机系 ...