BZOJ4262: Sum
Description
.jpg)
Input
Output
Sample Input
1 3 5 7
2 4 6 8
1 1 9 9
9 9 1 1
Sample Output
9025304064
1065645568
0
HINT
1<=t<=40000,1<=L1<=R1<=10^5,1<=L2<=R2<=10^5
#include<cstdio>
#include<cctype>
#include<queue>
#include<cstring>
#include<algorithm>
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define ren for(int i=first[x];i;i=next[i])
using namespace std;
const int BufferSize=1<<16;
char buffer[BufferSize],*head,*tail;
inline char Getchar() {
if(head==tail) {
int l=fread(buffer,1,BufferSize,stdin);
tail=(head=buffer)+l;
}
return *head++;
}
inline int read() {
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
typedef long long ll;
const int maxn=200010;
const int mod=1000000000;
int A[maxn],Lmin[maxn],Rmin[maxn],Lmax[maxn],Rmax[maxn];
int S[maxn],Log[maxn],maxv[20][maxn],minv[20][maxn];
void init(int n) {
Log[0]=-1;
rep(i,1,n) Log[i]=Log[i>>1]+1,maxv[0][i]=minv[0][i]=A[i];
for(int j=1;(1<<j)<=n;j++)
for(int i=1;i+(1<<j)-1<=n;i++) {
maxv[j][i]=max(maxv[j-1][i],maxv[j-1][i+(1<<j-1)]);
minv[j][i]=min(minv[j-1][i],minv[j-1][i+(1<<j-1)]);
}
}
void query(int l,int r,int& mx,int& mn) {
if(l>r) mx=-1,mn=2147483647;
else {
int k=Log[r-l+1];
mx=max(maxv[k][l],maxv[k][r-(1<<k)+1]);
mn=min(minv[k][l],minv[k][r-(1<<k)+1]);
}
}
struct Line {
int l,r,mx,mn;
}L1[maxn],L2[maxn];
ll solve(int l1,int r1,int l2,int r2) {
if(l1>r1||l2>r2) return 0;
int mx,mn;query(r1,l2,mx,mn);ll ans=0;
int p,pmin,pmax,c1=0,c2=0;
pmin=r1;pmax=r1;p=r1;
while(p>=l1) {
int nxt=max(l1-1,max(Lmin[pmin],Lmax[pmax]));
L1[++c1]=(Line){nxt+1,p,A[pmax],A[pmin]};
p=nxt;if(A[p]>A[pmax]) pmax=p;else pmin=p;
}
pmin=l2;pmax=l2;p=l2;
while(p<=r2) {
int nxt=min(r2+1,min(Rmin[pmin],Rmax[pmax]));
L2[++c2]=(Line){p,nxt-1,A[pmax],A[pmin]};
p=nxt;if(A[p]>A[pmax]) pmax=p;else pmin=p;
}
rep(i,1,c1) rep(j,1,c2) ans+=(ll)(L1[i].r-L1[i].l+1)*(L2[j].r-L2[j].l+1)*(max(mx,max(L1[i].mx,L2[j].mx))-min(mn,min(L1[i].mn,L2[j].mn)));
return ans;
}
struct Node {
int l,r;ll sumv;
Node operator + (const Node& b) const {
Node c;c.l=l;c.r=b.r;
c.sumv=sumv+b.sumv+solve(l,r,b.l,b.r);
return c;
}
}T[maxn<<2],ans;
void build(int o,int l,int r) {
if(l==r) T[o].l=T[o].r=l,T[o].sumv=0;
else {
int mid=l+r>>1,lc=o<<1,rc=lc|1;
build(lc,l,mid);build(rc,mid+1,r);
T[o]=T[lc]+T[rc];
}
}
int flag;
void query(int o,int l,int r,int ql,int qr) {
if(ql<=l&&r<=qr) {
if(!flag) ans=T[o],flag=1;
else ans=ans+T[o];
}
else {
int mid=l+r>>1,lc=o<<1,rc=lc|1;
if(ql<=mid) query(lc,l,mid,ql,qr);
if(qr>mid) query(rc,mid+1,r,ql,qr);
}
}
int main() {
ll xp1=1,xp2=1;
rep(i,1,100000) {
(xp1*=1023)%=mod;
(xp2*=1025)%=mod;
A[i]=xp1^xp2;
}
init(100000);
int top=1;S[top]=0;A[0]=-1;
rep(i,1,100000) {
while(top&&A[i]<=A[S[top]]) top--;
Lmin[i]=S[top];S[++top]=i;
}
top=1;S[top]=0;A[0]=2147483647;
rep(i,1,100000) {
while(top&&A[i]>=A[S[top]]) top--;
Lmax[i]=S[top];S[++top]=i;
}
top=1;S[top]=100001;A[100001]=-1;
dwn(i,100000,1) {
while(top&&A[i]<=A[S[top]]) top--;
Rmin[i]=S[top];S[++top]=i;
}
top=1;S[top]=100001;A[100001]=2147483647;
dwn(i,100000,1) {
while(top&&A[i]>=A[S[top]]) top--;
Rmax[i]=S[top];S[++top]=i;
}
build(1,1,100000);
int T=read();
while(T--) {
int l1=read(),r1=read(),l2=read(),r2=read();
l2=max(l2,l1);r1=min(r1,r2);flag=0;
if(r2<l1) puts("0");
else if(r1>=l2) {
query(1,1,100000,l2,r1);
printf("%lld\n",ans.sumv+solve(l1,r1,r1+1,r2)+solve(l1,l2-1,l2,r1));
}
else printf("%lld\n",solve(l1,r1,l2,r2));
}
return 0;
}
BZOJ4262: Sum的更多相关文章
- 【BZOJ4262】Sum 单调栈+线段树
[BZOJ4262]Sum Description Input 第一行一个数 t,表示询问组数. 第一行一个数 t,表示询问组数. 接下来 t 行,每行四个数 l_1, r_1, l_2, r_2. ...
- LeetCode - Two Sum
Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- BZOJ 3944 Sum
题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
随机推荐
- Noip2016 总结&反思
一直在期盼的联赛,真正来临时,却远不像我想象的样子. 有些事,真的不敢再想. 算法可以离线,时光却不能倒流.dfs可以回溯,现实却没有如果. 有些事,注定只能成为缺憾,抱恨终生. 不得不说今年Noip ...
- Linux下的压缩和解压缩命令——zip/unzip
zip命令 zip是个使用广泛的压缩程序,文件经它压缩后会另外产生具有".zip"扩展名 的压缩文件. 选项: -A 调整可执行的自动解压缩文件. -b<工作目录> ...
- 参数名ASCII码从小到大排序(字典序)
/// <summary> /// Hashtable字典排序 /// </summary> /// <param name="parameters" ...
- CRUD操作
1.增加 insert into 表名 values(列的值,列的值) insert into 表名(列名,列名)valuse(值,值) 2.删除 delete from 表明 delete from ...
- 终于遇到app不兼容,你遇到了么?
题记: 如果支付宝和QQ不兼容,要二选一,你会怎么选择? 首先了解一下背景: 笔者最近发现,微众银行的app升级到1.7.4, 而患有轻度强迫症的人是迫不及待的点了升级. 第一次,居然安装包安装不成功 ...
- 高可用thrift客户池的实现详解
最近,公司要求将组内的thrift客户端组件推广至公司内使用.基本的要求如下: 1.高可用 2.集成名称服务,也就配置文件支持服务发现 3.解耦,客户端和高可用组件解耦,简单来说就是,如果以后要切换其 ...
- php链接数据库 批量删除 和 注册审核
理解 : hiden value session name="a[]" 1. form 表单上传的 value=" "值 ...
- 一篇很好的Java、C、PHP、前端、Android、IOS的文章
http://www.cctime.com/html/2016-11-8/1238265.htm 很好的讲了这些技术的学习路线,其中的文档资料很丰富,值得学习参考
- c#线程间操作无效: 从不是创建控件“textBox1”的线程访问它
线程开始前: Control.CheckForIllegalCrossThreadCalls = false;
- (原创)ssm sql 例子(freemarker+jsp)
ssm整合地址:http://www.cnblogs.com/xiaohuihui96/p/6104351.html 接下讲解一个插入语句的流程和顺带讲解freemarker+jsp视图的整合 初次接 ...