C - Sigma Problem
排序后二分
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize(3,"Ofast","inline")
#define inf 0x3f3f3f3f
typedef long long LL;
typedef unsigned long long ULL;
const int N = 3e5+10;
const int P = 131;
int mod = 1e8;
LL n, A[N];
void solve()
{
cin >> n;
LL ans = 0;
for (int i = 0;i < n;i++)
{
cin >> A[i];
ans += A[i]*(n-1);
}
sort(A,A+n);
for (int i = 0;i < n;i++)
{
int st = lower_bound(A,A+n,mod-A[i])-A;
if (st <= i) ans -= (n-i-1)*mod;
else ans -=(n-st)*mod;
}
cout << ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t = 1;
// cin >> t;
while (t--)
{
solve();
}
return 0;
}
D - Another Sigma Problem
统计数位,记得膜
#include <bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long LL;
typedef unsigned long long ULL;
const int N = 3e5+10;
const int P = 131;
int mod = 998244353;
LL n, A[N];
LL B[N];
LL C[20];
LL D[20];
void solve()
{
D[1] = 10;
LL sum = 0;
for (int i = 2;i <= 15;i++)
{
D[i] = D[i-1] * 10;
}
cin >> n;
for (int i = 1;i <= n;i++)
{
cin >> A[i];
sum += A[i];
LL num = A[i];
while (num)
{
num /= 10;
B[i]++;
}
C[B[i]]++;
}
LL ans = 0;
for (int i = 1;i <= n;i++)
{
C[B[i]]--;
sum -= A[i];
for (LL j = 1;j <= 15;j++)
{
LL num = (((A[i]%mod)*(D[j]%mod))%mod*(C[j]%mod))%mod;
ans += num;
ans %= mod;
}
ans += sum;
ans %= mod;
}
cout << ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
LL t = 1;
// cin >> t;
while (t--)
{
solve();
}
return 0;
}
E - Yet Another Sigma Problem
字典树
#include <bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long LL;
typedef unsigned long long ULL;
const int N = 1e6+10;
const int P = 13331;
string s;
int ch[N][26], idx, cnt[N];
LL ans = 0;
void insert(string s)
{
int len = s.length();
int p = 0 ;
for (int i = 0;i < len;i++)
{
int j = s[i] - 'a';
if (!ch[p][j]) ch[p][j] = ++idx;
p = ch[p][j];
ans += cnt[p];
cnt[p]++;
// cout << ans << "===" << endl;
}
}
void solve()
{
int n;
cin >> n;
while (n--)
{
cin >> s;
insert(s);
}
cout << ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t = 1;
// cin >> t;
while (t--)
{
solve();
}
return 0;
}
G - Merchant Takahashi
线段树优化dp
#include <bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long LL;
typedef unsigned LLULL;
const LL N = 2e5+10;
const LL P = 131;
LL n, c, m, dp[N];
LL tr[N*4][2];
void pushup(LL x,LL k)
{
tr[x][k]=max(tr[x<<1][k],tr[x<<1|1][k]);
}
void add(LL x,LL l,LL r,LL L,LL R,long long v,LL k)
{
if(L <= l&&r <= R)
{
tr[x][k] += v;
return ;
}
LL mid = (l+r) >> 1;
if(L <= mid) add(x<<1, l,mid, L, R, v, k);
if(R > mid) add(x<<1|1, mid+1, r, L, R, v, k);
pushup(x,k);
return ;
}
LL que(LL ro,LL l,LL r,LL L,LL R,LL k)
{
if(L <= l&& r<= R)
{
return tr[ro][k];
}
LL mid=(l+r)>>1;
long long ans=- 99999999999999999;
if(L<=mid) ans=max(ans,que(ro<<1,l,mid,L,R,k));
if(R>mid) ans=max(ans,que(ro<<1|1,mid+1,r,L,R,k));
return ans;
}
void solve()
{
cin >> n >> c >> m;
for (LL i = 2;i <= n;i++) dp[i] = -1e18;
for (LL i = 1;i <= n;i++)
{
add(1,1,n,i,i,dp[i]-i*c,0);
add(1,1,n,i,i,dp[i]+i*c,1);
}
while (m--)
{
LL t, p;
cin >> t >> p;
LL num = max({dp[t],que(1,1,n,1,t,1)-t*c+p,que(1,1,n,t,n,0)+t*c+p});
LL num_ = num-dp[t];
add(1,1,n,t,t,num_,0);
add(1,1,n,t,t,num_,1);
dp[t] = num;
}
LL ans = 0;
for (LL i = 1;i <= n;i++)
{
ans = max(ans,dp[i]);
}
cout << ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
LL t = 1;
// cin >> t;
while (t--)
{
solve();
}
return 0;
}