Codeforces Round #433 (Div. 2) E. Boredom (主席树)//主席树优化二维计数(类似于二维树状数组)...

E. Boredom
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Ilya is sitting in a waiting area of Metropolis airport and is bored of looking at time table that shows again and again that his plane is delayed. So he took out a sheet of paper and decided to solve some problems.

First Ilya has drawn a grid of size n × n and marked n squares on it, such that no two marked squares share the same row or the same column. He calls a rectangle on a grid with sides parallel to grid sides beautiful if exactly two of its corner squares are marked. There are exactly n·(n - 1) / 2 beautiful rectangles.

Ilya has chosen q query rectangles on a grid with sides parallel to grid sides (not necessarily beautiful ones), and for each of those rectangles he wants to find its beauty degree. Beauty degree of a rectangle is the number of beautiful rectangles that share at least one square with the given one.

Now Ilya thinks that he might not have enough time to solve the problem till the departure of his flight. You are given the description of marked cells and the query rectangles, help Ilya find the beauty degree of each of the query rectangles.

Input

The first line of input contains two integers n and q (2 ≤ n ≤ 200 000, 1 ≤ q ≤ 200 000) — the size of the grid and the number of query rectangles.

The second line contains n integers p1, p2, ..., pn, separated by spaces (1 ≤ pi ≤ n, all pi are different), they specify grid squares marked by Ilya: in column i he has marked a square at row pi, rows are numbered from 1 to n, bottom to top, columns are numbered from 1 to n, left to right.

The following q lines describe query rectangles. Each rectangle is described by four integers: l, d, r, u (1 ≤ l ≤ r ≤ n1 ≤ d ≤ u ≤ n), here land r are the leftmost and the rightmost columns of the rectangle, d and u the bottommost and the topmost rows of the rectangle.

Output

For each query rectangle output its beauty degree on a separate line.

 

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
typedef long long ll;
int root[N],c[N*25],ls[N*25],rs[N*25],tot;
void update(int &rt,int last,int l,int r,int p){
    rt=++tot;c[rt]=c[last]+1;
    ls[rt]=ls[last];rs[rt]=rs[last];
    if(l==r) return;
    int mid=(l+r)>>1;
    if(p<=mid) update(ls[rt],ls[last],l,mid,p);
    else update(rs[rt],rs[last],mid+1,r,p);
}//首先连接之后在进行引用修改
int query(int rt1,int rt2,int l,int r,int ql,int qr){
    if(l>=ql&&r<=qr) return c[rt2]-c[rt1];
    int mid=(l+r)>>1;
    int res=0;
    if(mid>=ql) res+=query(ls[rt1],ls[rt2],l,mid,ql,qr);
    if(mid<qr)  res+=query(rs[rt1],rs[rt2],mid+1,r,ql,qr);
    return res;
} 
ll f(int x){return 1ll*x*(x-1)/2;}
int main(){
    int n,q,x,l,d,u,r;scanf("%d%d",&n,&q);
    for(int i=1;i<=n;i++){
        scanf("%d",&x);
        update(root[i],root[i-1],1,n,x);
    }
    while(q--){
        scanf("%d%d%d%d",&l,&r,&u,&d);
        ll ans=f(l-1)+f(r-1)+f(n-u)+f(n-d);
        if(r>1) ans-=f(query(root[0],root[l-1],1,n,1,r-1));
        if(r>1) ans-=f(query(root[u],root[n],1,n,1,r-1));
        ans-=f(query(root[0],root[l-1],1,n,d+1,n));
        ans-=f(query(root[u],root[n],1,n,d+1,n));
        ans=f(n)-ans;
        printf("%I64d\n",ans);
    }
    return 0;
} 
View Code

思路:二维计数,用主席树优化,对题意进行分析的重要性

 

转载于:https://www.cnblogs.com/vainglory/p/9636514.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值