zoukankan      html  css  js  c++  java
  • Codeforces 61E Enemy is weak 乞讨i<j<k && a[i]>a[j]>a[k] 对数的 树阵

    主题链接:点击打开链接

    意大利正在寻求称号 i<j<k && a[i]>a[j]>a[k] 的对数

    假设仅仅有2元组那就是求逆序数的做法

    三元组的话就用一个树状数组x表示 数字i前面有多少个比自己大的个数

    然后每次给这个y数组求和,再把x中>a[i]的个数存入y中就可以


    #include <algorithm>
    #include <cctype>
    #include <cassert>
    #include <cstdio>
    #include <cstring>
    #include <climits>
    #include <vector>
    #include<iostream>
    using namespace std;
    #define ll long long
    inline void rd(int &ret)
    {
    	char c;
    	do { c = getchar();
    	} while(c < '0' || c > '9');
    	ret = c - '0';
    	while((c=getchar()) >= '0' && c <= '9')
    		ret = ret * 10 + ( c - '0' );
    }
    #define N 1000005
    #define eps 1e-8
    #define inf 1000000
    
    
    ll n;
    struct node{
    	ll c[N];
    	inline ll lowbit(ll x){return x&-x;}
    	void init(){memset(c, 0, sizeof c);}
    	ll sum(ll x){
    		ll ans = 0;
    		while(x<=n+10)
    			ans += c[x], x+=lowbit(x);
    		return ans;
    	}
    	void change(ll x, ll y){
    		while(x)
    			c[x] +=y, x-=lowbit(x);
    	}
    }x, y;
    int haifei[1000000], panting[1000000];
    int main()
    {
    	ll i, j;
    	while(cin>>n)
    	{
    		ll ans = 0;
    		for(i = 0; i < n; i++)rd(haifei[i]), panting[i] = haifei[i];
    		x.init(); y.init();
    		sort(haifei, haifei+n);
    		for(i = 0; i < n; i++)
    		{
    			ll b = (lower_bound(haifei, haifei+n, panting[i]) - haifei) +1;
    			
    			ll siz = y.sum(b);
    			ans += siz;
    			y.change(b, x.sum(b));
    			x.change(b, 1);
    		}
    		cout<<ans<<endl;
    	}
    	return 0;
    }


    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    产品开发协作流程
    iOS性能优化
    iOS地图国内和国外的偏差
    Apple技术支持
    iOS应用架构谈 网络层设计方案
    form表单重复被提交
    SpringBoot+filter+listener+拦截器
    SpringBoot中的异常处理
    谷歌浏览器突然不能使用,并且出现报错码"STATUS_INVALID_IMAGE_HASH"解决方法
    form提交(图片,excel其他文件)
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/4627034.html
Copyright © 2011-2022 走看看