Codeforces

洛谷:咕咕咕


思路

设\(L_i,R_i\)为\(i\)左右第一个大于它的位置。

对于每一个询问\(l,r\),考虑区间每一个位置的贡献就是\(\min(r,R_i-1)-\max(l,L_i+1)+1\),加起来就是

\[r-l+1+\sum_{i=l}^r \min(r,R_i-1)-\sum_{i=l}^r \max(l,L_i+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 [线段树]的更多相关文章

  1. Codeforces 145E Lucky Queries 线段树

    Lucky Queries 感觉是很简单的区间合并, 但是好像我写的比较麻烦. #include<bits/stdc++.h> #define LL long long #define f ...

  2. Buses and People CodeForces 160E 三维偏序+线段树

    Buses and People CodeForces 160E 三维偏序+线段树 题意 给定 N 个三元组 (a,b,c),现有 M 个询问,每个询问给定一个三元组 (a',b',c'),求满足 a ...

  3. CodeForces 877E DFS序+线段树

    CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...

  4. [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路)

    [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路) 题面 有n个空心物品,每个物品有外部体积\(out_i\)和内部体积\(in_i\),如果\(in_i& ...

  5. [Codeforces 1199D]Welfare State(线段树)

    [Codeforces 1199D]Welfare State(线段树) 题面 给出一个长度为n的序列,有q次操作,操作有2种 1.单点修改,把\(a_x\)修改成y 2.区间修改,把序列中值< ...

  6. [Codeforces 316E3]Summer Homework(线段树+斐波那契数列)

    [Codeforces 316E3]Summer Homework(线段树+斐波那契数列) 顺便安利一下这个博客,给了我很大启发(https://gaisaiyuno.github.io/) 题面 有 ...

  7. 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 ...

  8. [CodeForces - 678F] Lena and Queries 线段树维护凸包

    大致题意: 给出三种操作 1.往平面点集中添加一个点 2.删除第i次添加的点 3.给出一个q,询问平面点集中的q*x+y的最大值 首先对于每个询问,可将z=q*x+y转化为y=z-q*x,即过点(x, ...

  9. Codeforces Gym 100231B Intervals 线段树+二分+贪心

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...

随机推荐

  1. NFine框架JqGrid导出选中行为Excel实现方法

    客户端 function PostAndGetFileByUrl(url,type,postdata) { var temp; $.ajax({ url: url, type: type, data: ...

  2. luogu 1314 聪明的质检员

    二分答案的边界问题还是要注意 double挨着,int+1-1, 此题用到long long,所以初始化ans要足够大,前缀和优化 依然根据check答案大小左右mid,虽然有s,但是有了+1-1加持 ...

  3. 多线程this逃逸

    this逃逸,  是指在构造函数返回之前,其它线程就持有该对象的引用,调用尚未构造完全的对象的方法,可能引发令人疑惑的错误,应该避免this逃逸事件的发生. this逃逸经常发生在构造函数中启动线程或 ...

  4. clam安装

    nodejs下,npm安装clam指令: npm  install  -g  clam

  5. excel数据处理,公式

    1. 替换 SUBSTITUTE(字符串, 原字符串, 新字符串) =SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(L2,"镇",""),& ...

  6. Python 爬虫六 性能相关

    前面已经讲过了爬虫的两大基础模块: requests模块:用来伪造请求爬取数据 bs4模块:用来整理,提取数据 当我们真正的开始有需求的时候通常都是批量爬取url这样的.那如何批量爬取呢? 按照正常的 ...

  7. B - Tree Rotations HYSBZ - 2212 (线段树合并)

    题目链接:https://cn.vjudge.net/contest/287725#problem/B 题目大意:现在有一棵二叉树,所有非叶子节点都有两个孩子.在每个叶子节点上有一个权值(有n个叶子节 ...

  8. python - 爬虫入门练习 爬取链家网二手房信息

    import requests from bs4 import BeautifulSoup import sqlite3 conn = sqlite3.connect("test.db&qu ...

  9. Javascript - ExtJs - 事件

    事件(ExtJs Event) Ext.Util.observable类  Ext.Util.observable是一个接口,为Ext组件的事件提供了支持,组件的事件不同于传统事件,所以需要有这么一套 ...

  10. shellb编程 之 实践出真知

    1.查询file1 里面空行的所在行号 纯空行:awk ‘{if($0~/^$/)print NR}’ file 空行和带空格,制表符等的行:awk '$0~/^\s*$/' file 2.查询fil ...