Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

来一发线段树

Posted by marszed at 2019-01-12 22:14:36 on Problem 3250
#include<iostream>
#include<algorithm>
#define mid (l+r)/2
using namespace std;

typedef long long LL;
const int maxn = 8e4 + 10;

int n;
int a[maxn], cover[maxn << 2];
LL ans;

void pushup(int rt) {
	cover[rt] = max(cover[rt << 1], cover[rt << 1 | 1]);
}

void update(int rt, int l, int r, int pos, int val) {
	if (l == r) {
		cover[rt] = val;
		return;
	}
	if (pos <= mid) update(rt << 1, l, mid, pos, val);
	else update(rt << 1 | 1, mid + 1, r, pos, val);
	pushup(rt);
}

int query(int rt, int l, int r, int qstart, int qend, int val) {
	if (l == r) {
		if (cover[rt] < val) return r;
		return -1;
	}
	if (qstart <= l&&r <= qend) {
		if (cover[rt] < val) return r;
		if (cover[rt << 1] >= val) return query(rt << 1, l, mid, qstart, qend, val);
		int ansR = query(rt << 1 | 1, mid + 1, r, qstart, qend, val);
		if (ansR == -1) return mid;
		return ansR;
	}
	if (qstart <= mid) {
		int ansL = query(rt << 1, l, mid, qstart, qend, val);
		if (ansL == -1) return -1;
		if (ansL != mid) return ansL;
		int ansR = query(rt << 1 | 1, mid + 1, r, qstart, qend, val);
		if (ansR != -1) return ansR;
		return ansL;
	}
	return query(rt << 1 | 1, mid + 1, r, qstart, qend, val);
}

int main()
{
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%d", a + i);
	update(1, 1, n, n, a[n]);
	for (int i = n - 1; i >= 1; i--) {
		int tmp = query(1, 1, n, i + 1, n, a[i]);
		if (tmp != -1) ans += 1LL * (tmp - i);
		update(1, 1, n, i, a[i]);
	}
	printf("%lld\n", ans);
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator