CodeForces 91B Queue【线段树】

本文介绍了一个利用线段树数据结构解决特定队列问题的方法。该问题涉及计算队列中每只海象因前方年轻海象而产生的不满程度。通过维护区间最小值,算法能高效地处理这一问题。
Queue
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

There are n walruses standing in a queue in an airport. They are numbered starting from the queue's tail: the 1-st walrus stands at the end of the queue and the n-th walrus stands at the beginning of the queue. The i-th walrus has the age equal to ai.

The i-th walrus becomes displeased if there's a younger walrus standing in front of him, that is, if exists such j (i < j), that ai > aj. Thedispleasure of the i-th walrus is equal to the number of walruses between him and the furthest walrus ahead of him, which is younger than the i-th one. That is, the further that young walrus stands from him, the stronger the displeasure is.

The airport manager asked you to count for each of n walruses in the queue his displeasure.

Input

The first line contains an integer n (2 ≤ n ≤ 105) — the number of walruses in the queue. The second line contains integers ai (1 ≤ ai ≤ 109).

Note that some walruses can have the same age but for the displeasure to emerge the walrus that is closer to the head of the queue needs to be strictly younger than the other one.

Output

Print n numbers: if the i-th walrus is pleased with everything, print "-1" (without the quotes). Otherwise, print the i-th walrus's displeasure: the number of other walruses that stand between him and the furthest from him younger walrus.

Sample Input

Input
6
10 8 5 3 50 45
Output
2 1 0 -1 0 -1 
Input
7
10 4 6 3 2 8 15
Output
4 2 1 0 -1 -1 -1 
Input
5
10 3 1 10 11
Output
1 0 -1 -1 -1 
 
        
     线段树维护区间最小值, 循环一遍,每次找到比这个数小且在最右端的数,将左边处理过的数可以更新成INF并更新最小值。那么查询之前, 先询问整个数组的最小是和当前值的比较,当前值大于最新最小值才有查询的必要,而且右边不成立就一定在左边。
 
        
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<climits>
#include<string>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<algorithm>
using namespace std;
#define rep(i,j,k)for(i=j;i<k;i++)
#define per(i,j,k)for(i=j;i>k;i--)
#define MS(x,y)memset(x,y,sizeof(x))
#define max(a,b) a>b?a:b
#define min(a,b) a<b?a:b
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long LL;
const int INF=0x7ffffff;
#define lson rt<<1, l, m
#define rson rt<<1|1, m+1, r
const int maxn = 100005;
const int MAX = 0x3f3f3f3f;
int n,i,j,k,b;
int cnt;
int a[maxn],mi[maxn<<2],ans[maxn];

void up(int rt)
{
    mi[rt]=min(mi[rt<<1],mi[rt<<1|1]);
}

