Tufurama CodeForces - 961E (cdq分治)
题面
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分治)的更多相关文章
- Tufurama CodeForces - 961E
Tufurama CodeForces - 961E 题意:有一部电视剧有n季,每一季有ai集.问有多少对i,j存在第i季第j集也同时存在第j季第i集. 思路:核心问题还是统计对于第i季,你要统计第i ...
- 【题解】Radio stations Codeforces 762E CDQ分治
虽然说好像这题有其他做法,但是在问题转化之后,使用CDQ分治是显而易见的 并且如果CDQ打的熟练的话,码量也不算大,打的也很快,思维难度也很小 没学过CDQ分治的话,可以去看看我的另一篇博客,是CDQ ...
- 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$降序排, ...
- Codeforces 669E cdq分治
题意:你需要维护一个multiset,支持以下操作: 1:在某个时间点向multiset插入一个数. 2:在某个时间点在multiset中删除一个数. 3:在某个时间点查询multiset的某个数的个 ...
- AI robots CodeForces - 1045G (cdq分治)
大意: n个机器人, 位置$x_i$, 可以看到$[x_i-r_i,x_i+r_i]$, 智商$q_i$, 求智商差不超过$k$且能互相看到的机器人对数. 这个题挺好的, 关键是要求互相看到这个条件, ...
- Educational Codeforces Round 41 967 E. Tufurama (CDQ分治 求 二维点数)
Educational Codeforces Round 41 (Rated for Div. 2) E. Tufurama (CDQ分治 求 二维点数) time limit per test 2 ...
- Codeforces 1093E Intersection of Permutations [CDQ分治]
洛谷 Codeforces 思路 一开始想到莫队+bitset,发现要T. 再想到分块+bitset,脑子一抽竟然直接开始写了,当然也T了. 最后发现这就是个裸的CDQ分治-- 发现\(a\)不变,可 ...
- Codeforces 1045G AI robots [CDQ分治]
洛谷 Codeforces 简单的CDQ分治题. 由于对话要求互相看见,无法简单地用树套树切掉,考虑CDQ分治. 按视野从大到小排序,这样只要右边能看见左边就可以保证互相看见. 发现\(K\)固定,那 ...
- Codeforces 848C Goodbye Souvenir [CDQ分治,二维数点]
洛谷 Codeforces 这题我写了四种做法-- 思路 不管做法怎样,思路都是一样的. 好吧,其实不一样,有细微的差别. 第一种 考虑位置\(x\)对区间\([l,r]\)有\(\pm x\)的贡献 ...
随机推荐
- php刷新当前页面,js刷新页面
echo "<script language=JavaScript> location.replace(location.href);</script>"; ...
- poj1087&&hdu1526 最大流
多源多汇. 比较明显的建图.对于电器,可以从源点与各个电器相连,容量为1,表示这个电器有1个,然后对于各种接头,那可以各个接头与汇点相连,容量为1,表示每个接头只能用一次. 然后对于能够相互转换的接头 ...
- Java面向对象----接口概念
接口语法 interface 接口名{ //静态常量,抽象方法 } 特点 接口中只能存放静态常量和抽象方法 java接口是对功能的扩展 通过实现接口,java类可以实现多实现 一个类可以同时继承(ex ...
- celery琐碎笔记
-l 指定日志等级 -n 指定任务名称 -Q 指定任务执行队列 -c 指定启动celery的cpu数量 --logfile 指定日志输出到文件,会输出任务函数里的print,而控制台不会,用于调试. ...
- 2018-7-21-win10-uwp-调用-Microsoft.Windows.Photos_8wekyb3d8bbwe-应用
title author date CreateTime categories win10 uwp 调用 Microsoft.Windows.Photos_8wekyb3d8bbwe 应用 linde ...
- @NOIP2018 - D2T3@ 保卫王国
目录 @题目描述@ @题解@ @代码@ @题目描述@ Z 国有n座城市,n−1 条双向道路,每条双向道路连接两座城市,且任意两座城市 都能通过若干条道路相互到达. Z 国的国防部长小 Z 要在城市中驻 ...
- 13 -1 BOM和定时器
一 BOM JavaScript基础分为三个部分: ECMAScript:JavaScript的语法标准.包括变量.表达式.运算符.函数.if语句.for语句等. DOM:文档对象模型,操作网页上的元 ...
- 如何安装 btsync
本文告诉大家如何在 windows 和 Linux 安装使用 Btsync 而且分享一些小东西给大家 btsync 是分布式网盘 在这高速运作的信息化时代,使用云端来衔接工作和生活的点滴已是寻常事.可 ...
- Hbase数据模型 行
- uda 2.C++ 向量
向量与矩阵代数 学习得不错!你已经学习了大量 C++ 句法.你也许注意到了,使用 C++ 编程无疑比使用 Python 困难.C++ 专为快速执行而设计,使用这门语言,你可以采用许多不同方式达到同一结 ...