定义一种操作f(u, v) = u^u+1^.......^v。 (u<=v), 给n个数, q个询问, 每个询问给出一个区间[l, r], 求这个区间里的f(a[i], a[j]) (l<=i<=j<=r)的最大值。
一开始竟然用n^2m的方法, 真是有点脑残..
#include <iostream> #include <vector> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <map> #include <set> #include <string> #include <queue> #include <stack> #include <bitset> using namespace std; #define pb(x) push_back(x) #define ll long long #define mk(x, y) make_pair(x, y) #define lson l, m, rt<<1 #define mem(a) memset(a, 0, sizeof(a)) #define rson m+1, r, rt<<1|1 #define mem1(a) memset(a, -1, sizeof(a)) #define mem2(a) memset(a, 0x3f, sizeof(a)) #define rep(i, n, a) for(int i = a; i<n; i++) #define fi first #define se second typedef pair<int, int> pll; const double PI = acos(-1.0); const double eps = 1e-8; const int mod = 1e9+7; const int inf = 1061109567; const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; int a[50005], ans[5005], b[1000005], dp[50005]; struct node { int l, r; }q[5005]; int main() { int n, m; cin>>n>>m; for(int i = 1; i<=n; i++) { scanf("%d", &a[i]); } for(int i = 1; i<=1000000; i++) { b[i] = i^b[i-1]; } for(int i = 0; i<m; i++) { scanf("%d%d", &q[i].l, &q[i].r); } for(int i = 1; i<=n; i++) { dp[i] = a[i]; for(int j = i+1; j<=n; j++) { int minn = min(a[i], a[j]); int maxx = max(a[i], a[j]); int num = b[maxx]^b[minn-1]; dp[j] = max(dp[j-1], num); } for(int j = 0; j<m; j++) { if(i>=q[j].l&&i<=q[j].r) { ans[j] = max(ans[j], dp[q[j].r]); } } } for(int i = 0; i<m; i++) printf("%d\n", ans[i]); return 0; }
时间: 2024-10-06 04:17:05