HDU 3333:http://acm.hdu.edu.cn/showproblem.php?pid=3333

这两个题是类似的,都是离线处理查询,对每次查询的区间的右端点进行排序。这里我们需要离散化处理一下,标记一下前面是否出现过这个值,然后不断更新last数组(该数组保存的是每个数最后一次出现的位置)。最后用树状数组维护。

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = ;
const int maxq = ;
struct node{
int l,r,index;
};
node query[maxq];
ll sum[maxn],ans[maxq];
ll a[maxn],b[maxn],last[maxn];
int n;
bool cmp(node a,node b){
return a.r < b.r;
}
ll getsum(int i){
ll s = ;
while(i > ){
s += sum[i];
i -= i&(-i);
}
return s;
}
void add(int i,ll x){
while(i <= n){
sum[i] += x;
i += i&(-i);
}
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i = ;i<=n;i++){
scanf("%I64d",&a[i]);
b[i] = a[i];//离散化用
}
sort(b+,b++n);//排序,以每个数的下标标记
int q;
scanf("%d",&q);
for(int i = ;i<=q;i++){
scanf("%d%d",&query[i].l,&query[i].r);
query[i].index = i;
}
sort(query+,query++q,cmp);
memset(sum,,sizeof(sum));
memset(last,,sizeof(last));
int cnt = ;//每个查询的下标
for(int i = ;i<=n;i++){
int index = lower_bound(b+,b++n,a[i])-b-;//找到该数对应的下标
if(last[index])//判断该数是否出现过,有的话减去
add(last[index],-a[i]);
add(i,a[i]);
last[index] = i;
while(query[cnt].r==i && cnt<=q){
ans[query[cnt].index] = getsum(query[cnt].r)-getsum(query[cnt].l-);
cnt++;
}
}
for(int i = ;i<=q;i++)
printf("%I64d\n",ans[i]);
}
return ;
}

Codeforces 703D:http://codeforces.com/contest/703/problem/D

这道题需要多思考的一步是,要求的区间内出现偶数次的数的异或和,等于这个区间内所有数字的异或和异或这个区间内不同数字的异或和,以1、2、1、3、3、2、3举例,结果就是(1^2^1^3^3^2^3)^(1^2^3),原理就是出现偶数次的数异或它自己等于它本身,出现奇数次的数异或它自己为0。对于区间的异或和,我们可以用数组很方便的求出,不同数字的异或和,只需要对上题进行一下改造就好了。

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll; const int maxn = ;
struct node{
int l,r,index;
};
node query[maxn];
ll sum[maxn],a[maxn],b[maxn],c[maxn],last[maxn],ans[maxn];
int n;
bool cmp(node a,node b){
return a.r < b.r;
}
ll getsum(int i){
ll s = ;
while(i > ){
s ^= sum[i];//注意
i -= i&(-i);
}
return s;
}
void add(int i,ll x){
while(i <= n){
sum[i] ^= x;//注意
i += i&(-i);
}
}
int main(){
scanf("%d",&n);
for(int i = ;i<=n;i++){
scanf("%I64d",&a[i]);
c[i] = a[i]^c[i-];//求前缀异或和
b[i] = a[i];
}
sort(b+,b++n);
int q;
scanf("%d",&q);
for(int i = ;i<=q;i++){
scanf("%d%d",&query[i].l,&query[i].r);
query[i].index = i;
}
sort(query+,query++q,cmp);
int cnt = ;
for(int i = ;i<=n;i++){
int index = lower_bound(b+,b++n,a[i])-b-;
if(last[index])
add(last[index],a[i]);
last[index] = i;
add(i,a[i]);
while(query[cnt].r==i && cnt<=q){
ans[query[cnt].index] = (c[query[cnt].r]^c[query[cnt].l-])^(getsum(query[cnt].r)^getsum(query[cnt].l-));//注意
cnt++;
}
}
for(int i = ;i<=q;i++)
printf("%I64d\n",ans[i]);
return ;
}

