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 ...
随机推荐
- java 单例模式
懒汉式 public class Singleton{ //@单例类只能有一个实例 //@单例类必须自行创建这个实例 //@单例类必须给所有对象提供这一个实例//必须向整个系统提供这个这个实例 pri ...
- Python全栈开发【re正则模块】
re正则模块 本节内容: 正则介绍 元字符及元字符集 元字符转义符 re模块下的常用方法 正则介绍(re) 正则表达式(或 RE)是一种小型的.高度专业化的编程语言. 在Python中,它内嵌在Pyt ...
- url带#号,微信授权,微信分享那些坑
微信授权的方法是,在项目里面配置拦截器(此处可以参考各个框架的拦截器)没有拦截器也可以,反正意思就是跳转到项目里的时候判断微信环境 如果是微信环境, 判断微信环境的方法是 var ua = windo ...
- JAVA BigDecimal 小数点处理
1,保留两位小数 方法一:{ double c=3.154215; java.text.DecimalFormat myformat=new java.text.DecimalFormat(" ...
- [转]IIS6.0迁移至IIS7.0
原文地址:http://www.splaybow.com/post/iis-6.0-7.0.html 公司的项目需要迁移到IIS7的目标机器中 在此做记录 原来server 2003系统 迁到2008 ...
- 基于SOUI开发的应用展示
本页面列出基于SOUI开发的产品 欢迎使用SOUI的朋友提供资源:setoutsoft#qq.com #->@ 千万级平台后台在线监测客户端 1, 主页:用于显示管理服务端在线情况,左侧栏包括 ...
- scrapy爬虫结果插入mysql数据库
1.通过工具创建数据库scrapy
- [NHibernate]利用LINQPad查看NHibernate生成SQL语句
上篇文章中我们提到可以通过重写NHibernate的 EmptyInterceptor 拦截器来监控NHibernate发送给数据库的SQL脚本,今天看到有朋友用LINQPad工具来进行NHibern ...
- Git 版本管理的简单理解
来源:百度知道 现在使用Git版本管理代码的项目非常多.但是Git本身是一条复杂的系统.我从几个简单的点来说明Git的基本功能.希望能帮助初学者快速入门. 工具/原料 Git code dot j ...
- Tween Animation----Rotate旋转动画
activity_main.xml布局里 在res/下面新建一个anim文件夹用来保存动画的xml属性 我们在anim文件夹下面创建一个rotate.xml文件 代码如下: <?xml vers ...