这里有个蒟蒻求助!!!!
  • 板块P5149 会议座位
  • 楼主ashore_
  • 当前回复2
  • 已保存回复2
  • 发布时间2022/11/24 16:13
  • 上次更新2023/10/27 01:42:15
查看原帖
这里有个蒟蒻求助!!!!
811016
ashore_楼主2022/11/24 16:13

https://www.luogu.com.cn/record/95274904

为什么后五个全RE啊

#include <bits/stdc++.h>
#define _i i << 1
#define __i i << 1 | 1
#define cl tree[i].l
#define cr tree[i].r
#define mod(x) ((x) % MOD)
#define lowbit(x) (x & -x)
#define g1(x) get<0>(x)
#define g2(x) get<1>(x)
#define g3(x) get<2>(x)
#define ufr(i, x, y) for (int i = x; i <= y; ++i)
#define lfr(i, x, y) for (int i = x; i >= y; --i)
#define all(x) x.begin(), x.end()
#define goto(x, y) x + 1, x + y + 1
#define mm(x, y) memset(x, y, sizeof(x))
using namespace std;
using ld = long double;
using ll = long long;
using pp = pair<int, int>;

namespace Solution {
class fastio {
 public:
  template <class T = int>
  inline T r() noexcept {
    T x = 0, w = 1;
    char ch = 0;
    for (; !isdigit(ch); ch = getchar())
      if (ch == '-') w = -1;
    for (; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch - '0');
    return x * w;
  }
  inline void rs(char* s) noexcept {
    int idx = 0;
    char ch = 0;
    for (ch = getchar(); ch == '\0' || ch == '\n' || ch == '\r'; ch = getchar())
      ;
    for (; ch != '\n' && ch != '\r' && ch != ' '; ch = getchar()) s[idx++] = ch;
    s[idx] = '\0';
  }
  inline char rc() noexcept {
    char ch = getchar();
    for (; ch == '\0' || ch == '\n' || ch == '\r' || ch == ' '; ch = getchar())
      ;
    return ch;
  }

  template <class T = int>
  inline fastio& pt(T x) noexcept {
    static char sta[128];
    int top = 0;
    if (x < 0) x = -x, putchar('-');
    do {
      sta[top++] = x % 10, x /= 10;
    } while (x);
    while (top) putchar(sta[--top] + 48);
    return *this;
  }

  inline fastio& pts(const string s) noexcept {
    for (int i = 0; s[i] != '\0'; ++i) putchar(s[i]);
    return *this;
  }
  inline fastio& ptc(const char c) noexcept {
    putchar(c);
    return *this;
  }
  inline fastio& ln() noexcept {
    putchar('\n');
    return *this;
  }
} f;
// 使用priority_queque时默认为最小堆
struct vertex {
  int from, to, cost;
  vertex() {}
  vertex(int from_, int to_, int cost_) : from(from_), to(to_), cost(cost_) {}
  vertex(int to_, int cost_) : to(to_), cost(cost_) {}
  vertex(int from_, int to_, bool token) : from(from_), to(to_) {}
  bool operator<(const vertex& v) const { return this->cost > v.cost; }
  bool operator==(const vertex& v) const {
    return this->from == v.from && this->to == v.to;
  }
};

struct pp_hash {
  template <class T1, class T2>
  std::size_t operator()(const std::pair<T1, T2>& p) const {
    auto h1 = std::hash<T1>{}(g1(p));
    auto h2 = std::hash<T2>{}(g2(p));
    return h1 ^ h2;
  }
};
// less --> return __x < __y; || greater --> return __x > __y;
#define MAX_N 150010
static constexpr ll MOD = 1e9 + 7;
static constexpr ll eps = -1e9;
static constexpr ll inf = 0x7fffffff;
#ifdef GRAPH
struct {
  int to, w, next;
} edge[MAX_N];
int head[MAX_N], cnt = 0;
#endif
namespace Trie {
    //字典树-------------------------------
int next[MAX_N][70], loca[MAX_N][70], idx = 0, t = 0;
//next值的是下一个字符在字典树中,loca是没更换为之前出现的位置
inline void init() { mm(next, 0), mm(loca, 0); }
inline int get_num(char ch) {
  if (ch >= 'A' && ch <= 'Z')
    return ch - 'A';
  else if (ch >= 'a' && ch <= 'z')
    return ch - 'a' + 27;
  return 10;
}
inline void insert(char* s) {
  int p = 0, c, l = 0;//
  for (int i = 0; s[i] != '\0'; ++i) {
    c = get_num(s[i]);
    if (!next[p][c]) next[p][c] = ++idx;
    l = p;//一个字符串最后一个字符出现的位置
    p = next[p][c];
  }
  //给最后一个字符赋上第一次出现的索引值
  loca[l][c] = ++t;
}
inline int find(char* s) {
  int p = 0, c, l = 0;
  for (int i = 0; s[i] != '\0'; ++i) {
    c = get_num(s[i]);
    if (!next[p][c]) return 0;
    l = p;
    p = next[p][c];
  }
  return loca[l][c];
}
}  // namespace Trie

int n;
char s[1000];
//树状数组----------------------------------
ll tree[MAX_N];
inline void add(int idx, int x) {
  for (int i = idx; i <= n; i += lowbit(i)) tree[i] += x;
}
inline int query(int idx) {
  int res = 0;
  while (idx) {
    res += tree[idx];
    idx -= lowbit(idx);
  }
  return res;
}
//求逆序对------------------
void solve() {
  ll ans = 0;
  ufr(i, 1, n) {
    f.rs(s);
    int ori = Trie::find(s);
    ans += query(ori - 1);
    add(ori, -1);
  }
  f.pt(ans).ln();
}

}  // namespace Solution

signed main() {
  using namespace Solution;
  using namespace Solution::Trie;
  mm(tree, 0);
  init();
  n = f.r();
  ufr(i, 1, n) {
    add(i, 1);
    f.rs(s);
    insert(s);
  }
  solve();
  return 0;
}
/*
5
A C B D E
B A D E C
*/
2022/11/24 16:13
加载中...