HLG 1752 Page Rank (线段树)

链接: 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;
}

HLG 1752 Page Rank (线段树),布布扣,bubuko.com

时间: 2024-11-12 02:26:31

HLG 1752 Page Rank (线段树)的相关文章

HLG 1808 绘画 (哈希 + map + 线段树)

链接: http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1808 Description 小胖子最近喜欢上了画画,但是他的画画技术很差,只能从零开始,小胖子在一条线上画了很多彩色的球,但是觉得不好看,就想修改. 小胖子站在画前想了又想,他有时会将一些连续的点涂成相同的颜色.但他感觉累的时候,就会无聊的数数某个颜色在某段区间内出现的次数. Input 每组数据的第一行输入两个正整数n和m(1&l

HLG 1524 最大 (离散化线段树)

链接: http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1524 Description 给包含n个数的初始序列,A[1], A[2], ..., A[n]. 给q多个操作,操作如下: 1 a b v, 把[a, b] 的值改为v,即A[a] = A[a+1] = ... = A[b] = v. 2 a b, 查询[a, b] 之间的相同数的连续和最大值. 在[a, b]区间内,相同数的连续和

(线段树+并查集) Codeforces Round #416 (Div. 2) E Vladik and Entertaining Flags

In his spare time Vladik estimates beauty of the flags. Every flag could be represented as the matrix n?×?m which consists of positive integers. Let's define the beauty of the flag as number of components in its matrix. We call component a set of cel

【BZOJ2733】永无乡[splay启发式合并or线段树合并]

题目大意:给你一些点,修改是在在两个点之间连一条无向边,查询时求某个点能走到的点中重要度第k大的点.题目中给定的是每个节点的排名,所以实际上是求第k小:题目求的是编号,不是重要度的排名.我一开始差点被这坑了. 网址:http://www.lydsy.com/JudgeOnline/problem.php?id=2733 这道题似乎挺经典的(至少我看许多神犇很早就做了这道题).这道题有两种写法:并查集+(splay启发式合并or线段树合并).我写的是线段树合并,因为--splay不会打+懒得学.

HDU5008 Boring String Problem(后缀数组 + 二分 + 线段树)

题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5008 Description In this problem, you are given a string s and q queries. For each query, you should answer that when all distinct substrings of string s were sorted lexicographically, which one is

BZOJ_3196_二逼平衡树(树套树:线段树+Treap)

描述 可以处理区间问题的平衡树. 分析 树套树.可以用线段树套Treap.人生第一道树套树的题... op1:如果在整区间,直接在该区间的treap上求解.否则分两个区间求解,然后相加.最后+1. op2:这个不太好直接做,可以二分,每次假定一个值,用这个值去做op1,以此求得一个rank=k+1的数,求rank=k的数等价与求这个数的前驱pre. op3:先删后加. op4&op5:如果在整区间,直接在该区间的treap上求解,否则分量个区间求解,pre取最大值,suc取最小值.注意有些数在有

ZOJ2112 动态区间Kth(单点修改) 线段树+Treap写法

---恢复内容开始--- 题意:给出一个序列和操作次数, 每次操作修改一个位置的数 或者 询问一个区间第k小的数 分析:单点修改可以考虑线段树, 区间排名可以用平衡树 所以线段树+Treap 用一颗线段树将序列划分 每颗Treap中插入的是对应区间的数 在每个数加入时, 顺便将该数插入线段树中包含该位置的那些区间上的Treap即可 单点修改同理, 将所有包含要修改的位置的区间上的Treap都删去原数并插入新数 询问第k小的数:由于询问的区间不一定恰好对应某棵Treap, 不便直接求出名次, 但是

BZOJ 3196 二逼平衡树 线段树+treap

题意:链接 方法:线段树+treap的模板题 题解: 首先是对于整个树的定义,其实treap部分并没有什么区别,只不过是单root改变为多root而已. #include <math.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <algorithm> #define lson l,mid,rt<<1 #define rson mid+1,

hdu 5195 线段树

题目描述:给定一个DAG,求出允许移除最多K条边后的字典序最大的拓扑序列. 思路:线段树,每次找入度不超过K的最大编号的顶点,将此顶点从图中移除,重复操作n次即可得到结果. 吐槽:当时打BC的时候写出了一个直接贪心+拓扑排序的复杂度为O(n)的错误代码(当时还没有意识到是错误代码),交到hdu oj上居然给过了,后来上西方文化的时候和csc得瑟,说那个题我300+ms就给过了,在best solutions里面Rank 1,复杂度还是O(n)的,然后和csc说了我的想法以后才发现这思路TM根本就