int rmq(int pos, int start, int end, int queryStart, int queryEnd) { if(start > queryEnd || end < queryStart) { return -1; } if(start >= queryStart && end <= queryEnd) { return segment_tree_array[pos]; } int middle = (start + end) / 2; int leftSon =(pos << 1); int rightSon = (pos << 1) + 1; int leftIndex = rmq(leftSon, start, middle, queryStart, queryEnd); int rightIndex = rmq(rightSon, middle + 1, end, queryStart, queryEnd); if(leftIndex == -1) { return rightIndex; } if(rightIndex == -1) { return leftIndex; } if(array[leftIndex] <= array[rightIndex]) { return leftIndex; } else { return rightIndex; } }
void work() { num[1]=1; for(int i=2;i<=n;++i) num[i]=a[i]==a[i-1]?num[i-1]+1:1; times[n]=num[n]; for(int i=n-1;i>=1;--i) times[i]=a[i]==a[i+1]?times[i+1]:num[i]; for(int i=1;i<=q;++i) { int ans=0; if(a[qr[i]]==a[ql[i]]) { printf("%d\n",qr[i]-ql[i]+1); continue; } ans=max(ans,times[ql[i]]-num[ql[i]]+1); // printf("\n%d %d\n",times[ql[i]],num[ql[i]]); // printf("%d\n",ans); ans=max(ans,num[qr[i]]); // printf("%d\n",ans); int l,r; for(l=ql[i];l<n&&a[l]==a[l+1];++l); ++l; for(r=qr[i];r>0&&a[r]==a[r-1];--r); --r; ans=max(ans,rmq(l,r)); printf("%d\n",ans); } }
/////////////////////////////////////////// // deepest_node(bp *b,i64 s) // returns the first node with the largest depth in the subtree of s /////////////////////////////////////////// i64 deepest_node(bp *b,i64 s) { i64 t,m; t = find_close(b,s); m = rmq(b,s,t, OPT_MAX); return m; }
int lcp(int a,int b) { if(a==b)return n-a; //a和b为同一后缀,直接输出,字串串长度为n int ra=rank[a],rb=rank[b]; if(ra>rb)swap(ra,rb); return rmq(ra+1,rb); }
TEST(SparseTable, SmallAll2) { const int n = 1024 + 1; vector<int> v(n); REP(i, n) v[i] = rand(); int min_idx[n][n+1] = {}; REP(i, n) { min_idx[i][i+1] = i; } for(int l = 2; l <= n; l++) { for(int i = 0; i+l <= n; i++) { const int p = min_idx[i][i+l-1]; if(v[p] > v[i+l-1]) { min_idx[i][i+l] = i+l-1; } else { min_idx[i][i+l] = p; } } } SparseTable<int> rmq(v); for(int i = 0; i < n; i++) { for(int j = i+1; j <= n; j++) { ASSERT_EQ(min_idx[i][j], rmq.query_idx(i, j)); } } }
int getLCA(int u, int v, int n){ int goFrom = H[u]; int goTo = H[v]; if(goFrom > goTo){ swap(goFrom, goTo); } return E[rmq(1, 0, 2*n-1, goFrom, goTo)]; //is the LCA of A and B; }
int lca(int l, int r) { l = f[l]; r = f[r]; if (l > r) { l = l + r; r = l - r; l = l - r; } return rmq(l, r); }
string longestPalindrome(string s) { string t=s; int n=s.size(); reverse(t.begin(),t.end()); s=s+"#"+t; int sa[2100]; int height[2100]; int rank[2100]; makesa(s.c_str(),sa,height,rank); // cout<<"sa====="<<endl; /* for(int i=0;i<2*n+2;i++) cout<<sa[i]<<" "; cout<<endl; for(int i=0;i<2*n+2;i++) cout<<height[i]<<" "; cout<<endl; */ int M[2100][20]; rmq_init(M,height,2*n+2); int m=0; int l,r; for(int i=0;i<n;i++) { //偶数个 int temp=rmq(M,rank[i],rank[2*n+1-i],height); if(2*temp>m){ m=2*temp; l=i-temp; r=i+temp-1; } temp=rmq(M,rank[i],rank[2*n-i],height); if(2*temp-1>m){ m=2*temp-1; l=i-temp+1; r=i+temp-1; } } // cout<<"M"<<m<<endl; // cout<<"["<<l<<","<<r<<"]"<<endl; return s.substr(l,r-l+1); }
TEST(SparseTable, BenchMarkBuild) { const int n = 1e6; const int q = 1; vector<int> v(n); REP(i, n) v[i] = rand(); SparseTable<int> rmq(v); int ans = 0; REP(i, q) { int b = rand() % n; int e = rand() % (n - b) + b+1; ans ^= rmq.query(b, e); }
//Get the vertex with the minimum height, then it will be the LCA of A and B. int rmq(int node, int l, int r, int ra, int rb){ if(l > rb || r < ra){ return -1; }else if(l >= ra && r <= rb){ return tree[node]; }else{ int mid = (l+r) >> 1; int q1 = rmq(node*2, l, mid, ra, rb); int q2 = rmq(node*2+1, mid+1, r, ra, rb); if(q1 == -1){ return q2; }else if(q2 == -1){ return q1; }else{ if(L[q1] <= L[q2]){ return q1; }else{ return q2; } } } }
int lca(int a, int b){ int x=app[a], y=app[b]; if(x<y) return rmq(x,y); else return rmq(y,x); }
i64 bp_construct(bp *b,i64 n, pb *B, i64 opt) { i64 i,j,d; i64 m=0,M=0,ds; i64 ns,nm; sb_type *sm, *sM; sb_type *sd = NULL; mb_type *mm, *mM; mb_type *md = NULL; i64 m_ofs; i64 r=0; // # of minimum values #if 0 if (SB % D != 0) { printf("warning: SB=%d should be a multiple of D=%d\n",SB,D); // not necessarily? } if (SB % RRR != 0) { printf("warning: SB=%d should be a multiple of RRR=%d\n",SB,RRR); } #endif b->B = B; b->n = n; b->opt = opt; b->idx_size = 0; mymalloc(b->da,1,0); darray_construct(b->da,n,B, opt & OPT_FAST_PREORDER_SELECT); b->idx_size += b->da->idx_size; printf("preorder rank/select table: %d bytes (%1.2f bpc)\n",(int)b->da->idx_size,(double)b->da->idx_size*8/n); make_matchtbl(); ns = (n+SB-1)/SB; mymalloc(sm, ns, 0); b->idx_size += ns * sizeof(*sm); mymalloc(sM, ns, 0); b->idx_size += ns * sizeof(*sM); b->sm = sm; b->sM = sM; if (opt & OPT_DEGREE) { mymalloc(sd, ns, 0); b->idx_size += ns * sizeof(*sd); b->sd = sd; printf("SB degree table: %d bytes (%1.2f bpc)\n",(int)ns * sizeof(*sd), (double)ns * sizeof(*sd) * 8/n); } printf("SB table: %d bytes (%1.2f bpc)\n",(int)ns * sizeof(*sm) * 2, (double)ns * sizeof(*sm)*2 * 8/n); for (i=0; i<n; i++) { if (i % SB == 0) { ds = depth(b,i); m = M = ds; r = 1; } else { d = depth(b,i); if (d == m) r++; if (d < m) { m = d; r = 1; } if (d > M) M = d; } if (i % SB == SB-1 || i==n-1) { ds = depth(b,(i/SB)*SB-1); if (m - ds + SB < 0 || m - ds + SB >= (1<<(8*sizeof(sb_type)))) { printf("error m=%d ds=%d\n",(int)m,(int)ds); } if (M - ds + 1 < 0 || M - ds + 1 >= (1<<(8*sizeof(sb_type)))) { printf("error M=%d ds=%d\n",(int)M,(int)ds); } sm[i/SB] = (sb_type)(m - ds + SB); sM[i/SB] = (sb_type)(M - ds + 1); if (opt & OPT_DEGREE) sd[i/SB] = (sb_type)r; } } #if 0 printf("sd: "); for (i=0; i<n/SB; i++) printf("%d ",sd[i]); printf("\n"); #endif nm = (n+MB-1)/MB; m_ofs = 1 << blog(nm-1); b->m_ofs = m_ofs; mymalloc(mm, nm + m_ofs, 0); b->idx_size += (nm+m_ofs) * sizeof(*mm); mymalloc(mM, nm + m_ofs, 0); b->idx_size += (nm+m_ofs) * sizeof(*mM); b->mm = mm; b->mM = mM; if (opt & OPT_DEGREE) { mymalloc(md, nm + m_ofs, 0); b->idx_size += (nm+m_ofs) * sizeof(*md); b->md = md; printf("MB degree table: %d bytes (%1.2f bpc)\n",(int)(nm+m_ofs) * sizeof(*md), (double)(nm+m_ofs) * sizeof(*md) * 8/n); } printf("MB table: %d bytes (%1.2f bpc)\n",(int)(nm+m_ofs) * sizeof(*mm) * 2, (double)(nm+m_ofs) * sizeof(*mm)*2 * 8/n); for (i=0; i<n; i++) { d = depth(b,i); if (i % MB == 0) { m = M = d; r = 1; } else { if (d == m) r++; if (d < m) { m = d; r = 1; } if (d > M) M = d; } if (i % MB == MB-1 || i==n-1) { mm[m_ofs+ i/MB] = m; mM[m_ofs+ i/MB] = M; if (opt & OPT_DEGREE) md[m_ofs+ i/MB] = r; } } for (j=m_ofs-1; j > 0; j--) { m = 0; if (j*2 < nm + m_ofs) m = mm[j*2]; if (j*2+1 < nm + m_ofs) m = min((mb_type)m,mm[j*2+1]); M = 0; if (j*2 < nm + m_ofs) M = mM[j*2]; if (j*2+1 < nm + m_ofs) M = max((mb_type)M,mM[j*2+1]); mm[j] = m; mM[j] = M; if (opt & OPT_DEGREE) { d = 0; if (j*2 < nm + m_ofs) d = md[j*2]; if (j*2+1 < nm + m_ofs) { if (mm[j*2] == mm[j*2+1]) d += md[j*2+1]; if (mm[j*2] > mm[j*2+1]) d = md[j*2+1]; } md[j] = d; } } mm[0] = -1; mM[0] = mM[1]; if (opt & OPT_DEGREE) { md[0] = -1; } #if 0 printf("md: "); for (i=0; i<m_ofs + n/MB; i++) printf("%d ",md[i]); printf("\n"); #endif if (opt & OPT_LEAF) { mymalloc(b->da_leaf,1,0); darray_pat_construct(b->da_leaf, n, B, 2, 0x2, opt & OPT_FAST_LEAF_SELECT); printf("leaf rank/select table: %d bytes (%1.2f bpc)\n",(int)b->da_leaf->idx_size,(double)b->da_leaf->idx_size*8/n); b->idx_size += b->da_leaf->idx_size; } else { b->da_leaf = NULL; } if (opt & OPT_INORDER) { mymalloc(b->da_inorder,1,0); darray_pat_construct(b->da_inorder, n, B, 2, 0x1, opt & OPT_FAST_INORDER_SELECT); printf("inorder rank/select table: %d bytes (%1.2f bpc)\n",(int)b->da_inorder->idx_size,(double)b->da_inorder->idx_size*8/n); b->idx_size += b->da_inorder->idx_size; } else { b->da_inorder = NULL; } if (opt & OPT_FAST_POSTORDER_SELECT) { mymalloc(b->da_postorder,1,0); darray_pat_construct(b->da_postorder, n, B, 1, 0x0, (opt & OPT_FAST_POSTORDER_SELECT) | OPT_NO_RANK); printf("postorder rank/select table: %d bytes (%1.2f bpc)\n",(int)b->da_postorder->idx_size,(double)b->da_postorder->idx_size*8/n); b->idx_size += b->da_postorder->idx_size; } else { b->da_postorder = NULL; } if (opt & OPT_DFUDS_LEAF) { mymalloc(b->da_dfuds_leaf,1,0); darray_pat_construct(b->da_dfuds_leaf, n, B, 2, 0x0, opt & OPT_FAST_DFUDS_LEAF_SELECT); printf("dfuds leaf rank/select table: %d bytes (%1.2f bpc)\n",(int)b->da_dfuds_leaf->idx_size,(double)b->da_dfuds_leaf->idx_size*8/n); b->idx_size += b->da_dfuds_leaf->idx_size; } else { b->da_dfuds_leaf = NULL; } if (opt & OPT_FAST_LCA) { i64 h,s,t; mb_type *rmq_tbl; b->opt &= ~OPT_FAST_LCA; h = blog(nm-1); mymalloc(rmq_tbl,nm*h,0); b->rmq_tbl = rmq_tbl; for (i=0; i<nm; i++) { for (j=0; j<h; j++) { s = i*MB; t = min((i+(1<<j))*MB-1,n-1); rmq_tbl[j*nm + i] = rmq(b,s,t,0); } } printf("fast_lca table: %d bytes (%1.2f bpc)\n",(int)(nm*h*sizeof(*rmq_tbl)),(double)nm*h*sizeof(*rmq_tbl)*8/n); b->opt |= OPT_FAST_LCA; } return 0; }
/////////////////////////////////////////// // lca(bp *b, i64 s, i64 t) // returns the lowest common ancestor of s and t /////////////////////////////////////////// i64 lca(bp *b, i64 s, i64 t) { return parent(b,rmq(b,s,t,0)+1); }
void company::sTest() { XSqlQuery companyTest; if (DEBUG) qDebug("company::sTest()"); QString dbURL; QString protocol = "psql"; QString host = _extServer->text(); QString db = _extDB->text(); QString port = _extPort->cleanText(); buildDatabaseURL(dbURL, protocol, host, db, port); if (DEBUG) qDebug("company::sTest() dbURL before login2 = %s", qPrintable(dbURL)); ParameterList params; params.append("databaseURL", dbURL); params.append("multipleConnections"); params.append("applicationName", _ConnAppName); login2 newdlg(this, "testLogin", false); newdlg.set(params); if (newdlg.exec() == QDialog::Rejected) return; dbURL = newdlg._databaseURL; if (DEBUG) qDebug("company::sTest() dbURL after login2 = %s", qPrintable(dbURL)); parseDatabaseURL(dbURL, protocol, host, db, port); QSqlDatabase testDB = QSqlDatabase::addDatabase("QPSQL7", db); testDB.setHostName(host); testDB.setDatabaseName(db); testDB.setUserName(newdlg.username()); testDB.setPassword(newdlg.password()); testDB.setPort(port.toInt()); if (testDB.open()) { if (DEBUG) qDebug("company::sTest() opened testDB!"); XSqlQuery rmq(testDB); rmq.prepare("SELECT fetchMetricText('ServerVersion') AS result;"); rmq.exec(); if (rmq.first()) { if (rmq.value("result").toString() != _metrics->value("ServerVersion")) { QMessageBox::warning(this, tr("Versions Incompatible"), tr("<p>The version of the child database is not " "the same as the version of the parent " "database (%1 vs. %2). The data cannot safely " "be synchronized.") .arg(rmq.value("result").toString()) .arg(_metrics->value("ServerVersion"))); return; } } else if (rmq.lastError().type() != QSqlError::NoError) { systemError(this, rmq.lastError().databaseText(), __FILE__, __LINE__); return; } rmq.exec("SELECT * FROM curr_symbol WHERE curr_base;"); if (_external->isChecked()) { companyTest.prepare("SELECT * FROM curr_symbol WHERE curr_id=:curr_id;"); companyTest.bindValue(":curr_id", _currency->id()); companyTest.exec(); } else companyTest.exec("SELECT * FROM curr_symbol WHERE curr_base;"); if (companyTest.first() && rmq.first()) { if (rmq.value("curr_symbol").toString() != companyTest.value("curr_symbol").toString() && rmq.value("curr_abbr").toString() != companyTest.value("curr_abbr").toString()) { QMessageBox::warning(this, tr("Currencies Incompatible"), tr("<p>The currency of the child database does " "not appear to match the selected currency for " "the company (%1 %2 %3 vs. %4 %5 %6). The data may " "not synchronize properly.") .arg(rmq.value("curr_name").toString()) .arg(rmq.value("curr_symbol").toString()) .arg(rmq.value("curr_abbr").toString()) .arg(companyTest.value("curr_name").toString()) .arg(companyTest.value("curr_symbol").toString()) .arg(companyTest.value("curr_abbr").toString())); return; } } else if (rmq.lastError().type() != QSqlError::NoError) { systemError(this, rmq.lastError().databaseText(), __FILE__, __LINE__); return; } else if (companyTest.lastError().type() != QSqlError::NoError) { systemError(this, companyTest.lastError().databaseText(), __FILE__, __LINE__); return; } else if (!rmq.first()) { QMessageBox::warning(this, tr("No Base Currency"), tr("<p>The child database does not appear to have " "a base currency defined. The data cannot safely " "be synchronized.")); return; } else if (!companyTest.first()) { QMessageBox::warning(this, tr("No Base Currency"), tr("<p>The parent database does not appear to have " "a base currency defined. The data cannot safely " "be synchronized.")); return; } rmq.prepare("SELECT * FROM company WHERE (company_number=:number);"); rmq.bindValue(":number", _number->text()); rmq.exec(); if (rmq.first()) { QMessageBox::warning(this, windowTitle(), tr("Test Successful.")); return; } else if (rmq.lastError().type() != QSqlError::NoError) { systemError(this, rmq.lastError().databaseText(), __FILE__, __LINE__); return; } else { QMessageBox::warning(this, tr("No Corresponding Company"), tr("<p>The child database does not appear to have " "a Company %1 defined. The data cannot safely " "be synchronized.").arg(_number->text())); return; } } }
//rangemax query, change returns to answer different questions. int rmq(int i, int L, int R, int l, int r){ if(l > R || r < L) return -1; if(L >= l && R <= r) return st[i]; return max( rmq(left(i), L,(L+R)/2, l,r, st), rmq(right(i),((L+R)/2)+1, R,l,r)); }
int range_minimum_query(int queryStart, int queryEnd) { return rmq(1, 0, size - 1, queryStart, queryEnd); }
node LCA::call(node u, node v) const { OGDF_ASSERT(u && v); return m_euler[rmq(m_representative[v], m_representative[u])]; }