CodeForces

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=30010;
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[1]=1; f[2]=1; rep(i,3,N) f[i]=(f[i-1]+f[i-2])%P;
    rep(i,1,N) scanf("%d",&a[i].F),a[i].F,a[i].S=i;
    sort(a+1,a+N+1);
    scanf("%d",&M);
    rep(i,1,M) {
        scanf("%d%d",&L[i],&R[i]);
        lt[i]=-1;
    }
    rep(i,1,N){
        int d=a[i].F%P;
        rep(j,1,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,1,M) printf("%d
",ans[i]);
    return 0;
}
原文地址:https://www.cnblogs.com/hua-dong/p/9676012.html