题目:F. Copy or Prefix Sum
分析:
参考题解
通过水平线优化空间和时间,可以将二维变成一维,并且使得每次循环map至多新增一个节点,看了一上午,终于看懂了
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;//三年竞赛一场空,不开long long见祖宗
//typedef __int128 lll;
#define print(i) cout << "debug: " << i << endl
#define close() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(a, b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define x first
#define y second
typedef pair<int, int> pii;
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const int maxn = 2e5 + 10;
const int inf = 0x3f3f3f3f;
ll b[maxn];
int main()
{
int t; cin >> t;
while(t--)
{
map<ll, ll> dp;
int n; cin >> n;
for(int i = 1; i <= n; i++) cin >> b[i];
dp[0] = 1;
ll sum = 1, delta = 0;
for(int i = 1; i <= n; i++)
{
delta += b[i];
ll add = ((sum - dp[b[i] - delta]) % mod + mod) % mod;
dp[b[i] - delta] += add, dp[b[i] - delta] %= mod;
sum += add, sum %= mod;
}
cout << sum << endl;
}
}