日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU 4638 Group(莫队)题解

發(fā)布時間:2025/6/17 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU 4638 Group(莫队)题解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題意:n個數(shù),每個數(shù)有一個值,每次詢問一個區(qū)間,問你這個區(qū)間能分成連續(xù)的幾段(比如7 1 2 8 就是兩端 1 2 和 7 8)

思路:莫隊(duì)。因?yàn)長、R移動順序wa了20發(fā)...問了一下別人,都是先擴(kuò)大范圍,再縮小...以后就這樣寫吧...

代碼:

#include<cmath> #include<cstdio> #include<vector> #include<cstring> #include <iostream> #include<algorithm> using namespace std; const int maxn = 100000 + 10; int vis[maxn], arr[maxn], ans[maxn]; int T, n, m, ret; struct node{int l, r;int pos, id;bool operator < (const node &x) const{if(pos == x.pos) return r < x.r;return pos < x.pos;} }p[maxn]; void add(int x){vis[x] = 1;if(vis[x - 1] && vis[x + 1]) ret--;else if(!vis[x - 1] && !vis[x + 1]) ret++; } void del(int x){vis[x] = 0;if(vis[x - 1] && vis[x + 1]) ret++;else if(!vis[x - 1] && !vis[x + 1]) ret--; } void solve(){memset(vis, 0 ,sizeof(vis));int L = 1, R = 0;ret = 0;for(int i = 0; i < m; i++){int l = p[i].l, r = p[i].r;if(r < L || l > R){ret = 0;for(int i = L; i <= R; i++)vis[arr[i]] = 0;for(int i = l; i <= r; i++)add(arr[i]);L = l, R = r;}while(L > l){L--;add(arr[L]);}while(R < r){R++;add(arr[R]);}while(L < l){del(arr[L]);L++;}while(R > r){del(arr[R]);R--;}ans[p[i].id] = ret;} } int main(){scanf("%d", &T);while(T--){scanf("%d%d", &n, &m);int block = sqrt(n * 1.0);for(int i = 1; i <= n; i++)scanf("%d", &arr[i]);for(int i = 0; i < m; i++){scanf("%d%d", &p[i].l, &p[i].r);p[i].id = i;p[i].pos = p[i].l / block;}sort(p, p + m);solve();for(int i = 0; i < m; i++){printf("%d\n", ans[i]);}}return 0; }

?

轉(zhuǎn)載于:https://www.cnblogs.com/KirinSB/p/10582224.html

總結(jié)

以上是生活随笔為你收集整理的HDU 4638 Group(莫队)题解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。