扫描线:按照其中一个区间的标记为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 ;
}

Helter Skelter (扫描线 + 离散化 + 树状数组)的更多相关文章

  1. CodeForces 540E - Infinite Inversions(离散化+树状数组)

    花了近5个小时,改的乱七八糟,终于A了. 一个无限数列,1,2,3,4,...,n....,给n个数对<i,j>把数列的i,j两个元素做交换.求交换后数列的逆序对数. 很容易想到离散化+树 ...

  2. Ultra-QuickSort(归并排序+离散化树状数组)

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 50517   Accepted: 18534 ...

  3. HDU 5862 Counting Intersections(离散化+树状数组)

    HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...

  4. BZOJ_4627_[BeiJing2016]回转寿司_离散化+树状数组

    BZOJ_4627_[BeiJing2016]回转寿司_离散化+树状数组 Description 酷爱日料的小Z经常光顾学校东门外的回转寿司店.在这里,一盘盘寿司通过传送带依次呈现在小Z眼前.不同的寿 ...

  5. poj-----Ultra-QuickSort(离散化+树状数组)

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 38258   Accepted: 13784 ...

  6. Code Forces 652D Nested Segments(离散化+树状数组)

     Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  7. hdu 3015 Disharmony Trees (离散化+树状数组)

    Disharmony Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. 【bzoj4627】[BeiJing2016]回转寿司 离散化+树状数组

    题目描述 给出一个长度为n的序列,求所有元素的和在[L,R]范围内的连续子序列的个数. 输入 第一行包含三个整数N,L和R,分别表示寿司盘数,满意度的下限和上限. 第二行包含N个整数Ai,表示小Z对寿 ...

  9. HDU 6318.Swaps and Inversions-求逆序对-线段树 or 归并排序 or 离散化+树状数组 (2018 Multi-University Training Contest 2 1010)

    6318.Swaps and Inversions 这个题就是找逆序对,然后逆序对数*min(x,y)就可以了. 官方题解:注意到逆序对=交换相邻需要交换的次数,那么输出 逆序对个数 即可. 求逆序对 ...

随机推荐

  1. shiro入门实例,基于ini配置

    基于ini或者关系数据库的,其实都是一样的,重要的是思想. # ==================================================================== ...

  2. Django+Celery 执行异步任务和定时任务

    celery是一个基于python开发的简单.灵活且可靠的分布式任务队列框架,支持使用任务队列的方式在分布式的机器/进程/线程上执行任务调度.采用典型的生产者-消费者模型,主要由三部分组成: 1. 消 ...

  3. 使用Maven根据WSDL生成生成Java代码

    转载:https://blog.csdn.net/pzasdq/article/details/52601473 为便于自己学习,整理 修改pom.xml <project xmlns=&quo ...

  4. Jenkins send build artifacts over ssh配置

    配置jenkins远程部署的时候遇到的配置问题: 首先在系统设置-系统设置-Publish over SSH-SSH Server中配置服务器信息 配置完成后可以点击Test Configuratio ...

  5. JavaScript的cookie和sessionStorage 、localStorage

    localStorage.sessionStorage和cookie的区别与用法请见下面的博客: https://segmentfault.com/a/1190000012057010 cookie的 ...

  6. oracle中is和as的区别

    在存储过程(PROCEDURE)和函数(FUNCTION)中没有区别:在视图(VIEW)中只能用AS不能用IS:在游标(CURSOR)中只能用IS不能用AS.

  7. (1.6)MySQL执行计划

    关键词:mysql执行计划 1.用法 [1.1]explain select * from tab_name........ [1.2]desc select * from tab_name..... ...

  8. Windows下用户变量和系统变量

    环境变量分为用户变量和系统变量. 系统变量,对所有用户起作用;而用户环境变量只对当前用户起作用. 例如你要用java,那么你把java的bin目录加入到path变量下面,那么它就是系统环境变量,所用用 ...

  9. axios的使用

    一.首先要安装axios npm install axios 使用: -先在main中配置: import axios from 'axios' //要把axios放进一个全局变量中 //把axios ...

  10. abap form格式的异常处理

    1: form 中有异常时,需要用 raising 将异常抛出. *&------------------------------------------------------------- ...