Hdu1754 I hate it (splay or 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): 27434 accepted submission (s): 10889


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 
 


Authorlinle

My first splay ~

Question: Chinese question, not explained.

Question Analysis: Single Point update, interval query, line segment tree entry-level questions.

However, this question can be done using a lot of data structures.

Today, I used this question to practice the first splay, although it is not necessary to use a cow.

Directly run the Code:

# Include <iostream> # include <cstdio> # include <cstring> using namespace STD; const int n = 210000; const int INF = 0x3f3f3f; struct node {int F, L, r, Val, MX, POs, size;} tree [N]; int next [N]; int LCM [N]; int M, N; int max (int, int B) {return A> B? A: B;} void Init () {tree [0]. size = 0; For (INT I = 0; I <n-1; I ++) next [I] = I + 1;} int newnode (INT POs, int key) {int P = next [0]; next [0] = next [p]; tree [p]. val = key; tree [p]. pos = Pos; tree [p]. F = tree [p]. L = tree [p]. r = 0; tree [p]. size = 1; return P;} void delnode (INT p) {next [p] = next [0]; next [0] = P;} void push_up (int rt) {If (! RT) return; int LL = tree [RT]. l; int RR = tree [RT]. r; tree [RT]. size = 1 + (tree [ll]. size + tree [RR]. size); tree [RT]. MX = max (tree [RT]. val, max (tree [ll]. MX, tree [RR]. MX);} void Zig (int x) {int P = tree [X]. f; tree [p]. L = tree [X]. r; If (tree [X]. r) tree [tree [X]. r]. F = P; push_up (p); tree [X]. R = P; push_up (x); tree [X]. F = tree [p]. f; tree [p]. F = x; If (tree [tree [X]. f]. pos> tree [X]. pos) // left child tree [Tree [X]. f]. L = x; else tree [tree [X]. f]. R = x;} void zag (int x) {int P = tree [X]. f; tree [p]. R = tree [X]. l; If (tree [X]. l) tree [tree [X]. l]. F = P; push_up (p); tree [X]. L = P; push_up (x); tree [X]. F = tree [p]. f; tree [p]. F = x; If (tree [tree [X]. f]. pos> tree [X]. pos) // left child tree [tree [X]. f]. L = x; else tree [tree [X]. f]. R = x;} int splay (int x, int FA) // put the x node splay under the FA node {int P; while (tree [X]. f! = FA) {P = tree [X]. f; // parent node if (tree [p]. F = FA) {If (tree [p]. L = x) // left Tree Zig (x); If (tree [p]. R = x) zag (x);} else {int G = tree [p]. f; // grandparent node if (tree [g]. L = P & tree [p]. L = x) // ll {Zig (p); zig (x);} else if (tree [g]. L = P & tree [p]. R = x) // LR {zag (x); zig (x);} else if (tree [g]. R = P & tree [p]. L = x) // RL {Zig (x); zag (x);} else if (tree [g]. R = P & tree [p]. R = X) // RR {zag (p); zag (x) ;}}return x ;}int build (int l, int R, int FA) {If (L> r) return 0; int mid = (L + r)> 1; int P = newnode (MID, LCM [Mid]); tree [p]. pos = mid; tree [p]. F = fa; tree [p]. L = build (L, mid-1, P); tree [p]. R = build (Mid + 1, R, P); push_up (p); Return P;} void prepare (Int & root) {root = newnode (0,-INF ); tree [root]. R = newnode (n + 1,-INF); tree [tree [root]. r]. F = root; tree [tree [root]. r]. l = Build (1, n, tree [root]. R);} int find (INT POs, int root) {If (! Root) return 0; If (Pos = tree [root]. pos) return root; If (Pos> tree [root]. pos) return find (Pos, tree [root]. r); else return find (Pos, tree [root]. l);} void Update (INT POs, int Val, Int & root) {int q = find (Pos, root); If (q) root = splay (Q, tree [root]. f); else return; tree [root]. val = val; If (tree [root]. val> tree [root]. MX) tree [root]. MX = tree [root]. val;} int main () {int root; int I, a, B; char op [3]; while (~ Scanf ("% d", & N, & M) {Init (); for (I = 1; I <= N; I ++) scanf ("% d", & LCM [I]); Prepare (Root); While (M --) {scanf ("% s", OP ); scanf ("% d", & A, & B); If (OP [0] = 'U') Update (a, B, root ); else {int TP = find (A-1, root); If (TP) root = splay (TP, tree [root]. f); TP = find (B + 1, root); If (TP) tree [root]. R = splay (TP, root); // tree [root]. r); printf ("% d \ n", tree [tree [root]. r]. l]. MX) ;}}return 0 ;}// 671ms7340k

The line segment Tree Code is attached:

#include <iostream>#include<cstdio>#include<cstring>using namespace std;const int N = 200001;int tree[N<<2];int lcm[N];int m,n;int Max(int a,int b){    return a > b?a:b;}void build(int num,int s,int e){    if(s == e)    {        tree[num] = lcm[s];        return;    }    int mid = (s + e)>>1;    build(num<<1,s,mid);    build(num<<1|1,mid + 1,e);    tree[num] = Max(tree[num<<1],tree[num<<1|1]);}void update(int num,int s,int e,int pos,int val){    if(s == e)    {        tree[num] = val;        return;    }    int mid = (s + e)>>1;    if(pos <= mid)        update(num<<1,s,mid,pos,val);    else        update(num<<1|1,mid + 1,e,pos,val);    tree[num] = Max(tree[num<<1],tree[num<<1|1]);}int query(int num,int s,int e,int l,int r){    if(s == l && r == e)        return tree[num];    int mid = (s + e)>>1;    if(r <= mid)        return query(num<<1,s,mid,l,r);    else    {        if(l > mid)            return query(num<<1|1,mid + 1,e,l,r);        else            return Max(query(num<<1,s,mid,l,mid),query(num<<1|1,mid + 1,e,mid + 1,r));    }}int main(){    int i,a,b,c;    char op[4];    while(~scanf("%d%d",&n,&m))    {        for(i = 1;i <= n;i ++)            scanf("%d",&lcm[i]);        build(1,1,n);        while(m --)        {            scanf("%s",op);            scanf("%d%d",&a,&b);            if(op[0] == 'U')                update(1,1,n,a,b);            else                printf("%d\n",query(1,1,n,a,b));        }    }    return 0;}//218MS3068K

This line segment tree seems to be more efficient.

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.