Highest amount

Test input comprising a plurality of sets of data.
The first input line of each of two positive integers N and M (0 <N <= 30000,0 <M <5000), the number of sub-tables represent the number of operations and the students.
Ed student ID number from 1 to N.
The second line contains N integers, representing the N initial student grades, where the i-th student ID is representative of the results i.
The next M rows. Each line has a character C (just take 'Q' or 'U'), and two positive integers A, B.
When C is the 'Q' time, that this is a query operation, it queries the ID from A to B (including A, B) among the students, the highest score is.
When C is the 'U' time, that this is an update operation, the ID required to change grades A student B.

#include<bits/stdc++.h>

using namespace std;

struct node

{

         int l, r, f, int zhi;

}a[120001];

int n,m,b[30001],x,y,z;char u;

void build(int k,int l,int r)

{

         a[k].l=l;a[k].r=r;

         if(l==r)

         {

                   a[k].zhi=b[l];

                   return;

         }

         int mid=(l+r)/2;

         build(k*2,l,mid);

         build(k*2+1,mid+1,r);

         a [k] .zhi = max (a [k * 2] .zhi, a [k * 2 + 1] .zhi);

}

 

void add(int k,int x,int zhi)

{

         if(a[k].l==a[k].r)

         {

                   a [k] .zhi = zhi;

                   return;

         }

         int mid=(a[k].l+a[k].r)/2;

         if(mid>=x)

         {

                   add(k*2,x,zhi);

         }

         else

         add(k*2+1,x,zhi);

         a [k] .zhi = max (a [k * 2] .zhi, a [k * 2 + 1] .zhi);

}

int find(int k,int l,int r)

{

         if(a[k].l>=l&&a[k].r<=r)

         {

                   return a [k] .zhi;

         }

         int years = -199 009 999, mid = (a [k] .l + a [k] x) / 2;

         if(mid>=l)

         {

                   years = max (years find (k * 2, l, r));

         }

         if(mid<r)

         {

                   years = max (years find (k * 2 + 1, l, r));

         }

         return years;

}

int main ()

{

         while(cin>>n>>m)

         {

                   memset(b,0,sizeof(b));

                   memset(a,0,sizeof(a));

                   for(int i=1;i<=n;i++)

                   {

                            cin>>b[i];

                   }

                   build(1,1,n);

                   for(int i=0;i<m;i++)

                   {

                            cin>>u;

                            if(u=='U')

                            {

                                     cin>>x>>y;

                                     add(1,x,y);

                            }

                            if(u=='Q')

                            {

                                     cin>>x>>y;

                                     cout<<find(1,x,y)<<endl;

                            }

                   }

         }

}

Guess you like

Origin www.cnblogs.com/fanhao050109/p/10939396.html