Codeforces 12D Ball (线段树)

D. Ball
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

N ladies attend the ball in the King's palace. Every lady can be described with three values: beauty, intellect and richness. King's Master of Ceremonies knows that ladies are very special creatures. If some lady understands that there is other lady at the ball which is more beautiful, smarter and more rich, she can jump out of the window. He knows values of all ladies and wants to find out how many probable self-murderers will be on the ball. Lets denote beauty of the i-th lady by Bi, her intellect by Ii and her richness by Ri. Then i-th lady is a probable self-murderer if there is some j-th lady that Bi < Bj, Ii < Ij, Ri < Rj. Find the number of probable self-murderers.

Input

The first line contains one integer N (1 ≤ N ≤ 500000). The second line contains N integer numbers Bi, separated by single spaces. The third and the fourth lines contain sequences Ii and Ri in the same format. It is guaranteed that 0 ≤ Bi, Ii, Ri ≤ 109.

Output

Output the answer to the problem.

Sample test(s)
input
3
1 4 2
4 3 2
2 5 3
output
1


题意:

给定n个三元组(B,L,R),求有多少个三元组(B[i],L[i],R[i]),存在(B[j],L[j],R[j])使得B[i]<B[j] && L[i]<L[j] && R[I]<R[j].


分析:

对每一维离散化,然后按第一维从大到小,第二维从小到大,第三维从大到小的顺序对这些三元组进行排序,建立一棵线段树,维护第二维取值对应第三维的最大值,每一次枚举时先查询区间[L[i]+1,n]的最大值k,如果k>R[i]则ans++,并更新线段树上L[i]处的值。注意到按前述方法排序之后依次枚举时保证第一维是不减的,且在第一维相同时第二维递增,保证先插入线段树的值不会对当前询问产生影响。复杂度O(nlogn)。


代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=500005;
int b[MAXN],l[MAXN],r[MAXN],t[MAXN];
void modify(int s[],int h[],int n)
{
    for(int i=1;i<=n;i++)h[i]=s[i];
    sort(h+1,h+n+1);
    int cnt=unique(h+1,h+n+1)-(h+1);
    for(int i=1;i<=n;i++)
        s[i]=lower_bound(h+1,h+cnt+1,s[i])-h;
}
struct person
{
    int b,l,r;
    person(int b=0,int l=0,int r=0):b(b),l(l),r(r){}
    bool operator < (const person &q)const
    {
        return b==q.b ? (l==q.l ? r>q.r : l<q.l) : b>q.b;
    }
}p[MAXN];
struct node
{
    int l,r,m,v;
}s[MAXN<<2];
void push_up(int num)
{
    s[num].v=max(s[num<<1].v,s[num<<1|1].v);
}
void build(int l,int r,int num)
{
    int m=(l+r)>>1;
    s[num].l=l;
    s[num].r=r;
    s[num].m=m;
    if(r-l==1)s[num].v=0;
    else
    {
        build(l,m,num<<1);
        build(m,r,num<<1|1);
        push_up(num);
    }
}
void update(int p,int k,int num)
{
    if(s[num].l==p && s[num].r==p+1)
    {
        s[num].v=max(s[num].v,k);
        return;
    }
    if(p<s[num].m)update(p,k,num<<1);
    else update(p,k,num<<1|1);
    push_up(num);
}
int query(int l,int r,int num)
{
    if(s[num].l==l && s[num].r==r)return s[num].v;
    if(r<=s[num].m)return query(l,r,num<<1);
    if(l>=s[num].m)return query(l,r,num<<1|1);
    return max(query(l,s[num].m,num<<1),query(s[num].m,r,num<<1|1));
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&b[i]);
    for(int i=1;i<=n;i++)scanf("%d",&l[i]);
    for(int i=1;i<=n;i++)scanf("%d",&r[i]);
    modify(b,t,n);
    modify(l,t,n);
    modify(r,t,n);
    for(int i=1;i<=n;i++)
    {
        p[i]=person(b[i],l[i],r[i]);
    }
    sort(p+1,p+n+1);
    build(1,n+2,1);
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        if(query(p[i].l+1,n+2,1)>p[i].r)ans++;
        update(p[i].l,p[i].r,1);
    }
    printf("%d\n",ans);
    return 0;
}


### Codeforces 1487D Problem Solution The problem described involves determining the maximum amount of a product that can be created from given quantities of ingredients under an idealized production process. For this specific case on Codeforces with problem number 1487D, while direct details about this exact question are not provided here, similar problems often involve resource allocation or limiting reagent type calculations. For instance, when faced with such constraints-based questions where multiple resources contribute to producing one unit of output but at different ratios, finding the bottleneck becomes crucial. In another context related to crafting items using various materials, it was determined that the formula `min(a[0],a[1],a[2]/2,a[3]/7,a[4]/4)` could represent how these limits interact[^1]. However, applying this directly without knowing specifics like what each array element represents in relation to the actual requirements for creating "philosophical stones" as mentioned would require adjustments based upon the precise conditions outlined within 1487D itself. To solve or discuss solutions effectively regarding Codeforces' challenge numbered 1487D: - Carefully read through all aspects presented by the contest organizers. - Identify which ingredient or component acts as the primary constraint towards achieving full capacity utilization. - Implement logic reflecting those relationships accurately; typically involving loops, conditionals, and possibly dynamic programming depending on complexity level required beyond simple minimum value determination across adjusted inputs. ```cpp #include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for(int i=0;i<n;++i){ cin>>a[i]; } // Assuming indices correspond appropriately per problem statement's ratio requirement cout << min({a[0], a[1], a[2]/2LL, a[3]/7LL, a[4]/4LL}) << endl; } ``` --related questions-- 1. How does identifying bottlenecks help optimize algorithms solving constrained optimization problems? 2. What strategies should contestants adopt when translating mathematical formulas into code during competitive coding events? 3. Can you explain why understanding input-output relations is critical before implementing any algorithmic approach? 4. In what ways do prefix-suffix-middle frameworks enhance model training efficiency outside of just tokenization improvements? 5. Why might adjusting sample proportions specifically benefit models designed for tasks requiring both strong linguistic comprehension alongside logical reasoning skills?
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值