题面

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.

题意

求a[i]>=j并且a[j]>=i的对数

思路

二维偏序问题,试图使用数状数组解决.

但是有些对会重复计算,所以加上限制条件,i<j

这便是一个三维偏序问题.

#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime> #define fuck(x) cerr<<#x<<" = "<<x<<endl;
#define debug(a, x) cerr<<#a<<"["<<x<<"] = "<<a[x]<<endl;
#define lson l,mid,ls
#define rson mid+1,r,rs
#define ls (rt<<1)
#define rs ((rt<<1)|1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int loveisblue = 486;
const int maxn = 400086;
const int maxm = 100086;
const int inf = 0x3f3f3f3f;
const ll Inf = 999999999999999999;
const int mod = 1000000007;
const double eps = 1e-6;
const double pi = acos(-1); struct node{
int f,x,y,z;
}a[maxn],tmp[maxn]; int bit[maxn];
int lowbit(int x) {
return x & -x;
} int query(int pos) {
int ans = 0;
while (pos) {
ans += bit[pos];
pos -= lowbit(pos);
}
return ans;
} void update(int pos,int val){
while(pos<maxn){
bit[pos]+=val;
pos+=lowbit(pos);
}
}
ll ans ; void cdq(int l,int r){
if(l==r){ return;}
int mid = (l+r)>>1;
cdq(l,mid);cdq(mid+1,r); int t1=l,t2=mid+1;
int cnt = l-1;
while (t1<=mid||t2<=r){ if(t2>r||(t1<=mid&&a[t1].y<=a[t2].y)){
if(a[t1].f==1){
update(a[t1].z,1);
}
tmp[++cnt]=a[t1];
t1++; }else{
if(a[t2].f==0){
ans+=query(maxn-5)-query(a[t2].z-1);
}
tmp[++cnt]=a[t2];
t2++;
}
}for(int i=l;i<=mid;i++){
if(a[i].f==1){
update(a[i].z,-1);
}
}
for(int i=l;i<=r;i++){
a[i]=tmp[i];
} } int main() {
ios::sync_with_stdio(true);
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif int n;
scanf("%d",&n);
int tot= 0;
// int ans= 0;
for(int i=1;i<=n;i++){
int x;
scanf("%d",&x);
x=min(x,n);
a[++tot]=node{0,i,x,i};
a[++tot]=node{1,i,i,x};
// if(i==x){ans--;}
} cdq(1,tot);
printf("%lld\n",ans); return 0;
}

Tufurama CodeForces - 961E (cdq分治)的更多相关文章

  1. Tufurama CodeForces - 961E

    Tufurama CodeForces - 961E 题意:有一部电视剧有n季,每一季有ai集.问有多少对i,j存在第i季第j集也同时存在第j季第i集. 思路:核心问题还是统计对于第i季,你要统计第i ...

  2. 【题解】Radio stations Codeforces 762E CDQ分治

    虽然说好像这题有其他做法,但是在问题转化之后,使用CDQ分治是显而易见的 并且如果CDQ打的熟练的话,码量也不算大,打的也很快,思维难度也很小 没学过CDQ分治的话,可以去看看我的另一篇博客,是CDQ ...

  3. Radio stations CodeForces - 762E (cdq分治)

    大意: 给定$n$个三元组$(x,r,f)$, 求所有对$(i,j)$, 满足$i<j, |f_i-f_j|\le k, min(r_i,r_j)\ge |x_i-x_j|$ 按$r$降序排, ...

  4. Codeforces 669E cdq分治

    题意:你需要维护一个multiset,支持以下操作: 1:在某个时间点向multiset插入一个数. 2:在某个时间点在multiset中删除一个数. 3:在某个时间点查询multiset的某个数的个 ...

  5. AI robots CodeForces - 1045G (cdq分治)

    大意: n个机器人, 位置$x_i$, 可以看到$[x_i-r_i,x_i+r_i]$, 智商$q_i$, 求智商差不超过$k$且能互相看到的机器人对数. 这个题挺好的, 关键是要求互相看到这个条件, ...

  6. Educational Codeforces Round 41 967 E. Tufurama (CDQ分治 求 二维点数)

    Educational Codeforces Round 41 (Rated for Div. 2) E. Tufurama (CDQ分治 求 二维点数) time limit per test 2 ...

  7. Codeforces 1093E Intersection of Permutations [CDQ分治]

    洛谷 Codeforces 思路 一开始想到莫队+bitset,发现要T. 再想到分块+bitset,脑子一抽竟然直接开始写了,当然也T了. 最后发现这就是个裸的CDQ分治-- 发现\(a\)不变,可 ...

  8. Codeforces 1045G AI robots [CDQ分治]

    洛谷 Codeforces 简单的CDQ分治题. 由于对话要求互相看见,无法简单地用树套树切掉,考虑CDQ分治. 按视野从大到小排序,这样只要右边能看见左边就可以保证互相看见. 发现\(K\)固定,那 ...

  9. Codeforces 848C Goodbye Souvenir [CDQ分治,二维数点]

    洛谷 Codeforces 这题我写了四种做法-- 思路 不管做法怎样,思路都是一样的. 好吧,其实不一样,有细微的差别. 第一种 考虑位置\(x\)对区间\([l,r]\)有\(\pm x\)的贡献 ...

随机推荐

  1. linux的简单操作

    查看当前用户who am i 创建用户:sudo adduser lilei然后输入密码 查看用户:ls /home 用新用户登陆:su -l lilei 查看所属用户组:groups 用户名 新建文 ...

  2. 通过反射 修改访问和修改属性的值 Day25

    package com.sxt.field; /* * 通过反射拿到属性值 * 修改public属性值 * 修改private属性值 * 缺点:可读性差:代码复杂 * 优点:灵活:可以访问修改priv ...

  3. JQ取消hover事件

    $('a').unbind('mouseenter').unbind('mouseleave');

  4. 《VIM教程》笔记

    一:vi ,vim, gvim简介 vi的功能是最弱的,它是*nix操作系统下最基本的文本编辑器. vim一开始的功能还不如vi,那个时候它的全称是"Vi IMitation",即 ...

  5. EL表达式多条件或判断用法

    简单记录一EL表达式的判断用法 <c:if test="${(order.status == '06'&& order.type=='02') || (order.st ...

  6. oralce GROUPING

    /*从上面的结果中我们很容易发现,每个统计数据所对应的行都会出现null, 如何来区分到底是根据那个字段做的汇总呢,grouping函数判断是否合计列!*/ select decode(groupin ...

  7. part11-LED驱动程序设计-part11.1-字符设备控制

  8. python基础之逻辑题(2)

    python基础之逻辑题(2) 1.若k为整数,下列while循环执行的次数为? 2.请输出正确结果-----numbers? 3.求结果-----math?   4.求结果-----sum? 5.输 ...

  9. Facebook 发布深度学习工具包 PyTorch Hub,让论文复现变得更容易

    近日,PyTorch 社区发布了一个深度学习工具包 PyTorchHub, 帮助机器学习工作者更快实现重要论文的复现工作.PyTorchHub 由一个预训练模型仓库组成,专门用于提高研究工作的复现性以 ...

  10. js递归遍历树结构(tree)

    如图: 代码: let datas = [] //是一个树结构的数据 setName(datas){ //遍历树 获取id数组 for(var i in datas){ this.expandedKe ...