题目

斐波那契数列\(f\),\(f\_1=f\_2=1,\ f\_n=f\_{n-1}+f\_{n-2}\ (n>2)\)。

给定长度为\(n\ (n\le 30000)\)的数列\(a\),\(q\)次询问,每次将\(a\)的区间\([l,r]\)排序去重,设得到\(k\)个数:

\[\begin{align}
ans=\sum _{i=1}^k f_ia_i
\end{align}
\]

分析

会写莫队啦!会写莫队啦!会写莫队啦!

莫队算法的排序以左端点所在块和右端点的大小为关键字。

询问区间,\(n\le 30000)\),可以离线,莫队解决。关键在于如何添加或删除一个数。我们列出式子之后发现,每次操作其实可以等价于两个操作:

  • 添加或删除一个数
  • 把一段区间的数的所有\(f\)向前或向后移

    这里的排序去重令我们想到可以用权值线段树处理一个数前面有多少个数,所以添加删除可以\(O(logn)\)解决。现在考虑如何处理移动问题。

    我们对于权值线段树的每个点记录两个值,\((x,y)\),分别表示当前这个点区间内的答案,以及整个区间向后移动一位后的答案。我在这里的做法是用一个标记记录每个区间向右移动的次数(向左为负),然后\(pushdown\)的时候用矩阵快速幂加一下即可。右移矩阵为:

\[\begin{pmatrix}
0 & 1 \\ 1 & 1 \\
\end{pmatrix}
\]

左移矩阵为右移矩阵的逆矩阵,即:

\[\begin{pmatrix}
-1 & 1 \\ 1 & 0 \\
\end{pmatrix}
\]

时间复杂度:\(O(n\sqrt n logn)\)

这里最重要的思想是用离散化+权值线段树处理排序去重区间操作的问题。

代码

函数记得return返回值。

还有就是每次要把gs清空,也可以用标记实现。

