Hdoj1754 I hate it [Line Segment tree]

Source: Internet
Author: User

I hate it Time Limit: 9000/3000 MS (Java/others) memory limit: 32768/32768 K (Java/Others) Total submission (s): 35417 accepted submission (s): 13958

Problem description many schools have a popular habit. Teachers like to ask, from xx to xx, what is the highest score.
This made many students very disgusted.

Whether you like it or not, what you need to do now is to write a program to simulate the instructor's inquiry according to the instructor's requirements. Of course, teachers sometimes need to update their scores.
Input this question contains multiple groups of tests, please process until the end of the file.
In the first row of each test, there are two positive integers n and M (0 <n <= 200000,0 <m <5000), representing the number of students and the number of operations respectively.
Student ID numbers are separated from 1 to n.
The second row contains N integers, indicating the initial score of the N students. The number of I represents the score of the students whose ID is I.
Next there are m rows. Each line has a character C (only 'q' or 'U'), and two positive integers A and B.
When C is 'Q', it indicates that this is a query operation. It asks the students whose ID ranges from A to B (including a and B) about the highest score.
When C is 'U', it indicates that this is an update operation. You must change the score of students whose ID is A to B.
 
Output outputs the highest score in one row for each query operation.
Sample Input
5 61 2 3 4 5Q 1 5U 3 6Q 3 4Q 4 5U 2 9Q 1 5
 
Sample output
5659HintHuge input,the C function scanf() will work better than cin

This proves that macro definition is slower than custom function. But I don't know why. Then I asked the teacher, and replied, "The function call parameter is calculated only once. The macro definition is not calculated, and the calculation is performed every time during the runtime ."

Run ID Submit time Judge status Pro. ID EXE. Time EXE. Memory Code Len. Language Author
10955721 11:20:55 Accepted 1754 1109 Ms 7156 K 1513 B G ++ Changmu
10955685 11:18:05 Time limit exceeded 1754 3000 Ms 7144 K 1491 B G ++ Changmu

AC:

#include <stdio.h>#define maxn 200002struct Node{int left, right;int max;} tree[maxn << 2];int stu[maxn];int MAX(int a, int b){return a > b ? a : b;}void build(int left, int right, int rt){tree[rt].left = left; tree[rt].right = right;if(left == right){tree[rt].max = stu[left]; return;}int mid = (left + right) >> 1;build(left, mid, rt << 1);build(mid + 1, right, rt << 1 | 1);tree[rt].max = MAX(tree[rt<<1].max, tree[rt<<1|1].max);}void update(int pos, int val, int rt){if(tree[rt].left == tree[rt].right){tree[rt].max = val; return;}int mid = (tree[rt].left + tree[rt].right) >> 1;if(pos <= mid) update(pos, val, rt << 1);else update(pos, val, rt << 1 | 1);tree[rt].max = MAX(tree[rt<<1].max, tree[rt<<1|1].max);}int query(int left, int right, int rt){if(tree[rt].left == left && right == tree[rt].right){return tree[rt].max;}int mid = (tree[rt].left + tree[rt].right) >> 1;if(right <= mid) return query(left, right, rt << 1);else if(left > mid) return query(left, right, rt << 1 | 1);return MAX(query(left, mid, rt << 1), query(mid+1, right, rt<<1|1));}int main(){int N, M, i, m, n;char com[2];while(scanf("%d%d", &N, &M) == 2){for(i = 1; i <= N; ++i)scanf("%d", stu + i);build(1, N, 1);while(M--){scanf("%s%d%d", com, &m, &n);if(com[0] == 'U') update(m, n, 1);else printf("%d\n", query(m, n, 1));}}return 0;}


TLE ::

#include <stdio.h>#define MAX(a, b) a > b ? a : b#define maxn 200002struct Node{    int lson, rson;    int max;} tree[maxn << 2];int stu[maxn];void build(int left, int right, int rt){    tree[rt].lson = left; tree[rt].rson = right;    if(left == right){        tree[rt].max = stu[left]; return;    }        int mid = (left + right) >> 1;    build(left, mid, rt << 1);    build(mid + 1, right, rt << 1 | 1);        tree[rt].max = MAX(tree[rt<<1].max, tree[rt<<1|1].max);}void update(int pos, int val, int rt){    if(tree[rt].lson == tree[rt].rson){        tree[rt].max = val; return;    }        int mid = (tree[rt].lson + tree[rt].rson) >> 1;    if(pos <= mid) update(pos, val, rt << 1);    else update(pos, val, rt << 1 | 1);        tree[rt].max = MAX(tree[rt<<1].max, tree[rt<<1|1].max);}int query(int left, int right, int rt){    if(tree[rt].lson == left && right == tree[rt].rson){        return tree[rt].max;    }        int mid = (tree[rt].lson + tree[rt].rson) >> 1;    if(right <= mid) return query(left, right, rt << 1);    else if(left > mid)         return query(left, right, rt << 1 | 1);    return MAX(query(left, mid, rt << 1), query(mid+1, right, rt<<1|1));}int main(){    int N, M, i, m, n;    char com[2];    while(scanf("%d%d", &N, &M) == 2){        for(i = 1; i <= N; ++i)            scanf("%d", stu + i);        build(1, N, 1);                while(M--){            scanf("%s%d%d", com, &m, &n);                        if(com[0] == 'U') update(m, n, 1);            else printf("%d\n", query(m, n, 1));        }    }    return 0;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.