Another Version of Inversion 二维树状数组求逆序对
2024-08-31 18:09:12
题意:只有2种走路方式,往右或者往下,求先走到一个大的数,在走到小的数的这种方式有多少。也就是说求出关于这个2维矩阵的逆序数。
题解:二维数组+逆序数就完事了。
代码:
#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const LL mod = 1e9+;
const int N = 1e5+;
struct Node{
int a, id;
int x, y;
}A[*];
bool cmp(Node x1, Node x2){
if(x1.a == x2.a) return x1.id > x2.id;
return x1.a > x2.a;
}
int lowbit(int x){
return x&(-x);
}
int n, m;
LL tree[][];
void update(int x, int y){
for(int i = x; i <= n; i+=lowbit(i))
for(int j = y; j <= m; j += lowbit(j))
tree[i][j]++;
}
LL query(int x, int y){
LL ret = ;
for(int i = x; i; i-=lowbit(i))
for(int j = y; j; j-=lowbit(j))
ret += tree[i][j];
return ret;
}
int main(){
///Fopen;
scanf("%d%d", &n, &m);
int t = ;
for(int i = ; i <= n; i++)
for(int j = ; j <= m; j++){
t++;
scanf("%d", &A[t].a);
A[t].x = i;
A[t].y = j;
A[t].id = t;
// cout <<'s' <<A[i].id << endl;
}
sort(A+, A++t, cmp);
LL ans = ;
for(int i = ; i <= t; i++){
//cout << A[i].id << ' ' << A[i].x << ' ' << A[i].y << endl;
ans += query(A[i].x,A[i].y);
update(A[i].x, A[i].y);
}
printf("%I64d", ans);
return ;
}
Another Version of Inversion
最新文章
- homework-01
- QT 中 关键字讲解(emit,signal,slot)
- 爬虫:获取多次跳转后的页面url
- java 网络编程复习(转)
- DCOM中的APPID的用处,以及RemoteServerName的传递问题
- $.ajax()方法解析
- Linux Linux程序练习十(网络编程大文件发送)
- 安装好mysql后允许远程连接
- 14 个 grep 命令的例子 【转】
- nginx和apache下的url rewrite
- Windows原生MPIO存储多路径软件详解与应用
- Lua学习笔记(六):协程
- 大神眼中的React Native--备用
- php如何做数据库攻击
- js-权威指南学习笔记4
- 0122——UITabBarController
- nide.js(二)文件I/O
- jQuery 迭代器
- Java调度框架Quartz简单示例
- JavaScript数据结构与算法(七) 双向链表的实现
热门文章
- java基础学习_io流之FileInputStream
- Mysql无法启动情况下,如何恢复数据?
- 算法与数据结构基础 - 滑动窗口(Sliding Window)
- 集成方法 Ensemble
- 谈谈我对Ext的认识,元芳,你怎么看
- .net软件开发脚本规范-代码标准(webform)
- size命令的sysv和berkeley格式差别
- DRF (Django REST framework) 中的Request 与 Response
- 小白学Python(6)——python-pptx 添加图表
- springboot的mybatis的xml相关的配置