Bzoj 1176 Balkan Mokia CDQ Division

Source: Internet
Author: User

Some operations, give a coordinate to represent the point plus a number, and find out a rectangle of all the number of the and.


Idea: one-eye question, two-bit tree-shaped array of water over.

。。。

。。

What's wrong? w<=2000000. Tease me? Can this n^2 open?

At this time CDQ the god ox to help us again.

This problem should be regarded as the cdq of the template problem, simple analysis, in fact, not difficult.

Before writing this question, it is suggested to write the problem of Bzoj 1935 SHOI Tree gardener's troubles, which is a simplified version of the subject.

According to the normal solution, we should establish a two-bit data structure, and then maintain two-dimensional information separately. If you use a dynamically open line segment tree, you may get stuck, but you should know that you have written a two-dimensional segment tree. This is not as good as a two-bit tree array.

So we try to shrink one dimension and make it a one-dimensional problem. Consider both the weighted value and the query as an action, a query action is split into 4, and then the operations are sorted by x-coordinate. In the process of CDQ division, we guarantee that the first half of the last part is less than the second half of the time when X is ordered and the answer is updated. Then , after each processing [l,mid], use the weighted value action in [L,mid] to update the Ask action in [Mid + 1,r].


CODE:

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 2000010#define INF 0x3f3f3f3fusing namespace std;    struct complex{int flag;    int x,y,c;         int Id,ans;    BOOL operator < (const Complex &a) Const {return x < a.x; }}src[max],t[max]; int asks,cnt,size; int Fenwick[max],t,v[max];        inline void Fix (int x,int c) {for (; x <= size; x + = X&-x) {if (v[x]! = T) v[x] = t,fenwick[x] = 0;    FENWICK[X] + = C;    }} inline int getsum (int x) {int re = 0;    for (; x; x = x&-x) if (v[x] = = T) Re + = fenwick[x]; return re;}    void CDQ (int l,int r) {if (L = = r) return;    int mid = (L + r) >> 1;    int L1 = L,L2 = mid + 1;        for (int i = l; I <= R; ++i) if (src[i].id <= mid) t[l1++] = Src[i];    else t[l2++] = src[i];    memcpy (src + l,t + l,sizeof (Complex) * (r-l + 1));    CDQ (L,mid);    ++t;    int now = l; for (int i = mid +1; I <= R;                ++i) {while (src[now].x <= src[i].x && now <= mid) {if (Src[now].flag = = 1)            Fix (SRC[NOW].Y,SRC[NOW].C);        ++now;    } if (Src[i].flag = = 2) Src[i].ans + = Getsum (SRC[I].Y);    } CDQ (mid + 1,r);    L1 = L,l2 = mid + 1; for (int i = l; I <= R; ++i) if ((Src[l1] < SRC[L2] && L1 <= mid) | | L2 > R) t[i] = SR        C[l1++];    else t[i] = src[l2++]; memcpy (src + l,t + l,sizeof (Complex) * (r-l + 1));}    BOOL CMP (const Complex &a,const Complex &b) {return a.id < b.id;} int main () {scanf ("%*d%d", &size);    int flag,x1,y1,x2,y2;            while (scanf ("%d", &flag), flag^3) {if (flag = = 1) {Src[++cnt].flag = 1,src[cnt].id = cnt;        scanf ("%d%d%d", &src[cnt].x,&src[cnt].y,&src[cnt].c);            } else {scanf ("%d%d%d%d", &x1,&y1,&x2,&y2); Src[++cnt].flag = 2,src[cnt].id = Cnt                         src[cnt].x = X2,src[cnt].y = Y2,SRC[CNT].C = INF;            Src[++cnt].flag = 2,src[cnt].id = cnt;                         src[cnt].x = X1-1,src[cnt].y = Y2,SRC[CNT].C = INF;            Src[++cnt].flag = 2,src[cnt].id = cnt;                         src[cnt].x = X2,src[cnt].y = Y1-1,SRC[CNT].C = INF;            Src[++cnt].flag = 2,src[cnt].id = cnt;        src[cnt].x = X1-1,src[cnt].y = Y1-1,SRC[CNT].C = INF;    }} sort (src + 1,src + cnt + 1);    CDQ (1,CNT);    Sort (src + 1,src + cnt + 1,cmp);            for (int i = 1; I <= cnt; ++i) if (Src[i].flag = = 2) {int ans = 0;            Ans + = Src[i++].ans;            Ans-= Src[i++].ans;            Ans-= Src[i++].ans;            Ans + = Src[i].ans;        printf ("%d\n", ans); } return 0;}


Bzoj 1176 Balkan Mokia CDQ Division

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.