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个数 然后你需要 ...
随机推荐
- 【noip 2011】提高组Day1T3.Mayan游戏
Description Mayan puzzle是最近流行起来的一个游戏.游戏界面是一个7行5列的棋盘,上面堆放着一些方块,方块不能悬空堆放,即方块必须放在最下面一行,或者放在其他方块之上.游戏通关是 ...
- python中yield的用法
---"在python中,当你定义一个函数,使用了yield关键字时,这个函数就是一个生成器" (也就是说,只要有yield这个词出现,你在用def定义函数的时候,系统默认这就不是 ...
- Hibernate持久化
一.主键生成策略 1)主键分类:1.自然主键:主键本身就是表中的某一个字段,实体中的一个具体属性,对象本身唯一的特性. 例如:创建一个学生,把其身份证号设为主键 2.代理主键:本身不是表中的一个必须的 ...
- 基于【字符】操作的IO接口:Writer、Reader
Reader public class BufferedReaderTest { public static void main(String[] args) throws IOException { ...
- Eclipse使用solrJ 7.7.0连接solr步骤
先写一个测试类: package com.taotao.rest.solrj; import org.apache.solr.client.solrj.SolrClient; import org.a ...
- python 三大框架之一Flask入门
Flask轻量级框架,Flask是python中的轻量级框架. 打开终端 输入pip install Flask 命令 下载以及安装Flask框架 检查是否下载成功及能否使用 首先导入python环境 ...
- 识别oracle数据库软件版本号
由于Oracle数据库不断发展并可能需要维护,因此Oracle会定期生成新版本.并非所有客户最初都订阅新版本或需要对其现有版本进行特定维护.因此,该产品的多个版本同时存在. 可能需要多达五个数字才能完 ...
- Django之Bootstrap使用
首先将bootstrap文件粘贴到static文件夹中,引入分为两部分,一是css文件引入,二是js文件引入. 1.css引入: <!DOCTYPE html> <html lang ...
- spring boot 笔记1
demon 目录,公共配置 SbMybatisApplication.java package com.example.sb_mybatis; import org.mybatis.spring.an ...
- 【转】python 面向对象(进阶篇)
[转]python 面向对象(进阶篇) 上一篇<Python 面向对象(初级篇)>文章介绍了面向对象基本知识: 面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 ...