CodeForces - 633H :Fibonacci-ish II(正解:莫对+线段树)
Yash is finally tired of computing the length of the longest Fibonacci-ish sequence. He now plays around with more complex things such as Fibonacci-ish potentials.
Fibonacci-ish potential of an array ai is computed as follows:
- Remove all elements j if there exists i < j such that ai = aj.
- Sort the remaining elements in ascending order, i.e. a1 < a2 < ... < an.
- Compute the potential as P(a) = a1·F1 + a2·F2 + ... + an·Fn, where Fi is the i-th Fibonacci number (see notes for clarification).
You are given an array ai of length n and q ranges from lj to rj. For each range j you have to compute the Fibonacci-ish potential of the array bi, composed using all elements of ai from lj to rj inclusive. Find these potentials modulo m.
Input
The first line of the input contains integers of n and m (1 ≤ n, m ≤ 30 000) — the length of the initial array and the modulo, respectively.
The next line contains n integers ai (0 ≤ ai ≤ 109) — elements of the array.
Then follow the number of ranges q (1 ≤ q ≤ 30 000).
Last q lines contain pairs of indices li and ri (1 ≤ li ≤ ri ≤ n) — ranges to compute Fibonacci-ish potentials.
Output
Print q lines, i-th of them must contain the Fibonacci-ish potential of the i-th range modulo m.
Example
5 10
2 1 2 1 2
2
2 4
4 5
3
3
题意:Q次询问,每次询问给一个区间,让你把区间排序,按顺序乘对应的斐波拉契数。
思路: 正解占位,暂时不会。 只把暴力的码了。 暴力:每次看每个数是否存在于当前区间,然后做乘即可。复杂度O(nq)。
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define pii pair<int,int>
#define F first
#define S second
using namespace std;
const int maxn=;
pii a[maxn]; int f[maxn],L[maxn],R[maxn],num[maxn],ans[maxn],lt[maxn];
int main()
{
int N,M,P;
scanf("%d%d",&N,&P);
f[]=; f[]=; rep(i,,N) f[i]=(f[i-]+f[i-])%P;
rep(i,,N) scanf("%d",&a[i].F),a[i].F,a[i].S=i;
sort(a+,a+N+);
scanf("%d",&M);
rep(i,,M) {
scanf("%d%d",&L[i],&R[i]);
lt[i]=-;
}
rep(i,,N){
int d=a[i].F%P;
rep(j,,M){
if(a[i].S>R[j]||a[i].S<L[j]) continue;//
if(a[i].F==lt[j]) continue;
ans[j]=(ans[j]+f[++num[j]]*d)%P;
lt[j]=a[i].F;
}
}
rep(i,,M) printf("%d\n",ans[i]);
return ;
}
最新文章
- String类的功能
- Linux中的输入重定向,变量
- WinForm支持拖拽效果
- Silverlight独立存储
- 【程序员的SQL金典】笔记(第6章~第11章)
- ORA-15025: could not open disk 处理
- hdu 2767 Proving Equivalences
- Delphi Register
- 设计模式 - 出厂模式(factory pattern) 详细说明
- 字段的参数 -- Django从入门到精通系列教程
- C语言中return 0和return 1和return -1
- LimeSDR 上手指南
- Xgboost: 一把屠龙刀的自我修养
- C#代码安装Windows服务
- ubuntu安装anaconda后,终端输入conda,出现未找到命令
- Bootstrap(6)辅组类和响应式工具
- libgdx学习记录23——图片移动选择
- Oracle学习笔记:a inner join b与from a,b where a.x=b.x的差异
- SVG渲染顺序及z轴显示问题(zIndex)
- squid搭建http/https代理服务器