hdu 3874 Necklace 线段树
2014-02-22 14:29
225 查看
跟hdu3333相同,改下N和Q数据范围即可
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int N=50005;
const int Q=200005;
typedef long long LL;
int n,tt,qq;
map<LL,int> hashs;
LL sum[N*4],A
,ans[Q];
struct QN{
int l,r,index;
}q[Q];
int cmp(QN a,QN b){
return a.r<b.r;
}
void build(int l,int r,int rt){
sum[rt]=0;
if(l==r) return;
int m=(l+r)/2;
build(lson);
build(rson);
}
void push_up(int rt){
sum[rt]=sum[rt*2]+sum[rt*2+1];
}
void update(int a,LL c,int l,int r,int rt){//单点更新a的值为c
if(l==r){
sum[rt]=c;return;
}
int m=(l+r)/2;
if(a<=m)
update(a,c,lson);
else
update(a,c,rson);
push_up(rt);
}
LL query(int a,int b,int l,int r,int rt){
if(a<=l&&b>=r)
return sum[rt];
int m=(l+r)/2;
LL rst=0;
if(a<=m)
rst+=query(a,b,lson);
if(b>m)
rst+=query(a,b,rson);
return rst;
}
void solve(){
int pos=1;
for(int i=0;i<qq;i++){
while(q[i].r>=pos){//加入a
if(hashs[A[pos]])//删除前一个
update(hashs[A[pos]],0,1,n,1);
hashs[A[pos]]=pos;
update(pos,A[pos],1,n,1);
pos++;//pos放上面好像出问题
}
ans[q[i].index]=query(q[i].l,q[i].r,1,n,1);
}
}
int main(){
for(cin>>tt;tt>0;tt--){
hashs.clear();
cin>>n;
build(1,n,1);
for(int i=1;i<=n;i++)
scanf("%d",&A[i]);
cin>>qq;
for(int i=0;i<qq;i++){//按r排序,离线查
scanf("%d %d",&q[i].l,&q[i].r);
q[i].index=i;
}
sort(q,q+qq,cmp);
solve();
for(int i=0;i<qq;i++)
printf("%I64d\n",ans[i]);
}
return 0;
}
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int N=50005;
const int Q=200005;
typedef long long LL;
int n,tt,qq;
map<LL,int> hashs;
LL sum[N*4],A
,ans[Q];
struct QN{
int l,r,index;
}q[Q];
int cmp(QN a,QN b){
return a.r<b.r;
}
void build(int l,int r,int rt){
sum[rt]=0;
if(l==r) return;
int m=(l+r)/2;
build(lson);
build(rson);
}
void push_up(int rt){
sum[rt]=sum[rt*2]+sum[rt*2+1];
}
void update(int a,LL c,int l,int r,int rt){//单点更新a的值为c
if(l==r){
sum[rt]=c;return;
}
int m=(l+r)/2;
if(a<=m)
update(a,c,lson);
else
update(a,c,rson);
push_up(rt);
}
LL query(int a,int b,int l,int r,int rt){
if(a<=l&&b>=r)
return sum[rt];
int m=(l+r)/2;
LL rst=0;
if(a<=m)
rst+=query(a,b,lson);
if(b>m)
rst+=query(a,b,rson);
return rst;
}
void solve(){
int pos=1;
for(int i=0;i<qq;i++){
while(q[i].r>=pos){//加入a
if(hashs[A[pos]])//删除前一个
update(hashs[A[pos]],0,1,n,1);
hashs[A[pos]]=pos;
update(pos,A[pos],1,n,1);
pos++;//pos放上面好像出问题
}
ans[q[i].index]=query(q[i].l,q[i].r,1,n,1);
}
}
int main(){
for(cin>>tt;tt>0;tt--){
hashs.clear();
cin>>n;
build(1,n,1);
for(int i=1;i<=n;i++)
scanf("%d",&A[i]);
cin>>qq;
for(int i=0;i<qq;i++){//按r排序,离线查
scanf("%d %d",&q[i].l,&q[i].r);
q[i].index=i;
}
sort(q,q+qq,cmp);
solve();
for(int i=0;i<qq;i++)
printf("%I64d\n",ans[i]);
}
return 0;
}
相关文章推荐
- hdu 3874 Necklace 线段树单点更新区间求和
- HDU - 3874Necklace(线段树离线查询)
- hdu 3874 Necklace(线段树)
- HDU 3874 Necklace(线段树啊 单点更新 区间求和)
- HDU 3874 Necklace (线段树单点更新+区间查询+离线操作)
- hdu 3874 Necklace 线段树 单点更新
- hdu 3874 Necklace(线段树)
- hdu 3874 Necklace(离线操作+树状数组或线段树)
- hdu 3874 Necklace(线段树)
- 线段树 HDU 3874 necklace
- HDU - 3874 Necklace (线段树 + 离线处理)
- 【HDU - 3874 Necklace】 线段树
- HDU 3874 Necklace (树状数组 | 线段树 的离线处理)
- hdu 3874【Necklace】
- HDU 3874 Necklace 区间查询的离线操作
- HDU 3874 Necklace (数状数组)
- hdu 3333 Turing Tree & hdu 3874 Necklace (成段更新)
- HDU 3874 离线段树
- hdu 3874 Necklace (树状数组)
- HDU 3874 Necklace 树状数组 + 离线处理