↓ 跳过正文
  1. Posts/

codeforces 459 D. Pashmak and Parmida's problem (离散化+线段树求逆序对数)

·800 字·2 分钟

题目链接 题意:定义_f_(l, r, x)为区间[l,r]中x出现的次数。现在要求calculate the number of pairs of indicies i, j (1 ≤ i < j ≤ n) such that_f_(1, i, a__i) > f(j, n, a__j).

思路:可以通过o(n)预处理出f(1,i,a[i])和f[j,n,a[j]],其实预处理的过程就是离散化的过程呢。。。

分别得到

1 1 2 3 2 3 4

4 3 3 2 2 1 1

所以答案其实就是第一组数在第二组数中找逆序数的过程。。。

我们不妨倒序处理。

需要注意的是,线段树维护的区间是0..mx,我整体增加了1.

线段树求逆序对和树状数组求逆序对是同样的思想。。。注意体会。。

代码实现
  1/* ***********************************************
  2Author :111qqz
  3Created Time :Mon 05 Sep 2016 02:06:05 PM CST
  4File Name :code/cf/problem/459D.cpp
  5************************************************ */
  6#include <cstdio>
  7#include <cstring>
  8#include <iostream>
  9#include <algorithm>
 10#include <vector>
 11#include <queue>
 12#include <set>
 13#include <map>
 14#include <string>
 15#include <cmath>
 16#include <cstdlib>
 17#include <ctime>
 18#define fst first
 19#define sec second
 20#define lson l,m,rt<<1
 21#define rson m+1,r,rt<<1|1
 22#define ms(a,x) memset(a,x,sizeof(a))
 23typedef long long LL;
 24#define pi pair < int ,int >
 25#define MP make_pair
 26using namespace std;
 27const double eps = 1E-8;
 28const int dx4[4]={1,0,0,-1};
 29const int dy4[4]={0,-1,1,0};
 30const int inf = 0x3f3f3f3f;
 31const int N=1E6+7;
 32map<int,int>mp;
 33int n ;
 34int a[N],b[N];
 35int tmp[N];
 36int tree[N<<2];//tree[i]表示的是以i节点为根节点的子树所代表的区间中数的个数。
 37void PushUp( int rt)
 38{
 39    tree[rt] = tree[rt<<1] + tree[rt<<1|1];
 40}
 41void update(int p,int l,int r,int rt)
 42{
 43    if (l==r)
 44    {
 45	tree[rt]++;
 46	return;
 47    }
 48    int m = (l+r)>>1;
 49    if (p<=m) update(p,lson);
 50    else update(p,rson);
 51    PushUp(rt);
 52}
 53int query(int L,int R,int l,int r,int rt)
 54{
 55    if (L<=l&&r<=R) return tree[rt];
 56    int m = (l+r)>>1;
 57    int ret = 0;
 58    if (L<=m)
 59    {
 60	int res  = query(L,R,lson);
 61	ret +=res;
 62    }
 63    if (R>=m+1)
 64    {
 65	int res = query(L,R,rson);
 66	ret +=res;
 67    }
 68    return ret;
 69}
 70int main()
 71{
 72	#ifndef  ONLINE_JUDGE
 73	freopen("code/in.txt","r",stdin);
 74  #endif
 75	cin>>n;
 76	mp.clear();
 77	int mx = 0 ;
 78	for ( int i = 1 ; i <= n ; i++)
 79	{
 80	    int x;
 81	    scanf("%d",&x);
 82	    tmp[i] = x;
 83	    if (!mp[x]) mp[x] = 2;  //mp表示的是出现的次数。。。是从0开始,但是线段树下标从1开始,因此整体+1,也就是第一次出现的时候设为2.
 84	    else mp[x]++;
 85	    a[i] = mp[x];
 86	    mx = max(mx,a[i]);
 87	}
 88	mp.clear();
 89	for ( int i = n ;  i >= 1;  i--)
 90	{
 91	    int x = tmp[i];
 92	    if (!mp[x]) mp[x] = 2;
 93	    else mp[x]++;
 94	    b[i] = mp[x];
 95	}
 96	ms(tree,0);
 97	LL ans = 0 ;
 98	for ( int i = n ; i >=1 ; i--)
 99	{
100	    ans = ans + LL(query(1,a[i]-1,1,mx,1));//查询在在a[i]之前插入的(下标比i大)且比a[i]小(因此查询区间是1..a[i]-1)的数的个数。
101	    update(b[i],1,mx,1); //在b[i]位置插入一个数。
102	}
103	cout<<ans<<endl;
104  #ifndef ONLINE_JUDGE
105  fclose(stdin);
106  #endif
107    return 0;
108}

