Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
来一发线段树#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator