题面:

刚开始想复杂了, 还以为是个笛卡尔树....

实际上我们发现, 对于询问(l,r)每个点的贡献是$min(r,R[i])-max(l,L[i])+1$

数据范围比较大在线树套树的话明显过不了, 还是想离线的算法好了, 只考虑求$\sum min(r,R[i])$, 对于$\sum max(l,L[i])$同理

将询问按$l$从大到小排, 将点$x$的贡献转化为$[x,R[x]-1]$区间加等差, $[R[x],n]$区间加$R[x]$, 这样$\sum min(r,R[i])$就变成对位置$r$单点求和了

离线的代码如下

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head const int N = 1e6+10;
int n, q;
int a[N], L[N], R[N];
struct _ {int l,r,id;} e[N];
int v1[N<<2], t1[N<<2];
ll v2[N<<2], t2[N<<2], ans[N]; void pd1(int o) {
if (t1[o]) {
v1[lc]+=t1[o];
v1[rc]+=t1[o];
t1[lc]+=t1[o];
t1[rc]+=t1[o];
t1[o]=0;
}
}
void pd2(int o) {
if (t2[o]) {
v2[lc]+=t2[o];
v2[rc]+=t2[o];
t2[lc]+=t2[o];
t2[rc]+=t2[o];
t2[o]=0;
}
}
void upd1(int o, int l, int r, int ql, int qr, int v) {
if (ql<=l&&r<=qr) return v1[o]+=v,t1[o]+=v,void();
pd1(o);
if (mid>=ql) upd1(ls,ql,qr,v);
if (mid<qr) upd1(rs,ql,qr,v);
}
void upd2(int o, int l, int r, int ql, int qr, int v) {
if (ql<=l&&r<=qr) return v2[o]+=v,t2[o]+=v,void();
pd2(o);
if (mid>=ql) upd2(ls,ql,qr,v);
if (mid<qr) upd2(rs,ql,qr,v);
}
ll qry1(int o, int l, int r, int x) {
if (l==r) return (ll)l*v1[o];
pd1(o);
if (mid>=x) return qry1(ls,x);
return qry1(rs,x);
}
ll qry2(int o, int l, int r, int x) {
if (l==r) return (ll)v2[o];
pd2(o);
if (mid>=x) return qry2(ls,x);
return qry2(rs,x);
} int main() {
scanf("%d%d", &n, &q);
REP(i,1,n) scanf("%d", a+i);
REP(i,1,n) {
L[i] = i-1;
while (L[i]&&a[i]>a[L[i]]) L[i]=L[L[i]];
}
PER(i,1,n) {
R[i] = i+1;
while (R[i]<=n&&a[i]>a[R[i]]) R[i]=R[R[i]];
}
REP(i,1,n) ++L[i],--R[i];
REP(i,1,q) scanf("%d", &e[i].l);
REP(i,1,q) scanf("%d", &e[i].r),e[i].id=i;
sort(e+1,e+1+q,[](_ a,_ b){return a.l>b.l;});
int now = n;
REP(i,1,q) {
while (now>=e[i].l) {
if (now<=R[now]-1) upd1(1,1,n,now,R[now]-1,1);
upd2(1,1,n,R[now],n,R[now]);
--now;
}
ans[e[i].id] += qry1(1,1,n,e[i].r)+qry2(1,1,n,e[i].r)+e[i].r-e[i].l+1;
}
memset(v1,0,sizeof v1);
memset(v2,0,sizeof v2);
memset(t1,0,sizeof t1);
memset(t2,0,sizeof t2);
sort(e+1,e+1+q,[](_ a,_ b){return a.r<b.r;});
now = 1;
REP(i,1,q) {
while (now<=e[i].r) {
if (L[now]+1<=now) upd1(1,1,n,L[now]+1,now,1);
upd2(1,1,n,1,L[now],L[now]);
++now;
}
ans[e[i].id] -= qry1(1,1,n,e[i].l)+qry2(1,1,n,e[i].l);
}
REP(i,1,q) printf("%lld ", ans[i]);hr;
}

在线的代码如下, RE on test 8.

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc TL[o]
#define rc TR[o]
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head const int N = 1e6+10, M = 1e7+10;
int n, q, tot;
int a[N], L[N], R[N], ql[N], qr[N];
int cntL[N], cntR[N], sumL[N], sumR[N];
int TL[M], TR[M];
ll val[M]; void add(int &o, int l, int r, int x, int v) {
if (!o) o=++tot;
val[o] += v;
if (l==r) return;
if (mid>=x) add(ls,x,v);
else add(rs,x,v);
}
ll query(int o, int l, int r, int ql, int qr) {
if (!o) return 0;
if (ql<=l&&r<=qr) return val[o];
ll ans = 0;
if (mid>=ql) ans += query(ls,ql,qr);
if (mid<qr) ans += query(rs,ql,qr);
return ans;
} int main() {
scanf("%d%d", &n, &q);
REP(i,1,n) scanf("%d", a+i);
REP(i,1,n) {
L[i] = i-1;
while (L[i]&&a[i]>a[L[i]]) L[i]=L[L[i]];
}
PER(i,1,n) {
R[i] = i+1;
while (R[i]<=n&&a[i]>a[R[i]]) R[i]=R[R[i]];
}
REP(i,1,n) ++L[i],--R[i];
REP(i,1,n) {
for (int j=R[i]; j<=n; j+=j&-j) {
add(cntR[j],1,n,i,1);
add(sumR[j],1,n,i,R[i]);
}
for (int j=L[i]; j; j^=j&-j) {
add(cntL[j],1,n,i,1);
add(sumL[j],1,n,i,L[i]);
}
}
REP(i,1,q) scanf("%d", ql+i);
REP(i,1,q) scanf("%d", qr+i);
REP(i,1,q) {
ll sumr=0,suml=0,cntr=0,cntl=0;
int l=ql[i], r=qr[i];
for (int j=r; j; j^=j&-j) {
sumr += query(sumR[j],1,n,l,r);
cntr += query(cntR[j],1,n,l,r);
}
for (int j=l; j<=n; j+=j&-j) {
suml += query(sumL[j],1,n,l,r);
cntl += query(cntL[j],1,n,l,r);
}
ll ans = (ll)(r-l+1-cntr)*r+sumr;
ans -= (ll)(r-l+1-cntl)*l+suml;
ans += r-l+1;
printf("%lld ", ans);
} hr;
}

Recursive Queries CodeForces - 1117G (线段树)的更多相关文章

  1. Mass Change Queries CodeForces - 911G (线段树合并)

    链接 大意: 给定序列, 每次操作将区间[l,r]中的x全改为y, 最后输出序列 权值范围比较小, 对每个权值开一颗线段树, 每次将x合并到y上即可 #include <iostream> ...

  2. [Codeforces 266E]More Queries to Array...(线段树+二项式定理)

    [Codeforces 266E]More Queries to Array...(线段树+二项式定理) 题面 维护一个长度为\(n\)的序列\(a\),\(m\)个操作 区间赋值为\(x\) 查询\ ...

  3. Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论

    Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...

  4. Can you answer these queries? HDU 4027 线段树

    Can you answer these queries? HDU 4027 线段树 题意 是说有从1到编号的船,每个船都有自己战斗值,然后我方有一个秘密武器,可以使得从一段编号内的船的战斗值变为原来 ...

  5. Codeforces 1114F Please, another Queries on Array? 线段树

    Please, another Queries on Array? 利用欧拉函数的计算方法, 用线段树搞一搞就好啦. #include<bits/stdc++.h> #define LL ...

  6. Please, another Queries on Array? CodeForces - 1114F (线段树,欧拉函数)

    这题刚开始看成求区间$\phi$和了........先说一下区间和的做法吧...... 就是说将题目的操作2改为求$(\sum\limits_{i=l}^{r}\phi(a[i]))\%P$ 首先要知 ...

  7. Codeforces 938G 线段树分治 线性基 可撤销并查集

    Codeforces 938G Shortest Path Queries 一张连通图,三种操作 1.给x和y之间加上边权为d的边,保证不会产生重边 2.删除x和y之间的边,保证此边之前存在 3.询问 ...

  8. CodeChef DISTNUM2 Easy Queries 节点数组线段树

    Description You are given an array A consisting of N positive integers. You have to answer Q queries ...

  9. spoj gss2 : Can you answer these queries II 离线&&线段树

    1557. Can you answer these queries II Problem code: GSS2 Being a completist and a simplist, kid Yang ...

随机推荐

  1. 使用token实现在有效期内APP自动登录功能

    实现此功能的场景是在当下用户对手机APP体验要求高,并且相对安全前提的推动下诞生:当你下载了一个QQ,微信第一次进行了账号和密码的登录,你从此以后打开应用免去了你每日打开应用都要输入账号跟密码的痛苦过 ...

  2. C 运算优先级口诀

    运算优先级口诀: 括号成员第一;        //括号运算符[]() 成员运算符.  ->  全体单目第二;        //所有的单目运算符比如!.~.++. --. +(正). -(负) ...

  3. 20145101 《Java程序设计》第7周学习总结

    20145101<Java程序设计>第7周学习总结 教材学习内容总结 第十二章 Lambda Lambda表达式中this的参考对象以及toString()的接受者,是来自Lambda的周 ...

  4. tensorflow之神经网络实现流程总结

    tensorflow之神经网络实现流程总结 1.数据预处理preprocess 2.前向传播的神经网络搭建(包括activation_function和层数) 3.指数下降的learning_rate ...

  5. Easyui 官网网址

    http://www.jeasyui.com/download/list.php 下载版本1.5.2的easyui中文API,可在CSDN网站http://download.csdn.net/down ...

  6. LTE-A 载波聚合(Carrier Aggregation)介绍【转】

    本文转自:https://blog.csdn.net/txgc1009/article/details/46467255 载波聚合(Carrier Aggregation) 首先介绍几个基本概念 Pr ...

  7. HDU 1848 Fibonacci again and again(SG函数入门)题解

    思路:SG打表 参考:SG函数和SG定理[详解] 代码: #include<queue> #include<cstring> #include<set> #incl ...

  8. zedgraph右键菜单的汉化

    http://blog.csdn.net/jeryler/article/details/7876376 修改 zedGraphControl的ContextMenuBuilder事件即可! zedG ...

  9. 【基础配置】Dubbo的配置及使用

    1. Dubbo是什么? Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案.简单的说,dubbo就是个服务框架,如果没有分布式的需求,其实是不需 ...

  10. Java8的新特性,二进制序列转十进制数字

    package kata_007_二进制序列转十进制int; /** * java8 Lambda表达式转换binary序列->十进制数 */ import java.util.ArrayLis ...