您的位置:首页 > 其它

ZOJ 3279 Ants(线段树单点更新和查询)

2016-03-04 20:01 387 查看
比较基础的线段树了。。

AC代码:

/* ***********************************************
Author :yzkAccepted
Created Time :2016/3/3 18:31:07
TASK :ggfly.cpp
LANG :C++
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <stack>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int maxn=100010;
int cnt[maxn<<2];
int have[maxn];
void updata(int num,int k,int l,int r,int rt)
{
if(l==r)
{
cnt[rt]=num;
return ;
}
int m=(l+r)/2;
if(k<=m) updata(num,k,lson);
else updata(num,k,rson);
cnt[rt]=cnt[rt<<1]+cnt[rt<<1|1];
}
int query(int pos,int l,int r,int rt)
{
if(l==r)
{
return l;
}
int m=(l+r)/2;
if(pos<=cnt[rt<<1])
return query(pos,lson);
else
{
pos-=cnt[rt<<1];
return query(pos,rson);
}
}
int main()
{
int n,q;
string nmd;
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
{
scanf("%d",&have[i]);
updata(have[i],i,1,n,1);
}
scanf("%d",&q);
while(q--)
{
cin>>nmd;
if(nmd[0]=='p')
{
int x,y;
scanf("%d%d",&x,&y);
updata(y,x,1,n,1);
}
else
{
int ans,x;
scanf("%d",&x);
ans=query(x,1,n,1);
/* for(int j=1;j<=5;j++)
printf("%d ",cnt[j]);
printf("\n");*/
printf("%d\n",ans);
}
}

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  线段树