Another Version of Inversion

题意:只有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

最新文章

  1. homework-01
  2. QT 中 关键字讲解(emit,signal,slot)
  3. 爬虫:获取多次跳转后的页面url
  4. java 网络编程复习(转)
  5. DCOM中的APPID的用处,以及RemoteServerName的传递问题
  6. $.ajax()方法解析
  7. Linux Linux程序练习十(网络编程大文件发送)
  8. 安装好mysql后允许远程连接
  9. 14 个 grep 命令的例子 【转】
  10. nginx和apache下的url rewrite
  11. Windows原生MPIO存储多路径软件详解与应用
  12. Lua学习笔记(六):协程
  13. 大神眼中的React Native--备用
  14. php如何做数据库攻击
  15. js-权威指南学习笔记4
  16. 0122——UITabBarController
  17. nide.js(二)文件I/O
  18. jQuery 迭代器
  19. Java调度框架Quartz简单示例
  20. JavaScript数据结构与算法(七) 双向链表的实现

热门文章

  1. java基础学习_io流之FileInputStream
  2. Mysql无法启动情况下,如何恢复数据?
  3. 算法与数据结构基础 - 滑动窗口(Sliding Window)
  4. 集成方法 Ensemble
  5. 谈谈我对Ext的认识,元芳,你怎么看
  6. .net软件开发脚本规范-代码标准(webform)
  7. size命令的sysv和berkeley格式差别
  8. DRF (Django REST framework) 中的Request 与 Response
  9. 小白学Python(6)——python-pptx 添加图表
  10. springboot的mybatis的xml相关的配置