Educational Codeforces Round 65 选做
好久没更博客了,随便水一篇
E. Range Deleting
题意
给你一个长度为 \(n\) 的序列 \(a_1,a_2,\dots a_n\) ,定义 \(f(l,r)\) 为删除 \(l\le a_i\le r\) 元素后的序列。求所有 \(f(l,r)\) 单调不降序列的数量。
\(n,a_i\le 10^6\)
题解
简单题,但还是调了一年(见代码注释)。
考虑删除后的区间,一定是一段前缀并上一段后缀。首先找到一段合法的极长后缀,然后枚举前缀,在保证前缀合法的情况下双指针统计有多少个合法的后缀即可。复杂度 \(O(n)\) 。
code
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
inline int gi()
{
char c=getchar(); int x=0;
for(;c<'0'||c>'9';c=getchar());
for(;c>='0'&&c<='9';c=getchar())x=(x<<1)+(x<<3)+c-'0';
return x;
}
const int N=1e6+5;
int a[N],n,x,ans,fst[N],lst[N],gst[N];
int main()
{
#ifdef lc
freopen("in.txt","r",stdin);
#endif
n=gi(),x=gi();
memset(fst,0x3f,sizeof(fst));
for(int i=1;i<=n;++i)
{
a[i]=gi();
if(fst[a[i]]==fst[0]) fst[a[i]]=i;
lst[a[i]]=i;
}
int r=x,f=fst[x]; gst[x]=fst[x];
for(;r>=1;--r)
{
if(gst[r]<lst[r-1]) break;
gst[r-1]=min(gst[r],fst[r-1]);
}
if(!r)
{
printf("%I64d",1ll*x*(x+1)/2);
return 0;
}
int l=0;
long long ans=0;
for(int i=1;i<=x;++i)
{
for(r=max(r,i);r<=x&&gst[r]<l;++r);
ans+=x-r+2; //注意不能 --r, ans+=x-r+1 ……
if(fst[i]<l) break;
l=max(l,lst[i]);
}
printf("%I64d",ans);
}
F. Scalar Queries
题意
给你一个长度为 \(n\) 的序列 \(a_1,a_2,\dots a_n\) ,设 \(f(l,r)\) 等于序列 \([l,r]\) 内每个数乘上在当前区间的排名的和。求 \(\sum_{1\le l\le r\le n} f(l,r)\) .
\(n\le 5\cdot 10^5, a_i\le 10^9\) 。
题解
比前一题还简单,直接考虑当前数的贡献。离散化后直接两个树状数组维护左边和右边比当前数小的数的个数和下标和,随便统计一下就好。\(O(n\log n)\) 。
代码略丑。
code
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=5e5+5,Mod=1e9+7;
int a[N],b[N],n,t1[N],t2[N],s1[N],s2[N],c[N],m,ans;
#define lowbit(x) (x&-x)
void upd(int* t, int i, int w) {
for(;i<=m;i+=lowbit(i)) t[i]=((t[i]+w)%Mod+Mod)%Mod;
}
int qry(int* t, int i)
{
int r=0;
for(;i;i-=lowbit(i)) r=(r+t[i])%Mod;
return r;
}
void add(int x) { ans=(ans+x)%Mod; }
int mul(int x, int y) {
x=1ll*(x+Mod)%Mod, y=1ll*(y+Mod)%Mod;
return 1ll*x*y%Mod;
}
int main()
{
#ifdef lc
freopen("in.txt","r",stdin);
#endif
scanf("%d",&n);
for(int i=1;i<=n;++i) scanf("%d",&a[i]), c[i]=a[i];
sort(c+1,c+1+n),m=unique(c+1,c+1+n)-c-1;
for(int i=1;i<=n;++i)
{
b[i]=lower_bound(c+1,c+1+m,a[i])-c;
upd(t2,b[i],1);
upd(s2,b[i],i);
}
for(int i=1;i<=n;++i)
{
int x1=qry(t1,b[i]),y1=qry(s1,b[i]),x2=qry(t2,b[i]),y2=qry(s2,b[i]);
add(mul(a[i],mul(i,((mul(n+1,x2)-y2)%Mod+Mod)%Mod)));
add(mul(a[i],mul(n-i+1,y1)));
upd(t2,b[i],-1),upd(s2,b[i],-i);
upd(t1,b[i],1),upd(s1,b[i],i);
}
printf("%d",ans);
}
Educational Codeforces Round 65 选做的更多相关文章
- Educational Codeforces Round 64 选做
感觉这场比赛题目质量挺高(A 全场最佳),难度也不小.虽然 unr 后就懒得打了. A. Inscribed Figures 题意 给你若干个图形,每个图形为三角形.圆形或正方形,第 \(i\) 个图 ...
- Educational Codeforces Round 63 选做
D. Beautiful Array 题意 给你一个长度为 \(n\) 的序列.你可以选择至多一个子段,将该子段所有数乘上给定常数 \(x\) .求操作后最大的最大子段和. 题解 考虑最大子段和的子段 ...
- Educational Codeforces Round 65 (Rated for Div. 2)题解
Educational Codeforces Round 65 (Rated for Div. 2)题解 题目链接 A. Telephone Number 水题,代码如下: Code #include ...
- Educational Codeforces Round 65 (Rated for Div. 2) D. Bicolored RBS
链接:https://codeforces.com/contest/1167/problem/D 题意: A string is called bracket sequence if it does ...
- Educational Codeforces Round 65 (Rated for Div. 2) C. News Distribution
链接:https://codeforces.com/contest/1167/problem/C 题意: In some social network, there are nn users comm ...
- Educational Codeforces Round 65 (Rated for Div. 2) B. Lost Numbers
链接:https://codeforces.com/contest/1167/problem/B 题意: This is an interactive problem. Remember to flu ...
- Educational Codeforces Round 65 (Rated for Div. 2) A. Telephone Number
链接:https://codeforces.com/contest/1167/problem/A 题意: A telephone number is a sequence of exactly 11 ...
- Educational Codeforces Round 65 (Div. 2)
A.前n-10个有8即合法. #include<cstdio> #include<cstring> #include<iostream> #include<a ...
- Educational Codeforces Round 65 E,F
E. Range Deleting 题意:给出一个序列,定义一个操作f(x,y)为删除序列中所有在[x,y]区间内的数.问能使剩下的数单调不减的操作f(x,y)的方案数是多少. 解法:不会做,思维跟不 ...
随机推荐
- 凤凰系统(Phoenix OS)PC版安装,电脑上体验功能丰富的安卓系统
PC版(X86版)ISO镜像下载地址:http://www.phoenixos.com/download_x86 下载完成后,可按照官方给出的安装教程进行安装. 凤凰系统帮助中心:http://www ...
- 如何编写README.md
一.标题写法 1.在文本下方加上 =,文本变为大标题 2.在文本下方加上-,文本变为中标题 3.单独向输入 = ,则需要空一行 标题的另一种写法: # 一级标题 ## 二级标题 ### 三级标题 ## ...
- 腾讯玄武实验室向(CNVD)提交了一个重大漏洞“BucketShock”
导读 11 月 21 日,在小米 IoT 安全峰会上,腾讯安全玄武实验室负责人于旸(花名:TK 教主)在演讲中透露,腾讯玄武实验室最近向国家信息安全漏洞共享平台(CNVD)提交了一个重大漏洞“Buck ...
- pytorch梯度下降法讲解(非常详细)
pytorch随机梯度下降法1.梯度.偏微分以及梯度的区别和联系(1)导数是指一元函数对于自变量求导得到的数值,它是一个标量,反映了函数的变化趋势:(2)偏微分是多元函数对各个自变量求导得到的,它反映 ...
- 更改windows系统的快捷键方法
众所周知,windows平台有很多快捷键使用非常的别扭. 现在提供windows 平台快捷键替换的绝佳软件:autohotkey 下载链接:http://ahkscript.org/ 中文帮助站点:h ...
- PHP中的异常知识
一.绪 首先明确一点:异常和错误不是一回事. 一个异常(Exception)是一个程序执行过程中出现的一个例外或是一个事件,它中断了正常指令的运行,跳转到其他程序模块继续执行. 基本格式: try { ...
- C语言书籍入门---第三章
=======变量和数据类型========= 说 明:字符型 短整型 整型 长整型 单精度浮点型 双精度浮点型 无类型 数据类型:char short int long float d ...
- 五、生产者消费者模型_ThreadLocal
1.生产者消费者模型作用和示例如下:1)通过平衡生产者的生产能力和消费者的消费能力来提升整个系统的运行效率 ,这是生产者消费者模型最重要的作用2)解耦,这是生产者消费者模型附带的作用,解耦意味着生产者 ...
- linux修改键盘按键
我的电脑:Fedora-19 $ uname -a Linux localhost.localdomain 3.11.10-200.fc19.i686 #1 SMP Mon Dec 2 20:48:2 ...
- 转:Nginx的超时keeplive_timeout配置详解
https://blog.csdn.net/weixin_42350212/article/details/81123932 Nginx 处理的每个请求均有相应的超时设置.如果做好这些超时时间的限定, ...