HDU 3874 树状数组 边查询边更新
2014-02-24 22:05
134 查看
树状数组的好题,
对于一对查询,边查询边更新。
那个只能出现一个这个条件就是更新
每次把上一次出现的地方的东西消灭掉,换成最新的这个地方的。
第一次做这种边查询,边更新的题目呢。
好题!
HDU 上要用 I64 代替 lld 啊 真不习惯
对于一对查询,边查询边更新。
那个只能出现一个这个条件就是更新
每次把上一次出现的地方的东西消灭掉,换成最新的这个地方的。
第一次做这种边查询,边更新的题目呢。
好题!
HDU 上要用 I64 代替 lld 啊 真不习惯
#include <iostream> #include <stdio.h> #include <algorithm> #include <string.h> using namespace std; #define MAX 1000009 #define INF 0x3f3f3f3f #define MS(x) memset(x,0,sizeof(x)) #define ll __int64 int pos[MAX]; ll num[MAX]; ll bit[MAX]; ll res[MAX]; bool mark[MAX]; int n; struct node { int l,r,index; bool operator < (const node &o) const { return r<o.r; } }query[MAX]; void add(int i,int x) { while(i<=n) { bit[i]+=x; i+=i&(-i); } } ll sum(int i) { ll ans=0; while(i>0) { ans+=bit[i]; i-=(i&(-i)); } return ans; } int main() { int T; freopen("acm.in","r",stdin); scanf("%d",&T); while(T--) { MS(mark); MS(bit); scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%I64d",&num[i]); int m; scanf("%d",&m); for(int i=1;i<=m;i++) { scanf("%d%d",&query[i].l,&query[i].r),query[i].index=i; if(query[i].l>query[i].r) swap(query[i].l,query[i].r); } sort(query+1,query+m+1); int i=1; for(int j=1;j<=m;j++) { while(i<=query[j].r) { if(!mark[num[i]]) add(i,num[i]),mark[num[i]]=1,pos[num[i]]=i; else add(pos[num[i]],-num[i]),add(i,num[i]),pos[num[i]]=i; i++; } res[query[j].index]=sum(query[j].r)-sum(query[j].l-1); } for(int i=1;i<=m;i++) printf("%I64d\n",res[i]); } return 0; }
相关文章推荐
- HDU 1556 Color the ball (一维树状数组,区间更新,单点查询)
- POJ 2892 Tunnel Warfare || HDU 1540(树状数组+二分 || 线段树的单点更新+区间查询)
- [HDU 4031]Attack[树状数组区间更新单点查询]
- hdu 4533(树状数组区间更新+单点查询)
- HDU 1556 Color the ball(树状数组区间更新点查询)
- 树状数组:HDU1166敌兵布阵 【单点更新,区间查询】
- hdu 1556Color the ball (树状数组,更新区间,查询单点)
- hdu 2642 二维树状数组 单点更新区间查询 模板水题
- hdu 2642 二维树状数组 单点更新区间查询 模板水题
- hdu 3584 Cube (三维树状数组,更新区间,查询单点)
- [HDU 1754]I Hate It[树状数组查询+更新区间最大值]
- HDu 1556 Color the ball【线段树&&树状数组】区间更新,单点查询
- hdu 3874 树状数组 离线处理
- (树状数组)hdu1556 Color the ball(区间更新,单点查询)
- poj 2155(二维树状数组,区间更新,单点查询)
- HDU 3874 Necklace(树状数组离线处理)
- 【HDU - 1541 - Stars 【 区间求和 单点更新 ==树状数组】
- A Simple Problem with Integers(树状数组,更新区间查询区间模板)
- HDU-3966 Aragorn's Story (树链剖分 树状数组 区间修改 点查询)(2011 Multi-University Training Contest 13)
- hdu 4970 树状数组区间更新 思维题