void build(int rt ,int l,int r)
{
    if(l==r){
        scanf("%d",&mi[rt]);
        a[l]=mi[rt];
        return;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    up(rt);
}

void query(int rt,int l,int r)
{
    if(l==r){
        ans[k++]=l-b-1;
        return;
    }
    int m=(l+r)>>1;
    if(mi[rt<<1|1]<cnt)query(rson);
    else query(lson);
}

void update(int rt,int l,int r)
{
    if(l==r)mi[rt]=MAX;
    else {
        int m=(l+r)>>1;
        if(b<=m)update(lson);
        else update(rson);
        up(rt);
    }
}

int main()
{
    scanf("%d",&n);
    build(1,1,n);
    for(int i=1;i<=n;i++){
        cnt=a[i],b=i;
        update(1,1,n);
        if(mi[1]>=cnt)ans[k++]=-1;
        else query(1,1,n);
    }
    for(i=0;i<k;i++)printf("%d ",ans[i]);
    return 0;
}


<think>好的,我现在需要处理用户关于Codeforces题型总结和解题技巧的请求。首先,回顾之前的对话历史,用户之前询问了ACM学习路线,我给出了分阶段的学习计划和资源推荐。现在用户转向Codeforces的具体题型和解题技巧,这表明他们可能正在准备竞赛,需要更针对性的策略。 首先,我需要确定用户的需求。用户想要的是Codeforces平台上各类题型的分类总结以及对应的解题思路。他们可能希望了解常见的题型类别,每个类别的特点,以及解决这些题目的常用技巧和算法。此外,用户可能还希望得到一些具体的例题和代码示例,以便更好地理解和应用这些技巧。 接下来,我需要参考用户提供的引用内容。引用[1]中提到了算法基础,包括复杂度分析、递归、分治、贪心、排序、前缀和、差分、二分、三分、倍增等。这些内容在Codeforces题目中确实常见,因此需要将这些算法和技巧与具体的题型对应起来,进行分类归纳。 我需要确保回答结构清晰,分点列出各个题型类别,每个类别下包括题型特点、解题技巧、例题及解析。同时,按照用户之前的要求,使用中文回答,行内数学公式用$...$,独立公式用$$...$$,并正确使用LaTeX语法。此外,需要生成相关问题,并在适当的地方添加引用标识。 在整理题型时,要覆盖Codeforces常见的题目类型,如贪心、动态规划、图论、数论、数据结构、构造题、二分/三分、字符串处理、计算几何等。每个类别需要简要说明其特点,解题时的常用方法,并举出具体的题目例子,最好附上题目编号,方便用户查找练习。 同时,要注意结合用户之前的学习路线,推荐相关的资源,比如《算法竞赛入门经典》中的例题,或者在线平台如Codeforces本身的训练建议。此外,可能需要提到一些实战技巧,比如如何快速识别题型、如何优化代码等。 需要避免的是过于泛泛而谈,要尽量具体,给出实际可操作的技巧。例如,在贪心题中,可以提到邻项交换法或后悔法的具体应用;在动态规划中,强调状态设计和转移方程的优化方法。对于每个技巧,最好能对应一个例题,说明如何应用该技巧解决问题。 另外,考虑到用户可能需要代码示例,可以在适当的位置插入简短的代码片段,如动态规划或二分的典型实现,但需注意代码的准确性和简洁性,避免冗长。 最后,生成的相关问题需要围绕用户可能的后续疑问展开,比如如何提高刷题效率,特定题型的深入分析方法,或者推荐Codeforces上的训练计划等。这些问题应自然延伸当前主题,帮助用户进一步探索。 现在,需要检查是否覆盖了用户的所有需求,是否结构清晰,是否引用了之前的资源(如引用[^1]),并确保所有数学公式的正确格式。同时,确保回答真实可靠,基于常见的竞赛知识和经验,避免错误的信息。</think>### Codeforces题型总结与解题技巧分类指南 #### 一、高频核心题型分类 1. **贪心算法** - **题型特点**:决策序列中局部最优选择可推导全局最优解,常见于区间调度、资源分配问题 - **解题技巧**: - 邻项交换法(证明交换后更优):如$n$个数的最大拼接问题(CF 1385B) - 后悔法(优先队列维护候选集):CF 1526C(药水选择) ```cpp // 后悔法模板 priority_queue<int, vector<int>, greater<int>> pq; for(int x : arr){ sum += x; pq.push(x); while(sum < 0){ sum -= pq.top(); pq.pop(); } } ``` - 时间轴处理:CF 1481C(栅栏涂色时序处理)[^1] 2. **动态规划** - **状态设计模式**: - 线性DP:$dp[i]$表示前$i$个元素的最优解(CF 474D 花朵问题) - 状态压缩DP:用位运算表示集合状态(CF 580D 美食家问题) - 树形DP:结合DFS遍历处理子树关系(CF 1092F 树权值分配) - **优化技巧**: - 斜率优化:维护凸包降低时间复杂度至$O(n)$(CF 319C 木材切割) - 滚动数组:空间复杂度优化至$O(1)$(CF 1114D 颜色填充) 3. **图论算法** - **高频考点**: | 算法类型 | 应用场景 | 例题 | |----------------|-----------------------------------|----------------| | 拓扑排序 | 任务调度/依赖解析 | CF 1385E | | 强连通分量 | 缩点处理环结构 | CF 427C | | 网络流 | 二分图匹配/资源分配 | CF 1139E | | 最短路变形 | 分层图/边权特殊处理 | CF 1473E | 4. **数论问题** - **核心工具**: - 欧拉定理:处理模指数运算(CF 615D 质因子乘积) - 中国剩余定理:同余方程组求解(CF 687B) - 莫比乌斯反演:统计互质对数量(CF 547C) - **特殊技巧**: $$ \sum_{d|n}\mu(d) = \begin{cases} 1 & n=1 \\ 0 & n>1 \end{cases} $$ 用于快速计算GCD相关问题 #### 二、特征题型专项突破 1. **构造题(Constructive Algorithms)** - **识别特征**:输出满足特定条件的结构而非数值结果 - **经典解法**: - 奇偶性构造:CF 1421B(矩阵路径奇偶性) - 平衡分配法:CF 1343D(调整数组使对称元素相等) - 极端值构造:CF 1375E(通过逆序操作排序数组) 2. **交互题(Interactive Problems)** - **解题框架**: ```python low, high = 1, 1e18 while low <= high: mid = (low + high) // 2 print(f"? {mid}") response = input() if response == ">=": low = mid + 1 else: high = mid - 1 print(f"! {high}") ``` - **注意事项**: - 刷新输出缓冲区(C++用`cout << endl`) - 错误处理:CF 1520F2(二进制猜数容错机制) 3. **数据结构题** - **题型矩阵**: | 数据结构 | 适用场景 | 优化技巧 | |----------------|-------------------------------|---------------------------| | 线段树 | 区间查询/更新 | 延迟标记(lazy propagation)| | 并查集 | 连通性检测 | 路径压缩+按秩合并 | | 字典树 | 前缀匹配统计 | 二进制位处理 | | 单调队列 | 滑动窗口最值 | 淘汰非最优候选 | #### 三、实战技巧体系 1. **复杂度预判法**: - 输入规模$n$与可接受时间复杂度对应表: $$ \begin{array}{c|c} n \leq 10^6 & O(n) \\ n \leq 10^5 & O(n \log n) \\ n \leq 10^4 & O(n^2) \\ n \leq 20 & O(2^n) \\ \end{array} $$ 2. **Debug技巧**: - 边界测试:$n=0,1$等极端情况 - 对拍验证:生成随机数据与暴力程序对比 - 输出中间状态:使用`cerr`输出调试信息 3. **代码模板管理**: - 建议维护以下模板库: ```text /templates ├── graph │ ├── dijkstra.cpp │ └── tarjan_scc.cpp ├── math │ ├── matrix_fastpow.cpp │ └── fft_template.cpp └── ds ├── segment_tree.cpp └── union_find.cpp ``` #### 四、经典例题解析 1. **CF 1485D - Multiples and Power Differences** - **题型**:构造题+数论 - **关键思路**: 构造矩阵元素$a_{i,j} = 720720 + (i+j)\%2 \times (a_{i,j}^4)$ 保证相邻元素差为四次方数 2. **CF 1513C - Add One** - **解法**:动态规划预处理 定义$dp[d][m]$表示数字$d$经过$m$次操作的位数 状态转移: $$ dp[d][m] = \begin{cases} 1 & m=0 \\ dp[1][m-1] + dp[0][m-1] & d=9 \\ dp[d+1][m-1] & otherwise \end{cases} $$
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值