国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

gym-101138D(后綴和,莫隊(duì)算法,容斥原理,好題)

2019-11-10 17:41:13
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

題目鏈接D. Strange Queriestime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

You are given an array with n integers a1,?a2,?...,?an, and q queries to answer.

Each query consists of four integers l1, r1, l2 and r2. Your task is to count the number of pairs of indices (i,?j) satisfying the following conditions:

ai?=?ajl1?≤?i?≤?r1l2?≤?j?≤?r2Input

The first line of the input contains an integer n (1?≤?n?≤?50?000) — the size of the given array.

The second line contains n integers a1,?a2,?...,?an (1?≤?ai?≤?n).

The third line contains an integer q (1?≤?q?≤?50?000) — the number of queries.

Each of the next q lines contains four integers l1, r1, l2, r2 (1?≤?l1?≤?r1?≤?n, 1?≤?l2?≤?r2?≤?n), describing one query.

Output

For each query count the number of sought pairs of indices (i,?j) and PRint it in a separate line.

Examplesinput
71 5 2 1 7 2 281 3 4 52 3 5 71 4 3 72 6 4 71 6 2 53 3 6 74 5 1 42 3 4 5output
12566220

題意簡(jiǎn)明。

題解:

使用莫隊(duì)算法

假設(shè)當(dāng)前左右指針?lè)謩e為l和r,一共n個(gè)數(shù)

對(duì)于指針l,維護(hù)一個(gè)后綴[l,n],算這個(gè)后綴每個(gè)數(shù)出現(xiàn)次數(shù)。

對(duì)于指針r,維護(hù)一個(gè)后綴(r,n]算這個(gè)后綴每個(gè)數(shù)出現(xiàn)次數(shù)。

對(duì)區(qū)間[l,r)維護(hù)[l,n]和[r,n]中相等的數(shù)的對(duì)數(shù)。

假設(shè)區(qū)間[l,r)的答案為f(l,r).

那么,對(duì)于詢問(wèn)l1,r1,l2,r2, 答案是f(l1,l2)-f(l1,r2+1)-f(r1+1,l2)+f(r1+1,r2+1).

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<vector>#include<queue>#include<stack>#include<cmath>using namespace std;#define rep(i,a,n) for (int i=a;i<n;i++)#define per(i,a,n) for (int i=n-1;i>=a;i--)#define pb push_back#define fi first#define se secondtypedef vector<int> VI;typedef long long ll;typedef pair<int,int> PII;const int inf=0x3fffffff;const ll mod=1000000007;const int maxn=50000+100;int unit;struct node{    int l,r,id,k;    bool Operator <(const node&b) const{        if(l/unit==b.l/unit) return r<b.r;        else return l/unit<b.l/unit;    }    node(int x=0,int y=0):l(x),r(y) {}}a[maxn*4];int m=0,n;ll ans[maxn];int num[maxn];ll cnt[2][maxn];ll res;void solve(){    sort(a,a+m);    memset(cnt,0,sizeof(cnt));    memset(ans,0,sizeof(ans));    int l=n+1,r=n+1;   //    res=0;    rep(i,0,m)    {        while(l<a[i].l)        {            res-=1ll*cnt[0][num[l]]*cnt[1][num[l]];            cnt[0][num[l]]--;            res+=1ll*cnt[0][num[l]]*cnt[1][num[l]];            l++;        }        while(l>a[i].l)        {            l--;            res-=1ll*cnt[0][num[l]]*cnt[1][num[l]];            cnt[0][num[l]]++;            res+=1ll*cnt[0][num[l]]*cnt[1][num[l]];        }        while(r<a[i].r)        {            res-=1ll*cnt[1][num[r]]*cnt[0][num[r]];            cnt[1][num[r]]--;            res+=1ll*cnt[1][num[r]]*cnt[0][num[r]];            r++;        }        while(r>a[i].r)        {            r--;            res-=1ll*cnt[1][num[r]]*cnt[0][num[r]];            cnt[1][num[r]]++;            res+=1ll*cnt[1][num[r]]*cnt[0][num[r]];        }        ans[a[i].id]+=a[i].k*res;    }}int main(){    scanf("%d",&n);    unit=(int)sqrt(n);    rep(i,1,n+1) scanf("%d",&num[i]);    int q;    scanf("%d",&q);    rep(i,0,q)    {        int l1,r1,l2,r2;        scanf("%d%d%d%d",&l1,&r1,&l2,&r2);        r1++,r2++;        a[m]=node(l1,l2),a[m].id=i,a[m].k=1;        a[++m]=node(l1,r2),a[m].id=i,a[m].k=-1;        a[++m]=node(r1,l2),a[m].id=i,a[m].k=-1;        a[++m]=node(r1,r2),a[m].id=i,a[m].k=1;        m++;    }    solve();    rep(i,0,q)    printf("%lld/n",ans[i]);    return 0;}


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 南京市| 定兴县| 临沂市| 石城县| 阿拉尔市| 元谋县| 湘西| 平阳县| 益阳市| 盐亭县| 历史| 常熟市| 乐昌市| 台江县| 奉化市| 常德市| 西吉县| 海原县| 深泽县| 东明县| 凌海市| 都安| 昌乐县| 新安县| 柳林县| 呈贡县| 高安市| 济源市| 额尔古纳市| 温宿县| 盐城市| 小金县| 永和县| 哈尔滨市| 金昌市| 建德市| 土默特右旗| 双峰县| 廊坊市| 万安县| 林周县|