# BZOJ4571 [Scoi2016]美味 【主席树】

## 题目

li 道到第 ri 道中选择。请你帮助他们找出最美味的菜。

## 输入格式

1≤n≤2×105，0≤ai,bi,xi＜105，1≤li≤ri≤n(1≤i≤m)；1≤m≤10^5

4 4

1 2 3 4

1 4 1 4

2 3 2 3

3 2 3 3

4 1 2 4

9

7

6

7

## 题解

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define LL long long int
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts("");
using namespace std;
const int maxn = 200005,maxm = 4000005,MAX = 100000,INF = 1000000000;
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int sum[maxm],ls[maxm],rs[maxm],rt[maxn],cnt;
int n,m,a[maxn],bin[20];
void add(int& u,int pre,int l,int r,int pos){
sum[u = ++cnt] = sum[pre] + 1;
ls[u] = ls[pre]; rs[u] = rs[pre];
if (l == r) return;
int mid = l + r >> 1;
}
int query(int u,int v,int l,int r,int L,int R){
if (l >= L && r <= R) return sum[u] - sum[v];
int mid = l + r >> 1;
if (mid >= R) return query(ls[u],ls[v],l,mid,L,R);
if (mid < L) return query(rs[u],rs[v],mid + 1,r,L,R);
return query(ls[u],ls[v],l,mid,L,R) + query(rs[u],rs[v],mid + 1,r,L,R);
}
int main(){
bin[0] = 1;
for (int i = 1; i <= 18; i++) bin[i] = bin[i - 1] << 1;
REP(i,n){
}
int b,x,L,R,l,r,tmp,ans;
while (m--){
ans = 0;
for (int i = 17; i >= 0; i--){
if (b & bin[i]){
l = max(ans - x,0);
r = min(max(ans + bin[i] - 1 - x,0),MAX);
if (l > MAX || r < 0) {ans |= bin[i]; continue;}
l = max(l,0);
r = min(r,MAX);
tmp = query(rt[R],rt[L - 1],0,MAX,l,r);
if (!tmp) ans |= bin[i];
}
else {
l = ans + bin[i] - x;
r = ans + bin[i + 1] - 1 - x;
if (l > MAX || r < 0) continue;
l = max(l,0);
r = min(r,MAX);
tmp = query(rt[R],rt[L - 1],0,MAX,l,r);
if (tmp) ans |= bin[i];
}
}
printf("%d\n",b ^ ans);
}
return 0;
}


posted @ 2018-04-18 14:27  Mychael  阅读(138)  评论(0编辑  收藏  举报