Recursive Queries CodeForces - 1117G (线段树)
题面:
刚开始想复杂了, 还以为是个笛卡尔树....
实际上我们发现, 对于询问(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 (线段树)的更多相关文章
- Mass Change Queries CodeForces - 911G (线段树合并)
链接 大意: 给定序列, 每次操作将区间[l,r]中的x全改为y, 最后输出序列 权值范围比较小, 对每个权值开一颗线段树, 每次将x合并到y上即可 #include <iostream> ...
- [Codeforces 266E]More Queries to Array...(线段树+二项式定理)
[Codeforces 266E]More Queries to Array...(线段树+二项式定理) 题面 维护一个长度为\(n\)的序列\(a\),\(m\)个操作 区间赋值为\(x\) 查询\ ...
- Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论
Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...
- Can you answer these queries? HDU 4027 线段树
Can you answer these queries? HDU 4027 线段树 题意 是说有从1到编号的船,每个船都有自己战斗值,然后我方有一个秘密武器,可以使得从一段编号内的船的战斗值变为原来 ...
- Codeforces 1114F Please, another Queries on Array? 线段树
Please, another Queries on Array? 利用欧拉函数的计算方法, 用线段树搞一搞就好啦. #include<bits/stdc++.h> #define LL ...
- Please, another Queries on Array? CodeForces - 1114F (线段树,欧拉函数)
这题刚开始看成求区间$\phi$和了........先说一下区间和的做法吧...... 就是说将题目的操作2改为求$(\sum\limits_{i=l}^{r}\phi(a[i]))\%P$ 首先要知 ...
- Codeforces 938G 线段树分治 线性基 可撤销并查集
Codeforces 938G Shortest Path Queries 一张连通图,三种操作 1.给x和y之间加上边权为d的边,保证不会产生重边 2.删除x和y之间的边,保证此边之前存在 3.询问 ...
- CodeChef DISTNUM2 Easy Queries 节点数组线段树
Description You are given an array A consisting of N positive integers. You have to answer Q queries ...
- 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 ...
随机推荐
- java网络基础知识的简述
TCP/UDP的介绍 TCP协议:面向连接的,字节流无差错地传输协议. UDP协议:一个不可靠的无连接的数据传输协议. 说明:TCP可以想象成电话通讯,双方在通话时必须建立连接,一方没听清,会要求对方 ...
- python进程编程
多进程multiprocess模块 multiprocessing is a package that supports spawning processes using an API similar ...
- python中hasattr, getattr,setattr及delattr四个方法
通过一个实例来说明,这四个函数的用法: 首先一个如下的一个简单的类: class Animal(object): def __init__(self,name, zone): self.name = ...
- Navicat连不上mysql8
今天使用navicat连接mysql8,发现错误连连 错误1:1130-Host '192.168.50.2' is not allowed to connect to this MySQL serv ...
- 根据wsdl文件,soupUI生成webservice客户端代码
根据wsdl文件,soupUI生成webservice客户端代码 功能介绍: 对于面向WebServie接口开发时,当我们已经获取到WSDL文件后,可以使用soapUI工具生成对应的客户端和服务端代码 ...
- apt-get build-dep
apt-get 里面有个 build-dep参数,手册写着:build-dep causes apt-get to install/remove packages in an attempt to s ...
- 【转】iOS学习之iOS禁止Touch事件
iOS程序中有时会有需要禁止应用接收Touch的要求(比如动画进行时,防止触摸事件触发新方法). 一.一般有两种: 1.弄个遮罩层,禁止交互: 2.使用UIApplication中的方法进行相关的交互 ...
- 51NOD 1133 不重叠的线段
1133 不重叠的线段 X轴上有N条线段,每条线段有1个起点S和终点E.最多能够选出多少条互不重叠的线段.(注:起点或终点重叠,不算重叠). 例如:[1 5][2 3][3 6],可以选[2 ...
- label表单的关联性
<input type="checkbox" id="cr" /> <label for="cr">点击关联复选框& ...
- EF、Repository、Factory、Service间关系
EF和Repository 实体(Entities):具备唯一ID,能够被持久化,具备业务逻辑,对应现实世界业务对象. 值对象(Value objects):不具有唯一ID,由对象的属性描述,一般为内 ...