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:

  1. Remove all elements j if there exists i < j such that ai = aj.
  2. Sort the remaining elements in ascending order, i.e. a1 < a2 < ... < an.
  3. 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

Input
5 10
2 1 2 1 2
2
2 4
4 5
Output
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 ;
}

CodeForces - 633H :Fibonacci-ish II(正解:莫对+线段树)的更多相关文章

  1. 洛谷P3246 序列 [HNOI2016] 莫队/线段树+扫描线

    正解:莫队/线段树+扫描线 解题报告: 传送门! 似乎是有两种方法的,,,所以分别港下好了QAQ 第一种,莫队 看到这种询问很多区间之类的就会自然而然地想到莫队趴?然后仔细思考一下,发现复杂度似乎是欧 ...

  2. [bzoj4358]permu:莫队+线段树/回滚莫队

    这道题是几天前水过去的,现在快没印象了,水一发. 首先我们看到它让求解的是最长的值域 连续段长度,很好. 然后就想到了山海经,但但是我还没有做. 然后又想到了很久以前的一次考试的T3旅馆hotel(我 ...

  3. Codeforces 666E E - Forensic Examination SA + 莫队 + 线段树

    E - Forensic Examination 我也不知道为什么这个复杂度能过, 而且跑得还挺快, 数据比较水? 在sa上二分出上下界, 然后莫队 + 线段树维护区间众数. #include< ...

  4. Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树)

    Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树) 题目链接 题意 给定一个nm的矩阵,每行取2k的矩阵,求总 ...

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

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

  6. 【CF633H】Fibonacci-ish II 莫队+线段树

    [CF633H]Fibonacci-ish II 题意:给你一个长度为n的序列$a_i$.m个询问,每个询问形如l,r:将[l,r]中的所有$a_i$排序并去重,设得到的新数列为$b_i$,求$b_1 ...

  7. Codeforces 1603D - Artistic Partition(莫反+线段树优化 dp)

    Codeforces 题面传送门 & 洛谷题面传送门 学 whk 时比较无聊开了道题做做发现是道神题( 介绍一种不太一样的做法,不观察出决策单调性也可以做. 首先一个很 trivial 的 o ...

  8. Codeforces Round #227 (Div. 2) E. George and Cards 线段树+set

    题目链接: 题目 E. George and Cards time limit per test:2 seconds memory limit per test:256 megabytes 问题描述 ...

  9. BZOJ 3836 Codeforces 280D k-Maximum Subsequence Sum (模拟费用流、线段树)

    题目链接 (BZOJ) https://www.lydsy.com/JudgeOnline/problem.php?id=3836 (Codeforces) http://codeforces.com ...

随机推荐

  1. Docker部署JavaWeb项目实战

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主要讲了如何在Ubuntu14.04 64位系统下来创建一个运行Javaweb应用程 ...

  2. Loadrunder之脚本篇——检查点

    VuGen判断脚本是否执行成功是根据服务器返回的状态来确定的,如果服务器返回的是HTTP状态为200 OK,那么VuGen就认为脚本正确地运行了,并且是运行通过的.而大多数系统出错时是不会返回错误页面 ...

  3. iOS 视图控制器转场动画/页面切换效果/跳转动画 学习

    一 学习 在 UINavigationController 中 push 和 pop 的转场效果  (基于iOS7 以上的转场方式) 经过学习了解到,重点分三块: (1)pushAnimation:  ...

  4. $《第一行代码:Android》读书笔记——第5章 Broadcast

    (一)广播机制简介 1.Android广播的分类: 如图所示: 2.发送广播:使用Intent:接收广播:Broadcast Receiver. (二)接收系统广播 1.动态注册监听网络变化 示例程序 ...

  5. ES6 随记(1)-- let 与 const

    1. const(声明一个只读的常量) 这个是很好理解的,且声明时就必须赋值而不能以后再赋,不然会报错. 而个人认为它最大的用处还是在于 {} 和 [] 上,const 保证了它的内存地址(指针)不变 ...

  6. 从引物序列出发查找pcr产物的内容和在基因组上的位置

    1.利用primer_blast工具,找出这对引物序列在基因组上的位置: 结果大概会像这样: 2.这些结果都是根据hg38基因组来定位的,转换成hg19: 利用UCSCde hgLiftover 在线 ...

  7. memcached 高级机制(一)

    memcached的高级机制 memcached内存机制 (1)我们知道操作系统对进程的处理方法,在多进程并发的操作系统中,程序的执行不可避免的会产生碎片.同样对于memcached,在存储value ...

  8. setfacl设置特定目录的权限

    现有一目录是虚拟机和linux共享的,但是每次程序调用新建的文件都发现没有权限. 于是指定特定目录及其子目录下新建的文件或目录对于用户qhfz都有读写执行的权限 -R表示递归 -m表示设置文件acl规 ...

  9. freemarker日志实现过程分析

    freemarker有自己的log类,这是一个抽象类,具体的日志打印委托给classpath里面合适的日志jar包来执行,寻找合适日志jar的查找顺序是:Apache Log4J, Apache Av ...

  10. QT QFtp使用实例 从FTP下载一个文件

    1. ftp://ftp.denx.de/pub/u-boot/lowboot-1.0.0.patch.gz  下载文件 FtpGet.h #ifndef FTPGET_H #define FTPGE ...