【小码匠自习室】AGC023-A :为啥总是N连发?为啥总遇到大神?

2022-08-08 13:11:34 浏览数 (1)

碎碎念

  • 老码农惯用小伎俩,每学一个知识点,总是N连发刷题,美其名曰:要慢下来,慢下来,慢下来
  • 又遇到大神的代码,大神连刷2天,就为了执行时长从15ms -> 8ms,估计对性能有洁癖
    • AC执行最长时长:1807ms
    • 大神时长:8ms
  • 意外惊喜:tourist大神也有不会做的题目,这次比赛的E题,大神也没搞定,排名第三,真是罕见
    • 看大神的代码犹如沐浴春风,望尘莫及,简洁的令人生畏。

标签

  • 累积和、map

题目地址

  • A - Zero-Sum Ranges
    • https://atcoder.jp/contests/agc023/tasks/agc023_a

题目描述

有一个整数序列A,其长度为N。

求 A 的和为 0 的非空连续子序列的个数。注意我们是在统计取出子序列的方式。也就是说,即使有的两个子序列的内容相同,如果它们取自不同的位置,也当成不同序列.

约束条件

  • 1 leq N leq 2 times 10^5
  • -10^9 leq A_i leq 10^9
  • 所有输入值都是整数.

输入

输入格式如下:

代码语言:javascript复制
N
A1 A2 ...... AN

输出

求和为 0 的 A 的非空连续子序列的个数.

输入示例 1

代码语言:javascript复制
6
1 3 -4 2 2 -2

输出示例 1

代码语言:javascript复制
3

There are three contiguous subsequences whose sums are 0: (1,3,-4), (-4,2,2) and (2,-2).

输入示例 2

代码语言:javascript复制
7
1 -1 1 -1 1 -1 1

输出示例 2

代码语言:javascript复制
12

In this case, some subsequences that have the same contents but are taken from different positions are counted individually. For example, three occurrences of (1, -1) are counted.

输入示例 3

代码语言:javascript复制
5
1 -2 3 -4 5

输出示例 3

代码语言:javascript复制
0

There are no contiguous subsequences whose sums are 0.

题解

小码匠题解

  • 楼下是大神:tourist题解,代码的每一个细节都值得学习
代码语言:javascript复制
void coder_solution() {
    // 提升cin、cout效率
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n;
    cin >> n;
    unordered_map<long long , long long > b;
    vector<long long> a(n);
    cin >> a[0];
    b[0] = 0;
    b[a[0]]  ;
    for(int i = 1; i < n;   i) {
        cin >> a[i];
        a[i]  = a[i - 1];
        b[a[i]]  ;
    }
    long long ans = 0;
    for(int i = 0; i < n;   i) {
        ans  = b[a[i]];
        if(a[i] != 0) {
            ans--;
        }
        b[a[i]]--;
    }
    cout << ans;
}

参考题解(tourist)

代码语言:javascript复制
void best_solution() {
    // 提升cin、cout效率
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    
    int n;
    cin >> n;
    
    map<long long, int> mp;
    mp[0] = 1;
    
    long long s = 0;
    long long ans = 0;
    for (int i = 0; i < n; i  ) {
        int x;
        cin >> x;
        s  = x;
        ans  = mp[s];
        mp[s]  ;
    }
    cout << ans << 'n';
}

参考题解

代码语言:javascript复制
#include <iostream>
#include <vector>
#include <map>
using namespace std;

int main() {
    int N; cin >> N;
    vector<long long> a(N);
    for (int i = 0; i < N;   i) cin >> a[i];

    // 累積和と連想配列
    vector<long long> s(N 1, 0);
    map<long long, long long> nums;
    for (int i = 0; i < N;   i) s[i 1] = s[i]   a[i];
    for (int i = 0; i < N 1;   i) nums[s[i]]  ;

    // 集計処理
    long long res = 0;
    for (auto it : nums) {
        long long num = it.second; // it.first が it.second 個ある
        res  = num * (num - 1) / 2;
    }
    cout << res << endl;
}

又遇大神

代码语言:javascript复制
#pragma GCC optimize ("O3")
#pragma GCC target ("avx2")
//#include<bits/stdc  .h>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i  )
#define rep1(i, n) for(int i = 1; i <= (n); i  )
#define co(x) cout << (x) << "n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please
 
const int CM = 1 << 17, CL = 12;
char cn[CM   CL], * ci = cn   CM   CL, * owa = cn   CM, ct;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint() {
	if (ci - owa > 0) {
		memcpy(cn, owa, CL);
		ci -= CM;
		fread(cn   CL, 1, CM, stdin);
	}
	int pn = 1;
	if (*ci == '-') {
		pn = -pn;
		ci  ;
	}
	ll tmp = *(ll*)ci;
	int dig = ((tmp & ma0) ^ ma0) ? 68 - __builtin_ctzll((tmp & ma0) ^ ma0) : 0;
	tmp = tmp << dig & ma1;
	tmp = tmp * 10   (tmp >> 8) & ma2;
	tmp = tmp * 100   (tmp >> 16) & ma3;
	tmp = tmp * 10000   (tmp >> 32) & ma4;
	ci  = (64 - dig >> 3);
	while ((ct = *ci  ) >= '0') tmp = tmp * 10   ct - '0';
	return pn * tmp;
}
 
ll A[200001];
void pakuri_sort(int N, ll A[]) {
	const int b = 8;
	ll tmp[200001];
	rep(k, 4) {
		int kazu[1 << b] = {}, kazu2[1 << b] = {};
		rep(i, N) kazu[A[i] >> k * b & ((1 << b) - 1)]  ;
		rep(i, (1 << b) - 1) kazu[i   1]  = kazu[i];
		for (int i = N - 1; i >= 0; i--) tmp[--kazu[A[i] >> k * b & ((1 << b) - 1)]] = A[i];
		k  ;
		rep(i, N) kazu2[tmp[i] >> k * b & ((1 << b) - 1)]  ;
		rep(i, (1 << b) - 1) kazu2[i   1]  = kazu2[i];
		for (int i = N - 1; i >= 0; i--) A[--kazu2[tmp[i] >> k * b & ((1 << b) - 1)]] = tmp[i];
	}
}
 
int main() {
	//cin.tie(0);
	//ios::sync_with_stdio(false);
 
 
	int N = getint();
	rep1(i, N) {
		A[i] = getint()   A[i - 1];
	}
 
	pakuri_sort(N   1, A);
 
	ll kotae = 0;
	ll mae = 1e18;
	int kazu = 0;
	rep(i, N   1) {
		if (mae != A[i]) {
			mae = A[i];
			kazu = 0;
		}
		kotae  = kazu  ;
	}
 
	printf("%lldn", kotae);
 
	Would you please return 0;
}

0 人点赞