zoukankan      html  css  js  c++  java
  • Naive Operations HDU多校(线段树上线段果)

    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.
    1n,q100000 , 1lrn , 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
     
    这题写不出的我不敢说话,
    线段树上线段果,线段树上你和我!
    我都要自挂线段树了
     求query ri=lai/bi 
      这个就要转化一下模型
    以为向下取整 所以一开始ai=0  
    所以可以每次add操作就相当于bi--
    当bi等于0的时候 sum++;然后bi变回最开始的值
    构造一个线段树维护区间最小值和sum
     
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long LL;
     4 const int maxn = 1e5 + 10;
     5 int n,m,a[maxn];
     6 struct node {
     7     LL val, key, lazy, sum;
     8 } tree[maxn << 2];
     9 void pushup(int rt) {
    10     tree[rt].sum = tree[rt << 1].sum + tree[rt << 1 | 1].sum;
    11     tree[rt].val = min(tree[rt << 1].val, tree[rt << 1 | 1].val);
    12 }
    13 void pushdown(int rt) {
    14     tree[rt << 1].lazy += tree[rt].lazy;
    15     tree[rt << 1 | 1].lazy += tree[rt].lazy;
    16     tree[rt << 1].val -= tree[rt].lazy;
    17     tree[rt << 1 | 1].val -= tree[rt].lazy;
    18     tree[rt].lazy = 0;
    19 }
    20 void build(int l, int r, int rt) {
    21     tree[rt].lazy = tree[rt].sum = 0;
    22     if (l == r) {
    23         tree[rt].key = tree[rt].val = a[l];
    24         return ;
    25     }
    26     int m = (l + r) >> 1;
    27     build(l, m, rt << 1);
    28     build(m + 1, r, rt << 1 | 1);
    29     pushup(rt);
    30 }
    31 void update(int L, int R, int l, int r, int rt) {
    32     if(l > r) return ;
    33     if (tree[rt].val > 1 && l == L && r == R) {
    34         tree[rt].val--;
    35         tree[rt].lazy++;
    36         return ;
    37     }
    38     if (tree[rt].val == 1 && l == r) {
    39         tree[rt].sum++;
    40         tree[rt].val = tree[rt].key;
    41         tree[rt].lazy = 0;
    42         return ;
    43     }
    44     if (tree[rt].lazy > 0) pushdown(rt);
    45     int m = (l + r) >> 1;
    46     if (R <= m) update(L, R, l, m, rt << 1);
    47     else if (L > m) update(L, R, m + 1, r, rt << 1 | 1);
    48     else {
    49         update(L, m, l, m, rt << 1);
    50         update(m + 1, R, m + 1, r, rt << 1 | 1);
    51     }
    52     pushup(rt);
    53 }
    54 LL query(int L, int R, int l, int r, int rt) {
    55     if(l > r) return 0;
    56     if (l == L && r == R) return tree[rt].sum;
    57     int m = (l + r) >> 1;
    58     if (R <= m) return query(L, R, l, m, rt << 1);
    59     else if (L > m) return query(L, R, m + 1, r, rt << 1 | 1);
    60     else return query(L, m, l, m, rt << 1) + query(m + 1, R, m + 1, r, rt << 1 | 1);
    61 }
    62 int main() {
    63     while(scanf("%d%d", &n, &m) != EOF) {
    64         for (int i = 1 ; i <= n ; i++ ) scanf("%d", &a[i]);
    65         build(1, n, 1);
    66         char str[10];
    67         int x, y;
    68         while(m--) {
    69             scanf("%s%d%d", str, &x, &y);
    70             if (str[0] == 'a')  update(x, y, 1, n, 1);
    71             else printf("%lld
    ", query(x, y, 1, n, 1));
    72         }
    73     }
    74     return 0;
    75 }
     
  • 相关阅读:
    08.设计模式,和ES6let
    H5之本地存储
    07..前后台交互,设计模式
    查询出总数集合
    06.JSON+ajax+跨域+onde 环境搭建 笔记
    05 this 在不同环境下的指向 和正则
    04学习 JS 作用域 继承 闭包
    技术盛宴 | 从实战浅析运营商云资源池—解析流量模型
    从实战浅析运营商云资源池网络—技术的抉择
    ovn-sbctl
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/9368789.html
Copyright © 2011-2022 走看看