扫描线:按照其中一个区间的标记为pos,然后左区间标记d为正影响,有区间标记d为负影响,然后根据所有的pos排序。pos从小扫到大,那么对于某一个区间一定会被扫过2次,那么经过2次之后就只剩下中间那一段的影响了,但是先提前扫过去的话后面的区间就会影响到前面的区间,反过来却不会。所以求答案的时候也是按照查询区间的左部那么我只需要一边录入数据,一边求就可以了。
然后对于1的区间只需要用一个线段树/树状数组就可以将判断是否符合了,还有就是树状数组离散化之后对速度有些许帮助。
#include<bits/stdc++.h>
using namespace std; const int maxm = 1e6 + ;
int in[maxm], discre[maxm << ], tr[maxm << ];
int l, r, L, R, opL, disL;
char ans[maxm]; void TreeAdd(int rt, int num){
while(rt <= disL){
tr[rt] += num;
rt = rt + (rt & -rt);
}
} int TreeSum(int rt){
int ret = ;
while(rt){
ret += tr[rt];
rt = rt - (rt & -rt);
}
return ret;
} struct OP{
int pos, d, L, R;
bool operator < (const OP& a)const{
return pos < a.pos;
}
}op[maxm << ]; struct QUERY{
int a, b, id;
bool operator < (const QUERY& A)const{
return a < A.a;
}
}query[maxm]; void join(){
op[opL].d = ;op[opL].pos = l;
op[opL].L = L;op[opL ++].R = R;
op[opL].d = -;op[opL].pos = r + ;
op[opL].L = L;op[opL ++].R = R;
discre[disL ++] = L;
discre[disL ++] = R;
} int main(){
int T, n, m, k;scanf("%d", &T);
while(T --){
k = disL = opL = ;
scanf("%d%d",&n, &m);
for(int i = ; i <= n; i ++)
scanf("%d",&in[i]);
for(int i = ; i < m; i ++){
scanf("%d%d",&query[i].a, &query[i].b), query[i].id = i;
discre[disL ++] = query[i].b;
}
sort(query, query + m);
for(int i = ; i <= n; i ++){
///单区间
if(i & ){l = , r = in[i], L = R = ; join();}
else {l = r = , L = , R = in[i]; join();}
///双区间
if(i & && i < n){l = L = ; r = in[i]; R = in[i + ]; join();}
else if(i < n) {l = L = ; r = in[i + ]; R = in[i]; join();}
///多区间
l = r = L = R = ;
for(int j = i + ; j < n; j ++){
l += ((j & ) ? in[j] : );
L += ((j & ) ? : in[j]);
r = l + ((i & ) ? in[i] : ) + ((j & ) ? : in[j + ]);
R = L + ((i & ) ? : in[i]) + ((j & ) ? in[j + ] : );
join();
}
}
sort(op, op + opL);
sort(discre, discre + disL);
disL = unique(discre, discre + disL) - discre;
for(int i = ; i <= disL; i ++) tr[i] = ;
for(int i = ; i < m; i ++){
for(;k < opL && query[i].a >= op[k].pos; k ++){
int ll = lower_bound(discre, discre + disL, op[k].L) - discre + ;
int rr = lower_bound(discre, discre + disL, op[k].R) - discre + ;
TreeAdd(ll, op[k].d); TreeAdd(rr + , - * op[k].d);
}
int qq = lower_bound(discre, discre + disL, query[i].b) - discre + ;
ans[query[i].id] = '' + (TreeSum(qq) > );
}
ans[m] = ;
printf("%s\n",ans);
}
return ;
}