20250722 总结

· · 个人记录

A.洛谷 P1637

题意

给定含有 n 个整数的序列 a_1,a_2,\ldots,a_n 中,请你求出 i<j<k 且 a_i<a_j<a_k 的三元组个数。

思路

对于每个 a_i,用两个树状数组维护前后的数值,然后在移动时将左侧树状数组的元素增加,右侧的树状数组减少

代码

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

const int N = 1e5 + 10, V = 1e5 + 1;
int n, a[N], b[N], sum;

struct tree{
  int n, a[N];
  void init(int n){
    this->n = n;
    for(int i = 1; i <= n; i++){
      a[i] = 0;
    }
  }
  void add(int x, int v) {
    for (; x <= n; x += x & -x) {
      a[x] += v;
    }
  }
  int sum(int x) {
    int ans = 0;
    for (; x; x -= x & -x) {
      ans += a[x];
    }
    return ans;
  }
}t1, t2;

signed main(){
  cin >> n;
  t1.init(V), t2.init(V);
  for(int i = 1; i <= n; i++){
    cin >> a[i];
    if(i > 1)
      t2.add(a[i], 1);
  }
  for(int i = 2; i < n; i++){
    t2.add(a[i], -1);
    t1.add(a[i - 1], 1);
    sum += t1.sum(a[i] - 1) * (t2.sum(V - 1) - t2.sum(a[i]));
  }
  cout << sum;
  return 0;
}

B.CodeForces 597C

题意

给定一个包含 n 个不同元素的序列,找出具有 k + 1 个元素的递增子序列的数量。

思路

对于每个序列长度,求出每个长度的子序列数量,接着放入树状数组中,最后求和统计答案

代码

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

const int N = 1e5 + 10, V = 1e5 + 1;

int n, k, sum;
int cnt[15][N], a[N];

struct tree{
  int n, a[N];
  void init(int n){
    this->n = n;
    for(int i = 1; i <= n; i++){
      a[i] = 0;
    }
  }
  void add(int x, int v) {
    for (; x <= n; x += x & -x) {
      a[x] += v;
    }
  }
  int sum(int x) {
    int ans = 0;
    for (; x; x -= x & -x) {
      ans += a[x];
    }
    return ans;
  }
}t[15];

signed main(){
  cin >> n >> k;
  k++;
  for(int i = 1; i <= k; i++){
    t[i].init(V);
  }
  for(int i = 1; i <= n; i++){
    cin >> a[i];
    cnt[1][i] = 1;
  }
  for(int i = 2; i <= k; i++){
    for(int j = 1; j <= n; j++){
      cnt[i][j] += t[i].sum(a[j]);
      t[i].add(a[j], cnt[i - 1][j]);
    }
  }
  for(int i = 1; i <= n; i++){
    sum += cnt[k][i];
  }
  cout << sum;
  return 0;
}

C.洛谷 P2163

题意

给定 n 个节点坐标 (x,y),接着给定 m 次询问,每次询问给出四个整数 a,b,c,d,求以 (a,b) 为左下角,(c,d) 为右上角的矩形内部(包括边界)有多少个节点。

思路

代码

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

const int N = 5e7 + 10, V = 1e7 + 10;

int n, m, ans[N];

struct P{
  int x, y, tag, id;
};

vector<P> g;

bool cmp(const P &a, const P &b){
  if(a.y != b.y) return a.y < b.y;
  return a.tag == 0;
}

struct tree{
  int n, a[N];
  void init(int n){
    this->n = n;
    for(int i = 1; i <= n; i++){
      a[i] = 0;
    }
  }
  void add(int x, int v) {
    for (; x <= n; x += x & (-x)) {
      a[x] += v;
    }
  }
  int sum(int x) {
    int ans = 0;
    for (; x; x -= x & (-x)) {
      ans += a[x];
    }
    return ans;
  }
}t;

int main(){
  cin >> n >> m;
  t.init(V - 1);
  for(int i = 1, x, y; i <= n; i++){
    cin >> x >> y;
    x += 5, y += 5;
    g.push_back({x, y, 0, i});
  }
  for(int i = 1, l1, r1, l2, r2; i <= m; i++){
    cin >> l1 >> r1 >> l2 >> r2;
    l1 += 5, l2 += 5, r1 += 5, r2 += 5;
    g.push_back({l1 - 1, r1 - 1, 1, i});
    g.push_back({l2, r2, 1, i});
    g.push_back({l1 - 1, r2, -1, i});
    g.push_back({l2, r1 - 1, -1, i});
  }
  sort(g.begin(), g.end(), cmp);
  for(int i = 0; i < g.size(); i++){
    auto [x, y, tag, id] = g[i];
    if(tag == 0){
      t.add(x, 1);
    }
    else if(tag == 1){
      ans[id] += t.sum(x);
    }
    else if(tag == -1){
      ans[id] -= t.sum(x);
    }
  }
  for(int i = 1; i <= m; i++){
    cout << ans[i] << "\n";
  }
  return 0;
}

D.AtCoder abc393_f

题意

给定一个长度为 N 的整数序列 A=(A_1,A_2,\dots,A_N) 。请处理 Q 个查询。第 i\ (1 \leq i \leq Q) 个查询如下:

  1. 数据较大需要离散化
  2. 按 X_i 从小到大排序,用扫描线处理答案

    注意

    排序比较器 X 相同将点放在前面,如果都为点,则将 id 大的放前面

    代码

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

const int N = 2e5 + 10, V = 2e5; int n, q, h[N], ans[N]; vector<int> b;

int Q(int x){ return lower_bound(b.begin(), b.end(), x) - b.begin() + 1; }

struct P{ int r, x; }w[N];

struct tree{ int n = V, a[N]; void init(){ for(int i = 1; i <= n; i++){ a[i] = 0; } } void add(int x, int v) { for (; x <= n; x += x & (-x)) { a[x] = max(a[x], v); } } int sum(int x) { int ans = 0; for (; x; x -= x & (-x)) { ans = max(a[x], ans); } return ans; } }t;

struct stc{ int x, tag, id; };

vector<stc> g;

bool cmp(const stc &a, const stc &b){ if(a.x != b.x) return a.x < b.x; if(a.tag != b.tag) return a.tag == 0; return a.id > b.id; }

int main(){ t.init(); cin >> n >> q; for(int i = 1; i <= n; i++){ cin >> h[i]; b.push_back(h[i]); } for(int i = 1; i <= q; i++){ cin >> w[i].r >> w[i].x; b.push_back(w[i].x); } sort(b.begin(), b.end()); b.erase(unique(b.begin(), b.end()), b.end()); for(int i = 1; i <= n; i++){ h[i] = Q(h[i]); //cout << h[i] << " "; g.push_back({h[i], 0, i}); } //cout << "\n"; for(int i = 1; i <= q; i++){ w[i].x = Q(w[i].x); //cout << w[i].x << " "; g.push_back({w[i].x, 1, i}); } //cout << "\n"; sort(g.begin(), g.end(), cmp); for(stc i : g){ auto [x, tag, id] = i; if(tag == 0){ t.add(id, t.sum(id - 1) + 1); } else{ ans[id] = t.sum(w[id].r); } } for(int i = 1; i <= q; i++){ cout << ans[i] << "\n"; } return 0; }