牛客网暑期ACM多校训练营(第九场)H. Prefix Sum(CDQ分治)
题目描述
Niuniu has learned prefix sum and he found an interesting about prefix sum.
Let's consider (k+1) arrays a[i] (0 <= i <= k)
The index of a[i] starts from 1.
a[i] is always the prefix sum of a[i-1].
"always" means a[i] will change when a[i-1] changes.
"prefix sum" means a[i][1] = a[i-1][1] and a[i][j] = a[i][j-1] + a[i-1][j] (j >= 2)
Initially, all elements in a[0] are 0.
There are two kinds of operations, which are modify and query.
For a modify operation, two integers x, y are given, and it means a[0][x] += y.
For a query operation, one integer x is given, and it means querying a[k][x].
As the result might be very large, you should output the result mod 1000000007.
输入描述:
The first line contains three integers, n, m, k. n is the length of each array. m is the number of operations. k is the number of prefix sum.In the following m lines, each line contains an operation. If the first number is 0, then this is a change operation. There will be two integers x, y after 0, which means a[0][x] += y; If the first number is 1, then this is a query operation. There will be one integer x after 1, which means querying a[k][x].1 <= n <= 100000 1 <= m <= 100000 1 <= k <= 40 1 <= x <= n 0 <= y < 1000000007
输出描述:
For each query, you should output an integer, which is the result.
输入
4 11 3 0 1 1 0 3 1 1 1 1 2 1 3 1 4 0 3 1 1 1 1 2 1 3 1 4
输出
1 3 7 13 1 3 8 16
题意:给你一个(k+1)*n的矩阵,初始全为0,m个操作
操作①0 x y表示将a[0][x] += y,并且计算∀(i∈[1,k],j∈[1,n]),a[i][j] = a[i-1][j]+a[i][j-1]
操作②1 x 表示询问a[k][x]的值
很容易算出:a[0][x]对a[k][y]的贡献为,因为k的范围很小所以可以O(1)预处理所有组合数
用上面的结论,直接暴力每个操作对后面所有询问的贡献,复杂度是O(n*m)的,肯定会超时
对于这种每次操作对后面询问产生特定贡献的题,考虑对询问时间进行CDQ分治
步骤如下:
- 按照操作的时间进行排序(其实根本不用排,因为本来就有序,输入顺序就是了)
- 对操作进行二分,假设当前二分区间为[L, R],一个很显然的结论是对于[L, m]的所有修改操作,一定对(m, R]区间中的询问操作有效
- 所以对于每个二分区间[L, R],只需要计算[L, m]中的所有修改操作对(m, R]区间中所有询问操作的贡献即可
- 如果区间长度len≤2000,直接暴力所有操作对所有询问的贡献,复杂度O(len²)
- 如果区间长度len>2000,将所有操作全部加到矩阵中,然后暴力计算出矩阵所有位置的值,最后对于所有询问直接查矩阵对应位置(这里可以优化:不用暴力整个矩阵的值,只需要用组合数计算最后一排的值即可)复杂度O(nk)或O(n)
- 分析下整体复杂度:len>2000的递归次数最坏情况下(n=m=100000)约为15次,所以这部分复杂度为O(15n), len<2000的递归次数约为m次,每次复杂度均摊log²(m)次,所以这部分复杂度O(mlogm),整体复杂度O(15n+mlog²m)
- 搞定!
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<string>
#include<math.h>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
#define LL long long
#define mod 1000000007
typedef struct Res
{LL x, y;int t, op;
}Res;
Res s[200005], x1[200005], x2[200005];
int n, m, k;
LL ans[200005], p[45][200005], C[200005][45];
void CDQ(int L, int R)
{int M, i, j, a, b;M = (L+R)/2;if(L>=R)return;if(M-L+1>=1000){for(i=0;i<=n;i++)p[0][i] = 0;for(i=L;i<=M;i++){if(s[i].op==1)p[0][s[i].x] = (p[0][s[i].x]+s[i].y)%mod;}for(i=1;i<=k+1;i++){for(j=1;j<=n;j++)p[i][j] = (p[i-1][j]+p[i][j-1])%mod;}for(i=M+1;i<=R;i++){if(s[i].op==0)ans[s[i].t] = (ans[s[i].t]+p[k+1][s[i].x])%mod;}}else{a = b = 0;for(i=L;i<=M;i++){if(s[i].op==1)x1[++a] = s[i];}for(i=M+1;i<=R;i++){if(s[i].op==0)x2[++b] = s[i];}for(i=1;i<=a;i++){for(j=1;j<=b;j++){if(x2[j].x<x1[i].x)continue;ans[x2[j].t] = (ans[x2[j].t]+x1[i].y*C[k+x2[j].x-x1[i].x][k]%mod)%mod;}}}CDQ(L, M);CDQ(M+1, R);
}
int main(void)
{int i, j;for(i=0;i<=200002;i++)C[i][0] = 1;for(i=1;i<=200002;i++){for(j=1;j<=42;j++)C[i][j] = (C[i-1][j-1]+C[i-1][j])%mod;}memset(ans, -1, sizeof(ans));scanf("%d%d%d", &n, &m, &k);k -= 1;for(i=1;i<=m;i++){s[i].t = i;scanf("%d", &s[i].op);s[i].op ^= 1;if(s[i].op)scanf("%lld%lld", &s[i].x, &s[i].y);else{scanf("%d", &s[i].x);ans[i] = 0;}}CDQ(1, m);for(i=1;i<=m;i++){if(ans[i]!=-1)printf("%lld\n", ans[i]);}return 0;
}
牛客网暑期ACM多校训练营(第九场)H. Prefix Sum(CDQ分治)相关推荐
- 牛客网暑期ACM多校训练营(第二场):J. farm(暴力)
链接:https://www.nowcoder.com/acm/contest/140/J 来源:牛客网 题目描述 White Rabbit has a rectangular farmland of ...
- 2018牛客网暑期ACM多校训练营第二场 D - money(贪心)
题目链接 https://www.nowcoder.com/acm/contest/140#question [题目描述] White Cloud is exercising in the playg ...
- 牛客网暑期ACM多校训练营(第十场)F.Rikka with Line Graph
牛客网暑期ACM多校训练营(第十场)F.Rikka with Line Graph 做法:\(G'\) 中的对应原图两条边(a,b) (c,d)的最短路为: \[ w[a][b] + w[c][d] ...
- 牛客网暑期ACM多校训练营(第九场)
牛客网暑期ACM多校训练营(第九场) A. Circulant Matrix 做法:看到下标 \(xor\) 这种情况就想 \(FWT\),可是半天没思路,于是放弃了..其实这个 \(n\) 疯狂暗示 ...
- 牛客网暑期ACM多校训练营(第五场)
牛客网暑期ACM多校训练营(第五场) A. gpa 二分答案,然后就转化为是否满足 \(\frac {\sum s[i]c[i]}{\sum s[i]} ≥ D\), \(\sum s[i]c[i] ...
- 牛客网暑期ACM多校训练营(第三场)
牛客网暑期ACM多校训练营(第三场) A. PACM Team 01背包,输出方案,用bool存每种状态下用的哪一个物品,卡内存.官方题解上,说用char或者short就行了.还有一种做法是把用的物品 ...
- 牛客网暑期ACM多校训练营(第一场)
牛客网暑期ACM多校训练营(第一场) A. Monotonic Matrix 考虑0和1的分界线,1和2的分界线,发现问题可以转化为两条不互相穿过的路径的方案数(可重叠),题解的做法就是把一条路径斜着 ...
- 牛客网暑期ACM多校训练营(第十场)D Rikka with Prefix Sum
链接:https://www.nowcoder.com/acm/contest/148/D 来源:牛客网 题目描述 Prefix Sum is a useful trick in data struc ...
- 牛客网暑期ACM多校训练营(第三场)A.PACM Team(多重01背包)
链接:https://www.nowcoder.com/acm/contest/141/A 来源:牛客网 题目描述 Eddy was a contestant participating in ACM ...
- 2018牛客网暑期ACM多校训练营(第十场)A Rikka with Lowbit (树状数组)
链接:https://ac.nowcoder.com/acm/contest/148/A 来源:牛客网 Rikka with Lowbit 时间限制:C/C++ 5秒,其他语言10秒 空间限制:C/C ...
最新文章
- Java 基础 之 continue和 break
- 三态门有一个信号控制端en_W25Q32JVSSIQ|哪些PCB设计会影响信号质量?
- java 中文怎么截取,java String 中文 字符串 截取
- MySQL分片 --转自Peter Zaitsev对MySQL分片的建议
- Collection 和 Map接口及其实现类总结
- C++设计模式-外观模式
- 【Clickhouse】Clickhouse 多路径存储策略
- 常用图像处理相关图像数据库
- E-Prime1.1安装教程及软件下载
- Type-c与micro usb 的对应关系
- 生成翻转棋子游戏数据
- 可“一键”轻松激活Office 2010系列“VOL”版本的又一款迷你“KMS服务器”
- 【Python3之面向对象的程序设计】
- 关于英语学习和字幕的那点事儿
- 报表数据源之JSON
- latex数学公式神器Mathpix
- 小米手机安装推特后频繁闪退
- centos8搭建maven私服(含nexus-3.28.1-01.tar.gz下载链接)
- ssh报错:no matching host key type found. Their offer: ssh-rsa
- 天九共享:突破“邓巴数字”桎梏 创造资源能力圈
热门文章
- php和python-python与php比较
- python编程入门经典-Python编程入门经典
- 100.Day12反射机制_qq_38205875的博客
- 密码加密模块 bcrypt (详细,好理解,示例源码)
- linux如何重置网络,Ubuntu 重新设置网络
- opencv物品定位_使用OpenCV获取零件位置的学习笔记
- 深度拷贝 java_java 深度拷贝 复制 深度复制
- opencv的android.mk,android-opencv 版本下JNI Android.mk文件的书写
- zTree节点增删改
- 地理信息考c语言,南师地理信息系统专业01方向真题C语言