题面:

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

实际上我们发现, 对于询问(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. excel选择元角分下拉菜单选择框自动变更数字

    excel选择元角分下拉菜单选择框自动变更数字 (M2列),数据-->数据有效性-->在“允许”栏中选择序列-->在“来源”栏中输入:分,角,元单位倍数公式(M4列):=IF(M2= ...

  2. oracle_存储过程小记

    # 刷新会员标签函数 {color:red} fun_refresh_code{color} {noformat}CREATE OR REPLACE FUNCTION fun_refresh_code ...

  3. 根据wsdl文件,Java工程自动生成webservice客户端调用

    根据wsdl文件,Java工程自动生成webservice客户端调用 1,工具:带有webservice插件的myeclips 2,步骤: (1),新建一个Java工程:relationship (2 ...

  4. QtQuickcontrols2控件使用参考

    随着Qt的版本升级,其自带的controls控件库也不断升级,目前已经到了2.3的版本.本文通过解读Qt自带的gallery例程,说明新版本controls控件库的相关特性.其具体位置于: 因为相关的 ...

  5. 20145122 《Java程序设计》第8周学习总结

    教材学习内容总结 1.NIO使用频道(channel)来衔接数据节点,对数据区的标记提供了clear(),rewind(),flip(),compact()等高级操作. 2.想要取得channel的操 ...

  6. 20165310 java_blog_week4

    2165310 <Java程序设计>第4周学习总结 教材学习内容总结 继承(extends) 同一个包内:继承除了private修饰的变量与方法 不同包内:不继承private和友好,继承 ...

  7. payload

    payload就是协议报文中的有效载荷所占报文的百分比,用报文中去除协议的长度/报文总长度,协议设计的时候需要考虑到有效载荷所占的比重,避免出现payload很小的情况,比如TCP在设计的时候,就考虑 ...

  8. Java求两个数平均值

    如何正确的求2个数的平均值.在练习算法二分查找的时候发现的,以前没有注意到的bug 备注:数据以int类型为例 一.以前的通用写法 /** * 求a+b平均值 * @param a * @param ...

  9. Wannafly14挑战赛 C(tarjan缩点)题解

    题目:牛客题目链接 思路:这道题有点像这道题 先缩点,缩完之后判断一下整个强连通分量入度是不是0,如果是的话向ans压入该强连通分量最小的那个值.最后排序一下ans输出就行了. 思路一下就想到了,就是 ...

  10. Windows 上安装 pip

    1 从  https://pypi.python.org/pypi/pip#downloads  下载安装包 pip-9.0.1.tar.gz 2 解压 pip-9.0.1.tar.gz 3 用CMD ...