zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 41 (Rated for Div. 2) E. Tufurama

    E. Tufurama
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    One day Polycarp decided to rewatch his absolute favourite episode of well-known TV series "Tufurama". He was pretty surprised when he got results only for season 7 episode 3 with his search query of "Watch Tufurama season 3 episode 7 online full hd free". This got Polycarp confused — what if he decides to rewatch the entire series someday and won't be able to find the right episodes to watch? Polycarp now wants to count the number of times he will be forced to search for an episode using some different method.

    TV series have n seasons (numbered 1 through n), the i-th season has ai episodes (numbered 1 through ai). Polycarp thinks that if for some pair of integers x and y (x < y) exist both season x episode y and season y episode x then one of these search queries will include the wrong results. Help Polycarp to calculate the number of such pairs!

    Input

    The first line contains one integer n (1  ≤ n  ≤  2·105) — the number of seasons.

    The second line contains n integers separated by space a1, a2, ..., an (1 ≤ ai ≤ 109) — number of episodes in each season.

    Output

    Print one integer — the number of pairs x and y (x < y) such that there exist both season x episode y and season y episode x.

    Examples
    input
    Copy
    5
    1 2 3 4 5
    output
    Copy
    0
    input
    Copy
    3
    8 12 7
    output
    Copy
    3
    input
    Copy
    3
    3 2 1
    output
    Copy
    2
    Note

    Possible pairs in the second example:

    1. x = 1, y = 2 (season 1 episode 2  season 2 episode 1);
    2. x = 2, y = 3 (season 2 episode 3  season 3 episode 2);
    3. x = 1, y = 3 (season 1 episode 3  season 3 episode 1).

    In the third example:

    1. x = 1, y = 2 (season 1 episode 2  season 2 episode 1);
    2. x = 1, y = 3 (season 1 episode 3  season 3 episode 1).

    思路一:对于每一个a[i], 我们要统计的是[i + 1, a[i] ]区间内有多少个a[j] >= i,于是就是一个很显然的线段树套pbds,时间复杂度nlog2n, 空间复杂度nlogn。

     1 #include <iostream>
     2 #include <fstream>
     3 #include <sstream>
     4 #include <cstdlib>
     5 #include <cstdio>
     6 #include <cmath>
     7 #include <string>
     8 #include <cstring>
     9 #include <algorithm>
    10 #include <queue>
    11 #include <stack>
    12 #include <vector>
    13 #include <set>
    14 #include <map>
    15 #include <list>
    16 #include <iomanip>
    17 #include <cctype>
    18 #include <cassert>
    19 #include <bitset>
    20 #include <ctime>
    21 
    22 using namespace std;
    23 
    24 #define ll long long
    25 #define pii pair<int, int>
    26 #define pb push_back
    27 #define mp make_pair
    28 #define clr(a, x) memset(a, x, sizeof(a))
    29 
    30 const double pi = acos(-1.0);
    31 const int INF = 0x3f3f3f3f;
    32 const int MOD = 1e9 + 7;
    33 const double EPS = 1e-9;
    34 
    35 
    36 #include <ext/pb_ds/assoc_container.hpp>
    37 #include <ext/pb_ds/tree_policy.hpp>
    38 
    39 using namespace __gnu_pbds;
    40 
    41 tree<pii, null_type, greater<pii>, rb_tree_tag, tree_order_statistics_node_update> T[800015];
    42 int n, a[200015];
    43 
    44 void build(int i, int l, int r) {
    45     for (int j = l; j <= r; ++j) {
    46         T[i].insert(pii(a[j], j));
    47     }
    48     if (l == r) {
    49         return;
    50     }
    51     int mi = l + r >> 1;
    52     build(i << 1, l, mi);
    53     build(i << 1 | 1, mi + 1, r);
    54 }
    55 int query(int i, int l, int r, int x, int y, int z) {
    56     if (x <= l && r <= y) {
    57         return T[i].order_of_key(pii(z, 0));
    58     }
    59     int mi = l + r >> 1;
    60     int res = 0;
    61     if (x <= mi) res += query(i << 1, l, mi, x, y, z);
    62     if (mi < y) res += query(i << 1 | 1, mi + 1, r, x, y, z);
    63     return res;
    64 }
    65 int main() {
    66     scanf("%d", &n);
    67     for (int i = 1; i <= n; ++i) {
    68         scanf("%d", &a[i]);
    69     }
    70     build(1, 1, n);
    71     ll res = 0;
    72     for (int i = 1; i <= n; ++i) {
    73         if (i < a[i]) {
    74             res += query(1, 1, n, i + 1, min(a[i], n), i);
    75         }
    76     }
    77     printf("%lld
    ", res);
    78     return 0;
    79 }
    View Code

    思路二:我们将每个值向后的影响称为种韭菜,将统计每个值之前有影响的点对称为割韭菜。我们不按照下标的顺序割韭菜,而是处理一下,对于每个点,若a[i] >= i, 则之前种的韭菜都能收割到,割韭菜的时机还是i;否则割韭菜的时机就要改为a[i]。所以最终就树状数组正常更新就行了,只是更改一下查询的时间点。代码比思路一快了10倍。

     1 #include <iostream>
     2 #include <fstream>
     3 #include <sstream>
     4 #include <cstdlib>
     5 #include <cstdio>
     6 #include <cmath>
     7 #include <string>
     8 #include <cstring>
     9 #include <algorithm>
    10 #include <queue>
    11 #include <stack>
    12 #include <vector>
    13 #include <set>
    14 #include <map>
    15 #include <list>
    16 #include <iomanip>
    17 #include <cctype>
    18 #include <cassert>
    19 #include <bitset>
    20 #include <ctime>
    21 
    22 using namespace std;
    23 
    24 #define pau system("pause")
    25 #define ll long long
    26 #define pii pair<int, int>
    27 #define pb push_back
    28 #define mp make_pair
    29 #define clr(a, x) memset(a, x, sizeof(a))
    30 
    31 const double pi = acos(-1.0);
    32 const int INF = 0x3f3f3f3f;
    33 const int MOD = 1e9 + 7;
    34 const double EPS = 1e-9;
    35 
    36 /*
    37 #include <ext/pb_ds/assoc_container.hpp>
    38 #include <ext/pb_ds/tree_policy.hpp>
    39 
    40 using namespace __gnu_pbds;
    41 tree<pli, null_type, greater<pli>, rb_tree_tag, tree_order_statistics_node_update> T;
    42 */
    43 
    44 int n, a[200015], c[200015];
    45 vector<int> q[200015];
    46 void add(int p, int x) {for (; p <= n; p += p & -p) c[p] += x;}
    47 int query(int p) {
    48     int res = 0;
    49     for (; p; p -= p & -p) res += c[p];
    50     return res;
    51 }
    52 int main() {
    53     scanf("%d", &n);
    54     for (int i = 1; i <= n; ++i) {
    55         scanf("%d", &a[i]);
    56         if (a[i] > i) q[i].pb(i);
    57         else q[a[i]].pb(i);
    58     }
    59     ll ans = 0;
    60     for (int i = 1; i <= n; ++i) {
    61         if (i < a[i]) {
    62             add(i + 1, 1);
    63             add(a[i] + 1, -1);
    64         }
    65         for (int j = 0; j < q[i].size(); ++j) {
    66             ans += query(q[i][j]);
    67         }
    68     }
    69     printf("%lld
    ", ans);
    70     return 0;
    71 }
    View Code
  • 相关阅读:
    连载《一个程序猿的生命周期》-《发展篇》- 34.如果自己有想法去“创业”,怎么平衡与工作之间的关系
    连载《一个程序猿的生命周期》-《发展篇》- 33.是不是又走在“创业”的路上?!
    连载《一个程序猿的生命周期》-《发展篇》- 32.疫情中复工,跌宕起伏的2019,发展元年的2020
    连载《一个程序猿的生命周期》-《发展篇》- 28.恰逢五四,我们又走在奋斗的路上吗?
    连载《一个程序猿的生命周期》-《发展篇》- 27.从来没有996过,仍然需要人生的选择权
    连载《一个程序猿的生命周期》-《发展篇》- 26.且听风吟,静待花开,慢慢走向人生赢家
    连载《一个程序猿的生命周期》-《发展篇》- 25.论一个非正式项目经理的自我修养
    连载《一个程序猿的生命周期》-《发展篇》- 24.你所掌握的技术,创造的价值会越来越低
    连载《一个程序猿的生命周期》-《发展篇》- 23.两年多的时间,从孤家寡人到10多人的团体,经历了什么
    6年前端开发被实习生替代,所谓“经验”一文不值!
  • 原文地址:https://www.cnblogs.com/BIGTOM/p/9146144.html
Copyright © 2011-2022 走看看