zoukankan      html  css  js  c++  java
  • Codeforces Round #169 (Div. 2) C. Little Girl and Maximum Sum(线段树区间更新)

    C. Little Girl and Maximum Sum
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    The little girl loves the problems on array queries very much.

    One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers li, ri (1 ≤ li ≤ ri ≤ n). You need to find for each query the sum of elements of the array with indexes from li to ri, inclusive.

    The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum.

    Input

    The first line contains two space-separated integers n (1 ≤ n ≤ 2·105) and q (1 ≤ q ≤ 2·105) — the number of elements in the array and the number of queries, correspondingly.

    The next line contains n space-separated integers ai (1 ≤ ai ≤ 2·105) — the array elements.

    Each of the following q lines contains two space-separated integers li and ri (1 ≤ li ≤ ri ≤ n) — the i-th query.

    Output

    In a single line print a single integer — the maximum sum of query replies after the array elements are reordered.

    Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

    Sample test(s)
    Input
    3 3
    5 3 2
    1 2
    2 3
    1 3
    Output
    25
    Input
    5 3
    5 2 4 1 3
    1 5
    2 3
    2 3
    Output
    33

    对每个数a[i],统计其被访问的次数cnt[i],将a和cnt分别从小到大排列,maxSum = a[0]*cnt[0]+a[1]*cnt[1]+……

      1 #include <iostream>
      2 #include <string>
      3 #include <set>
      4 #include <map>
      5 #include <vector>
      6 #include <stack>
      7 #include <queue>
      8 #include <cmath>
      9 #include <cstdio>
     10 #include <cstring>
     11 #include <algorithm>
     12 using namespace std;
     13 #define LL long long
     14 #define cti const int
     15 #define dg(i) cout << "*" << i << endl;
     16 
     17 cti MAXN = 200002;
     18 struct Node
     19 {
     20     int r, l;
     21     LL inc;  //新增查询次数
     22     LL cnt;  //该区间被查询次数
     23 }tree[MAXN<<2|1];
     24 int lastNode;  //记录最后一个结点的下标
     25 LL a[MAXN];
     26 bool tag[MAXN<<2|1];  //标记是否叶子
     27 LL cnt[MAXN]; //记录叶子的访问次数
     28 
     29 bool cmp (const LL& x, const LL& y) {return x > y;}
     30 
     31 void Build(int left, int right, int rt)
     32 {
     33     if(lastNode < rt) lastNode = rt;
     34     tree[rt].l = left;
     35     tree[rt].r = right;
     36     if(left == right)
     37     {
     38         tag[rt] = true;
     39         return ;
     40     }
     41     int mid = (left + right) >> 1;
     42     Build(left, mid, rt << 1);
     43     Build(mid + 1, right, rt << 1 | 1);
     44 }
     45 
     46 void Update(int from, int to, int rt)
     47 {
     48     if(from <= tree[rt].l && tree[rt].r <= to)
     49     {
     50         tree[rt].inc++;
     51         tree[rt].cnt++;
     52         return ;
     53     }
     54     if(tree[rt].inc)
     55     {
     56         tree[rt<<1].cnt += tree[rt].inc;
     57         tree[rt<<1|1].cnt += tree[rt].inc;
     58         tree[rt<<1].inc += tree[rt].inc;
     59         tree[rt<<1|1].inc += tree[rt].inc;
     60         tree[rt].inc = 0;
     61     }
     62     int mid = (tree[rt].l + tree[rt].r) >> 1;
     63     if(from <= mid) Update(from, to, rt << 1);
     64     if(to > mid) Update(from, to, rt << 1 | 1);
     65 }
     66 
     67 int main()
     68 {
     69     int n, q, from, to;
     70     while(scanf("%d %d", &n, &q) != EOF)
     71     {
     72         memset(tag, false, sizeof(tag));
     73         for(int i = 0; i < n; i++) scanf("%I64d", &a[i]);
     74         for(int i = 1; i < (n<<2); i++)
     75             tree[i].cnt = tree[i].inc = 0;
     76         lastNode = 0;
     77         Build(1, n, 1);
     78         while(q--)
     79         {
     80             scanf("%d %d", &from, &to);
     81             Update(from, to, 1);
     82         }
     83         //此循环保证区间更新彻底往下传递
     84         for(int i = 1, j = 0; i <= lastNode; i++)
     85         {
     86             if(tree[i].inc && (i<<1) < lastNode)
     87             {
     88                 tree[i<<1].cnt += tree[i].inc;
     89                 tree[i<<1|1].cnt += tree[i].inc;
     90                 tree[i<<1].inc += tree[i].inc;
     91                 tree[i<<1|1].inc += tree[i].inc;
     92             }
     93             if(tag[i])  //叶子
     94             {
     95                 cnt[j++] = tree[i].cnt;
     96             }
     97         }
     98         sort(a, a + n, cmp);
     99         sort(cnt, cnt + n, cmp);
    100         LL sum = 0;
    101         for(int i = 0; i < n; i++)
    102             sum += (cnt[i] * a[i]);
    103         printf("%I64d\n", sum);
    104     }
    105     return 0;
    106 }
  • 相关阅读:
    微软Office团队做产品的十大经验教训 沧海
    Definitio of Business Intelligence 沧海
    爱购家园,轻松工作,轻松生活 沧海
    SAAS概述 沧海
    一定要做自己最内行的东西,一定要在自己本身的职位上来提升自己 沧海
    Android应用的构成 沧海
    管理的最高境界,就是激发每个员工的潜力。让每个员工都觉得自己的工作是一份事业。 沧海
    The life cycle of a typical project 一个典型的项目生命周期 沧海
    印度英语的特点归纳 沧海
    3G时代的移动业务赢利模式分析 沧海
  • 原文地址:https://www.cnblogs.com/cszlg/p/2939114.html
Copyright © 2011-2022 走看看