E. Tufurama

One day Polycarp decided to rewatch his absolute favourite episode of well-known TV series "Tufurama". He was pretty surprised when he got results only for season 7 episode 3 with his search query of "Watch Tufurama season 3 episode 7 online full hd free". This got Polycarp confused — what if he decides to rewatch the entire series someday and won't be able to find the right episodes to watch? Polycarp now wants to count the number of times he will be forced to search for an episode using some different method.

TV series have n seasons (numbered 1 through n), the i-th season has ai episodes (numbered 1 through ai). Polycarp thinks that if for some pair of integers x and y (x < y) exist both season x episode y and season y episode x then one of these search queries will include the wrong results. Help Polycarp to calculate the number of such pairs!

Input

The first line contains one integer n (1  ≤ n  ≤  2·10^5) — the number of seasons.

The second line contains n integers separated by space a1, a2, ..., an (1 ≤ ai ≤ 10^9) — number of episodes in each season.

Output

Print one integer — the number of pairs x and y (x < y) such that there exist both season x episode y and season y episode x.

题意:

有一部电视剧有n季,每一季有ai集。定义二元组(i,j):存在第i季有第j集。求(i,j)与(j,i)同时合法(i<j)的对数。

真实题意就是:求<i,j>对数,使得a[i]≥j,a[j]≥i并且(i<j)


BIT做法

像很多其他题一样,对于这样的、关于元素大小关系之间的限制的题目,先排个序总是能够解决个一维限制掉去的。

我们使用一个结构体node x,x.i表示季数;x.a表示该季的集数。首先对x.a排序。那么就变成这个样子:

p[].a(j)  3  5  1  2
p[].i(i)  1  2  3  4
  |
  |
p[].a(j)  1  2  3  4 (取min之后)
p[].i(i)  3  4  1  2

先考虑每次的统计,那么只要ans+=query(a[i])就可以了。意思就是说ans加上1..a[i]季的贡献(其中每一季的贡献要么是0要么是1,但是由于之后会有修改,所以我们用BIT维护)

now用来更新那些已经 过气 没有贡献的答案,这里「没有贡献的答案」指的是p[now].a<i的情况,就是p[now]的电视剧集数太小了,已经不会再有贡献了,由于p[i].a是单增的,因此now++,扫一遍即可。

代码:

 //#include"bits/stdc++.h"
#include<sstream>
#include<iomanip>
#include"cstdio"
#include"map"
#include"set"
#include"cmath"
#include"queue"
#include"vector"
#include"string"
#include"cstring"
#include"time.h"
#include"iostream"
#include"stdlib.h"
#include"algorithm" #define db double
#define ll long long
#define vec vector<ll>
#define mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
//#define rep(i, x, y) for(int i=x;i<=y;i++)
#define rep(i, n) for(int i=0;i<n;i++)
const int N = 1e6 + ;
const int mod = 1e9 + ;
const int MOD = mod - ;
const int inf = 0x3f3f3f3f;
const db PI = acos(-1.0);
const db eps = 1e-;
using namespace std;
ll ans;
int n, now, a[N],bit[N];
struct node {
int a, i;
bool operator<(node &xx) const {
return a < xx.a;
}
} p[N];
int read() {
char ch = getchar();
int ret = ;
for (; !isdigit(ch); ch = getchar());
for (; isdigit(ch); ch = getchar())
ret = (ret << ) + (ret << ) + ch - ;
return ret;
}
void add(int x, int c) { for (; x <= n + ; x += x & -x)bit[x] += c; }
int sum(int x) {
int ret = ;
for (; x; x -= x & -x)
ret += bit[x];
return ret;
}
int main() {
n = read();
now = ;
for (int i = ; i <= n; i++)
a[i] = min(read(), n), p[i].a = a[i], p[i].i = i, add(i, );
sort(p + , p + n + );
for (int i = ; i <= n; i++) {
while (now <= n && p[now].a < i)add(p[now++].i, -);//去掉无贡献的值
ans += sum(a[i]);
if (a[i] >= i) ans--;//(i,j) i=j的情况去掉
}
cout << ans / << endl;
return ;
}

