zoukankan      html  css  js  c++  java
  • [CF91B] Queue

    有n个人在队列中等待。假如某个人前面有一个人年龄比他小,那他就会不高兴;定义他的“不高兴度”为他前面留他最远的年龄比他小的人与他的距离,求每个人的不高兴度。 n<=10^5

    Solution

    权值线段树

    #include <bits/stdc++.h>
    using namespace std;
    const int inf = 1e+9+7;
    const int N = 4000005;
    int n,a[N],b[N],ch[N][2],ind=1,ans[N];
    void modify(int p,int l,int r,int pos,int key) {
        if(l==r) {
            a[p]=max(a[p],key);
        }
        else {
            if(ch[p][0]==0) ch[p][0]=++ind;
            if(ch[p][1]==0) ch[p][1]=++ind;
            if(pos<=(l+r)/2) modify(ch[p][0],l,(l+r)/2,pos,key);
            else modify(ch[p][1],(l+r)/2+1,r,pos,key);
            a[p]=max(a[ch[p][0]],a[ch[p][1]]);
        }
    }
    
    int query(int p,int l,int r,int ql,int qr) {
        if(l>qr || r<ql) return 0;
        if(l>=ql && r<=qr) return a[p];
        return max(query(ch[p][0],l,(l+r)/2,ql,qr),query(ch[p][1],(l+r)/2+1,r,ql,qr));
    }
    
    int main() {
        ios::sync_with_stdio(false);
        cin>>n;
        for(int i=1;i<=n;i++) cin>>b[i];
        for(int i=n;i>=1;--i) {
            int tmp=query(1,1,inf,1,b[i]-1);
            if(tmp==0) ans[i]=-1;
            else ans[i]=tmp-i-1;
            modify(1,1,inf,b[i],i);
        }
        for(int i=1;i<=n;i++) {
            cout<<ans[i]<<" ";
        }
    }
    
  • 相关阅读:
    【z02】选择客栈
    JavaEE(22)
    驱动表问题
    影响子查询展开的情况
    SQL*Net message from client
    等值链接和非等值链接
    ||拼接列关联和直接关联区别
    分页SQL优化
    SQL*Net more data to client
    SQL*Net message to client
  • 原文地址:https://www.cnblogs.com/mollnn/p/12296991.html
Copyright © 2011-2022 走看看