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;
}
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;
}
相关文章推荐
- 线段树题集
- 线段树
- hdu1754
- HDU1394
- 敌兵布阵 (1)
- I Hate It (1)
- LCIS (2)
- A Simple Problem with Integers (2)
- Mayor's posters (3)
- Buy Tickets (3)
- 线段树
- UVA - 12532 Interval Product
- POJ 3264 Balanced Lineup
- hdu 1542 求矩形并的面积
- 关于数据结构之线段树
- poj 3225 关于集合运算
- poj 2352
- hihocoder #1069 线段树
- hdu1166敌兵布阵(线段树点修改)
- 【51nod 6级题目】XOR key 问题