Code:
#include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h>using namespace std;const int max=200010;int rmq[max+10];int total[max];int sum[35];int n,m,cnt;char ctr[35];int bit (int x) {// Each subscript governs the scope of return x& (-X);} int query (int first,int second) {//query int res=total[second]; while (first<=second) {int key=bit (second); if (Second-key>=first) {if (Res>rmq[second]) Res=rmq[second]; Second=second-key; } else {if (Res>total[second]) Res=total[second]; second--; }} return res;} int updata (int x) {//update x position node for (int i=x;i<=n;i+=bit (i)) {rmq[i]=total[i];//Use the original array to update the tree array for (int j= 1;j<bit (i); j<<=1) {//This is the focus of another scan I node of the range Rmq[i]=min (Rmq[i],rmq[i-j]); }}}void Solve () {int Len=strlen (CTR); cnt=0; memset (sum,0,sizeof (sum)); for (int i=6; I<len; i++) {if (ctr[i]== ') ') break; if (ctr[i]== ', ') {cnt++; Continue } int t=ctr[i]-' 0 '; sum[cnt]=t+sum[cnt]*10; } if (ctr[0]== ' Q ') printf ("%d\n", Query (Sum[0],sum[1])); else {int key=total[sum[0]]; for (int i=cnt; i>=0; i--) {int q=total[sum[i]]; total[sum[i]]=key;//first update the original array updata (Sum[i]); key=q; }}}int Main () {scanf ("%d%d", &n,&m); for (int i=1;i<=n;i++) {scanf ("%d", &total[i]); Updata (i); } for (int i=0;i<m;i++) {scanf ("%s", CTR); Solve (); } return 0;}
Tree-like array max (RMQ with Shifts)