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 |
线段树水过,就是时间有点问题,3313+MS,哪位大牛能指点一下,OrzOrzOrz#include <stdio.h> #define inf 0x7fffffff #define N 50010 struct node { int l, r; int min, max; }node[N*4]; int num[N], nmax, nmin; void creat(int t, int l, int r) { node[t].l = l; node[t].r = r; node[t].min = inf; node[t].max = -inf; if(l == r - 1) { if(num[l] > num[r]) { node[t].min = num[r]; node[t].max = num[l]; } else { node[t].min = num[l]; node[t].max = num[r]; } return; } int mid = (l + r) >> 1; creat(t << 1, l, mid); creat(t << 1 | 1, mid , r); node[t].min = node[t << 1].min < node[t << 1 | 1].min ? node[t << 1].min : node[t << 1 | 1].min; node[t].max = node[t << 1].max > node[t << 1 | 1].max ? node[t << 1].max : node[t << 1 | 1].max; } void query(int t, int l, int r) { if(node[t].l >= l && node[t].r <= r) { nmax = node[t].max > nmax ? node[t].max : nmax; nmin = node[t].min < nmin ? node[t].min : nmin; return ; } if(node[t].l == node[t].r - 1) return; int mid = (node[t].l + node[t].r) >> 1; if(l >= mid) query(t << 1 | 1, l, r); else if(r <= mid) query(t << 1, l, r); else { query(t << 1, l, mid); query(t << 1 | 1, mid, r); } } int main() { int n, q, i; //freopen("data.in", "r", stdin); scanf("%d%d", &n, &q); for(i = 1; i <= n; i++) scanf("%d", &num[i]); creat(1, 1, n); while(q--) { int x, y; scanf("%d%d", &x, &y); if(x == y) { printf("0\n"); continue; } nmax = -inf; nmin = inf; query(1, x, y); printf("%d\n", nmax - nmin); } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator