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. linux练习命令

    任务一:按要求完成以下操作1)显示日期格式2)在/tmp/下新建目录test ,并指定权限6643)显示环境变量path,但将/root加入到$PATH中4)用cat显示/etc/passwd,并打印 ...

  2. Jquery.ScrollLoading图片延迟加载技术

    关于分屏加载图片,像天猫.京东等电商图片较多页面很长,就采用了延迟加载技术. 目前很流行的做法就是滚动动态加载,显示屏幕之外的图片默认是不加载的, 随着页面的滚动,显示区域图片才被动态加载. 原理其实 ...

  3. Linux 函数库

    概述 函数库其实就是函数,只不过是系统所调用的函数.这样说吧,我写了一个软件,所有的功能都需要我自己完成吗?其实是不需要的,因为很多功能是别人已经写好的,我只需要拿来用就好了.这些有独立功能并且可以被 ...

  4. 【HackerRank】 Chocolate Feast

    Little Bob loves chocolates, and goes to the store with $N money in his pocket. The price of each ch ...

  5. imx6qsbd kpp

    转: https://blog.csdn.net/zyaiwmy/article/details/54313025 https://www.aliyun.com/jiaocheng/123973.ht ...

  6. 后向传播算法“backpropragation”详解

    为什么要使用backpropagation? 梯度下降不用多说,如果不清楚的可以参考梯度下降算法. 神经网络的参数集合theta,包括超级多组weight和bais. 要使用梯度下降,就需要计算每一个 ...

  7. setup in xunit

    https://xunit.github.io/docs/shared-context Shared Context between Tests It is common for unit test ...

  8. 最长k可重区间集

      P3358 最长k可重区间集问题 P3357 最长k可重线段集问题 P3356 火星探险问题 P4012 深海机器人问题 P3355 骑士共存问题 P2754 [CTSC1999]家园 题目描述 ...

  9. 关于tcp的知识记录

    1. 概念 TCP(Transmission Control Protocol,传输控制协议),是一种面向连接的,可靠地,基于字节流的传输层通信协议.当应用层向TCP层发送用于网络间传输的用8位字节表 ...

  10. pandas通过字典生成dataframe

    1.将一个字典输入: 该字典必须满足:value是一个list类型的元素,且每一个key对应的value长度都相同: (以该字典的key为columns) >>> import pa ...