#include<cstdio>
#include<cctype>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
typedef long long giant;
int read() {
int x=0,f=1;
char c=getchar();
for (;!isdigit(c);c=getchar()) if (c=='-') f=-1;
for (;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int maxn=3e4+10;
int n,a[maxn],qn;
giant q;
struct ques {
int s,t,id;
bool operator < (const ques a) const {
int sk=s/qn,ak=a.s/qn;
return sk==ak?t<a.t:sk<ak;
}
} b[maxn];
int gs[maxn],tp,tic[maxn<<2],tim=0,tg[maxn];
giant f[maxn],c[maxn];
struct node {
giant a,b;
int mv,g;
void clear() {
a=b=0,mv=g=0;
}
} t[maxn<<2];
struct matrix {
giant a[3][3];
matrix (int e=0,int f=0,int g=0,int h=0) {
a[1][1]=e,a[1][2]=f,a[2][1]=g,a[2][2]=h;
}
void operator = (matrix x) {
for (int i=1;i<=2;++i) for (int j=1;j<=2;++j) a[i][j]=x.a[i][j];
}
} mt,rmt,the;
matrix operator * (matrix &x,matrix &y) {
the=matrix();
for (int i=1;i<=2;++i) for (int j=1;j<=2;++j) for (int k=1;k<=2;++k) (the.a[i][j]+=x.a[i][k]*y.a[k][j]+q)%=q;
return the;
}
matrix mi(int y) {
rmt=mt,--y;
while (y) {
if (y&1) rmt=rmt*mt;
y>>=1,mt=mt*mt; // here
}
return rmt; // here
}
void mvit(node &k,int c) {
if (c==0) return;
int a=k.a,b=k.b;
if (c>0) mt=matrix(0,1,1,1); else mt=matrix(-1,1,1,0);
c=abs(c);
mt=mi(c);
k.a=(mt.a[1][1]*a+mt.a[2][1]*b+q)%q,k.b=(mt.a[2][1]*a+mt.a[2][2]*b+q)%q;
}
void pushdown(int x) {
if (!t[x].mv) return;
if (t[x<<1].g) mvit(t[x<<1],t[x].mv),t[x<<1].mv+=t[x].mv;
if (t[x<<1|1].g) mvit(t[x<<1|1],t[x].mv),t[x<<1|1].mv+=t[x].mv;
t[x].mv=0;
}
int qnum(int x,int L,int R,int l,int r) {
if (tic[x]!=tim) t[x].clear(),tic[x]=tim;
if (l>r) return 0;
pushdown(x);
if (L==l && R==r) return t[x].g;
int mid=(L+R)>>1;
if (r<=mid) return qnum(x<<1,L,mid,l,r);
if (l>mid) return qnum(x<<1|1,mid+1,R,l,r);
return qnum(x<<1,L,mid,l,mid)+qnum(x<<1|1,mid+1,R,mid+1,r);
}
void update(int x) {
t[x].g=t[x<<1].g+t[x<<1|1].g;
t[x].a=(t[x<<1].a+t[x<<1|1].a)%q;
t[x].b=(t[x<<1].b+t[x<<1|1].b)%q;
}
void add(int x,int l,int r,int p,int wh,int d) {
if (tic[x]!=tim) t[x].clear(),tic[x]=tim;
pushdown(x);
if (l==r) {
t[x].g+=d;
if (d==1) {
t[x].a=(f[wh]*c[p])%q;
t[x].b=(f[wh+1]*c[p])%q;
} else t[x].a=t[x].b=0;
return;
}
int mid=(l+r)>>1;
if (p<=mid) add(x<<1,l,mid,p,wh,d); else add(x<<1|1,mid+1,r,p,wh,d);
update(x);
}
void moveit(int x,int L,int R,int l,int r,int d) {
if (tic[x]!=tim) t[x].clear(),tic[x]=tim;
if (l>r) return;
pushdown(x);
if (L==l && R==r) {
t[x].mv+=d;
mvit(t[x],d);
return;
}
int mid=(L+R)>>1;
if (r<=mid) moveit(x<<1,L,mid,l,r,d); else
if (l>mid) moveit(x<<1|1,mid+1,R,l,r,d); else {
moveit(x<<1,L,mid,l,mid,d);
moveit(x<<1|1,mid+1,R,mid+1,r,d);
}
update(x);
}
void inc(int x) {
if (tg[x]!=tim) tg[x]=tim,gs[x]=0;
gs[x]++;
if (gs[x]>1) return;
int wh=qnum(1,1,tp,1,x-1)+1;
add(1,1,tp,x,wh,1);
moveit(1,1,tp,x+1,tp,1);
}
void dec(int x) {
if (tg[x]!=tim) tg[x]=tim,gs[x]=0;
gs[x]--;
if (gs[x]) return;
add(1,1,tp,x,0,-1);
moveit(1,1,tp,x+1,tp,-1);
}
giant ans[maxn];
int main() {
#ifndef ONLINE_JUDGE
freopen("test.in","r",stdin);
freopen("my.out","w",stdout);
#endif
n=read(),q=read();
f[1]=f[2]=1;
for (int i=3;i<=n+5;++i) f[i]=(f[i-1]+f[i-2])%q;
qn=sqrt(n);
for (int i=1;i<=n;++i) c[i]=a[i]=read();
sort(c+1,c+n+1);
tp=unique(c+1,c+n+1)-c-1;
for (int i=1;i<=n;++i) a[i]=lower_bound(c+1,c+tp+1,a[i])-c;
int m=read();
for (int i=1;i<=m;++i) b[i].s=read(),b[i].t=read(),b[i].id=i;
sort(b+1,b+m+1);
int nk=-1,l,r;
for (int i=1;i<=m;++i) {
if ((b[i].s-1)/qn>nk) {
nk=(b[i].s-1)/qn;
++tim;
//memset(gs,0,sizeof gs); // here
r=b[i].s-1,l=b[i].s;
}
while (r<b[i].t) inc(a[++r]);
while (l>b[i].s) inc(a[--l]);
while (r>b[i].t) dec(a[r--]);
while (l<b[i].s) dec(a[l++]);
ans[b[i].id]=t[1].a;
}
for (int i=1;i<=m;++i) printf("%lld\n",ans[i]);
}

Codeforces633H-Fibonacci-ish II的更多相关文章

  1. 【TOJ 3600】Fibonacci II (对数+斐波那契通项式)

    描述 2007年到来了.经过2006年一年的修炼,数学神童zouyu终于把0到100000000的Fibonacci数列(f[0]=0,f[1]=1;f[i] = f[i-1]+f[i-2](i> ...

  2. ZOJ3774_Power of Fibonacci

    求fibonacci数列前N个数的K次方和. 通项公式:F[n]=((1+sqrt(5))/sqrt(5)-(1-sqrt(5))/sqrt(5))/sqrt(5). 有点乱,不过由于可以保证最后的结 ...

  3. poj3070 Fibonacci

    Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...

  4. 1143 多少个Fibonacci数

    时间限制:500MS  内存限制:65536K提交次数:270 通过次数:16 题型: 编程题   语言: C++;C Description 给你如下Fibonacci 数的定义: F1 = 1 F ...

  5. Codeforces Gym 100286F Problem F. Fibonacci System 数位DP

    Problem F. Fibonacci SystemTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudg ...

  6. LeetCode: Linked List Cycle II 解题报告

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

  7. 【CF914G】Sum the Fibonacci 快速??变换模板

    [CF914G]Sum the Fibonacci 题解:给你一个长度为n的数组s.定义五元组(a,b,c,d,e)是合法的当且仅当: 1. $1\le a,b,c,d,e\le n$2. $(s_a ...

  8. Manthan, Codefest 16 H. Fibonacci-ish II 大力出奇迹 莫队 线段树 矩阵

    H. Fibonacci-ish II 题目连接: http://codeforces.com/contest/633/problem/H Description Yash is finally ti ...

  9. codeforces914G Sum the Fibonacci

    题目大意:给定一个长为$n$($n\leq 10^6$)的序列S,定义一个合法的五元组$(a,b,c,d,e)$合法当且仅当 $$ ( S_a \mid S_b ) and S_c and ( S_d ...

  10. CF446C [DZY loves Fibonacci]

    Description Transmission Gate 你需要维护一个长度为\(n \leq 300000\) 的数列,兹词两个操作: 1.给一个区间加上一个fibonacci数列,规定\(f[0 ...

随机推荐

  1. 笔记-python-float(‘inf’)

    笔记-python-float(‘inf’) 看算法时发现了flaot(‘inf’). Python中可以用如下方式表示正负无穷: float("inf"), float(&quo ...

  2. SQL 注入、XSS 攻击、CSRF 攻击

    SQL 注入.XSS 攻击.CSRF 攻击 SQL 注入 什么是 SQL 注入 SQL 注入,顾名思义就是通过注入 SQL 命令来进行攻击,更确切地说攻击者把 SQL 命令插入到 web 表单或请求参 ...

  3. 武汉Uber优步司机奖励政策(8月31日~9月6日)

    ·奖励前提 *必须满足当周平均评分4.7星及以上,且当周接单率70%及以上,当周在线5小时且完成5单,才有资格获得奖励 * 各组别必须满足当周要求的成单率才有资格获得奖励,成单率由当周 滴滴快车单单2 ...

  4. OpenCV代码提取:dft函数的实现

    The Fourier Transform will decompose an image into its sinus and cosines components. In other words, ...

  5. 华为LiteOS系统使用-任务调度函数-第一篇

    1.最近项目遇到华为的LiteOS小型操作系统,使用学习 2. 先打开一个工程LiteOS_Kernel-master\projects\LPC824_LITE_KEIL 3. main.c里面2个关 ...

  6. iReport jasperReports 生成表格

    使用iReport生成表格   一 环境:iReport-5.6.0  JDK7 1.注意,iReport的最新版本目前还不支持JDK8,如果项目工程已经配置了JDK8,那也不用去修改环境变量和工程的 ...

  7. vcf-tools 笔记

    vcf-query: 通过 vcf-query 提取DP (reads depth). ~/zengs/Tools/vcftools/perl/vcf-query -f '%CHROM\t%POS\t ...

  8. Ruby基础教程 1-10

    类结构 1.数值类结构     Fixnum到Bignum会自动转换   2.常用数值表示   3. ans=10.divmod(3) ans[0]是商  ans[1]是余数   4.实例方法roun ...

  9. MySQL☞关联查询

    关联查询:所需要的数据来源于多张表,通过表的连接查询(关联查询)来查询多张表中的数据 格式: select 别名1 . */列名 , 别名2 . */列名 from 表名1  别名1 , 表名2  别 ...

  10. Python3 Tkinter-Checkbutton

    1.多选按钮创建 from tkinter import * root=Tk() Checkbutton(root,text='python').pack() root.mainloop() 2.绑定 ...