Codeforces 1117G Recursive Queries [线段树]
洛谷:咕咕咕
思路
设\(L_i,R_i\)为\(i\)左右第一个大于它的位置。
对于每一个询问\(l,r\),考虑区间每一个位置的贡献就是\(\min(r,R_i-1)-\max(l,L_i+1)+1\),加起来就是
\]
后面那两个东西显然可以差分后用线段树搞。
代码
#include<bits/stdc++.h>
clock_t t=clock();
namespace my_std{
using namespace std;
#define pil pair<int,ll>
#define fir first
#define sec second
#define MP make_pair
#define rep(i,x,y) for (int i=(x);i<=(y);i++)
#define drep(i,x,y) for (int i=(x);i>=(y);i--)
#define go(x) for (int i=head[x];i;i=edge[i].nxt)
#define sz 1010010
#define templ template<typename T>
typedef long long ll;
typedef double db;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
templ inline T rnd(T l,T r) {return uniform_int_distribution<T>(l,r)(rng);}
templ inline bool chkmax(T &x,T y){return x<y?x=y,1:0;}
templ inline bool chkmin(T &x,T y){return x>y?x=y,1:0;}
templ inline void read(T& t)
{
t=0;char f=0,ch=getchar();double d=0.1;
while(ch>'9'||ch<'0') f|=(ch=='-'),ch=getchar();
while(ch<='9'&&ch>='0') t=t*10+ch-48,ch=getchar();
if(ch=='.'){ch=getchar();while(ch<='9'&&ch>='0') t+=d*(ch^48),d*=0.1,ch=getchar();}
t=(f?-t:t);
}
template<typename T,typename... Args>inline void read(T& t,Args&... args){read(t); read(args...);}
char sr[1<<21],z[20];int C=-1,Z=0;
inline void Ot(){fwrite(sr,1,C+1,stdout),C=-1;}
inline void print(register int x)
{
if(C>1<<20)Ot();if(x<0)sr[++C]='-',x=-x;
while(z[++Z]=x%10+48,x/=10);
while(sr[++C]=z[Z],--Z);sr[++C]='\n';
}
void file()
{
#ifndef ONLINE_JUDGE
freopen("a.in","r",stdin);
#endif
}
inline void chktime()
{
#ifndef ONLINE_JUDGE
cout<<(clock()-t)/1000.0<<'\n';
#endif
}
#ifdef mod
ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x%mod) if (y&1) ret=ret*x%mod;return ret;}
ll inv(ll x){return ksm(x,mod-2);}
#else
ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x) if (y&1) ret=ret*x;return ret;}
#endif
// inline ll mul(ll a,ll b){ll d=(ll)(a*(double)b/mod+0.5);ll ret=a*b-d*mod;if (ret<0) ret+=mod;return ret;}
}
using namespace my_std;
int n,m;
int a[sz];
int L[sz],R[sz];
const int Tree=sz<<2;
struct TREE
{
ll sum[Tree];
int cnt[Tree];
#define ls k<<1
#define rs k<<1|1
#define lson k<<1,l,mid
#define rson k<<1|1,mid+1,r
void insert(int k,int l,int r,ll x)
{
++cnt[k];sum[k]+=x;
if (l==r) return;
int mid=(l+r)>>1;
if (x<=mid) insert(lson,x);
else insert(rson,x);
}
pil query(int k,int l,int r,int x,int y)
{
if (x>r||y<l) return MP(0,0);
if (x<=l&&r<=y) return MP(cnt[k],sum[k]);
int mid=(l+r)>>1;
pil L=query(lson,x,y),R=query(rson,x,y);
return MP(L.fir+R.fir,L.sec+R.sec);
}
pil query(int l,int r){return query(1,0,n+1,l,r);}
}l,r;
int LL[sz],RR[sz];
int id[sz];
inline bool cmp(const int &x,const int &y){return a[x]<a[y];}
void init()
{
rep(i,1,n) LL[i]=i-1,RR[i]=i+1,id[i]=i;
RR[0]=1;LL[n+1]=n;
sort(id+1,id+n+1,cmp);
int x;
rep(i,1,n) x=id[i],L[x]=LL[x],R[x]=RR[x],RR[LL[x]]=RR[x],LL[RR[x]]=LL[x];
}
int ql[sz],qr[sz];
ll Ans[sz];
struct hh{int x,l,r,id,v;}q[sz<<1];
inline bool cmpp(const hh &x,const hh &y){return x.x<y.x;}
signed main()
{
file();
read(n,m);
rep(i,1,n) read(a[i]);
init();
int x,y;
rep(i,1,m) read(ql[i]); rep(i,1,m) read(qr[i]);
rep(i,1,m) q[i*2-1]=(hh){ql[i]-1,ql[i],qr[i],i,-1},q[i*2]=(hh){qr[i],ql[i],qr[i],i,1};
sort(q+1,q+m+m+1,cmpp);
int p=1;
while (!q[p].x) ++p;
rep(i,1,n)
{
if (p>m*2) break;
l.insert(1,0,n+1,L[i]+1),r.insert(1,0,n+1,R[i]-1);
while (p<=m*2&&q[p].x==i)
{
x=q[p].l,y=q[p].r;
ll ans=0;
pil a,b;
a=r.query(y+1,n+1),b=r.query(0,y);
ans+=1ll*y*a.fir+b.sec;
a=l.query(0,x-1),b=l.query(x,n+1);
ans-=1ll*x*a.fir+b.sec;
Ans[q[p].id]+=1ll*q[p].v*ans;
++p;
}
}
rep(i,1,m) printf("%lld ",Ans[i]+1ll*(qr[i]-ql[i]+1));
return 0;
}
Codeforces 1117G Recursive Queries [线段树]的更多相关文章
- Codeforces 145E Lucky Queries 线段树
Lucky Queries 感觉是很简单的区间合并, 但是好像我写的比较麻烦. #include<bits/stdc++.h> #define LL long long #define f ...
- Buses and People CodeForces 160E 三维偏序+线段树
Buses and People CodeForces 160E 三维偏序+线段树 题意 给定 N 个三元组 (a,b,c),现有 M 个询问,每个询问给定一个三元组 (a',b',c'),求满足 a ...
- CodeForces 877E DFS序+线段树
CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...
- [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路)
[Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路) 题面 有n个空心物品,每个物品有外部体积\(out_i\)和内部体积\(in_i\),如果\(in_i& ...
- [Codeforces 1199D]Welfare State(线段树)
[Codeforces 1199D]Welfare State(线段树) 题面 给出一个长度为n的序列,有q次操作,操作有2种 1.单点修改,把\(a_x\)修改成y 2.区间修改,把序列中值< ...
- [Codeforces 316E3]Summer Homework(线段树+斐波那契数列)
[Codeforces 316E3]Summer Homework(线段树+斐波那契数列) 顺便安利一下这个博客,给了我很大启发(https://gaisaiyuno.github.io/) 题面 有 ...
- Codeforces Round #590 (Div. 3) D. Distinct Characters Queries(线段树, 位运算)
链接: https://codeforces.com/contest/1234/problem/D 题意: You are given a string s consisting of lowerca ...
- [CodeForces - 678F] Lena and Queries 线段树维护凸包
大致题意: 给出三种操作 1.往平面点集中添加一个点 2.删除第i次添加的点 3.给出一个q,询问平面点集中的q*x+y的最大值 首先对于每个询问,可将z=q*x+y转化为y=z-q*x,即过点(x, ...
- Codeforces Gym 100231B Intervals 线段树+二分+贪心
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...
随机推荐
- Android上禁止屏幕旋转
看网上讲了很多,设置很多属性,设置了很多,其实最关键的一点是这个 @Overrideprotected void onResume() { /** * 设置为横屏 */ if(getRequested ...
- Javaweb学习笔记——(十三)——————JSTL、JSTL核心标签库、自定义标签、有标签体的标签、带有属性的标签、MVC、Javaweb三层框架
JSTLApache提供的标签库 jar包:jstl-1.2.jar,如果传MyEclipse,他会在我们导入jar包,无需自己导入,如果没有使用MyEclipse那么需要自行导入.--------- ...
- 从零开始搭建轻量级个人XSS平台
一. 前言 决定搭建XSS平台是因为自己想深入学习一下XSS相关的知识,多多进行实践,上网搜索了一下XSS平台有很多,但是总觉得不是很安全,这个毕竟敏感信息要传输到陌生人的服务器上,而且服务器端测试代 ...
- 【51nod 1785】数据流中的算法
Description 51nod近日上线了用户满意度检测工具,使用高级人工智能算法,通过用户访问时间.鼠标轨迹等特征计算用户对于网站的满意程度. 现有的统计工具只能统计某一个窗口中,用户的满意程 ...
- Dapper.net Insert mssql unicode 乱码问题
1.效果: 2.处理方法: /// <summary> /// insert single sql /// </summary> /// <typeparam name= ...
- TensorFlow从入门到理解(一):搭建开发环境【基于Ubuntu18.04】
*注:教程及本文章皆使用Python3+语言,执行.py文件都是用终端(如果使用Python2+和IDE都会和本文描述有点不符) 一.安装,测试,卸载 TensorFlow官网介绍得很全面,很完美了, ...
- LOJ #6053. 简单的函数
$Min$_$25$筛模版题 为什么泥萌常数都那么小啊$ QAQ$ 传送门:Here 题意: $ f(1)=1$$ f(p^c)=p⊕c(p 为质数,⊕ 表示异或)$$ f(ab)=f(a)f(b)( ...
- 通过Java构造参数列表
背景:我们在进行性能测试时,需要构造测试数据,即参数化文件,如下: 上面的文件内容,我们可以通过Java代码轻松实现,主要代码解释: All 代码(其实我也看不懂,但是会改就行啦) package f ...
- const关键字的作用
1.防止被修饰的成员的内容被改变. 2.修饰类的成员函数时,表示其为一个常函数,意味着成员函数将不能修改类成员变量的值. 3.在函数声明时修饰参数,表示在函数访问时参数(包括指针和实参)的值不会发生变 ...
- P1456 Monkey King
题目地址:P1456 Monkey King 一道挺模板的左偏树题 不会左偏树?看论文打模板,完了之后再回来吧 然后你发现看完论文打完模板之后就可以A掉这道题不用回来了 细节见代码 #include ...