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个数 然后你需要 ...
随机推荐
- getaddrinfo函数
一.功能 对于IPv4和IPv6均适用,可以处理名字到地址以及服务到端口这两种变换,返回的是一个sockaddr结构而不是一个地址队列 二.函数原型 #include <netdb.h> ...
- 多态(upcast)减少分支判断 以及 多态继承设计、具体类型判断。
Influenced by <java 八荣八耻>,翻了下<java编程思想> 印象中多态产生的条件:1.子类继承父类 2.父类[指针]指向子类 3.父类引用调用重写(@Ove ...
- seleniums私房菜系列一 ---- selenium简介
一.Selenium是什么? Selenium是ThroughtWorks公司一个强大的开源Web功能测试工具系列,本系列现在主要包括以下4款: 1.Selenium Core:支持DHTML的测试案 ...
- promise.all方法合并请求接口的两个值
function promise1 = new Promise((resolve,reject)=>{ return result1 }) function promise2 = new Pro ...
- 第26月第28天 avplayer cache
1.urlsession https - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticati ...
- Hive通过mysql元数据表删除分区
1 创建表 hive命令行操作 CREATE TABLE IF NOT EXISTS emp( name STRING, salary FLOAT, subordinates ARRAY<STR ...
- 高程小tips
1.DOM操作往往是JS程序中开销最大的部分,应尽量减少DOM操作.-P285 P297例子 2.元素的classList属性: 元素的classLis即该元素的class的值的集合,是一个列表(数 ...
- cmder使用简介
简介 cmder是一个增强型命令行工具,不仅可以使用windows下的所有命令,更爽的是可以使用linux的命令,shell命令. 下载 官网地址:http://cmder.net/ 下载的时候,会有 ...
- valueForKeyPath用途
可能大家对- (id)valueForKeyPath:(NSString *)keyPath方法不是很了解. 其实这个方法非常的强大,举个例子: NSArray *array = @[@"n ...
- spring-session+Redis实现Session共享
关于session共享的方式有多种: (1)通过nginx的ip_hash,根据ip将请求分配到对应的服务器 (2)基于关系型数据库存储 (3)基于cookie存储 (4)服务器内置的session复 ...