题面

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;
}

最新文章

  1. html标签中meta属性使用介绍
  2. 【解决】若要使用报表生成器,必须在此计算机上安装 .Net Framework 3.5
  3. Percona XtraBackup User Manual 阅读笔记
  4. SQL server 数据库备份还原Sql
  5. 10 months then free? 10个月,然后自由
  6. server返回arraylist时,juqery在客户端的处理
  7. jQuery.extend源码深层分析
  8. linux下查看分区信息和剩余空间大小
  9. 纯css3代码写无缝滚动效果
  10. STL容器的本质
  11. Java基础知识强化39:StringBuffer类之StringBuffer的删除功能
  12. VMware Player 使用错误集锦
  13. 【Demo 0008】标签控制器
  14. 第20章 数据库操作----JDBC概述
  15. 使用一个for循环将N*N的二维数组的所有值置1
  16. ●洛谷P2664 树上游戏
  17. Vue-Router路由 Vue-CLI脚手架和模块化开发 之 路由常用配置与路由嵌套
  18. Tomcat:At least one JAR was scanned for TLDs yet contained no TLDs
  19. springBoot基础2
  20. hadoop-hdfs编程

热门文章

  1. Oracle日期
  2. spring-data-jpa实体类继承抽象类如何映射父类的属性到数据库
  3. SDUT-2117_数据结构实验之链表二:逆序建立链表
  4. 自动编码(AE)器的简单实现
  5. @gym - 101137K@ Knights of the Old Republic
  6. Resharper 如何把类里的类移动到其他文件
  7. react框架下,在页面内加载显示PDF文件,关于react-pdf-js的使用注意事项
  8. Android教程 -06 Activity的生命周期
  9. 读取hive的表结构,生成带comment的视图建表语句
  10. echarts细节的修改(2):矩形数图,柱状图,折线图,雷达图等