HDU 6315
题意
a数组长度为n 初始值为 0
给你n长度的b数组
add 操作每次给a数组加 1
query操作 询问 i 从 l 到 r 之间的 a[i]/b[i] 的和
我们想想看 用一个最小值数组 一开始为 b[i] 值 代表要几次删到 0
那么我每次只要一个区间的 minn > 1 那么我可以区间更新
否则就单点更新
复杂度最坏是 (1-n)中每个数的约数个数和
相当于加几次能进行一个操作
外面再用个树状数组记录答案即可
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
const int MAX_N = 100025;
int arr[MAX_N];
ll C[MAX_N<<1];
void add(int x,int v)
{
for(;x<MAX_N;x+=x&(-x))
C[x]+=v;
}
ll getsum(int x)
{
ll res = 0;
for(;x;x-=x&(-x))
res+=C[x];
return res;
}
int Min(int a,int b) {return a < b?a:b;}
namespace sgt
{
#define mid ((l+r)>>1)
int minn[MAX_N<<2],col[MAX_N<<2];
void up(int rt)
{
minn[rt] = Min(minn[rt<<1],minn[rt<<1|1]);
}
void down(int rt,int l,int r)
{
if(col[rt])
{
col[rt<<1]+=col[rt];
col[rt<<1|1] += col[rt];
minn[rt<<1] += col[rt];
minn[rt<<1|1] += col[rt];
col[rt] = 0;
}
}
void build(int rt,int l,int r)
{
col[rt] = 0;
if(l==r)
{
minn[rt] = arr[l];
return ;
}
build(rt<<1,l,mid);
build(rt<<1|1,mid+1,r);
up(rt);
}
void update(int rt,int l,int r,int x,int y,int v)
{
if(x<=l&&r<=y && minn[rt]>1)
{
minn[rt]--;
col[rt] += v;
return ;
}
if(l==r)
{
if(minn[rt]==1)
{
col[rt] = 0;
minn[rt] = arr[l];
add(l+1,1);
}
else minn[rt]--;
return ;
}
down(rt,l,r);
if(x>mid) update(rt<<1|1,mid+1,r,x,y,v);
else if(y<=mid) update(rt<<1,l,mid,x,y,v);
else update(rt<<1,l,mid,x,y,v),update(rt<<1|1,mid+1,r,x,y,v);
up(rt);
}
#undef mid
}
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int n,m,x,y;
char str[10];
while(scanf("%d%d",&n,&m)==2)
{
memset(C,0,sizeof(C));
for(int i = 1;i<=n;++i) scanf("%d",&arr[i]);
sgt::build(1,1,n);
while(m--)
{
scanf("%s%d%d",str,&x,&y);
if(str[0]=='a') sgt::update(1,1,n,x,y,-1);
else printf("%d\n",getsum(y+1) - getsum(x));
}
}
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}