树状数组 ---- 树状数组+动态维护前缀中位数 D. Omkar and Medians

题目大意:

在这里插入图片描述


解题思路:

首先我们看他们的定义:
b i b_i bi a 1 , a 2 , a 3 , . . . . , a i , a i + 1 , . . . . . . . a 2 i − 1 a_1,a_2,a_3,....,a_i,a_{i+1},.......a_{2i-1} a1,a2,a3,....,ai,ai+1,.......a2i1
b i + 1 b_{i+1} bi+1 a 1 , a 2 , a 3 , . . . . , a i , a i + 1 , . . . . . . . a 2 i + 1 a_1,a_2,a_3,....,a_i,a_{i+1},.......a_{2i+1} a1,a2,a3,....,ai,ai+1,.......a2i+1
我们发现 b i + 1 b_{i+1} bi+1 b i b_i bi只多了 a 2 i , a 2 i + 1 a_{2i},a_{2i+1} a2i,a2i+1
那么就每次加入两个数!

那么我们看一下每一次中位数会怎么变换?
假设 c 1 , c 2 , c 3 . . . . . c i , c i + 1 , . . . . . . c 2 i − 1 c_1,c_2,c_3.....c_i,c_{i+1},......c_{2i-1} c1,c2,c3.....ci,ci+1,......c2i1 a 1 , a 2 , a 3 , . . . . , a i , a i + 1 , . . . . . . . a 2 i − 1 a_1,a_2,a_3,....,a_i,a_{i+1},.......a_{2i-1} a1,a2,a3,....,ai,ai+1,.......a2i1排好序的结果
那么 b i = c i 中 位 数 b_i=c_i中位数 bi=ci

对于 a 2 i , a 2 i + 1 a_{2i},a_{2i+1} a2i,a2i+1有三种情况

  1. 它们分别是在 c i c_i ci两端那么中位数不变
  2. 它们有两个都在左边,那么中位数会往右边移动一位!
  3. 它们有两个都在右边,那么中位数会往左边移动一位!

那么对于相邻的两个中位数 b i 和 b i + 1 b_i和b_{i+1} bibi+1是没有数在它们之间,因为每次只移动一位!
这个可以用树状数组去维护,不过要先离散化


#include <bits/stdc++.h>
#define mid ((l + r) >> 1)
#define Lson rt << 1, l , mid
#define Rson rt << 1|1, mid + 1, r
#define ms(a,al) memset(a,al,sizeof(a))
#define log2(a) log(a)/log(2)
#define lowbit(x) ((-x) & x)
#define IOS std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define INF 0x3f3f3f3f
#define LLF 0x3f3f3f3f3f3f3f3f
#define f first
#define s second
#define endl '\n'
using namespace std;
const int N = 2e6 + 10, mod = 1e9 + 9;
const int maxn = 500010;
const long double eps = 1e-5;
const int EPS = 500 * 500;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef pair<double,double> PDD;
template<typename T> void read(T &x)
{
   x = 0;char ch = getchar();ll f = 1;
   while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
   while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
template<typename T, typename... Args> void read(T &first, Args& ... args) 
{
   read(first);
   read(args...);
}
int tr[maxn];
int arr[maxn];
vector<int> lis;
map<int,int> mp;

inline void add(int pos, int val) {
    while(pos < maxn) {
        tr[pos] += val;
        pos += lowbit(pos);
    }
}

inline int sum(int x) {
    int res = 0;
    while(x) {
        res += tr[x];
        x -=lowbit(x);
    }
    return res;
}

int main() {
    IOS;
    int T;
    cin >> T;
    while(T --) {
        int n;
        cin >> n;
        for(int i = 1; i <= n; ++ i) {
            cin >> arr[i];
            lis.push_back(arr[i]);
        }
        sort(lis.begin(),lis.end());
        lis.erase(unique(lis.begin(),lis.end()),lis.end());
        for(int i = 1; i <= n; ++ i) 
            arr[i] = lower_bound(lis.begin(),lis.end(),arr[i]) - lis.begin() + 1;
        bool flag = 1;
        for(int i = 1; i <= n; ++ i) {
            if(!mp.count(arr[i])) {
                mp[arr[i]] = true;
                add(arr[i],1);
            }
            if(i == 1) continue;
            int x = sum(arr[i]);
            int y = sum(arr[i-1]);
            if(max(x,y) - min(x,y) > 1) {
                flag = 0;
                break;
            }
        }
        if(flag) cout << "YES\n";
        else cout << "NO\n";
        for(auto it : mp) add(it.first,-1);
        mp.clear();
        lis.clear();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值