hdu 3874(树状数组)
来源:互联网 发布:ea交易编程零基础教学 编辑:程序博客网 时间:2024/05/16 02:49
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
const int maxn=50010;
//离线算法
int n;
__int64 c[maxn];
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int val)
{
for(int i=x;i<=n;i+=lowbit(i))
{
c[i]+=val;
}
}
__int64 getsum(int x)
{
__int64 cnt=0;
for(int i=x;i>=1;i-=lowbit(i))
{
cnt+=c[i];
}
return cnt;
}
int a[maxn];
map<int,int> hash;
struct Node
{
int l,r;
int idx;
};
Node qu[maxn*4];
__int64 ans[maxn*4];
bool cmp(Node h,Node k)
{
return h.r<k.r;
}
int main()
{
int ci;scanf("%d",&ci);
while(ci--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
int q;
scanf("%d",&q);
for(int i=0;i<q;i++)
{
int l,r;
scanf("%d%d",&l,&r);
if(l>r) swap(l,r);
qu[i].l=l,qu[i].r=r;
qu[i].idx=i;
}
sort(qu,qu+q,cmp); //以r排序
for(int i=1;i<=n;i++)
c[i]=0; //树状数组
hash.clear();
int rr=1;
for(int i=0;i<q;i++)
{
while(rr<=qu[i].r)
{
if(hash[a[rr]]!=0)
{
update(hash[a[rr]],-a[rr]);
}
hash[a[rr]]=rr;
update(rr,a[rr]);
rr++;
}
ans[qu[i].idx]=getsum(qu[i].r)-getsum(qu[i].l-1);
}
for(int i=0;i<q;i++)
printf("%I64d\n",ans[i]);
}
return 0;
}
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
const int maxn=50010;
//离线算法
int n;
__int64 c[maxn];
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int val)
{
for(int i=x;i<=n;i+=lowbit(i))
{
c[i]+=val;
}
}
__int64 getsum(int x)
{
__int64 cnt=0;
for(int i=x;i>=1;i-=lowbit(i))
{
cnt+=c[i];
}
return cnt;
}
int a[maxn];
map<int,int> hash;
struct Node
{
int l,r;
int idx;
};
Node qu[maxn*4];
__int64 ans[maxn*4];
bool cmp(Node h,Node k)
{
return h.r<k.r;
}
int main()
{
int ci;scanf("%d",&ci);
while(ci--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
int q;
scanf("%d",&q);
for(int i=0;i<q;i++)
{
int l,r;
scanf("%d%d",&l,&r);
if(l>r) swap(l,r);
qu[i].l=l,qu[i].r=r;
qu[i].idx=i;
}
sort(qu,qu+q,cmp); //以r排序
for(int i=1;i<=n;i++)
c[i]=0; //树状数组
hash.clear();
int rr=1;
for(int i=0;i<q;i++)
{
while(rr<=qu[i].r)
{
if(hash[a[rr]]!=0)
{
update(hash[a[rr]],-a[rr]);
}
hash[a[rr]]=rr;
update(rr,a[rr]);
rr++;
}
ans[qu[i].idx]=getsum(qu[i].r)-getsum(qu[i].l-1);
}
for(int i=0;i<q;i++)
printf("%I64d\n",ans[i]);
}
return 0;
}
- hdu 3874 树状数组
- hdu 3874(树状数组)
- hdu 3874 树状数组
- HDU 3874 树状数组
- hdu 3874 树状数组
- HDU 3874 Necklace 树状数组
- hdu (3874)树状数组
- HDU 3874 Necklace (树状数组)
- Hdu 3874 Necklace 树状数组
- hdu 3874 Necklace (树状数组)
- HDU(3874)树状数组+离线
- hdu 3874 树状数组+离散化
- HDU 3874 Necklace 树状数组的应用
- HDU 3874 树状数组 + 离线处理
- hdu 3874 Necklace【树状数组简单应用】
- hdu 3874 树状数组 成段不重复求和
- hdu 3874 Necklace(离线+树状数组)
- HDU 3874 Necklace 树状数组 + 离线处理
- NYOJ skiing(BFS)
- 进度条图片,进度条素材
- H-JTAG检测不到内核
- HDU 3264 求两圆相交的面积
- l静态化页面
- hdu 3874(树状数组)
- 在Eclipse RCP Workbench 启动前弹出自定义对话框
- C++0x
- Mysql 即时写表 缓存 MSSQL
- [Visual C++系列]3.4 文档,视,框架之间相互作用
- Eclipse RCP 开发中的常见问题
- android模拟器创建和使用SD卡
- [iPhone开发]用Leaks检查泄露
- zoj1259Rails(栈)