Educational Codeforces Round 41 E. Tufurama (961E)

【题解】
第一眼看题飞快地想到一种做法,然后假掉了。
这道题其实是主席树的模板题来着。但是也有别的水法。
我们可以发现每个位置的查询区间是[1,min(a[i],i-1)],所以我们可以把查询区间按照右端点排序。开一个权值树状数组记录前i个a[i]的出现情况。我们从1到n按顺序插入a[i],每个位置上都统计min(a[j],j-1)=i的j的答案。
#include<cstdio>
#include<algorithm>
#define rg register
#define N 200010
using namespace std;
int n,a[N],b[N],d[N],t[N];
long long ans;
inline int read(){
int k=,f=; char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(''<=c&&c<='')k=k*+c-'',c=getchar();
return k*f;
}
inline void add(int x){
for(;x<=n;x+=(x&-x)) t[x]++;
}
inline int query(int x){
int ret=; for(;x>;x-=(x&-x)) ret+=t[x]; return ret;
}
inline bool cmp(int x,int y){
return d[x]<d[y];
}
int main(){
n=read();
for(rg int i=;i<=n;i++) a[i]=min(read(),n),b[i]=i,d[i]=min(a[i],i-);
sort(b+,b++n,cmp);
for(rg int i=,j=;i<=n;i++){
add(a[i]);
while(d[b[j]]<i&&j<=n) j++;
while(j<=n&&d[b[j]]==i) ans+=i-query(b[j++]-);
}
printf("%I64d\n",ans);
return ;
}
Educational Codeforces Round 41 E. Tufurama (961E)的更多相关文章
- Educational Codeforces Round 41 967 E. Tufurama (CDQ分治 求 二维点数)
Educational Codeforces Round 41 (Rated for Div. 2) E. Tufurama (CDQ分治 求 二维点数) time limit per test 2 ...
- Educational Codeforces Round 41
Educational Codeforces Round 41 D. Pair Of Lines 考虑先把凸包找出来,如果凸包上的点数大于\(4\)显然不存在解,小于等于\(2\)必然存在解 否则枚 ...
- Educational Codeforces Round 41 (Rated for Div. 2)
这场没打又亏疯了!!! A - Tetris : 类似俄罗斯方块,模拟一下就好啦. #include<bits/stdc++.h> #define fi first #define se ...
- Educational Codeforces Round 41 (Rated for Div. 2) ABCDEF
最近打的比较少...就只有这么点题解了. A. Tetris time limit per test 1 second memory limit per test 256 megabytes inpu ...
- Educational Codeforces Round 41 A B C D E
A. Tetris 题意 俄罗斯方块,问能得多少分. 思路 即求最小值 Code #include <bits/stdc++.h> #define F(i, a, b) for (int ...
- Educational Codeforces Round 41 B、C、D
http://codeforces.com/contest/961 B题 可以将长度为k的连续区间转化成1 求最大和 解析 简单尺取 #include <stdio.h> #include ...
- Educational Codeforces Round 41 (Rated for Div. 2)F. k-substrings
题意比较麻烦略 题解:枚举前缀的中点,二分最远能扩展的地方,lcp来check,然后线段树维护每个点最远被覆盖的地方,然后查询线段树即可 //#pragma GCC optimize(2) //#pr ...
- Educational Codeforces Round 41 (Rated for Div. 2)(A~D)
由于之前打过了这场比赛的E题,而后面两道题太难,所以就手速半个多小时A了前4题. 就当练手速吧,不过今天除了C题数组开小了以外都是1A A Tetris 题意的抽象解释可以在Luogu里看一下(话说现 ...
- D. Pair Of Lines( Educational Codeforces Round 41 (Rated for Div. 2))
#include <vector> #include <iostream> #include <algorithm> using namespace std; ty ...
随机推荐
- java中两个map比较
一 /** * 用map的keySet()的迭代器(性能效率较低) * */ public void compareMap1 (){ Map<String, String> m1 = ne ...
- 常用快捷键—Webstorm
常用快捷键—Webstorm入门指南 提高代码编写效率,离不开快捷键的使用,Webstorm拥有丰富的代码快速编辑功能,你可以自由配置功能快捷键. 快捷键配置 点击“File”-> “setti ...
- android :fragmentation使用中遇到的 NullPointerException
背景:fragmentation(单ACTIVITY+多个fragments)+brvah( recyclerView多级自定义菜单功能) 目的:实现 菜单栏的点击,fragment 显示相应的内 ...
- Xiaocms 去版权
Xiaocms 去版权 后台去版权: 1. 登录页面 修改文件:\admin\template\login.tpl.php 代码: <td width="190" rows ...
- python中的getcwd
Help on built-in function getcwd in module posix: getcwd(...) getcwd() -> path Return a ...
- Jquery ajax中表单提交被拦截的问题处理方法
在实际开发项目中,由于要做支付宝的批量退款处理,需要用到ajax中去提交表单数据,项目截图如下: 由于在第二张截图“确认退款”那里需要异步ajax提交数据到服务器处理信息,处理成功后将返回的数据装载到 ...
- SQL Server 父子迭代查询语句
-- 根据父ID得到所有子ID -- Get childs by parent idWITH TreeAS( SELECT Id,ParentId FROM dbo.Node P WHERE P.Id ...
- CentOS7.2上安装Python3.6
CentOS 7下安装Python3.6 1)安装python3.6可能使用的依赖yum -y install openssl-devel bzip2-devel expat-devel gdbm-d ...
- javaSe-线程2
package com.java.chap09.sec02; public class Thread3 implements Runnable{ private int baoZi=1; privat ...
- UVA 12673 Erratic Expansion 奇怪的气球膨胀 (递推)
不难发现,每过一个小时,除了右下方的气球全都是蓝色以外,其他都和上一个小时的气球是一样的,所以是可以递推的.然后定义一类似个前缀和的东西f(k,i)表示k小时之后上面i行的红气球数.预处理出k小时的红 ...