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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU 3874 Necklace (数状数组)

發布時間:2025/5/22 编程问答 12 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU 3874 Necklace (数状数组) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題意:求[l, r]區間內不重復的數的和。N個數,M次詢問

解:離線處理M次詢問,看得別人的思路后才知道的。。。思維局限在預處理N個數上了。。。

對M次詢問按右區間的值從小到大排序。掃一遍N個數,如果發現當前這個數在之前出現過,就從之前出現這個數的位置上把這個數刪除,在新位置上插入這個數。這樣做的好處是刪除前面重復的不會影響后面的。時間復雜度控制在(N + M)logN的數量級上。

ps:午不眠,困乎,我命休矣。

?

#include <iostream> #include <cstdio> #include <cmath> #include <vector> #include <cstring> #include <algorithm> #include <string> #include <set> #include <functional> #include <numeric> #include <sstream> #include <stack> #include <map> #include <queue>#define CL(arr, val) memset(arr, val, sizeof(arr)) #define REP(i, n) for((i) = 0; (i) < (n); ++(i)) #define FOR(i, l, h) for((i) = (l); (i) <= (h); ++(i)) #define FORD(i, h, l) for((i) = (h); (i) >= (l); --(i)) #define L(x) (x) << 1 #define R(x) (x) << 1 | 1 #define MID(l, r) (l + r) >> 1 #define Min(x, y) (x) < (y) ? (x) : (y) #define Max(x, y) (x) < (y) ? (y) : (x) #define E(x) (1 << (x)) #define iabs(x) (x) < 0 ? -(x) : (x) #define OUT(x) printf("%I64d\n", x) #define Read() freopen("data.in", "r", stdin) #define Write() freopen("data.out", "w", stdout);typedef __int64 LL; const double eps = 1e-6; const double PI = acos(-1.0); const int inf = 0x1F1F1F1F;using namespace std;const int N = 50010; const int M = 200010;struct node {int l, r;int id;bool operator < (const node& cmp) const {return r < cmp.r;} }p[M];LL c[N]; LL ans[M]; int num[N]; map<int, int> mp;int lowbit(int i) {return i&(-i); }void add(int p, int val) {for( ; p < N; p += lowbit(p)) {c[p] += val;} }LL sum(int p) {LL res = 0;for( ; p > 0; p -= lowbit(p)) {res += c[p];}return res; }int main() {//Read();int T, n, m, i, r, t;scanf("%d", &T);while(T--) {scanf("%d", &n);for(i = 1; i <= n; ++i) scanf("%d", num + i);scanf("%d", &m);for(i = 0; i < m; ++i) {scanf("%d%d", &p[i].l, &p[i].r);if(p[i].l > p[i].r) swap(p[i].l, p[i].r);p[i].id = i;}sort(p, p + m);memset(c, 0, sizeof(c));mp.clear();r = 1;for(i = 0; i < m; ++i) {while(r <= p[i].r) {t = num[r];if(mp[t] != 0) {add(mp[t], -t);}add(r, t);mp[t] = r++;}ans[p[i].id] = sum(p[i].r) - sum(p[i].l - 1);}for(i = 0; i < m; ++i) {OUT(ans[i]);}}return 0; } View Code

?

?

?

?

?

?

?

轉載于:https://www.cnblogs.com/vongang/archive/2013/06/05/3118811.html

總結

以上是生活随笔為你收集整理的HDU 3874 Necklace (数状数组)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。