HDU 3333 Turing Tree 线段树 or 树状数组

/*
ID: sdj22251
PROG: subset
LANG: C++
*/
#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
#define MAXN 66666
#define MAXM 104444
#define INF 100000000
#define eps 1e-7
#define L(X) X<<1
#define R(X) X<<1|1
using namespace std;
struct interval
{
int l, r, id;
bool operator <(const interval &a)const
{
return r < a.r;
}
}p[MAXM];
int x[MAXN], a[MAXN], h[MAXN], n;
long long tx[MAXN], ans[MAXM];
int bin(int v, int bound)
{
int low = 0, high = bound - 1;
while(low <= high)
{
int mid = (low + high) >> 1;
if(x[mid] == v) return mid;
if(x[mid] < v) low = mid + 1;
else high = mid - 1;
}
return -1;
}
int lowbit(int x)
{
return x & -x;
}
void modify(int x, int v)
{
for(int i = x; i <= n; i += lowbit(i))
tx[i] += v;
}
long long get_sum(int x)
{
long long sum = 0;
for(int i = x; i > 0; i -= lowbit(i))
sum += tx[i];
return sum;
}
int main()
{
int T, m;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
int cnt = 0;
for(int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
x[cnt++] = a[i];
}
memset(tx, 0, sizeof(tx));
sort(x, x + cnt);
cnt = unique(x, x + cnt) - x;
scanf("%d", &m);
for(int i = 0; i < m; i++)
{
scanf("%d%d", &p[i].l, &p[i].r);
p[i].id = i;
}
sort(p, p + m);
for(int i = 0; i < cnt; i++)
h[i] = 0;
int j = 0;
for(int i = 1; i <= n; i++)
{
int id = bin(a[i], cnt);
if(h[id]) modify(h[id], -a[i]);
modify(i, a[i]);
h[id] = i;
for(; j < m; j++)
{
if(p[j].r == i) ans[p[j].id] = get_sum(p[j].r) - get_sum(p[j].l - 1);
else break;
}
}
for(int i = 0; i < m; i++)
printf("%I64d\n", ans[i]);
}
return 0;
}


hdu3333-Turing Tree 线段树+离散化

2017-07-18 20:01:04

HDU 3333 线段树+离线处理

2016-06-12 21:03:43

HDU 3333 Turing Tree（树状数组 || 线段树）

2015-08-06 01:19:24

hdu 3333 Turing Tree 线段树/树状数组

2013-04-28 19:58:40

HDU 3333 Turing Tree（离线操作+线段树||树状数组）

2017-07-27 09:45:12

hdu 3333 Turing Tree 树状数组或者线段树

2013-07-20 12:58:20

HDU - 3333 Turing Tree(树状数组)

2015-10-20 16:04:28

HDU 3333 Turing Tree（树状数组）

2013-12-03 13:29:13

hdu 3333 Turing Tree 树状数组

2015-07-17 18:45:45

图说线段树和树状数组

2014-08-25 17:30:36