zoukankan      html  css  js  c++  java
  • HDU 6315 Naive Operations(线段树区间整除区间)

    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数组为0,给你一个全排列的b数组,q次询问add x y为a数组区间x y增加1,query x y查询a数组整除b数组对应下标的和

    题解

    区间操作很容易想到线段树

    初始每个叶子节点赋值为b[i],维护一个区间最小值min,和区间和sum

    对于每个add,区间[X,Y]最小值减1,如果当前区间最小值=1,就继续往下更新,如果更新到叶子节点并且min=1,sum+1

    对于每个query,查询区间[X,Y]sum,如果区间min=0,再去暴力更新区间(可以知道一共q次询问,q/1+q/2+q/3+....q/n为调和级数,复杂度O(logn))

    总复杂度O(nlog^2 n)

    代码

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 const int N=1e5+5;
     5 
     6 int a[N<<2],lazy[N<<2],b[N],sum[N<<2];
     7 int n;
     8 void PushUp(int rt)
     9 {
    10     a[rt]=min(a[rt<<1],a[rt<<1|1]);
    11     sum[rt]=sum[rt<<1]+sum[rt<<1|1];
    12 }
    13 void PushDown(int rt)
    14 {
    15     if(lazy[rt]==0)return;
    16     lazy[rt<<1]+=lazy[rt];
    17     lazy[rt<<1|1]+=lazy[rt];
    18     a[rt<<1]-=lazy[rt];
    19     a[rt<<1|1]-=lazy[rt];
    20     lazy[rt]=0;
    21 }
    22 void Build(int l,int r,int rt)
    23 {
    24     lazy[rt]=0;sum[rt]=0;
    25     if(l==r)
    26     {
    27         a[rt]=b[l];
    28         return;
    29     }
    30     int mid=(l+r)>>1;
    31     Build(l,mid,rt<<1);
    32     Build(mid+1,r,rt<<1|1);
    33     PushUp(rt);
    34 }
    35 void Update(int L,int R,int l,int r,int rt)
    36 {
    37     if(a[rt]>1&&L<=l&&r<=R)
    38     {
    39         lazy[rt]++;
    40         a[rt]--;
    41         return;
    42     }
    43     if(a[rt]==1&&l==r)
    44     {
    45         sum[rt]++;
    46         lazy[rt]=0;
    47         a[rt]=b[l];
    48         return;
    49     }
    50     int mid=(l+r)>>1;
    51     PushDown(rt);
    52     if(L<=mid)Update(L,R,l,mid,rt<<1);
    53     if(R>mid)Update(L,R,mid+1,r,rt<<1|1);
    54     PushUp(rt);
    55 }
    56 int Query(int L,int R,int l,int r,int rt)
    57 {
    58     if(L<=l&&r<=R)return sum[rt];
    59     if(a[rt]==0)Update(L,R,1,n,1);
    60     int mid=(l+r)>>1,ans=0;
    61     PushDown(rt);
    62     if(L<=mid)ans+=Query(L,R,l,mid,rt<<1);
    63     if(R>mid)ans+=Query(L,R,mid+1,r,rt<<1|1);
    64     PushUp(rt);
    65     return ans;
    66 }
    67 int main()
    68 {
    69     int q,x,y;
    70     char op[10];
    71     while(scanf("%d%d",&n,&q)!=EOF)
    72     {
    73         for(int i=1;i<=n;i++)
    74             scanf("%d",&b[i]);
    75         Build(1,n,1);
    76         for(int i=0;i<q;i++)
    77         {
    78             scanf("%s%d%d",op,&x,&y);
    79             if(op[0]=='a')
    80                 Update(x,y,1,n,1);
    81             else
    82                 printf("%d
    ",Query(x,y,1,n,1));
    83         }
    84     }
    85     return 0;
    86 }
  • 相关阅读:
    蓝桥杯算法训练 区间k大数查询
    【模板】快读
    [ACM] hdu 2544 最短路(dijkstra算法)
    [ACM] hdu 3791 二叉搜索树
    [ACM] hdu 2141 Can you find it? (二分查找)
    [ACM] hdu 2025查找最大元素(水题)
    [ACM] hdu 1232 畅通工程(并查集)
    [ACM] hdu 1022 Train Problem I(栈的使用)
    [ACM] hdu 2857 Mirror and Light (对称点+两条直线的交点)
    [ACM] hdu 爆头(点到直线距离)
  • 原文地址:https://www.cnblogs.com/taozi1115402474/p/9370310.html
Copyright © 2011-2022 走看看