CF1042D Petya and Array

Posted wangyiming

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF1042D Petya and Array相关的知识,希望对你有一定的参考价值。

思路:

离散化+树状数组(或权值线段树)。bit数组一定要开够。和求逆序对数量很像。

实现:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int MAXN = 200005;
 5 ll a[MAXN], b[MAXN], bit[2 * MAXN];
 6 int n;
 7 int lowbit(int x)
 8 {
 9     return x & -x;
10 }
11 ll sum(int x)
12 {
13     ll ans = 0;
14     while (x)
15     {
16         ans += bit[x];
17         x -= lowbit(x);
18     }
19     return ans;
20 }
21 void add(int i, ll x, int maxn)
22 {
23     while (i <= maxn)
24     {
25         bit[i] += x;
26         i += lowbit(i);
27     }
28 }
29 int main()
30 {
31     ll t;
32     while (cin >> n >> t)
33     {
34         memset(b, 0, sizeof b);
35         memset(bit, 0, sizeof bit);
36         for (int i = 1; i <= n; i++) 
37         {
38             cin >> a[i];
39             b[i] = b[i - 1] + a[i];
40         }
41         vector<ll> v;
42         for (int i = 0; i <= n; i++)
43         {
44             v.push_back(b[i]);
45             v.push_back(b[i] + t);
46         }
47         sort(v.begin(), v.end());
48         v.erase(unique(v.begin(), v.end()), v.end());
49         ll ans = 0;
50         for (int i = n; i >= 1; i--)
51         {
52             int pos = lower_bound(v.begin(), v.end(), b[i]) - v.begin() + 1; 
53             add(pos, 1, v.size());
54             pos = lower_bound(v.begin(), v.end(), b[i - 1] + t) - v.begin() + 1;
55             ans += sum(pos - 1);
56         }
57         cout << ans << endl;
58     }
59     return 0;
60 }

 

以上是关于CF1042D Petya and Array的主要内容,如果未能解决你的问题,请参考以下文章

CF1042D - Petya and Array

Petya and Array CodeForces - 1042D (树状数组)

CF886C Petya and Catacombs

CF1082G Petya and Graph

CF832B Petya and Exam

1042.D Petya and Array 前缀 + 树状数组