답안 #1048144

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1048144 2024-08-08T01:49:06 Z vjudge1 Selling RNA Strands (JOI16_selling_rna) C++17
100 / 100
131 ms 46356 KB
#include<bits/stdc++.h>
using namespace std;
#define FOR(i, a, b)  for(int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, b, a) for(int i = (b), _a = (a); i >= _a; --i)
#define REP(i, a, b)  for(int i = (a), _b = (b); i < _b;  ++i)
#define REPD(i, b, a) for(int i = (b), _a = (a); i > _a;  --i)
#define MASK(i) (1LL << (i))
#define BIT(mask, i) (((mask) >> (i)) & 1)
#define __builtin_popcount __builtin_popcountll
#define all(val) val.begin(), val.end()
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sz(v) (int)v.size()
#define fi first
#define se second

mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll Rand(ll l, ll r) {return l + rng() % (r - l + 1);}

template<class X, class Y>
    bool minimize(X &x, const Y &y) {
        if (x > y) {
            x = y;
            return true;
        } else return false;
    }
template<class X, class Y>
    bool maximize(X &x, const Y &y) {
        if (x < y) {
            x = y;
            return true;
        } else return false;
    }
template<class T>
    T Abs(const T &x) {
        return (x < 0 ? -x : x);
    }

const int mod = 1e9 + 7;
const int inf = 1e9;
const int N = 1e5 + 5;

struct node {
    int val, l, r;
    node() {
        val = l = r = 0;
    }
};

int n, q, nNodes, ver[N];
string s[N], rev[N];
vector<pair<string, int>> v;
node st[(1 << 21) + 5];

void upd(int id) {
    st[id].val = st[st[id].l].val + st[st[id].r].val;
}

int update(int pos, int val, int old, int l = 1, int r = n) {
    int cur = ++nNodes;
    if(l == r) {
        st[cur].val = st[old].val + val;
        return cur;
    }
    int m = l + r >> 1;
    if(pos <= m) {
        st[cur].l = update(pos, val, st[old].l, l, m);
        st[cur].r = st[old].r;
    }
    else {
        st[cur].l = st[old].l;
        st[cur].r = update(pos, val, st[old].r, m + 1, r);
    }
    upd(cur);
    return cur;
}

int get(int ver1, int ver2, int u, int v, int l = 1, int r = n) {
    if(u > r || v < l) return 0;
    if(u <= l && r <= v) {
        return st[ver2].val - st[ver1].val;
    }
    int m = l + r >> 1;
    return get(st[ver1].l, st[ver2].l, u, v, l, m) + get(st[ver1].r, st[ver2].r, u, v, m + 1, r);
}

pii Find(string x, string *S) {
    int l = lower_bound(S + 1, S + n + 1, x) - S;
    x.back()++;
    int r = lower_bound(S + 1, S + n + 1, x) - S - 1;
    return {l, r};
}

void process() {
    cin >> n >> q;
    FOR(i, 1, n) cin >> s[i];
    sort(s + 1, s + n + 1);
    FOR(i, 1, n) {
        rev[i] = s[i];
        reverse(all(rev[i]));
        v.emplace_back(rev[i], i);
    }
    sort(rev + 1, rev + n + 1);
    sort(all(v));
    FOR(i, 1, n) {
        ver[i] = update(v[i - 1].se, 1, ver[i - 1]);
    }
    while(q--) {
        string pref, suf; cin >> pref >> suf;
        reverse(all(suf));
        auto [l1, r1] = Find(pref, s);
        auto [l2, r2] = Find(suf, rev);
        cout << get(ver[l2 - 1], ver[r2], l1, r1) << '\n';
    }
}

signed main() {
    if(fopen("test.inp","r")) {
        freopen("test.inp","r",stdin);
        freopen("test.out","w",stdout);
    }
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    // clock_t start = clock();

    int ntests = 1;
    // cin >> ntests;
    while(ntests--) process();

    // cerr << "Time elapsed: " << clock() - start << " ms!\n";
    return 0;
}

Compilation message

selling_rna.cpp: In function 'int update(int, int, int, int, int)':
selling_rna.cpp:67:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   67 |     int m = l + r >> 1;
      |             ~~^~~
selling_rna.cpp: In function 'int get(int, int, int, int, int, int)':
selling_rna.cpp:85:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   85 |     int m = l + r >> 1;
      |             ~~^~~
selling_rna.cpp: In function 'int main()':
selling_rna.cpp:121:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  121 |         freopen("test.inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
selling_rna.cpp:122:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  122 |         freopen("test.out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 31324 KB Output is correct
2 Correct 6 ms 31324 KB Output is correct
3 Correct 4 ms 31388 KB Output is correct
4 Correct 4 ms 31352 KB Output is correct
5 Correct 5 ms 31320 KB Output is correct
6 Correct 4 ms 31324 KB Output is correct
7 Correct 4 ms 31344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 37468 KB Output is correct
2 Correct 23 ms 37828 KB Output is correct
3 Correct 19 ms 37640 KB Output is correct
4 Correct 20 ms 37612 KB Output is correct
5 Correct 16 ms 35512 KB Output is correct
6 Correct 15 ms 35376 KB Output is correct
7 Correct 15 ms 35928 KB Output is correct
8 Correct 22 ms 37772 KB Output is correct
9 Correct 23 ms 37756 KB Output is correct
10 Correct 18 ms 37556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 33996 KB Output is correct
2 Correct 31 ms 32716 KB Output is correct
3 Correct 39 ms 34000 KB Output is correct
4 Correct 36 ms 34000 KB Output is correct
5 Correct 31 ms 32720 KB Output is correct
6 Correct 38 ms 34000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 31324 KB Output is correct
2 Correct 6 ms 31324 KB Output is correct
3 Correct 4 ms 31388 KB Output is correct
4 Correct 4 ms 31352 KB Output is correct
5 Correct 5 ms 31320 KB Output is correct
6 Correct 4 ms 31324 KB Output is correct
7 Correct 4 ms 31344 KB Output is correct
8 Correct 15 ms 37468 KB Output is correct
9 Correct 23 ms 37828 KB Output is correct
10 Correct 19 ms 37640 KB Output is correct
11 Correct 20 ms 37612 KB Output is correct
12 Correct 16 ms 35512 KB Output is correct
13 Correct 15 ms 35376 KB Output is correct
14 Correct 15 ms 35928 KB Output is correct
15 Correct 22 ms 37772 KB Output is correct
16 Correct 23 ms 37756 KB Output is correct
17 Correct 18 ms 37556 KB Output is correct
18 Correct 44 ms 33996 KB Output is correct
19 Correct 31 ms 32716 KB Output is correct
20 Correct 39 ms 34000 KB Output is correct
21 Correct 36 ms 34000 KB Output is correct
22 Correct 31 ms 32720 KB Output is correct
23 Correct 38 ms 34000 KB Output is correct
24 Correct 32 ms 38096 KB Output is correct
25 Correct 46 ms 38248 KB Output is correct
26 Correct 26 ms 38072 KB Output is correct
27 Correct 33 ms 38004 KB Output is correct
28 Correct 131 ms 46356 KB Output is correct
29 Correct 111 ms 39348 KB Output is correct