$n \leq 100000$的数列,数字范围$-1e9,1e9$,现$q \leq 1e5$个每次问在一个区间玩游戏,能得到的最大的数。“游戏”:选相邻两个数$a_x,a_y$,然后把他们删掉,变成$a_x+2a_y$,直到序列中只剩一个数。答案$\mod \ \ 1e9+7$。

单次询问可用贪心解决:首先第一个数系数一定是1,后面的数的系数是$2^k,k\geq 1$且$k$不会比上一个数的$k$多2以上。用一块表示给某个区间分配了系数2,4,8,16,。。。,也就是这个区间从左到右一个个合并,那么在数列右边加入一个新的数时,如果这个数是负数,那给他新开一块,否则合并到上一块中,若上一块的答案因此变成了正的,那就继续往前合并。

多次询问只需离线一下,记一下每个块的位置,然后按上面的方法模拟,询问时二分一下就好了。不过要注意,询问时可能左端点处不是一个完整块,这时需要把那个块的后缀单独拿出来算答案。

有个大问题:数字很大,如何判断大小以决定某个块是否要往前并?可以发现负权值最小只到2e9,因此超过2e9的正权值记成2e9+1就行了。

 //#include<iostream>
#include<cstring>
#include<cstdio>
//#include<math.h>
//#include<set>
//#include<queue>
//#include<vector>
#include<algorithm>
#include<stdlib.h>
using namespace std; #define LL long long
int qread()
{
char c; int s=,f=; while ((c=getchar())<'' || c>'') (c=='-') && (f=-);
do s=s*+c-''; while ((c=getchar())>='' && c<=''); return s*f;
} //Pay attention to '-' , LL and double of qread!!!! int n,m;
#define maxn 100011
const int mod=1e9+; int a[maxn],pos[maxn],bin[maxn],inv[maxn],sum[maxn],lp=,val[maxn],vv[maxn],svv[maxn];
struct Ques{int l,r,id; bool operator < (const Ques &b) const {return r<b.r;} }q[maxn];
int ans[maxn]; int WWW(LL v) {return v>?:v;} int main()
{
n=qread(); m=qread();
bin[]=inv[]=; for (int i=;i<=n;i++)
a[i]=qread(),bin[i]=(bin[i-]<<)%mod,inv[i]=1ll*inv[i-]*((mod+)>>)%mod,
sum[i]=((sum[i-]+1ll*a[i]*bin[i])%mod+mod)%mod;
for (int i=;i<=m;i++) {q[i].l=qread(); q[q[i].id=i].r=qread();}
sort(q+,q++m); pos[lp=]=; int j=; val[]=a[]*; vv[]=svv[]=((a[]*)%mod+mod)%mod;
while (j<=m && q[j].r==) ans[q[j].id]=(a[]+mod)%mod,j++;
for (int i=;i<=n;i++)
{
if (a[i]<=) pos[++lp]=i,val[lp]=*a[i],vv[lp]=(a[i]*2ll%mod+mod)%mod,svv[lp]=(svv[lp-]+vv[lp])%mod;
else
{
int cur=WWW(0ll+val[lp]+bin[pos[lp]-pos[lp-]+]*1ll*a[i]);
int cvv=(vv[lp]+bin[pos[lp]-pos[lp-]+]*1ll*a[i])%mod; lp--;
while (lp && cur>)
{
cur=WWW(0ll+val[lp]+(pos[lp]-pos[lp-]>?:bin[pos[lp]-pos[lp-]])*1ll*cur);
cvv=(vv[lp]+bin[pos[lp]-pos[lp-]]*1ll*cvv)%mod; lp--;
}
val[++lp]=cur; pos[lp]=i; vv[lp]=cvv; svv[lp]=(svv[lp-]+vv[lp])%mod;
}
while (j<=m && q[j].r==i)
{
if (q[j].l==q[j].r) {ans[q[j].id]=(a[i]+mod)%mod; j++; continue;}
int l=q[j].l+,Ans=(a[q[j].l]+mod)%mod;
int L=,R=lp;
while (L<R)
{
int mid=(L+R)>>;
if (pos[mid]>=l) R=mid; else L=mid+;
}
Ans=(Ans+svv[lp]-svv[L])%mod; Ans=(Ans+mod)%mod;
Ans=(Ans+(sum[pos[L]]-sum[l-]+mod)*1ll*inv[l-])%mod;
ans[q[j].id]=Ans;
j++;
}
} for (int i=;i<=m;i++) printf("%d\n",ans[i]);
return ;
}

