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\)的贡献 ...
随机推荐
- 2019-8-31-C#-循环的判断会进来几次
title author date CreateTime categories C# 循环的判断会进来几次 lindexi 2019-08-31 16:55:58 +0800 2018-06-14 0 ...
- 洛谷P1879 玉米田
题目描述 农场主John新买了一块长方形的新牧场,这块牧场被划分成M行N列(1 ≤ M ≤ 12; 1 ≤ N ≤ 12),每一格都是一块正方形的土地.John打算在牧场上的某几格里种上美味的草,供他 ...
- day4_python-之装饰器、迭代器、生成器
一.装饰器 1.为何要用装饰器 #开放封闭原则:对修改封闭,对扩展开放 2. 什么是装饰器 装饰器他人的器具,本身可以是任意可调用对象,被装饰者也可以是任意可调用对象. 强调装饰器的原则:1 不修改被 ...
- BZOJ 1834网络扩容题解
一道不算太难的题目 但是真的很恶心 显然,对于第一问,我们直接无脑打模板就好了 第二问也不是很难,我们将每条边再连一条容量为inf,费用为w的边 但是流量只要小于第一问的答案加k就行了 所以我们增加一 ...
- Linux下如何切换用户
切换用户的命令为:su username 从普通用户切换到root用户,还可以使用命令:sudo su 在终端输入exit或logout或使用快捷方式ctrl+d,可以退回到原来用户,其实ctrl+d ...
- Android 高仿微信语音聊天页面高斯模糊效果
目前的应用市场上,使用毛玻璃效果的APP随处可见,比如用过微信语音聊天的人可以发现,语音聊天页面就使用了高斯模糊效果. 先看下效果图: 仔细观察上图,我们可以发现,背景图以用户头像为模板,对其进行了高 ...
- php-textarea 换行
PHP接收表单提交的信息之后 存入数据库 再次从数据库获取数据再前端显示时 空格还有回车都消失了: 解决办法: 1,存入数据库时候进行替换 2,或者在取出数据之后进行替换 然后再在html中显示 s ...
- 修改eclipse默认注释
windows-->preference-->Java-->Code Style-->Code Templates -->Comments :注释--> ... 关 ...
- Liunx vi/vim 2
移动光标的方法 H 光标移动到这个屏幕的最上方那一行的第一个字符 M 光标移动到这个屏幕的中央那一行的第一个字符 L 光标移动到这个屏幕的最下方那一行的第一个字符 G 移动到这个档案的最后一行(常用 ...
- et al.
et al. 英 [ˌet ˈæl] adv. <拉>以及其他人; [例句]Earlier research in conventional RCS modelling for d ...