Question: Calculate the sum of non-repeated numbers in the [L, R] interval. N count, M queries
Solution: It is only known when you process M inquiries offline and view others' ideas... Thinking is limited to preprocessing n numbers...
For M requests, the values in the right range are sorted in ascending order. Scan the N count. If you find that the current number has appeared before, delete the number from the position where the previous number appeared and insert the number in the new position. The advantage of doing so is that deleting the previous duplicate will not affect the latter. Time complexity is controlled by the order of magnitude of (n + M) logn.
PS: sleeping at noon, sleepy, I'm dead.
#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