ZOJ 3279 Ants(線分ツリーの単点更新とクエリー)
1846 ワード
ベースの線分ツリーを比較しました.の
ACコード:
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("
");*/
printf("%d
",ans);
}
}
}
return 0;
}