[BZOJ2789][Poi2012]Letters(树状数组求逆序对)
来源:互联网 发布:windows 2012 kms激活 编辑:程序博客网 时间:2024/05/17 01:12
题目描述
传送门
题解
预处理之后求逆序对。
代码
#include<iostream>#include<cstring>#include<cstdio>using namespace std;#define LL long longconst int max_n=1e6+5;char a[max_n],b[max_n];int n,last[100],next[max_n],st[100],bit[max_n];LL C[max_n],ans=0;inline void add(int loc,int val){ for (int i=loc;i<=n;i+=i&(-i)) C[i]+=val;}inline int query(int loc){ int ans=0; for (int i=loc;i>=1;i-=i&(-i)) ans+=C[i]; return ans;}int main(){ scanf("%d\n",&n); gets(a); gets(b); for (int i=0;i<n;++i) next[i]=n; memset(last,-1,sizeof(last)); for (int i=0;i<n;++i){ if (last[a[i]]!=-1) next[last[a[i]]]=i; else st[a[i]]=i; last[a[i]]=i; } for (int i=0;i<n;++i){ bit[i+1]=st[b[i]]+1; st[b[i]]=next[st[b[i]]]; } for (int i=n;i>=1;--i){ ans+=query(bit[i]); add(bit[i],1); } printf("%lld\n",ans);}
0 0
- [BZOJ2789][Poi2012]Letters(树状数组求逆序对)
- 树状数组求逆序对-洛谷P3531 [POI2012]LIT-Letters
- bzoj 2789: [Poi2012]Letters 树状数组求逆序对
- 【BZOJ2789】【树状数组】[Poi2012]Letters 题解
- bzoj2789 letters 树状数组
- bzoj 2789 [Poi2012]Letters 求逆序对
- 【POI2012】【BZOJ2789】Letters
- BZOJ2789: [Poi2012]Letters
- BZOJ2789: [Poi2012]Letters
- 【bzoj2789】 [Poi2012]Letters
- 【bzoj2789】[Poi2012]Letters
- 【BZOJ】【P2789】【Poi2012】【Letters】【题解】【逆序对】
- 求逆序对 (树状数组版)
- BZOJ 2789 Poi2012 Letters 树状数组
- bzoj 2789 [Poi2012]Letters 树状数组
- bzoj2789.Letters(贪心 && 逆序数)
- 树状数组求逆序对
- 树状数组求逆序对
- Minimum Depth of Binary Tree
- HDU 2055
- 10.1趣味数组
- hdu 5437 Alisha’s Party(优先队列)
- 《电路基础》一阶电路的零输入响应
- [BZOJ2789][Poi2012]Letters(树状数组求逆序对)
- 高级指针的概念和应用完整版(图文讲解)
- asp+access sql手工注入步骤
- Android图片压缩终极解决方案,是的,终极,终极,终极
- tuple的应用
- Android技巧小结之新旧版本Notification
- 导出mongo中的数据
- NYOJ - 599 奋斗的小蜗牛
- [BZOJ3994][SDOI2015]约数个数和(莫比乌斯反演)