相关文章

hdu 1754 I Hate It (线段树模板题,炒鸡详细注释版)

·1717 字·4 分钟
hdu 1754 题目链接 题意:单点更新,区间查询最大值。 思路:线段树。 一开始借鉴了 clj 的 pointer 写法,wjmzbmr’s code 直接 MLE,看来也许只能在 cf 上用。 下面是 MLE 的代码: 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2016年08月18日 星期四 18时40分24秒 4File Name :code/hdu/1754.cpp 5************************************************ */ 6#include <cstdio> 7#include <cstring> 8#include <iostream> 9#include <algorithm> 10#include <vector> 11#include <queue> 12#include <stack> 13#include <set> 14#include <map> 15#include <string> 16#include <cmath> 17#include <cstdlib> 18#include <deque> 19#define fst first 20#define sec second 21#define lson l,m,rt<<1 22#define rson m+1,r,rt<<1|1 23#define ms(a,x) memset(a,x,sizeof(a)) 24typedef long long LL; 25#define pi pair < int ,int > 26#define MP make_pair 27using namespace std; 28const double eps = 1E-8; 29const int dx4[4]={1,0,0,-1}; 30const int dy4[4]={0,-1,1,0}; 31const int inf = 0x3f3f3f3f; 32const int N=2E5+7; 33int a[N],n,m; 34int _max( int x,int y) 35{ 36 if (x==-1||y==-1) 37 return x==-1?y:x; 38 return a[x]>a[y]?x:y; 39} 40struct Tree 41{ 42 Tree *pl,*pr; 43 int l,r,mx; 44 void update() 45 { 46 mx = _max(pl->mx,pr->mx); 47 } 48 Tree(int l,int r) : 49 l(l),r(r) 50 { 51 if ( l + 1 == r) 52 { 53 mx = l ; 54 return ; 55 } 56 pl = new Tree(l,(l+r)>>1); 57 pr = new Tree((l+r)>>1,r); 58 update(); 59 } 60 void change(int p,int x) 61 { 62 if (p < l|| p>=r) return; 63 if (l+1==r) 64 { 65 a[l] = x; 66 return ; 67 } 68 pl->change(p,x); 69 pr->change(p,x); 70 update(); 71 } 72 int queryMax(int L,int R) 73 { 74 if (L <= l && r <= R) return mx; 75 if (L>=r || l >=R) 76 return -1; 77 return _max(pl->queryMax(L,R),pr->queryMax(L,R)); 78 } 79}*rt; 80int main() 81{ 82 #ifndef ONLINE_JUDGE 83 freopen("code/in.txt","r",stdin); 84 #endif 85 while (~scanf("%d%d",&n,&m)) 86 { 87 ms(a,0); 88 for ( int i = 0 ; i < n ; i++) scanf("%d",a+i); 89 rt = new Tree(0,n); 90 while (m--) 91 { 92 char opt[3]; 93 int x,y; 94 scanf("%s %d %d",opt,&x,&y); 95 x--; 96 if (opt[0]=='Q') 97 printf("%d\n",a[rt->queryMax(x,y)]); 98 else rt->change(x,y); 99 } 100 } 101 #ifndef ONLINE_JUDGE 102 fclose(stdin); 103 #endif 104 return 0; 105} 关于线段树的理解,见代码注释:

线段树学习笔记

·141 字·1 分钟
嘛,终于下定决心搞定线段树了。 之前几次都是被lazy标记卡住,这次大概不会了吧2333 放一些学习资料,最后比较zkw线段树和普通线段树的区别。 codeforces上非递归线段树讲解 (其实就是zkw吧) 线段树进阶(各种花式技巧) 找到了一篇非常赞的tutorial(含lazy标记) 链接