[线段树]分数修改
2016-04-19 12:26
211 查看
题目描述
包含多组测试,请处理到文件结束。在每个测试的第一行,有两个正整数 N (0<N<=10000000)和 M(0<M<=500000)分别代表学生的数目和操作的数目。学生ID编号分别从1编到N。第二行包含N个整数,代表这N个学生的初始成绩,其中第i个数ai代表ID为i的学生的成绩。接下来有M行。每一行有一个字符 C (只取'Q'或'U') ,和两个正整数A,B。当C为'Q'的时候,表示这是一条询问操作,它询问ID从A到B(包括A,B)的学生当中,成绩最高的是多少。当C为'U'的时候,表示这是一条更新操作,要求把ID为A的学生的成绩更改为B。输入
见题目描述输出
输出相应询问。样例输入
5 61 2 3 4 5Q 1 5U 3 6Q 3 4Q 4 5U 2 9Q 1 5样例输出
5659ACM周训练中的一道线段树的题目,这类题目的很明显的一个特点就是数据非常大,如果不是考虑很大的数据,就只需要使用简单的循环和数组就可以解决,但由于数据的庞大,线段树的优势就可以显现的很明显。
#include <cstdio> #include <cmath> #include <iostream> const int MAXNODE=1000000; const int MAX=1000000; using namespace std; struct NODE { int value; int left,right; }node[MAXNODE]; int father[MAX]={0}; void Buildtree(int i,int left,int right) { node[i].left=left; node[i].right=right; node[i].value=0; if(left==right){ father[left]=i; return; } Buildtree(i<<1,left,(left+right)/2); Buildtree((i<<1)+1,(left+right)/2+1,right); } void Updatatree(int ri) { if(ri<=1) return; int fi=ri/2; int x1=fi*2,x2=fi*2+1; int a=node[x1].value; int b=node[x2].value; node[fi].value=(a>b?a:b); Updatatree(ri/2); } int Max=-(1<<20); void Query(int i,int l,int r) { if(node[i].left==l&&node[i].right==r){ Max=(Max<node[i].value?node[i].value:Max); return; } i=i<<1; if(l<=node[i].right){ if(r<=node[i].right) Query(i,l,r); else Query(i,l,node[i].right); } i+=1; if(r>=node[i].left){ if(l>=node[i].left) Query(i,l,r); else Query(i,node[i].left,r); } } int main() { int m,n; int x; while(scanf("%d%d",&n,&m)!=EOF){ Buildtree(1,1,n); for(int i=1;i<=n;++i) { scanf("%d",&x); node[father[i]].value=x; Updatatree(father[i]); } char o; int A,B; while(m--) { cin>>o>>A>>B; if(A==0) A=1; if(o=='Q') { Max=-(1<<20); Query(1,A,B); printf("%d\n",Max); } else if(o=='U') { node[father[A]].value=B; Updatatree(father[A]); } } } return 0; }
相关文章推荐
- Linux共享Windows文件夹
- 将retrofit2和rxjava的初始化配置到application
- 【Leetcode】:238. Product of Array Except Self 问题 in JAVA
- 1017
- ES配置文件
- Qt 使用statusBar来显示程序打印信息
- RecyclerView解决加载和刷新
- (LeetCode 299) Bulls and Cows (HashTable)
- MS SQL执行大脚本文件时,提示“内存不足”的解决办法
- 常见http状态码
- mysql语句常用关键字
- A first chance exception of type 'System.NullReferenceException' occurred发生的时候你确实是遇到了Bug
- 数组操作
- 如何把SQLServer数据库从高版本降级到低版本?
- 原生JS实现的简单“瀑布流”布局
- Spring事务管理
- 矩阵和向量的乘法顺序
- 个人工作总结2
- input type date 解决移动端显示placeholder.
- retrofit2拦截器和请求封装