线段树。。。。

值得一提的是作者处理括号内的方法,我还傻傻的用sscanf哭T T

可以直接这样的。。。。。速度快还简单

inline void action_position()
{len=0;pos[0]=0;int L=strlen(action);for(int i=6;i<L;i++){if(isdigit(action[i]))pos[len]=pos[len]*10+ action[i]- 48;elsepos[++len]=0;}
}

上代码,sscanf的

#include <cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=500000+10;
const int INF=1000000000;
int a[MAXN],pos[100],start[100],ql,qr,p,v,len;
char action[100];void action_position()
{len=0;for(int i=6;action[i]!=')';)if(action[i]!=','){sscanf(action+i,"%d",&pos[len++]);i=strchr(action+i,',')-action+1;if(i<=0) break;}
}
struct IntervalTree
{int data[MAXN],n;void build(int cur,int L,int R){if(L==R)data[cur]=a[L];else{int M=L+(R-L)/2;build(cur*2,L,M);        //leftbuild(cur*2+1,M+1,R);   //rightdata[cur]=min(data[cur*2],data[cur*2+1]);}}int query(int cur,int L,int R){int M=L+(R-L)/2,ans=INF;if(ql<=L&&R<=qr)  return data[cur];if(ql<=M)  ans= min(ans,query(cur*2,L,M));        //leftif(qr>M)   ans=min(ans,query(cur*2+1,M+1,R));   //rightreturn ans;}void update(int cur,int L,int R){int M=L+(R-L)/2;if(L==R) data[cur]=v;else{if(p<=M) update(cur*2,L,M);else    update(cur*2+1,M+1,R);data[cur]=min( data[cur *2 ],data[cur*2+1]);}}
};IntervalTree tree;
int main()
{int n,m;while(scanf("%d%d",&n,&m)!=EOF){for(int i=1;i<=n;i++)scanf("%d",&a[i]);tree.build(1,1,n);for(int ri=0;ri<m;ri++){scanf("%s",action);action_position(); if(action[0]=='q'){ql=pos[0];qr=pos[1];printf("%d\n",tree.query(1,1,n));}else{int temp=a[ pos[len-1] ];for(int i=0;i<len;i++)start[i]=a[ pos[i] ];  start[len]=start[0];for(int i=0;i<len;i++){p = pos[i];v =a[ p ]= start[i+1];tree.update(1, 1, n);}}}}return 0;
}

改进了处理数据(读取括号里面的数)

#include <cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
using namespace std;
const int MAXN=500000+10;
const int INF=1000000000;
int a[MAXN],pos[100],start[100],ql,qr,p,v,len;
char action[100];inline void action_position()
{len=0;pos[0]=0;int L=strlen(action);for(int i=6;i<L;i++){if(isdigit(action[i]))pos[len]=pos[len]*10+ action[i]- 48;elsepos[++len]=0;}
}
struct IntervalTree
{int data[MAXN],n;void build(int cur,int L,int R){if(L==R)data[cur]=a[L];else{int M=L+(R-L)/2;build(cur*2,L,M);        //leftbuild(cur*2+1,M+1,R);   //rightdata[cur]=min(data[cur*2],data[cur*2+1]);}}int query(int cur,int L,int R){int M=L+(R-L)/2,ans=INF;if(ql<=L&&R<=qr)  return data[cur];if(ql<=M)  ans= min(ans,query(cur*2,L,M));        //leftif(qr>M)   ans=min(ans,query(cur*2+1,M+1,R));   //rightreturn ans;}void update(int cur,int L,int R){int M=L+(R-L)/2;if(L==R) data[cur]=v;else{if(p<=M) update(cur*2,L,M);else    update(cur*2+1,M+1,R);data[cur]=min( data[cur *2 ],data[cur*2+1]);}}
};IntervalTree tree;
int main()
{int n,m;while(scanf("%d%d",&n,&m)!=EOF){for(int i=1;i<=n;i++)scanf("%d",&a[i]);tree.build(1,1,n);for(int ri=0;ri<m;ri++){scanf("%s",action);action_position(); if(action[0]=='q'){ql=pos[0];qr=pos[1];printf("%d\n",tree.query(1,1,n));}else{int temp=a[ pos[len-1] ];for(int i=0;i<len;i++)start[i]=a[ pos[i] ];  start[len]=start[0];for(int i=0;i<len;i++){p = pos[i];v =a[ p ]= start[i+1];tree.update(1, 1, n);}}}}return 0;
}

转载于:https://www.cnblogs.com/murmured/p/5004295.html

UVA 12299 - RMQ with Shifts 线段树相关推荐

  1. UVA 12299 RMQ with Shifts 线段树

    简单的单点更新 //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> # ...

  2. 【luogu1816】忠(RMQ问题、线段树)

    题面 RMQ问题 题解 线段树 #include<iostream> #include<algorithm> using namespace std; const int ma ...

  3. AOJ 351 rmq或 基础线段树 求解

    题目链接:AOJ 351 RMQ: #include<cstdio> #include<cstring> #include<algorithm> #include& ...

  4. 蓝书4.1-4.4 树状数组、RMQ问题、线段树、倍增求LCA

    这章的数据结构题很真实 T1 排队 bzoj 1699 题目大意: 求静态一些区间的最大值-最小值 思路: ST表裸题 1 #include<iostream> 2 #include< ...

  5. hicoder - 1068 【RMQ or 线段树】

    hicoder - 1068 [RMQ or 线段树] RMQ: 1 #include <bits/stdc++.h> 2 #define rep(i,a,b) for(int i = a ...

  6. UVA - 11992 线段树

    UVA - 11992 题意:有一个 r*c 的全 0矩阵, 进行 3 种操作. 1 x1 y1 x2 y2 val 表示将(x1,y1,x2,y2)(x1<=x2,y1<=y2)子矩阵中 ...

  7. poj 3264 Balanced Lineup RMQ问题 线段树

    For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One d ...

  8. CodeForces - 1422F Boring Queries(主席树+线段树/RMQ)

    题目链接:点击查看 题目大意:给出 n 个数组成的数组 a ,再给出 m 次询问,每次询问需要回答区间 [ l , r ] 中所有元素的最小公倍数,强制在线 题目分析:首先考虑多个数的最小公倍数该如何 ...

  9. HDU3183 A Magic Lamp —— 贪心(单调队列优化)/ RMQ / 线段树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 题解: 方法一:贪心. 在草稿纸上试多几次可以知道,删除数字中从左到右最后一位递增(可以等于)的 ...

  10. 线段树/扫描线问卷调查反馈——Rmq Problem / mex(主席树),Boring Queries(二分+st表+主席树),Colorful Squares(扫描线)

    文章目录 Rmq Problem / mex Boring Queries Colorful Squares Rmq Problem / mex luogu4137 对aia_iai​建权值线段树 再 ...

最新文章

  1. 2.innodb后台线程
  2. 刘未鹏谈如何学习算法
  3. YUI事件体系之Y.CustomEvent
  4. KODExplorer 简介
  5. Qt Dialog捕捉鼠标移动事件
  6. 如何将日志系统切换到 Logback?
  7. 用ExpandableListView实现好友分组
  8. cacti mysql 压缩 备份_CactiEZ备份和恢复
  9. 外贸出口业务管理解决方案丨汇信
  10. 轻量级私有云存储企业主要功能探析
  11. mysql ubb html_自动闭合HTML/ubb标签函数
  12. 全球四大卫星导航系统年鉴
  13. Python编曲实践(九):如何计算并估计音乐的调性(大/小调+主音)?Krumhansl-Schmuckler调性分析算法的原理与实现
  14. 为防止用户自定义Bixby按键,三星先给T-Mobile用户的S8发送更新
  15. oidc_使用OIDC和Ionic for JHipster保护您的移动应用程序
  16. 大学生提高计算机应用能力,大学生计算机应用能力的培养
  17. 小白的模拟电路初步学习20日打卡(18)
  18. Windows组策略应用全攻略
  19. 绿色出行之路 中国越走越宽
  20. H5项目常见问题汇总及解决方案 1

热门文章

  1. 如何解决PS软件工作区不能吸附到工具栏?
  2. 如何在Mac的内置词典中添加和删除单词
  3. mysql 查询优化 ~ 分区表查询的探索
  4. 一些常用的css小技巧
  5. 动态修改log4net设置
  6. Linux_MySQL下(4) 未整理
  7. Linux系统文件的隐藏属性
  8. DBCC PAGE、DBCC LOG、DBCC EXTENTINFO、DBCC IND、DBCC SHOW_STATISTICS、DBCC SHOWCONTIG、DBCC SQLPERF...
  9. POJ-1087 A Plug for UNIX 网络流
  10. APUE习题[实现dup2函数功能,不使用fcntl]