Codeforces878E. Numbers on the blackboard的更多相关文章

  1. 【CF878E】Numbers on the blackboard 并查集

    [CF878E]Numbers on the blackboard 题意:给你一个长度为n个数列,你每次可以进行如下操作: 选取两个相邻的数x,y(x在y左面),然后将这两个数去掉,用x+2y替换它. ...

  2. Codeforces 878 E. Numbers on the blackboard

    Codeforces 878 E. Numbers on the blackboard 解题思路 有一种最优策略是每次选择最后面一个大于等于 \(0\) 的元素进行合并,这样做完以后相当于给这个元素乘 ...

  3. CF 878E Numbers on the blackboard 并查集 离线 贪心

    LINK:Numbers on the blackboard 看完题觉得很难. 想了一会发现有点水 又想了一下发现有点困难. 最终想到了 但是实现的时候 也很难. 先观察题目中的这个形式 使得前后两个 ...

  4. Codeforces Beta Round #51 B. Smallest number dfs

    B. Smallest number Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/pro ...

  5. ZOJ 3529 A Game Between Alice and Bob(博弈论-sg函数)

    ZOJ 3529 - A Game Between Alice and Bob Time Limit:5000MS     Memory Limit:262144KB     64bit IO For ...

  6. ZOJ 3529 A Game Between Alice and Bob 博弈好题

    A Game Between Alice and Bob Time Limit: 5 Seconds      Memory Limit: 262144 KB Alice and Bob play t ...

  7. ural1682 Crazy Professor

    Crazy Professor Time limit: 1.0 secondMemory limit: 64 MB Professor Nathan Mathan is crazy about mat ...

  8. [HDU - 5170GTY's math problem 数的精度类

    题目链接:HDU - 5170GTY's math problem 题目描述 Description GTY is a GodBull who will get an Au in NOI . To h ...

  9. BestCoder Round #29——A--GTY's math problem(快速幂(对数法))、B--GTY's birthday gift(矩阵快速幂)

    GTY's math problem Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

随机推荐

  1. cocos2dx for iOS fmod的音效引擎接入

    上一个博客我写了一篇fmod的android接入过程,这一次介绍一下ios接入fmod的方法. 首先下载fmod的api包,解压后,在FMOD Programmers API/api文件夹下有lowl ...

  2. Where do I belong-freecodecamp算法题目

    Where do I belong(数组排序并找出元素索引) 要求 给数组排序 找到指定的值在数组的位置,并返回位置对应的索引. 思路 设定.sort()需要的返回函数 将要搜索的值添加到数组内 用. ...

  3. matplotlib绘图(四)

    控制文字属性的方法: 所有的方法都会返回一个matplotlib.text.Text对象  文本注释: annnotate() xy参数设置箭头指示的位置,xytext参数设置注释文字的位置 arro ...

  4. composer安装laravel-u-editor及其使用

    前言  使用的框架是laravel5.1,是composer搭建的,可以直接配置composer,如果不是composer搭建的larave,需要先安装composer,具体安装发放可以参考compo ...

  5. python里字典的用法介绍

    一.什么是字典 字典是python里的一种数据类型,特点是元素的无序性,和键key的唯一性.字典的创建方法是{key:values},字典里的键key只能是不可变的数据类型(整型,字符串或者是元组), ...

  6. ACM训练联盟周赛 A. Teemo's bad day

    65536K   Today is a bad day. Teemo is scolded badly by his teacher because he didn't do his homework ...

  7. Hadoop4.2HDFS测试报告之四

    第二组:文件存储读过程记录 测试系统组成 存储类型 测试程序或命令 测试文件大小(Mb) 文件个数(个) 客户端并发数(个) 读速率 (M/s) NameNode:1 DataNode:1 本地存储 ...

  8. Python虚拟机之异常控制流(五)

    Python中的异常控制语义结构 在Python虚拟机之异常控制流(四)这一章中,我们考察了Python的异常在虚拟机中的级别上是什么东西,抛出异常这个动作在虚拟机的级别上对应的行为,最后,我们还剖析 ...

  9. dubbo doc入门文档

    dubbo document http://dubbo.apache.org/zh-cn/docs/user/references/xml/dubbo-protocol.html

  10. 【Netty】Netty入门之WebSocket小例子

    服务端: 引入Netty依赖 <!-- netty --> <dependency> <groupId>io.netty</groupId> <a ...