题目链接:hdoj 5200 Trees
题意:给你
n
棵树以及每棵树的高度,有
块的定义:
一、临近的树在同一个块;
二、若
x
与
思路:我们先按高度升序
sort
,这样处理第
i
棵树时,它的状态来源于第
AC 代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#define PI acos(-1.0)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define fi first
#define se second
#define ll o<<1
#define rr o<<1|1
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 5*1e4+10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
void getmax(int &a, int b) {a = max(a, b); }
void getmin(int &a, int b) {a = min(a, b); }
void add(LL &x, LL y) { x += y; x %= MOD; }
bool cmp(pii a, pii b) {
return a.fi != b.fi ? a.fi < b.fi : a.se < b.se;
}
pii a[MAXN];
bool cut[MAXN];
int ans[MAXN];
int main()
{
int N, Q;
while(scanf("%d%d", &N, &Q) != EOF)
{
for(int i = 0; i < N; i++) {
scanf("%d", &a[i].fi);
a[i].se = i;
cut[i] = false;
}
sort(a, a+N, cmp); int cnt = 1;
for(int i = 0; i < N; i++)
{
int id = a[i].se;
int cutnum;
if(id == 0) {
if(cut[id+1]) {
cnt--;
}
else {
//cnt++;
}
}
else if(id == N-1) {
if(cut[id-1]) {
cnt--;
}
else {
//cnt++;
}
}
else {
if(cut[id+1] && cut[id-1]) {
cnt--;
}
else if(!cut[id-1] && !cut[id+1]) {
cnt++;
}
}
cut[id] = true; ans[id] = cnt;
//cout << id << " " << ans[id] << endl;
}
while(Q--)
{
int v; scanf("%d", &v);
int p = lower_bound(a, a+N, pii(v+1, -1)) - a;
if(p == 0) cout << 1 << endl;
else {
int id = a[p-1].se;
cout << ans[id] << endl;
}
}
}
return 0;
}