HDU 3333 | Codeforces 703D 树状数组、离散化的更多相关文章

  1. HDU 3333 Turing Tree (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3333 题意就是询问区间不同数字的和. 比较经典的树状数组应用. //#pragma comment(l ...

  2. HDU 4325 Flowers(树状数组+离散化)

    http://acm.hdu.edu.cn/showproblem.php?pid=4325 题意:给出n个区间和m个询问,每个询问为一个x,问有多少个区间包含了x. 思路: 因为数据量比较多,所以需 ...

  3. HDU 3333 - Turing Tree (树状数组+离线处理+哈希+贪心)

    题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...

  4. HDU 3333 Turing Tree --树状数组+离线处理

    题意:统计一段序列[L,R]的和,重复元素只算一次. 解法:容易看出在线做很难处理重复的情况,干脆全部讲查询读进来,然后将查询根据右端点排个序,然后离散化数据以后就可以操作了. 每次读入一个数,如果这 ...

  5. hdu4605 树状数组+离散化+dfs

    Magic Ball Game Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  6. BZOJ_5055_膜法师_树状数组+离散化

    BZOJ_5055_膜法师_树状数组+离散化 Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然 ...

  7. POJ 2299 【树状数组 离散化】

    题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...

  8. Educational Codeforces Round 10 D. Nested Segments 离线树状数组 离散化

    D. Nested Segments 题目连接: http://www.codeforces.com/contest/652/problem/D Description You are given n ...

  9. HDU 4325 Flowers 树状数组+离散化

    Flowers Problem Description As is known to all, the blooming time and duration varies between differ ...

随机推荐

  1. MATLAB的一些基础知识

    1.已知a1=sin(sym(pi/4)+exp(sym(0.7)+sym(pi/3)))产生精准符号数字,请回答:以下产生的各种符号数哪些是精准的?若不精准,误差又是多少?能说出产生误差的原因吗? ...

  2. [No000084]C# 使用Log4Net(1)-快速建立一个demo

    1.下载Log4Net: http://logging.apache.org/log4net/download_log4net.cgi 2.新建一个WinForm程序,取名Log4NetDemo 3. ...

  3. 操作系统Unix、Windows、Mac OS、Linux的故事

    电脑,计算机已经成为我们生活中必不可少的一部分.无论是大型的超级计算机,还是手机般小巧的终端设备,都跑着一个操作系统.正是这些操作系统,让那些硬件和芯片得意组合起来,让那些软件得以运行,让我们的世界在 ...

  4. git没有changId解决方法

    1.git branch work(从最新节点建立分支,相当于将自己的修改备份到新的分支) 2.git reset --hard HEAD~10(强制回滚多个节点) 3.git status 如果显示 ...

  5. [LeetCode] The Skyline Problem

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

  6. LeetCode:N-Queens I II(n皇后问题)

    N-Queens The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no tw ...

  7. SharePoint远程发布Solution

    1.在本地修改好代码,选择publish,将.wsp文件保存到本地: 2.将wsp文件上传到目标网站,停止原解决方案并删除,上传新解决方法并且激活: 停止原来的解决方案 上传解决方案 3.激活Solu ...

  8. php中关于引用(&)详解

    php中关于引用(&)详解 php的引用(就是在变量或者函数.对象等前面加上&符号) 在PHP 中引用的意思是:不同的变量名访问同一个变量内容. 与C语言中的指针是有差别的.C语言中的 ...

  9. url 中 # ? & 的作用

    1. # 10年9月,twitter改版.一个显著变化,就是URL加入了"#!"符号.比如,改版前的用户主页网址为http://twitter.com/username改版后,就变 ...

  10. struts2报错:There is no Action mapped for namespace [/] and action name [userAction!add]

    使用struts2.3进行动态方法调用时出现: There is no Action mapped for namespace [/user] and action name [user!add] a ...