博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CodeForces 103D Time to Raid Cowavans 询问分块
阅读量:6878 次
发布时间:2019-06-26

本文共 1639 字,大约阅读时间需要 5 分钟。

  

题意:

询问 下标满足 a + b * k 的和是多少。

题解:

将询问分块。

将b >= blo直接算出答案。

否则存下来。

存下来之后,对于每个b扫一遍数组,然后同时处理相同b的询问。

 

代码:

#include
using namespace std;#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);#define LL long long#define ULL unsigned LL#define fi first#define se second#define pb push_back#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define max3(a,b,c) max(a,max(b,c))#define min3(a,b,c) min(a,min(b,c))#define Show(x) cout << x << ' ';typedef pair
pll;const int INF = 0x3f3f3f3f;const LL mod = (int)1e9+7;const int N = 3e5 + 100;int n, m, k, p;int w[N];LL tot[N];LL ans[N];struct Node{ int a, b, id;}q[N];bool cmp(Node x1, Node x2){ return x1.b < x2.b;}int main(){ scanf("%d", &n); for(int i = 1; i <= n; i++) scanf("%d", &w[i]); scanf("%d", &p); k = sqrt(n); int t = 0, a, b; LL tmp; for(int i = 1; i <= p; i++){ scanf("%d%d", &a, &b); if(b >= k){ tmp = 0; for(int i = a; i <= n; i += b) tmp += w[i]; ans[i] = tmp; } else { q[t].a = a; q[t].b = b; q[t].id = i; t++; } } sort(q,q+t,cmp); for(int i = 0; i < t; i++){ if(i == 0 || q[i].b != q[i-1].b){ b = q[i].b; for(int i = n; i >= 1; i--){ if(i+b > n) tot[i] = w[i]; else tot[i] = tot[i+b] + w[i]; } } ans[q[i].id] = tot[q[i].a]; } for(int i = 1; i <= p; i++){ printf("%I64d\n", ans[i]); } return 0;}
View Code

 

转载于:https://www.cnblogs.com/MingSD/p/10886130.html

你可能感兴趣的文章
Javascript中的原型prototype
查看>>
个人随想:对于一个.Neter来说,如果一直想走技术路线,该怎么走下去
查看>>
深浅拷贝
查看>>
Mysql 解锁处理
查看>>
源码来袭!!!基于jquery的ajax分页插件(demo+源码)
查看>>
JDBC的基本用法
查看>>
二分查找的递归和非递归实现
查看>>
Hadoop基本命令
查看>>
TCP协议与UDP协议的区别
查看>>
 P2152 [SDOI2009]SuperGCD (luogu)
查看>>
8086汇编——作业总结1——ASCII码0~9转为8位二进制输出
查看>>
查询01_DML锁和DDL锁的处理
查看>>
下载(打开)PDF文件 代码
查看>>
effective c++ 思维导图
查看>>
谈一下我们是怎么做数据库单元测试(Database Unit Test)的
查看>>
007-请问你怎么看待软件测试的潜力和挑战
查看>>
SQLite
查看>>
在AndroidManifest(清单文件)中注册activity(活动)及配置主活动、更改App图标、App名称、修改隐藏标题栏...
查看>>
bootloader启动提速之使用ICACHE
查看>>
[翻译] 使用 Python 创建你自己的 Shell:Part I
查看>>