20250809 总结

· · 个人记录

C.洛谷 P7286

题意

有一个数组 k,下标为 1 到 n 。请求出对于任意的 1 \le x,y \le n,f(x,y) 的最大值是多少,

思路

对于题目给定的式子,可以分开处理。

代码

#include<bits/stdc++.h>
using namespace std;
#define int long long

const int N = 1e6 + 10;
int n, ans, mx;

struct P{
  int v, id;
}a[N];

bool cmp(const P &a, const P &b){
  return a.v > b.v;
}

signed main(){
  cin >> n;
  for(int i = 1; i <= n; i++){
    cin >> a[i].v;
    a[i].id = i;
    ans = max(ans, a[i].v * i);
  }
  sort(a + 1, a + 1 + n, cmp);
  for(int i = 1; i <= n; i++){
    ans = max(ans, a[i].v * (a[i].id + mx));
    mx = max(mx, a[i].id);
  }
  cout << ans;
  return 0;
}

F.洛谷 P3939

题意

有 n 只编号为 1\sim n 的兔子,第 i 只颜色为 a_i。给定 m 次询问,有以下两种操作:

  1. 询问在区间 [l_j,r_j] 里有多少只颜色为 c_j 的兔子;

思路

对于每种颜色,用 vector 记录每次出现的为位置即可。

每次查询时,使用二分找出在区间 [l, r] 在颜色 c 中的第一次和最后一次的值,相减即为答案。

每次修改时,如果 a_x = a_{x+1} 则忽略操作;否则,就让 x 对应的值 +1,让 x+ 1 对应的值 -1。这样就能保证每个颜色的出现下标保持单调性。

代码

#include<bits/stdc++.h>
using namespace std;

const int N = 3e5 + 10;

int n, m, a[N];
vector<int> g[N];

int get(int c, int x){
  return lower_bound(g[c].begin(), g[c].end(), x) -  g[c].begin();
}

int gett(int c, int x){
  return upper_bound(g[c].begin(), g[c].end(), x) -  g[c].begin();
}

int main(){
  cin >> n >> m;
  for(int i = 1; i <= n; i++){
    cin >> a[i];
    g[a[i]].push_back(i);
  }
  for(int i = 1; i <= m; i++){
    int op, l, r, c, x;
    cin >> op;
    if(op == 1){
      cin >> l >> r >> c;
      cout << gett(c, r) - get(c, l) << "\n";
    }
    else{
      cin >> x;
      if(a[x] == a[x + 1]) continue;
      l = get(a[x], x);
      r = get(a[x + 1], x + 1);
      g[a[x]][l]++;
      g[a[x + 1]][r]--;
      swap(a[x], a[x + 1]);
    }
  }
  return 0;
}

I.洛谷 P9350

题意

有 n 个点第 i 个点 有坐标 x_i 和影响 e_i。要从中选择 k 个点,使所有点都被考虑(选择)。如果第 i 个点被选择或考虑,所有满足以下条件的点 j(1\le j \le n) 也将被考虑:

请求出 k 的最小值。

思路

首先按照坐标从小到大排序保证单调性。

接着用单调栈维护需要选择的点。考虑式子 |x_i - x_j| \le e_i - e_j 可化为 e_{now} - e_{top} >= x_{now} - x_{top},now 表示当前下标,top 表示栈顶下标。对于满足式子的所有栈内元素,都弹出。最后判断当前节点是否受栈顶元素影响,如果不被则将当前元素插入栈。

代码

#include<bits/stdc++.h>
using namespace std;
#define int long long

const int N = 5e5 + 10;

int n;

struct P{
  int x, e;
}a[N];

bool cmp(const P &a, const P &b){
  return a.x < b.x;
}

stack<int> st;

signed main(){
  cin >> n;
  for(int i = 1; i <= n; i++){
    auto &[x, e] = a[i];
    cin >> x >> e;
  }
  sort(a + 1, a + 1 + n, cmp);
  for(int i = 1; i <= n; i++){
    auto [x, e] = a[i];
    while(!st.empty() && e - a[st.top()].e >= x - a[st.top()].x){
      st.pop();
    }
    if(st.empty() || a[st.top()].e - e  < x - a[st.top()].x){
      st.push(i);
    }
  }
  cout << st.size();
  return 0;
}

J.洛谷 P9816

题意

给定多项式 f(x)=\sum_{i=1}^ma_ix^{b_i}。定义 f_1(x)=f(x),f_n(x)=f(f_{n-1}(x))。

给定模数 p。有 q 次询问,每次给出 x,y,查询 f_y(x)\bmod p 的值。

思路

考虑倍增。

对于每个 f(x),可以先预先处理出往后走一步的值,接着使用倍增处理走 2^k 步的值。

在每次询问时,先计算一边 f(x),使其值在 10^5 范围内。最后使用位枚举计算出跳完后的结果。

代码

#include<bits/stdc++.h>
using namespace std;
using ll = long long;

const int N = 1e5 + 10;

ll m, q, mod, cnt;
ll f[N][30], a[25], b[25];

ll ksm(ll a, ll b){
  ll sum = 1;
  while(b){
    if(b & 1){
      (sum *= a) %= mod;
    }
    a = a * a % mod;
    b /= 2;
  }
  return sum % mod;
}

int main(){
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> m >> q >> mod;
  for(ll i = 1; i <= m; i++){
    cin >> a[i]; a[i] %= mod;
    cin >> b[i];
  }
  for(ll i = 0; i <= 100000; i++){
    for(ll j = 1; j <= m; j++){
      (f[i][0] += a[j] * ksm(i, b[j]) % mod) %= mod;
    }
  }
  for(ll j = 1; j <= 25; j++){
    for(ll u = 0; u <= 100000; u++){
      f[u][j] = f[f[u][j - 1]][j - 1];
    }
  }
  while(q--){
    ll x, y;
    cin >> x >> y;
    x %= mod;
    ll sum = 0;
    for(ll i = 1; i <= m; i++){
      sum += (a[i] * ksm(x, b[i])) % mod;
      sum %= mod;
    }
    y--;
    for(ll i = 0; i <= 25; i++){
      if((y >> i) & 1){
        sum = f[sum][i];
      }
    }
    cout << sum << "\n";
  }
  return 0;
}