您的位置:首页 > 产品设计 > UI/UE

UESTC 1059 线段树离散化

2015-11-24 10:22 483 查看


秋实大哥与小朋友


Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)


Submit 
Status

秋实大哥以周济天下,锄强扶弱为己任,他常对天长叹:安得广厦千万间,大庇天下寒士俱欢颜。
所以今天他又在给一群小朋友发糖吃。
他让所有的小朋友排成一行,从左到右标号。在接下去的时间中,
他有时会给一段区间的小朋友每人v颗糖,有时会问第x个小朋友手里有几颗糖。
这对于没上过学的孩子来说实在太困难了,所以你看不下去了,请你帮助小朋友回答所有的询问。


Input

第一行包含两个整数n,m,表示小朋友的个数,以及接下来你要处理的操作数。
接下来的m行,每一行表示下面两种操作之一:
0 l r v : 表示秋实大哥给[l,r]这个区间内的小朋友每人v颗糖

1 x : 表示秋实大哥想知道第x个小朋友手里现在有几颗糖

1≤m,v≤100000,1≤l≤r≤n,1≤x≤n,1≤n≤100000000。


Output

对于每一个1 x操作,输出一个整数,表示第x个小朋友手里现在的糖果数目。


Sample input and output

Sample InputSample Output
3 4
0 1 3 1
1 2
0 2 3 3
1 3

1
4

题解:先离散化,再建树,错了10次忘记了使用hash后的的值,记得每次查询需要都要使

用hash后的的值,这里可以用set,map方便的去重,映射。

#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
using namespace std;
#define LL long long
#define N  300000
set<int>li;
map<int,int>Hash;
struct point
{
int lc,rc;
LL add,sum;
}tree[N*4];
struct guiyi
{
int k1,x1,y1;
LL v1;
guiyi(){}
guiyi(int _k,int _x,int _y,LL _v):k1(_k),x1(_x),y1(_y),v1(_v){}
};
vector<guiyi>eg;
void pushdown(int rt)
{
if(tree[rt].add)
{
tree[rt<<1].add+=tree[rt].add;
tree[rt<<1|1].add+=tree[rt].add;
tree[rt<<1].sum+=tree[rt].add;
tree[rt<<1|1].sum+=tree[rt].add;
tree[rt].add=0;
}
}
void build(int L,int R,int rt)
{
tree[rt].lc=L;
tree[rt].rc=R;
tree[rt].add=0;
tree[rt].sum=0;
if(L==R)return ;
int mid=(tree[rt].lc+tree[rt].rc)>>1;
build(L,mid,rt<<1);
build(mid+1,R,rt<<1|1);
}
void update(int L,int R,int rt,LL v)
{
if(tree[rt].lc==L&&tree[rt].rc==R)
{
tree[rt].add+=v;
tree[rt].sum+=v;
return ;
}
pushdown(rt);
int mid=(tree[rt].lc+tree[rt].rc)>>1;
if(R<=mid)update(L,R,rt<<1,v);
else if(L>mid)update(L,R,rt<<1|1,v);
else
{
update(L,mid,rt<<1,v);
update(mid+1,R,rt<<1|1,v);
}
}
LL query(int pos,int rt)
{
if(tree[rt].lc==tree[rt].rc&&tree[rt].lc==pos)
{
return tree[rt].sum;
}
pushdown(rt);
int mid=(tree[rt].lc+tree[rt].rc)>>1;
if(pos<=mid)query(pos,rt<<1);
else query(pos,rt<<1|1);
}
int main()
{
#ifdef CDZSC
freopen("i.txt","r",stdin);
#endif
int k,x,y;
LL v;
int n,m;
while(~scanf("%d%d",&n,&m))
{
int poli=1;
Hash.clear();
eg.clear();
li.clear();
while(m--)
{
scanf("%d",&k);
if(k==1)
{
scanf("%d",&x);
eg.push_back(guiyi(k,x,0,0));
li.insert(x);
}
else
{
scanf("%d%d%lld",&x,&y,&v);
eg.push_back(guiyi(k,x,y,v));
li.insert(x);li.insert(y);
}
}
for(set<int>::iterator it=li.begin();it!=li.end();it++)
{
Hash[*it]=poli;
poli++;
}
build(1,poli-1,1);
for(int i=0;i<eg.size();i++)
{
if(eg[i].k1==1)
{
printf("%lld\n",query(Hash[eg[i].x1],1));
}
else
{
int Lx,Ry;
LL V;
Lx=Hash[eg[i].x1];
Ry=Hash[eg[i].y1];
V=eg[i].v1;
update(Lx,Ry,1,V);
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: