题意:给你一个序列,和标准序列连线,求交点数。

题解:就是求逆序对个数,用树状数组优化就行了。具体过程就是按照顺序往树状数组了插点(根据点的大小),因为第i大的点应该排在第i位,插进去的时候他前面本该是有i-1个点的,如果少了,那么一定就和这个元素构成了逆序对。

#include<cstdio>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<cstring> using namespace std; //#define local
typedef long long ll;
const int maxn = +;
#define lowbit(x) ((x)&-(x))
ll C[maxn];
ll n,a,b;
void add(ll x,ll d)
{
while(x <= n) {
C[x]+=d; x+=lowbit(x);
}
} ll sum(ll x)
{
ll ret = ;
while(x > ){
ret += C[x]; x-=lowbit(x);
}
return ret;
} ll Count()
{
ll res = ;
ll ele = b;
for(int i = ; i < n; i++){
ll pre = sum(ele+);
res += ele - pre;
add(ele+,);
ele = (ele+a)%n;
}
return res;
} int main()
{
#ifdef local
freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
#endif // local
int cas = ;
while(~scanf("%I64d%I64d%I64d",&n,&a,&b)&&n){
memset(C,,sizeof(C));
printf("Case %d: %I64d\n",++cas,Count());
} return ;
}

CF Gym 100463A (树状数组求逆序数)的更多相关文章

  1. poj 2299 Ultra-QuickSort(树状数组求逆序数)

    链接:http://poj.org/problem?id=2299 题意:给出n个数,求将这n个数从小到大排序,求使用快排的需要交换的次数. 分析:由快排的性质很容易发现,只需要求每个数的逆序数累加起 ...

  2. hdu 5147 Sequence II (树状数组 求逆序数)

    题目链接 Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. SGU180 Inversions(树状数组求逆序数)

    题目: 思路:先离散化数据然后树状数组搞一下求逆序数. 离散化的方法:https://blog.csdn.net/gokou_ruri/article/details/7723378 自己对用树状数组 ...

  4. HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number                         ...

  5. poj 2299 Ultra-QuickSort(树状数组求逆序数+离散化)

    题目链接:http://poj.org/problem?id=2299 Description In this problem, you have to analyze a particular so ...

  6. Codeforces645B【树状数组求逆序数】

    题意: 给你1-n的序列,然后有k次机会的操作,每一次你可以选择两个数交换. 求一个最大的逆序数. 思路: 感觉就是最后一个和第一个交换,然后往中间逼近,到最终的序列,用树状数组求一下逆序数. #in ...

  7. HDU 1394 Minimum Inversion Number(线段树/树状数组求逆序数)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  8. hdu 1394 Minimum Inversion Number (裸树状数组 求逆序数 && 归并排序求逆序数)

    题目链接 题意: 给一个n个数的序列a1, a2, ..., an ,这些数的范围是0-n-1, 可以把前面m个数移动到后面去,形成新序列:a1, a2, ..., an-1, an (where m ...

  9. Codeforces Round #261 (Div. 2) D. Pashmak and Parmida's problem (树状数组求逆序数 变形)

    题目链接 题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数.i和j符合f(1,i,a[i])>f(j,n,a[j]),求i和j的种类数. 我们可以用map预处理出  ...

随机推荐

  1. 怎么将vim的剪切版设置成系统的剪切版

    如果你用vim敲完了代码,怎么把代码提交到ACMoj的粘贴版上呢. 这是个问题. 去网上查了一下,首先有人说可以在vimrc里面添加 set clipboard=unnamed 我试了一下,没有效果. ...

  2. iOS证书和描述文件的配置

    1.登录Apple开发者账号,进入Apple Developer主页,点击Account 2.点击Certificates,ID&Profiles 3.生成CRS文件 1.打开mac上的钥匙串 ...

  3. Python学习笔记(yield与装饰器)

    yeild:返回一个生成器对象: 装饰器:本身是一个函数,函数目的装饰其他函数(调用其他函数) 功能:增强被装饰函数的功能 装饰器一般接受一个函数对象作为参数,以便对其增强 @原函数名  来调用其他函 ...

  4. 求n位水仙花数

    求n位水仙花数 A.两个关键 1.n位水仙花数的范围是什么? n位水仙花数的范围是[10n-1,10n) 2.如何判断是否为水仙花数 核心操作: 2-1.如何得到每一位? A.核心思想 对得到的数进行 ...

  5. PAT甲级——1103 Integer Factorization (DFS)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90574720 1103 Integer Factorizatio ...

  6. loj#6169. 相似序列 hash+主席树

    因为他的相似是在排完序下的 那我就在排序的情况下hash啊 这怎么hash啊 主席树啊! 没了 #include <bits/stdc++.h> #define MAXNODE 50000 ...

  7. namedJDBC查询

    import java.util.ArrayList; import java.util.List; import org.apache.log4j.Logger; import org.spring ...

  8. PartTime_一些网站

    1. http://www.sxsoft.com/ 貌似 搜搜"破解",无符合条件的结果 http://www.taskcity.com/ "智城",貌似 符合 ...

  9. 1.7hashmap并发成环

    https://mp.weixin.qq.com/s?__biz=MzIxMjE5MTE1Nw==&mid=2653192000&idx=1&sn=118cee6d1c67e7 ...

  10. 使用xadmin覆盖Django的admin

    安装xadmin pip 安装 pip install xadmin  可能会报错 pip install git+git://github.com/sshwsfc/xadmin.git 安装后要使用 ...