zoukankan      html  css  js  c++  java
  • CS Academy Remove Update

    题目链接https://csacademy.com/contest/round-45/task/remove-update/

    题目大意:N个数初始值为0,现在给出Q个操作(l,r,x),每个操作将区间[l,r]的值加x。倘若除去其中一次更新,那么整个数列的最大值就会改变。问除去一次更新能达到的数列最大值的最小值是多少。

    解题思路:线段树区间更新,维护最大值。先进行Q此操作,之后枚举每个操作,执行操作(l,r,-x),记录最大值,之后执行(l,r,x)还原。

    代码:

     1 const int maxn = 1e6 + 5;
     2 struct node{
     3     int v, inc;
     4 };
     5 node tree[maxn];
     6 int n, q;
     7 int al[maxn], ar[maxn], ax[maxn];
     8 
     9 void build(int l, int r, int k){
    10     tree[k].v = tree[k].inc = 0;
    11     if(l == r) return;
    12     int mid = (l + r) >> 1, lc = k << 1, rc = k << 1 | 1;
    13     build(l, mid, lc);
    14     build(mid + 1, r, rc);
    15 }
    16 void update(int ul, int ur, int x, int l, int r, int k){
    17     if(ul <= l && ur >= r){
    18         tree[k].inc += x;
    19         return;
    20     }
    21     if(ul > r || ur < l) return;
    22     int mid = (l + r) >> 1, lc = k << 1, rc = k << 1 | 1;
    23     tree[k].v = tree[k].v + tree[k].inc;
    24     tree[lc].inc += tree[k].inc; tree[rc].inc += tree[k].inc; 
    25     tree[k].inc = 0;
    26     update(ul, ur, x, l, mid, lc);
    27     update(ul, ur, x, mid + 1, r, rc);
    28     tree[k].v = max(tree[lc].v + tree[lc].inc, tree[rc].v + tree[rc].inc);
    29 }
    30 
    31 void solve(){
    32     build(1, n, 1);
    33     for(int i = 1; i <= q; i++){
    34         update(al[i], ar[i], ax[i], 1, n, 1);
    35     }
    36     int ans = inf;
    37     for(int i = 1; i <= q; i++){
    38         update(al[i], ar[i], -ax[i], 1, n, 1);
    39         ans = min(ans, tree[1].v + tree[1].inc);
    40         update(al[i], ar[i], ax[i], 1, n, 1);
    41     }
    42     printf("%d
    ", ans);
    43 }
    44 int main(){
    45     scanf("%d %d", &n, &q);
    46     for(int i = 1; i <= q; i++){
    47         int l, r, x;
    48         scanf("%d %d %d", &l, &r, &x);
    49         al[i] = l; ar[i] = r; ax[i] = x;
    50     }
    51     solve();
    52 }

    题目:

    Remove Update

    Time limit: 1000 ms
    Memory limit: 256 MB

     

    You have an array AA of size NN. Initially all the elements of AA are equal to 00.

    You have QQ updates of the form:

    • Given ll, rr and xx (1 leq l leq r leq N1lrN, xx is positive), add xx to A_l, A_{l+1},..., A_rAl​​,Al+1​​,...,Ar​​ 

    You can choose to skip exactly one of the updates. In the end you want the maximum element of AA to be as small as possible.

    Standard input

    The first line contains two integers NN and QQ.

    Each of the next QQ lines contains three integers ll, rr and xx.

    Standard output

    Print on the first line the smallest maximum value you can get by skipping one update.

    Constraints and notes

    • 1 leq N leq 10^51N105​​ 
    • 1 leq Q leq 10^51Q105​​ 
    • 1 leq l leq r leq N1lr
    • 1 leq x leq 10^91x109​​ 
    • It is guaranteed that if you perform all the updates A_i leq 10^9Ai​​109​​
    InputOutput
    4 2
    1 3 1
    2 4 1
    
    1
    
    4 3
    2 4 2
    1 2 2
    3 4 1
    
    2
    
    6 4
    2 3 1
    4 6 2
    3 4 3
    1 4 2
    
    4
  • 相关阅读:
    Python爬虫实践 —— 4.好听音乐网轻音乐资源下载
    Python爬虫实践 —— urllib.request和requests
    Python爬虫实践 —— 3.利用爬虫提取返回值,模拟有道词典接口
    Python爬虫实践 —— 2.百度贴吧html文件爬取
    Python爬虫实践 —— 1.对反爬机制的认识
    用gcd库函数求最大公约数
    对结构体进行排序
    python菜鸟入门知识
    python安装及typora使用
    python基础一整型、bool、字符串
  • 原文地址:https://www.cnblogs.com/bolderic/p/7473754.html
Copyright © 2011-2022 走看看