CF961E Tufurama 树状数组的更多相关文章

  1. Codeforces 961E - Tufurama 树状数组

    转自:https://blog.csdn.net/my_sunshine26/article/details/79831362 题目大意: i从1开始 基本思路: 完全没思路,所以上来就二分,果不其然 ...

  2. 【树状数组】CF961E Tufurama

    挺巧妙的数据结构题(不过据说这是一种套路? E. Tufurama One day Polycarp decided to rewatch his absolute favourite episode ...

  3. 树状数组 简单题 cf 961E

    题目链接 : https://codeforces.com/problemset/problem/961/E One day Polycarp decided to rewatch his absol ...

  4. BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2221  Solved: 1179[Submit][Sta ...

  5. bzoj1878--离线+树状数组

    这题在线做很麻烦,所以我们选择离线. 首先预处理出数组next[i]表示i这个位置的颜色下一次出现的位置. 然后对与每种颜色第一次出现的位置x,将a[x]++. 将每个询问按左端点排序,再从左往右扫, ...

  6. codeforces 597C C. Subsequences(dp+树状数组)

    题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...

  7. BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]

    2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2545  Solved: 1419[Submit][Sta ...

  8. BZOJ 3529: [Sdoi2014]数表 [莫比乌斯反演 树状数组]

    3529: [Sdoi2014]数表 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1399  Solved: 694[Submit][Status] ...

  9. BZOJ 3289: Mato的文件管理[莫队算法 树状数组]

    3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 2399  Solved: 988[Submit][Status][Di ...

随机推荐

  1. nopcommerce3.6中文包

    nopCommerce 语言包,xml文件 点击下载:3.60_language_pack_zh.rar (60.82 kb) 下载后解压通过后台导入即可使用.如何导入?点击这里

  2. vue打包后CSS中引用的背景图片不显示问题

    vue项目中,在css样式中引用了一张背景图片,开发环境下是可以正常显示,build之后背景图片不显示. 解决方法: 找到build/utils.js文件 修改成为如下所示内容:  添加红框中的内容即 ...

  3. C++ Knowledge series overloading

    What does the compiler behind our programming? Overloading in C++ Override all of overloaded functio ...

  4. Android监听安装卸载

    需要通过receiver来监听: 在AndroidManifest.xml文件中注册的receiver中必须加上<data android:scheme="package"/ ...

  5. 简单粗暴的更换固态硬盘及WIN10 Ubuntu双系统

    简介:本文希望帮助如本人一样的计算机小白,能够以最快的速度更换固态硬盘及安装系统. 1.提前将准备空白U盘(至少8G),利用UltraISO工具,或者软媒魔方,制作U盘启动盘. PS:需提前下载好WI ...

  6. PHP : 数据库中int类型保存时间并通过年月份时分秒进行显示

    1.表设计: 2.数据库操作页面:将时间戳插入到数据库中 我们到数据库中可以看到: 3.我们将数据进行显示: 页面结果:(二维数组) 4.以为用mysqli_fetch_all得到的是二维数组,那么我 ...

  7. C4C和CRM里获取当前登录用户分配的Organization Unit信息

    C4C 如何查看某个用户分配的组织单元ID: 在Employee的Organization Data区域内看到分配的组织名称,如下图红色下划线所示: 现在的需求就是使用ABSL获取当前登录用户分配的O ...

  8. POJ-2352 && hdu-1541 Stars---树状数组的运用

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1541 题目大意 : 在坐标上有n个星星,如果某个星星坐标为(x, y), 它的左下位置为:(x0,y ...

  9. VMware 12安装Mac OS X 10.11

    去年写了一篇安装Mac OS X 10.10的文章,看到大家都想体验OS X,大多数都能成功,但也在其中发现了一些问题,所以更新一下,希望对大家有所帮助.   1048VMware 11安装Mac O ...

  10. Adobe CS2提供免费序列号

    据Adobe官方博客报道,自2012年12月13日起,因为技术故障,该公司已停止使用Creative Suite(CS2)产品及Acrobat 7的激活服务器. 这些产品大多是7年前发布,很多已经无法 ...