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 ...
随机推荐
- Swift5.1 语言指南(二十九)高级运算符
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- 模拟 HDOJ 5387 Clock
题目传送门 /* 模拟:这题没啥好说的,把指针转成角度处理就行了,有两个注意点:结果化简且在0~180内:小时13点以后和1以后是一样的(24小时) 模拟题伤不起!计算公式在代码内(格式:hh/120 ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛-B-Perfect Numbers(完数)
题目描述 We consider a positive integer perfect, if and only if it is equal to the sum of its positive d ...
- *RelativeLayout的布局参数含义表,如android:layout_alignParentTop等
RelativeLayout 参数规则 一个控件的位置由横,纵两个方向上的距离决定 控件默认的位置在左上角. 单独使用以下属性都只是改变一个方向的相对位置. 如:只使用了android:layout_ ...
- Android 性能优化(20)多核cpu入门:SMP Primer for Android
SMP Primer for Android 1.In this document Theory Memory consistency models Processor consistency CPU ...
- 国际化------international
1.配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=& ...
- 实例化WebService实例对象时出现localhost未引用命名空间
//实例化WebService实例对象 localhost.WebService WebserviceInstance = new localhost.WebService(); 在实例化WebSer ...
- 6.13---example
example如何使用?简单查询这个例子展示了如何用生成后的Example类去生成一个简单的where子句: TestTableExample example = new TestTableExamp ...
- dubbo与springmvc的简单使用
什么是Dubbo? dubbo是阿里巴巴公司开源的高性能优秀服务框架,通过高性能的RPC(远程服务调用)实现服务的输入输出功能,可以与spring框架无缝整合: 传统的架构所有的模块都在一台服务器上, ...
- ValueError: multi-byte encodings are not supported
pyton解析xml时,报错 是因为编码的问题,把xml的头 <?xml version="1.0" encoding="gb2312"?> 改成 ...