Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5172
Test instructions: Give the number of N, m query, ask you [l,r] within the interval is 1 to r-l+1 of the full arrangement. The size is easy for us to record the prefix and it is easy to find out, but the key is to go heavy. Considering the line-segment tree approach, we record the position of the nearest left element of each point, and then the maximum value of the entire interval (that is, the largest precursor) if it is less than L, which satisfies the condition, the output is yes.
Well, in fact, this topic I was searching RMQ algorithm out, because I want to practice the RMQ algorithm, so I looked at someone else's blog, I also wrote a bit, the results of the mle ...
Well, I looked carefully, 1000000 of the data volume with RMQ open a two-dimensional array 1000000*20, obviously will be super memory ...
As a result I had to go back and write with the line tree. Orz ....
RMQ's MLE code:
#include <iostream> #include <string> #include <cstdio> #include <cstring> #include <queue > #include <map> #include <cmath> #include <stack> #include <set> #include <vector># Include<algorithm> #define LL long long#define inf 1<<30#define SF (a) scanf ("%d", &a); #define CLEAR (A, B) memset (A,b,sizeof (a)) using namespace std;/* TLE ... Obviously hyper memory; */const int n=1000005;int n,m,a,b;int num[n],pre[n];int dp[n][21];int sum[n];void ST (int len) {for (int i=1;i<= n;i++) Dp[i][0]=num[i]; for (int j=1;1<<j < n;j++) {for (int i=1;i+ (1<<J) -1<n;i++) {Dp[i][j]=max (dp[i][j-1],dp[i +1<< (j-1)][j-1]); }}}int rmq (int s,int v) {int k= (int) (log (v-s+1) *1.0/log (2.0)); Return Max (Dp[s][k],dp[v-1<<k+1][k]);} int main () {while (~scanf ("%d%d", &n,&m)) {CLEAR (pre,0); CLEAR (sum,0); for (int i=1;i<=n;i++) {SF (a); Sum[i]+=sum[i-1]+a; num[i]=Pre[a]; Pre[a]=i; } ST (n); while (m--) {SF (a); SF (b); Double tmp= (b-a+2) * (b-a+1) *1.0/2.0; if (Tmp!=sum[b]-sum[a-1]) {//cout<<tmp<< ' <<sum[b]-sum[a-1]<< '; printf ("no\n"); Continue }else{if (RMQ (A, b) <a) printf ("yes\n"); else printf ("no\n"); }}} return 0;}
Line Tree AC code:
#include <iostream> #include <string> #include <cstdio> #include <cstring> #include <queue > #include <map> #include <cmath> #include <stack> #include <set> #include <vector># Include<algorithm> #define LL Long long#define inf 1<<30#define s (a) scanf ("%d", &a) #define CLEAR (A, B) memset (A,b,sizeof (a)) using namespace Std;const int n=1000005;int n,m,a,b;int pre[n],sum[n]; Sum deposit sum, Pre locates the position of the number last occurrence; struct node{int l,r; int pre; Maintains the most recent repeating number with a segment tree;}node[n<<2];void pushup (int rt) {Node[rt].pre=max (node[rt<<1].pre,node[rt<<1| 1].PRE);} void build (int l,int R,int RT) {node[rt].l=l; Node[rt].r=r; node[rt].pre=0; if (l!=r) {int mid= (L+R) >>1; Build (l,mid,rt<<1); Build (mid+1,r,rt<<1|1); }}void Insert (int v,int p,int RT) {int LL=NODE[RT].L,RR=NODE[RT].R; if (LL==RR&&P==LL) {node[rt].pre=pre[v]; Return } int mid= (LL+RR) >>1; if (p<=mid) Insert (v,p,rt<<1); else Insert (v,p,rt<<1|1); Pushup (RT);} int query (int l,int r,int RT) {int LL=NODE[RT].L,RR=NODE[RT].R; if (ll==l&&rr==r) {return node[rt].pre; } int mid= (LL+RR) >>1; if (r<=mid) return query (l,r,rt<<1); else if (l>mid) return query (L,R,RT<<1|1); else return max (query (l,mid,rt<<1), query (mid+1,r,rt<<1|1));} int main () {while (~s (n)) {s (m); CLEAR (pre,0); CLEAR (sum,0); Build (1,n,1); for (int i=1;i<=n;i++) {s (a); Sum[i]+=sum[i-1]+a; Insert (a,i,1); Pre[a]=i; } while (m--) {s (a); s (b); int tmp= (b-a+2) * (b-a+1) *1.0/2.0; If the whole arrangement is so final and necessarily conforms to this formula, if (sum[b]-sum[a-1]!=tmp) {printf ("no\n"); Continue }else{if (query (a,b,1) <a) printf ("yes\n");//Query The position of the last duplicate number in the interval, if it is smaller than a, it indicates that there are no duplicate numbers in the interval; else printf ("no\n"); }}} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hdu-5172-gty ' s gay friends-segment tree single point update