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 ;
}
CodeForces - 633H :Fibonacci-ish II(正解:莫对+线段树)的更多相关文章
- 洛谷P3246 序列 [HNOI2016] 莫队/线段树+扫描线
正解:莫队/线段树+扫描线 解题报告: 传送门! 似乎是有两种方法的,,,所以分别港下好了QAQ 第一种,莫队 看到这种询问很多区间之类的就会自然而然地想到莫队趴?然后仔细思考一下,发现复杂度似乎是欧 ...
- [bzoj4358]permu:莫队+线段树/回滚莫队
这道题是几天前水过去的,现在快没印象了,水一发. 首先我们看到它让求解的是最长的值域 连续段长度,很好. 然后就想到了山海经,但但是我还没有做. 然后又想到了很久以前的一次考试的T3旅馆hotel(我 ...
- Codeforces 666E E - Forensic Examination SA + 莫队 + 线段树
E - Forensic Examination 我也不知道为什么这个复杂度能过, 而且跑得还挺快, 数据比较水? 在sa上二分出上下界, 然后莫队 + 线段树维护区间众数. #include< ...
- Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树)
Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树) 题目链接 题意 给定一个nm的矩阵,每行取2k的矩阵,求总 ...
- Manthan, Codefest 16 H. Fibonacci-ish II 大力出奇迹 莫队 线段树 矩阵
H. Fibonacci-ish II 题目连接: http://codeforces.com/contest/633/problem/H Description Yash is finally ti ...
- 【CF633H】Fibonacci-ish II 莫队+线段树
[CF633H]Fibonacci-ish II 题意:给你一个长度为n的序列$a_i$.m个询问,每个询问形如l,r:将[l,r]中的所有$a_i$排序并去重,设得到的新数列为$b_i$,求$b_1 ...
- Codeforces 1603D - Artistic Partition(莫反+线段树优化 dp)
Codeforces 题面传送门 & 洛谷题面传送门 学 whk 时比较无聊开了道题做做发现是道神题( 介绍一种不太一样的做法,不观察出决策单调性也可以做. 首先一个很 trivial 的 o ...
- 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 问题描述 ...
- BZOJ 3836 Codeforces 280D k-Maximum Subsequence Sum (模拟费用流、线段树)
题目链接 (BZOJ) https://www.lydsy.com/JudgeOnline/problem.php?id=3836 (Codeforces) http://codeforces.com ...
随机推荐
- 通用TryParse
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Refle ...
- NavigationDrawer和NavigationView-Android M新控件
Translucent System Bars-4.4新特性 Toolbar-5.0新特性 NavigationDrawer 简介 NavigationDrawer 是 Google 在 Materi ...
- PAT 天梯赛 L2-021. 点赞狂魔 【水】
题目链接 https://www.patest.cn/contests/gplt/L2-021 题意 给出一个若干个人名,后面给出点赞的总数,以及每个赞的标签类型,输出前三个点赞狂魔,按标签类型不同数 ...
- $ 用python处理Excel文档(1)——用xlrd模块读取xls/xlsx文档
本文主要介绍xlrd模块读取Excel文档的基本用法,并以一个GDP数据的文档为例来进行操作. 1. 准备工作: 1. 安装xlrd:pip install xlrd 2. 准备数据集:从网上找到的1 ...
- Python与硬件学习笔记:蜂鸣器(转)
相信大家对蜂鸣器都不会陌生,很多产品和方案中都会用到蜂鸣器,大部分都是使用蜂鸣器来做提示或报警,比如按键按下.开始工作.工作结束或是故障等等.这里对单片机在蜂鸣器驱动上的应用作一下描述. 蜂鸣器的介绍 ...
- samtools的基本用法
1.sam,bam的格式转换: $samtools view -sb file.sam >file.bam $samtools view -sb file.sam -o file.bam #sa ...
- 20145222黄亚奇 《网络对抗技术》 MAL_逆向与Bof基础
学习目的 通过一些方法,使能够运行本不该被运行的代码部分,或得到shell的使用: 将正常运行代码部分某处call后的目标地址,修改为另一部分我们希望执行.却本不应该执行的代码部分首地址(这需要我们有 ...
- 在shell中使用sendmail发送邮件
cat > sendmymail.sh #!/bin/bash/usr/sbin/sendmail -t <<EOFFrom: Mail testing <abc@gmail. ...
- 【bzoj1899】[Zjoi2004]Lunch 午餐(贪心+dp)
题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=1899 显然为了节省时间,吃饭慢的人要先打饭.于是我们可以先把所有人按吃饭时间排序,于是 ...
- Spring初学之FactoryBean配置Bean
实体bean: Car.java: package spring.beans.factorybean; public class Car { private String name; private ...