链接: http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1752
Description
There are n webpages, each of which has its respective page rank. The content is constantly updated and page rank is also constantly changing. Can you immediately find the page with highest weight?
Note: We set the page numbers from 1 to n.
Input
There are multiple test cases, process to the end of file.
For each test case:
Line 1: This line contains an integer n, indicating the number of pages.
Line 2: This line contains n integer, pr1, pr2, pr3, ..., prn, representing the page rank of each page.
Line 3: This line contains an integer q, indicating the number of operations.
Line 4..q+3: Each line indicating an operation.
There are two operation formats:
C i pr : change ith page's page rank to pr.
Q : query a page with the highest page rank, output the page's number and its page rank.
Limits
1<=n<=100,000
1<=q<=200,000
0<=pr<=1,000,000,000
Output
For each case:
Each "Q" query outputs a line containing the page number and page rank of the page of the highest page rank. If there is more than one page which has the highest page rank, output the page with largest number.
After each test case, output a blank line.
Sample Input
5
30 9 0 20 5
6
C 1 19
C 3 22
C 3 4
Q
C 4 12
Q
5
13 10 20 7 7
7
C 4 22
C 4 21
C 4 11
C 3 10
C 5 15
C 2 17
Q
Sample Output
4 20
1 19
2 17
代码如下:
/** * 单点更新(单点值直接替换为另一个值)),查询区间最大值 */ #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <climits> #include <set> #include <map> #include <algorithm> #define MAXN 111111 #define LSON l, m, rt<<1 #define RSON m+1, r, (rt<<1)|1 #define RST(N)memset(N, 0, sizeof(N)) using namespace std; int maxn[MAXN<<2], posn[MAXN<<2]; int n, m, a, b; char op[2]; ///把当前节点的信息更新到父节点 void pushUp(int rt) { if(maxn[rt<<1] > maxn[rt<<1|1]) { maxn[rt] = maxn[rt<<1]; posn[rt] = posn[rt<<1]; }else { maxn[rt] = maxn[rt<<1|1]; posn[rt] = posn[rt<<1|1]; } } ///建树 void build(int l, int r, int rt) { if(l == r) { scanf("%d", &maxn[rt]); posn[rt] = l; }else { int m = (l + r) >> 1; build(LSON), build(RSON); pushUp(rt); } } ///更新单点值(单点值直接替换为另一个值) void update(int p, int val, int l, int r, int rt) { if(l == r) maxn[rt] = val; else { int m = (l + r) >> 1; if(p <= m) update(p, val, LSON); else update(p, val, RSON); pushUp(rt); } } ///查询区间最大值 int query(int L, int R, int l, int r, int rt, int *pos) { if(L <= l && r <= R) { *pos = posn[rt]; return maxn[rt]; }else { int m = (l + r) >> 1; int ret1 = INT_MIN, ret2 = INT_MIN; int pa, pb; int *pos1 = &pa, *pos2 = &pb; if(L <= m) ret1 = query(L, R, LSON, pos1); if(R > m) ret2 = query(L, R, RSON, pos2); if(ret1 > ret2) *pos = pa; else *pos = pb, ret1 = ret2; return ret1; } } int main() { while(~scanf("%d", &n)) { build(1, n, 1); scanf("%d", &m); while(m--) { scanf("%s", op); if(op[0] == 'Q') { int pos; printf("%d %d\n", pos, query(1, n, 1, n, 1, &pos)); }else { scanf("%d%d", &a, &b); update(a, b, 1, n, 1); } } printf("\n"); } return 0; }