Hdu6315(Naive Operations)2018 Multi-University Training Contest 2 G

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=l⌊ai/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≤l≤r≤n, 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

思路:根据直播的杜教分析,因为a[i]<=q,那么a[i]/b[i]<=q/b[i],那么将题目中2加起来就是调和级数得出调和级数其为nlong n的复杂度。(b[i]是n的全排列,a[i]/b[i]发生质的变化是当a[i]=b[i]时)

所以直接线段树维护区间最小值,每次操作将最小值减1,当减到0是深搜将叶子中每一个都恢复原值。

/*#include <set>
#include <map>
#include <deque>
#include <stack>
#include <queue>
#include <time.h>
#include <vector>
#include <string>
#include <math.h>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define PI acos(-1)
#define ll long long
#define LL long long
#define inf 0x3f3f3f3f
#define ull unsigned long long
using namespace std;*/


#include<bits/stdc++.h>
#include<iostream>
#define MAXN 100010
#define inf 0x3f3f3f3f
using namespace std;

struct node
{
    int l,r;
    int add;
    int mn;
}tree[MAXN<<2];
int b[MAXN<<2];
int c[MAXN],n;
int lowbit(int i) {return i&(-i);}
void update(int i,int val)
{
    while(i<=n)
    {
        c[i]+=val;
        i+=lowbit(i);
    }
}
int getsum(int i)
{
    int sum=0;
    while(i>0)
    {
        sum+=c[i];
        i-=lowbit(i);
    }
    return sum;
}
void pushup(int index)
{
    tree[index].mn = min(tree[index<<1].mn,tree[index<<1|1].mn);
}
void pushdown(int index)
{
    if(tree[index].add)
    {
        tree[index<<1].mn += tree[index].add;
        tree[index<<1|1].mn += tree[index].add;
        tree[index<<1].add += tree[index].add;
        tree[index<<1|1].add += tree[index].add;
        tree[index].add = 0;
    }
}
void build(int l,int r,int index)
{
    tree[index].l = l;
    tree[index].r = r;
    tree[index].add = 0;
    if(l == r)
    {
        scanf("%d",&b[index]);
        tree[index].mn=b[index];
        return ;
    }
    int mid = (l+r)>>1;
    build(l,mid,index<<1);
    build(mid+1,r,index<<1|1);
    pushup(index);
}
void solve(int index)
{
    if(tree[index].l==tree[index].r)
    {
        if(tree[index].mn==0) tree[index].mn=b[index];
        update(tree[index].l,1);
        return ;
    }
    pushdown(index);
    int mid=(tree[index].l+tree[index].r)>>1;
    if(tree[index*2].mn==0) solve(index*2);
    if(tree[index*2+1].mn==0) solve(index*2+1);
    pushup(index);
}
void updata(int l,int r,int index,int val)
{
    if(l <= tree[index].l &&r>=tree[index].r)
    {
        tree[index].mn += val;
        tree[index].add += val;
        if(tree[index].mn==0) solve(index);
        return ;
    }
    pushdown(index);
    int mid = (tree[index].l+tree[index].r)>>1;
    if(l <= mid) updata(l,r,index<<1,val);
    if(r > mid) updata(l,r,index<<1|1,val);
    pushup(index);
}
int main()
{
    int q,l,r;
    char s[20];
    while(scanf("%d%d",&n,&q)!=EOF)
    {
        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));
        memset(tree,0,sizeof(tree));
        build(1,n,1);
        while(q--)
        {
            scanf("%s%d%d",s,&l,&r);
            if(s[0]=='a') updata(l,r,1,-1);
            else cout<<getsum(r)-getsum(l-1)<<endl;
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值