hdu 3333 Turing Tree (求区间中不相同数的和,离线+线段树/树状数组)
喵呜,离散树状数组。
这道题由于相同的值加和的时候只算一次,所以比较伤脑筋==
怎么办呢?
我们发现对于一个值,由于相同的只算一次,所以在任意时间内,这个值只需要出现一次。
如果我们从作往右将原数组更新到树状数组,如果某个值之前出现过,那么我在更新当前的时候,还需要删掉之前那个。
这样就可以保证当前的序列中一个值只出现了一次,而且位置更新成最后出现的位置。
如果只出现一次的话那就又是我们喜闻乐见的那个熟悉的树状数组了呢 喵呜!
但是这样删掉前面出现的元素真心大丈夫? 万一之后又查询了之前你删掉的点岂不是整个人都萌萌哒了?
所以我们可以按照查询区间的又断点排序,保证前面删掉的点以后不会再被查询。
也就是按照顺序,从左往右,边查询,边更新原数组到树状数组。
由于查询区间是无序的,按照右端点排序之后打乱了原来的查询顺序,所以要离线操作。
由于查询区间是无序的,按照右端点排序之后打乱了原来的查询顺序,所以要离线操作。
由于查询区间是无序的,按照右端点排序之后打乱了原来的查询顺序,所以要离线操作。
** **重要的事情说三遍。
所谓离线操作,就是查询的时候不直接输出答案,而是将答案存起来,然后最后一起输出。
我们需要开一个数组pre [x] 记录x这个数的上一个位置,初始为-1
由 x的范围太大,数组存不下,所以要离散化。
离散化是为了记录一个数上次出现的位置!
注意要开LL 。
1/*************************************************************************
2 > File Name: code/hdu/3333.cpp
3 > Author: 111qqz
4 > Email: rkz2013@126.com
5 > Created Time: 2015年08月07日 星期五 17时04分07秒
6 ************************************************************************/
7
8#include<iostream>
9#include<iomanip>
10#include<cstdio>
11#include<algorithm>
12#include<cmath>
13#include<cstring>
14#include<string>
15#include<map>
16#include<set>
17#include<queue>
18#include<vector>
19#include<stack>
20#define y0 abc111qqz
21#define y1 hust111qqz
22#define yn hez111qqz
23#define j1 cute111qqz
24#define tm crazy111qqz
25#define lr dying111qqz
26using namespace std;
27#define REP(i, n) for (int i=0;i<int(n);++i)
28typedef long long LL;
29typedef unsigned long long ULL;
30const int inf = 0x7fffffff;
31const int N=3E4+3;
32LL c[N];
33LL n,m,qur;
34LL ref[N];
35LL pre[N];
36LL ori[N];
37struct Q
38{
39 LL val,id;
40}q[N];
41
42struct S
43{
44 LL x,y;
45 LL id,ans;
46}s[100005];
47
48bool cmp( Q a,Q b)
49{
50 if (a.val<b.val) return true;
51 return false;
52}
53bool cmp2(S a,S b)
54{
55 if (a.y<b.y) return true;
56 return false;
57}
58
59LL lowbit ( int x)
60{
61 return x&(-x);
62}
63
64void update ( LL x,LL delta)
65{
66 for ( LL i = x ; i <= n ; i = i + lowbit(i))
67 {
68 c[i] = c[i] + delta;
69 }
70}
71LL sum( LL x)
72{
73 LL res = 0 ;
74 for ( LL i = x; i >= 1 ; i = i - lowbit(i))
75 {
76 res = res + c[i];
77 }
78 return res;
79}
80int main()
81{
82 int T;
83 cin>>T;
84 while (T--)
85 {
86 memset(ref,0,sizeof(ref));
87 memset(c,0,sizeof(c));
88 memset(pre,-1,sizeof(pre)); //标记上次出现位置的数组
89 scanf("%lld",&n);
90 for ( LL i = 1 ; i <= n ; i++)
91 {
92 scanf("%lld",&q[i].val);
93 q[i].id = i;
94 }
95 sort(q+1,q+n+1,cmp);
96 LL cnt = 0;
97 for ( LL i = 1 ; i <= n ; i++ )
98 {
99 if (q[i].val!=q[i-1].val)
100 {
101 cnt++;
102 }
103 ref[q[i].id] = cnt;
104 ori[q[i].id] = q[i].val;
105 }
106
107 scanf("%lld",&qur);
108 for ( LL i = 1 ;i <= qur; i++ )
109 {
110 scanf("%lld %lld",&s[i].x,&s[i].y);
111 s[i].id = i;
112 }
113 sort(s+1,s+1+qur,cmp2);
114 s[0].y = 0;
115 for ( LL i = 1 ; i <= qur ; i++)
116 {
117 for ( LL j = s[i-1].y+1 ; j <= s[i].y ; j++)
118 {
119 int tmp = ref[j];
120 if (pre[tmp]==-1)
121 {
122 update(j,ori[j]);
123 }
124 else
125 {
126 update (j,ori[j]);
127 update (pre[tmp],-ori[j]);
128 }
129 pre[tmp] = j;
130 }
131 s[s[i].id].ans = sum(s[i].y)-sum(s[i].x-1);
132 }
133 for ( int i = 1 ; i <= qur ; i++ )
134 {
135 cout<<s[i].ans<<endl;
136 }
137
138 }
139
140 return 0;
141}
update:20160916,写了个线段树的版本。
1/* ***********************************************
2Author :111qqz
3Created Time :Fri 16 Sep 2016 08:26:34 PM CST
4File Name :code/hdu/r3333.cpp
5************************************************ */
6#include <cstdio>
7#include <cstring>
8#include <iostream>
9#include <algorithm>
10#include <vector>
11#include <queue>
12#include <set>
13#include <map>
14#include <string>
15#include <cmath>
16#include <cstdlib>
17#include <ctime>
18#define fst first
19#define sec second
20#define lson l,m,rt<<1
21#define rson m+1,r,rt<<1|1
22#define ms(a,x) memset(a,x,sizeof(a))
23typedef long long LL;
24#define pi pair < int ,int >
25#define MP make_pair
26using namespace std;
27const double eps = 1E-8;
28const int dx4[4]={1,0,0,-1};
29const int dy4[4]={0,-1,1,0};
30const int inf = 0x3f3f3f3f;
31const int N=3E4+7;
32const int M=1E5+7;
33LL tree[N<<2];
34LL a[N];
35int n,m;
36struct node
37{
38 int l,r;
39 int id;
40 bool operator < (node b)
41 {
42 if (r==b.r) return l<b.l;
43 return r<b.r;
44 }
45}q[M];//把M写成N
46void PushUp( int rt)
47{
48 tree[rt] = tree[rt<<1] + tree[rt<<1|1];
49}
50void update( int p,LL sc,int l,int r,int rt)
51{
52 if (l==r)
53 {
54 tree[rt]+=sc;
55 return;
56 }
57 int m = (l+r)>>1;
58 if (p<=m)update(p,sc,lson);
59 else update(p,sc,rson);
60 PushUp(rt);
61}
62LL query(int L,int R,int l,int r,int rt)
63{
64 if (L<=l&&r<=R) return tree[rt];
65 int m = (l+r)>>1;
66 LL ret = 0 ;
67 if (L<=m) ret+= query(L,R,lson);
68 if (R>=m+1) ret+=query(L,R,rson);
69 return ret;
70}
71map<LL,int>mp;
72LL ans[M]; //把M写成N
73int main()
74{
75 #ifndef ONLINE_JUDGE
76 freopen("code/in.txt","r",stdin);
77 #endif
78 int T;
79 scanf("%d",&T);
80 while (T--)
81 {
82 ms(tree,0);
83 ms(ans,0);
84 mp.clear();
85 scanf("%d",&n);
86 for ( int i = 1; i <= n ; i++) scanf("%lld",a+i);
87 scanf("%d",&m);
88 for ( int i = 1 ; i <= m ; i++)
89 {
90 int a,b;
91 scanf("%d%d",&a,&b);
92 q[i].l = a;
93 q[i].r = b;
94 q[i].id = i ;
95 }
96 sort(q+1,q+m+1);
97 int l = 1;
98 for ( int i = 1 ; i <= m ; i++)
99 {
100 for ( ; l <= q[i].r ; l++)
101 {
102 if (mp[a[l]]) update(mp[a[l]],-a[l],1,n,1);
103 mp[a[l]] = l ;
104 update(mp[a[l]],a[l],1,n,1);
105 }
106 ans[q[i].id] = query(q[i].l,q[i].r,1,n,1);
107 }
108 for ( int i = 1 ; i <= m ; i++) printf("%lld\n",ans[i]);
109 }
110 #ifndef ONLINE_JUDGE
111 fclose(stdin);
112 #endif
113 return 0;
114}