题面

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. 管理神话之"我还能做大量的技术工作"

    “你要知道,如果你想做好一件事,你就必须自己动手.”Clive一边咕哝着,一边走回自己的房间. Susan原本在埋头工作.她抬起头来,叹了口气.然后起身,跟着Clive穿过走廊,来到他的房间门口.她敲 ...

  2. js表格拖拽

    html部分 <div id="chenkbox"> <div id="tableSort"> <ol> <li> ...

  3. jquery on事件

    可以给后添加的动态元素绑定事件

  4. json 2016-09-18 22:03 207人阅读 评论(18) 收藏

    JSON:JavaScript 对象表示法(JavaScript Object Notation) JSON是什么? JSON(JavaScript Object Notation) 是一种轻量级的数 ...

  5. React Native错误汇总(持续更新)

    错误1 Element type is invalid-: 错误描述: Element type is invalid: expected a String(for built-in componen ...

  6. linux环境变量设置和默认执行语句设置

    环境变量设置 1.export export ORACLE_HOME=/usr/local/instantclient_12_2export PATH=$ORACLE_HOME:$PATHexport ...

  7. qt开发ROS gui 遇到:global.h:1087:4: error: #error &quot;You must build your code with position independent code if Qt was built with -reduce-relocations. &quot......

    具体错误如下: 一共出现38个错误 这个错误是在导入cmakelists.txt时产生的,其实不是工程本身的问题,是因为我卸载ros,再重新安装ros的过程中把qtcreator的部分包给删除了,导致 ...

  8. 看看国外的JavaScript题目

    ---恢复内容开始--- 题目一 (function(){    return typeof arguments;})(); 答案:“object” arguments是对象,伪数组有两件事要注意这里 ...

  9. Python基础:14生成器

    yield表达式只用于定义生成器函数,且只能存在于函数的定义体中.只要一个函数内部使用了yield表达式,则该函数就成为生成器函数. 当调用生成器函数时,它返回一个称为生成器的迭代器.然后该生成器控制 ...

  10. @loj - 2480@ 「CEOI2017」One-Way Streets

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一张 n 个点 m 条边的无向图,现在想要把这张图定向. 有 ...