标签:integer this air bsp contains opera using first value
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5014 Accepted Submission(s): 1777
题目链接:HDU 3333
对于莫队式暴力就没啥好说的了,时间慢不说代码量也比较大,还是离线+BIT快
代码:
#include <stdio.h> #include <bits/stdc++.h> using namespace std; #define INF 0x3f3f3f3f #define LC(x) (x<<1) #define RC(x) ((x<<1)+1) #define MID(x,y) ((x+y)>>1) #define CLR(arr,val) memset(arr,val,sizeof(arr)) #define FAST_IO ios::sync_with_stdio(false);cin.tie(0); typedef pair<int, int> pii; typedef long long LL; const double PI = acos(-1.0); const int N = 30010; const int M = 200010; struct info { int k, l, r, flag, id; info() {} info(int _k, int _l, int _r, int _flag, int _id): k(_k), l(_l), r(_r), flag(_flag), id(_id) { } bool operator<(const info &rhs)const { return k < rhs.k; } }; info Q[M]; LL T[N], ans[M >> 1]; int arr[N]; map<int, int>last; void init() { CLR(T, 0); CLR(ans, 0); last.clear(); } void add(int k, LL v) { while (k < N) { T[k] += v; k += (k & -k); } } LL getsum(int k) { LL ret = 0; while (k) { ret += T[k]; k -= (k & -k); } return ret; } int main(void) { int tcase, n, m, i; scanf("%d", &tcase); while (tcase--) { init(); scanf("%d", &n); for (i = 1; i <= n; ++i) scanf("%d", &arr[i]); scanf("%d", &m); int qcnt = 0; for (i = 0; i < m; ++i) { int l, r; scanf("%d%d", &l, &r); Q[qcnt++] = info(l, l, r, 0, i); Q[qcnt++] = info(r, l, r, 1, i); } sort(Q, Q + qcnt); int x = 1; for (i = 0; i < qcnt; ++i) { while (x <= Q[i].k) { if (last[arr[x]]) add(last[arr[x]], -arr[x]); add(x, arr[x]); last[arr[x]] = x; ++x; } if (Q[i].flag) ans[Q[i].id] += getsum(Q[i].r) - getsum(Q[i].l - 1); else { add(x - 1, -arr[x - 1]); ans[Q[i].id] -= getsum(Q[i].r) - getsum(Q[i].l - 1); add(x - 1, arr[x - 1]); } } for (i = 0; i < m; ++i) printf("%I64d\n", ans[i]); } return 0; }
标签:integer this air bsp contains opera using first value
原文地址:http://www.cnblogs.com/Blackops/p/6390957.html