Naive Operations

Naive Operations

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 502768/502768 K (Java/Others)
Total Submission(s): 1853    Accepted Submission(s): 802


 

Problem Description

In a galaxy far, far away, there are two integer sequence a and b of length n.
b is a static permutation of 1 to n. Initially a is filled with zeroes.
There are two kind of operations:
1. add l r: add one for al,al+1...ar
2. query l r: query ∑ri=lai/bi

 

 

Input

There are multiple test cases, please read till the end of input file.
For each test case, in the first line, two integers n,q, representing the length of a,b and the number of queries.
In the second line, n integers separated by spaces, representing permutation b.
In the following q lines, each line is either in the form 'add l r' or 'query l r', representing an operation.
1≤n,q≤100000, 1≤lrn, there're no more than 5 test cases.

 

 

Output

Output the answer for each 'query', each one line.

 

 

Sample Input

 

5 12 1 5 2 4 3 add 1 4 query 1 4 add 2 5 query 2 5 add 3 5 query 1 5 add 2 4 query 1 4 add 2 5 query 2 5 add 2 2 query 1 5

 

 

Sample Output

 

1 1 2 4 4 6

 题意:给一个长度n值为0的区间数组a,和长度为n的一个排列b,有q次操作,add(l,r)表示a的l,r区间内的数+1。query(l,r)表示查询一个区间内ai/bi的值。

分析:由于起初a都是0,所以当ai加到与bi相等答案才会+1。可以反过来想,把bi放入对应节点,维护区间最小值,当最小值减到0时就再用线段维护一个答案,使得答案+1,并且重新给该节点赋值bi。

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<algorithm>
#define INF 0x3f3f3f3f
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;
#define gcd(a,b) __gcd(a,b)
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int Minx[MAX<<2];//区间最小值
int ans[MAX<<2];//记录最终答案
int lazy[MAX<<2];//懒惰标记
int b[MAX<<2];//记录b数组
int n,q;
void pushup(int rt)
{
    Minx[rt]=min(Minx[rt<<1],Minx[rt<<1|1]);//最小值
    ans[rt]=ans[rt<<1]+ans[rt<<1|1];//答案求区间和
}
void pushdown(int rt)
{
    if(lazy[rt])
    {
        lazy[rt<<1]+=lazy[rt];
        lazy[rt<<1|1]+=lazy[rt];
        Minx[rt<<1|1]-=lazy[rt];//最小值一直减1
        Minx[rt<<1]-=lazy[rt];
        lazy[rt]=0;
    }
}
void build(int l,int r,int rt)
{
    lazy[rt]=0;
    ans[rt]=0;
    if(l==r)
    {
        scanf("%d",&b[rt]);
        Minx[rt]=b[rt];
        return ;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    pushup(rt);
}
void updata(int L,int R,int l,int r,int rt)
{
    if(L<=l && r<=R)
    {
        Minx[rt]--;
        if(Minx[rt])
        {
            lazy[rt]++;
            return ;
        }
        else//区间最小值减小到0了
        {
            if(l==r)
            {
                ans[rt]++;
                Minx[rt]=b[rt];//重新赋值区间最小值
                return ;
            }
        }
    }
    pushdown(rt);
    int m=(l+r)>>1;
    if(L<=m) updata(L,R,lson);
    if(m<R) updata(L,R,rson);
    pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
    if(L<=l && R>=r)
        return ans[rt];
    pushdown(rt);
    int m=(l+r)>>1;
    int sum=0;
    if(m>=L) sum+=query(L,R,lson);
    if(m<R) sum+=query(L,R,rson);
    return sum;
}
int main()
{
    int l,r;
    while(scanf("%d%d",&n,&q)!=EOF)
    {
        build(1,n,1);
        for(int i=1;i<=q;i++)
        {
            char op[10];
            scanf("%s%d%d",op,&l,&r);
            if(op[0]=='a')
                updata(l,r,1,n,1);
            else
                printf("%d\n",query(l,r,1,n,1));
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值