zoukankan      html  css  js  c++  java
  • HDU 6315 Naive Operations

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 502768/502768 K (Java/Others)

    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 $a_l$,$a_{l+1}$……$a_r$
    2. query l r: query $sum_{i=l}^{r}iggllfloorfrac{a_i}{b_i}iggr floor$

    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]要增加b[i]次才能对答案产生1的贡献。线段树更新时,先将区间内maxa加一,然后判断,对于区间内a的最大值小于等于b的最小值的,我们只更新maxa,不更新这个区间对答案的贡献ans,然后lazy标记打上,返回。对于最大值大于等于b的最小值的,我们接着更新下去,直到叶子结点,这样不会超时,原因先留坑

    填坑啦2019年03月09日15:48:41

      https://blog.csdn.net/weixin_38287798/article/details/81210396 关键就在于b数组是1到n的全排列,不能全是1,所以可以暴力到叶子

    源代码

     1 #include<stdio.h>
     2 #include<algorithm>
     3 
     4 int n,q;
     5 int b[100010];
     6 
     7 struct Segtree{
     8     int l,r,ans;
     9     int maxa,minb;
    10 }t[400010];
    11 int lazy[400010];
    12 inline int lson(int x){return x<<1;}
    13 inline int rson(int x){return x<<1|1;}
    14 void maketree(int x,int l,int r)
    15 {
    16     lazy[x]=0;
    17     if(l==r)
    18     {
    19         t[x]={l,r,0,0,b[l]};
    20         return;
    21     }
    22     t[x]={l,r,0,0,0};
    23     int mid=(l+r)>>1;
    24     maketree(lson(x),l,mid);
    25     maketree(rson(x),mid+1,r);
    26     t[x].minb=std::min(t[lson(x)].minb,t[rson(x)].minb);
    27 }
    28 void pushdown(int x)
    29 {
    30     if(!lazy[x]) return;
    31     t[lson(x)].maxa+=lazy[x];
    32     t[rson(x)].maxa+=lazy[x];
    33     lazy[lson(x)]+=lazy[x];
    34     lazy[rson(x)]+=lazy[x];
    35     lazy[x]=0;
    36 }
    37 void pushup(int x)
    38 {
    39     if(t[x].l==t[x].r) return;
    40     t[x].minb=std::min(t[lson(x)].minb,t[rson(x)].minb);
    41     t[x].maxa=std::max(t[lson(x)].maxa,t[rson(x)].maxa);
    42     t[x].ans=t[lson(x)].ans+t[rson(x)].ans;
    43 }
    44 void update(int x,int l,int r)//l到r的a增加1
    45 {
    46     if(l>t[x].r||r<t[x].l) return;
    47     if(l<=t[x].l&&t[x].r<=r)
    48     {
    49         t[x].maxa++;
    50         if(t[x].maxa<t[x].minb)
    51         {
    52             lazy[x]++;
    53             return;
    54         }
    55         if(t[x].l==t[x].r&&t[x].maxa>=t[x].minb)
    56         {
    57             t[x].ans++;
    58             t[x].minb+=b[t[x].l];//为下次更新t[x].ans做准备
    59             return;
    60         }
    61     }
    62     pushdown(x);
    63     update(lson(x),l,r);
    64     update(rson(x),l,r);
    65     pushup(x);
    66 }
    67 int que(int x,int l,int r)
    68 {
    69     if(l>t[x].r||r<t[x].l) return 0;
    70     if(l<=t[x].l&&t[x].r<=r)
    71         return t[x].ans;
    72     pushdown(x);
    73     return que(lson(x),l,r)+que(rson(x),l,r);
    74 }
    75 
    76 int main()
    77 {
    78     //freopen("test.in","r",stdin);
    79     while(~scanf("%d%d",&n,&q))
    80     {
    81         for(int i=1;i<=n;i++) scanf("%d",b+i);
    82         maketree(1,1,n);
    83         while(q--)
    84         {
    85             char s[10];
    86             int l,r;
    87             scanf("
    %s %d %d",s,&l,&r);
    88             if(s[0]=='a') update(1,l,r);
    89             else printf("%d
    ",que(1,l,r));
    90         }
    91     }
    92     return 0;
    93 }
  • 相关阅读:
    移动端touch事件获取事件坐标
    详解webpack中的hash、chunkhash、contenthash区别
    textarea placeholder 设置主动换行
    js-xlsx的使用
    关于Blob对象的介绍与使用
    spring boot zuul集成kubernetes等第三方登录
    Spring Boot 获取yaml配置文件信息
    spring boot @Value源码解析
    java.lang.StackOverflowError解决
    Jpa 重写方言dialect 使用oracle / mysql 数据库自定义函数
  • 原文地址:https://www.cnblogs.com/wawcac-blog/p/10487141.html
Copyright © 2011-2022 走看看