Educational Codeforces Round 37 (Rated for Div. 2) F. SUM and REPLACE(线段树)
2018-02-11 21:22
585 查看
题目链接:http://codeforces.com/contest/920/problem/F
线段树,每次暴力更新,如果发现更新后和更新前一样的话,就停止更新,理性分析一下,其实每个点不会更新几次,预处理出来即可
代码:#include<bits/stdc++.h>
#define xx first
#define yy second
using namespace std;
typedef long long ll;
const int MAXN=3e5+5;
const int M=1e6+5;
struct node
{
int pos,val;
node(int _pos=0,int _val=0):pos(_pos),val(_val){}
bool operator < (const node &o)const
{
return pos<o.pos;
}
};
set<node> s;
int a[MAXN];
struct seg
{
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
ll tr[MAXN<<2];
void push_up(int rt)
{
tr[rt]=tr[rt<<1]+tr[rt<<1|1];
}
void build(int l,int r,int rt)
{
if(l==r)
{
tr[rt]=a[l];
return ;
}
int mid=(l+r)>>1;
build(lson);
build(rson);
push_up(rt);
}
ll query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
return tr[rt];
int mid=(l+r)>>1;
ll ret=0;
if(L<=mid)
ret+=query(L,R,lson);
if(mid<R)
ret+=query(L,R,rson);
return ret;
}
void update(int pos,ll val,int l,int r,int rt)
{
if(l==r)
{
tr[rt]=val;
return ;
}
int mid=(l+r)>>1;
if(pos<=mid)
update(pos,val,lson);
if(mid<pos)
update(pos,val,rson);
push_up(rt);
}
}se;
inline char nc()
{
static char buf[100000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline void rea(int &x)
{
char c=nc();x=0;
for(;c>'9'||c<'0';c=nc());for(;c>='0'&&c<='9';x=x*10+c-'0',c=nc());
}
int D[M];
void init()
{
for(int i=1;i<M;i++)
{
for(int j=i;j<M;j+=i)
{
D[j]++;
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n,m;
init();
rea(n);rea(m);
//scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
rea(a[i]);
//scanf("%d",&a[i]);
s.insert(node(i,a[i]));
}
se.build(1,n,1);
while(m--)
{
int op,l,r;
rea(op);rea(l);rea(r);
//scanf("%d%d%d",&op,&l,&r);
if(op==1)
{
auto it=s.lower_bound(node(l,0));
while(it!=s.end()&&it->pos<=r)
{
int pos=it->pos;
int val=it->val;
if(val==D[val])
{
it=s.erase(it);
}
else
{
s.erase(it);
it=s.insert(node(pos,D[val])).xx;
se.update(pos,D[val],1,n,1);
it++;
}
}
}
if(op==2)
{
printf("%lld\n",se.query(l,r,1,n,1));
}
}
return 0;
}
线段树,每次暴力更新,如果发现更新后和更新前一样的话,就停止更新,理性分析一下,其实每个点不会更新几次,预处理出来即可
代码:#include<bits/stdc++.h>
#define xx first
#define yy second
using namespace std;
typedef long long ll;
const int MAXN=3e5+5;
const int M=1e6+5;
struct node
{
int pos,val;
node(int _pos=0,int _val=0):pos(_pos),val(_val){}
bool operator < (const node &o)const
{
return pos<o.pos;
}
};
set<node> s;
int a[MAXN];
struct seg
{
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
ll tr[MAXN<<2];
void push_up(int rt)
{
tr[rt]=tr[rt<<1]+tr[rt<<1|1];
}
void build(int l,int r,int rt)
{
if(l==r)
{
tr[rt]=a[l];
return ;
}
int mid=(l+r)>>1;
build(lson);
build(rson);
push_up(rt);
}
ll query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
return tr[rt];
int mid=(l+r)>>1;
ll ret=0;
if(L<=mid)
ret+=query(L,R,lson);
if(mid<R)
ret+=query(L,R,rson);
return ret;
}
void update(int pos,ll val,int l,int r,int rt)
{
if(l==r)
{
tr[rt]=val;
return ;
}
int mid=(l+r)>>1;
if(pos<=mid)
update(pos,val,lson);
if(mid<pos)
update(pos,val,rson);
push_up(rt);
}
}se;
inline char nc()
{
static char buf[100000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline void rea(int &x)
{
char c=nc();x=0;
for(;c>'9'||c<'0';c=nc());for(;c>='0'&&c<='9';x=x*10+c-'0',c=nc());
}
int D[M];
void init()
{
for(int i=1;i<M;i++)
{
for(int j=i;j<M;j+=i)
{
D[j]++;
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n,m;
init();
rea(n);rea(m);
//scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
rea(a[i]);
//scanf("%d",&a[i]);
s.insert(node(i,a[i]));
}
se.build(1,n,1);
while(m--)
{
int op,l,r;
rea(op);rea(l);rea(r);
//scanf("%d%d%d",&op,&l,&r);
if(op==1)
{
auto it=s.lower_bound(node(l,0));
while(it!=s.end()&&it->pos<=r)
{
int pos=it->pos;
int val=it->val;
if(val==D[val])
{
it=s.erase(it);
}
else
{
s.erase(it);
it=s.insert(node(pos,D[val])).xx;
se.update(pos,D[val],1,n,1);
it++;
}
}
}
if(op==2)
{
printf("%lld\n",se.query(l,r,1,n,1));
}
}
return 0;
}
相关文章推荐
- Educational Codeforces Round 37 (Rated for Div. 2)-F-SUM and REPLACE(线段树)
- Educational Codeforces Round 37 (Rated for Div. 2) F. SUM and REPLACE(线段树,区间更新)
- Educational Codeforces Round 37 (Rated for Div. 2)F. SUM and REPLACE
- Educational Codeforces Round 37 (Rated for Div. 2) 【F】【线段树】
- Educational Codeforces Round 37 F - SUM and REPLACE (线段树)
- Educational Codeforces Round 37 (Rated for Div. 2) 920E E. Connected Components?
- Educational Codeforces Round 37 (Rated for Div. 2)【A B C】【水】【模拟】
- 【Educational Codeforces Round 37 F】SUM and REPLACE
- 【Educational Codeforces Round 48 (Rated for Div. 2) D】Vasya And The Matrix
- Educational Codeforces Round 37 (Rated for Div. 2)
- Educational Codeforces Round 37 (Rated for Div. 2) E. Connected Components?(连通分量的个数,STL)
- Educational Codeforces Round 37 (Rated for Div. 2)(A、B、C)
- 【Educational Codeforces Round 48 (Rated for Div. 2) C】 Vasya And The Mushrooms
- Educational Codeforces Round 37 (Rated for Div. 2)-E-Connected Components?(模拟/暴力)
- Wannafly挑战赛9+Educational Codeforces Round 37 (Rated for Div. 2)
- Educational Codeforces Round 37 (Rated for Div. 2) C. Swap Adjacent Elements
- Educational Codeforces Round 37 (Rated for Div. 2)
- [Codeforces]Educational Codeforces Round 37 (Rated for Div. 2)
- Educational Codeforces Round 37 (Rated for Div. 2)
- Educational Codeforces Round 33 (Rated for Div. 2) F. Subtree Minimum Query (线段树)