1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| #include<bits/stdc++.h> using namespace std;
const int N = 1e6 + 10; int n, m; string *tr[N << 2], a[N], init;
inline string* str_max(string *a, string *b){ if(*a > *b) return a; return b; }
void build(int u = 1, int cl = 1, int cr = n){ if(cl == cr) return void(tr[u] = &a[cl]); int mid = cl + cr >> 1; build(u << 1, cl, mid); build(u << 1 | 1, mid + 1, cr); tr[u] = str_max(tr[u << 1], tr[u << 1 | 1]); }
string* query(int l, int r, int u = 1, int cl = 1, int cr = n){ if(l <= cl && cr <= r) return tr[u]; int mid = cl + cr >> 1; string *mx = &init; if(mid >= l) mx = str_max(mx, query(l, r, u << 1, cl, mid)); if(mid < r) mx = str_max(mx, query(l, r, u << 1 | 1, mid + 1, cr)); return mx; }
signed main(){ ios::sync_with_stdio(0); cin.tie(0); cin >> n; for(int i = 1; i <= n; i++) cin >> a[i]; build(); cin >> m; while(m--){ int l, r; cin >> l >> r; for(auto &ch : *query(l, r)) putchar(ch); putchar('\n'); } return 0; }
|