BZOJ4571: [Scoi2016]美味

来源:互联网 发布:mmd怎么自己做动作数据 编辑:程序博客网 时间:2024/06/12 01:32

Description

一家餐厅有 n 道菜,编号 1...n ,大家对第 i 道菜的评价值为 ai(1≤i≤n)。有 m 位顾客,第 i 位顾客的期
望值为 bi,而他的偏好值为 xi 。因此,第 i 位顾客认为第 j 道菜的美味度为 bi XOR (aj+xi),XOR 表示异或
运算。第 i 位顾客希望从这些菜中挑出他认为最美味的菜,即美味值最大的菜,但由于价格等因素,他只能从第 
li 道到第 ri 道中选择。请你帮助他们找出最美味的菜。

Input

第1行,两个整数,n,m,表示菜品数和顾客数。
第2行,n个整数,a1,a2,...,an,表示每道菜的评价值。
第3至m+2行,每行4个整数,b,x,l,r,表示该位顾客的期望值,偏好值,和可以选择菜品区间。
1≤n≤2×10^5,0≤ai,bi,xi<10^5,1≤li≤ri≤n(1≤i≤m);1≤m≤10^5

Output

 输出 m 行,每行 1 个整数,ymax ,表示该位顾客选择的最美味的菜的美味值。

Sample Input

4 4
1 2 3 4
1 4 1 4
2 3 2 3
3 2 3 3
4 1 2 4

Sample Output

9
7
6
7

HINT

Source

主席树
首先看到异或就是按位贪心 看到l,r就想到建主席树
然后加法不管,一位一位卡范围就好了
(貌似很水)
#include <bits/stdc++.h>using namespace std;const int MAXN = 200020;const int MAXM = 262144;inline int read(){int sc = 0; char ch = getchar();while( ch < '0' || ch > '9' ) ch = getchar();while( ch >= '0' && ch <= '9' ) sc = sc * 10 + ch - '0', ch = getchar();return sc;}int root[MAXN], sum[MAXN * 20], ls[MAXN * 20], rs[MAXN * 20];int n, Q, tot;inline void modify(int &x, int y, int l, int r, int v){x = ++tot;ls[ x ] = ls[ y ]; rs[ x ] = rs[ y ]; sum[ x ] = sum[ y ] + 1;if( l == r ) return ;int mid = l + r >> 1;if( v <= mid ) modify( ls[ x ], ls[ y ], l, mid, v );else modify( rs[ x ], rs[ y ], mid + 1, r, v );}inline bool query(int x, int y, int l, int r, int ql, int qr){if( l == ql && r == qr ) return sum[ y ] - sum[ x ];int mid = l + r >> 1;if( qr <= mid ) return query( ls[ x ], ls[ y ], l, mid, ql, qr );if( ql > mid ) return query( rs[ x ], rs[ y ], mid + 1, r, ql, qr );return query( ls[ x ], ls[ y ], l, mid, ql, mid ) || query( rs[ x ], rs[ y ], mid + 1, r, mid + 1, qr );}int main(){n = read(), Q = read();for( int i = 1 ; i <= n ; i++ )modify( root[ i ], root[ i - 1 ], 0, MAXM, read() );while( Q-- ){int b = read(), x = read(), L = read(), R = read(), a = 0;L--;for( int i = 17 ; i >= 0 ; i-- ){if( !( ( b >> i ) & 1 ) ) a ^= ( 1 << i );int l = max( a - x, 0 ), r = a + ( 1 << i ) - 1 - x;if( r < 0 || !query( root[ L ], root[ R ], 0, MAXM, l, r ) ) a ^= ( 1 << i );}printf( "%d\n", a ^ b );}}


0 0
原创粉丝点击