Codeforces Round #310 (Div. 1) b,c,d (set+ segment tree) __ Tree array/segment tree

Source: Internet
Author: User
Tags abs integer numbers
B. Case of Fugitive

Andrewid The Android is a galaxy-famous detective. He's now chasing a criminal hiding on the planet Oxa-5, the planet almost fully covered with water.

The only dry land there are a archipelago of N narrow islands located in a row. For more comfort let's represent them as non-intersecting segments on a straight Line:island i has coordinates [Li, RI], Besides, RI < li + 1 for 1≤i≤n-1.

To reach the goal, Andrewid needs to place a bridge between each pair of adjacent islands. A bridge of length a can be placed between the i-th and the (i + 1)-th islads, if there are such coordinates of x and Y, t Hat Li≤x≤ri, Li + 1≤y≤ri + 1 and y-x = A.

The detective is supplied with M bridges, and each bridge can is used at most once. Help him determine whether the bridges him got are to connect each enough of pair adjacent.
Input

The contains integers n (2≤n≤2 105) and M (1≤m≤2 105)-the number of islands and bridges.

Next n lines each contain two integers li and ri (1≤li≤ri≤1018)-the coordinates of the island.

The last line contains M integer numbers a1, a2, ..., AM (1≤ai≤1018)-the lengths of the bridges that Andrewid got.
Output

If It is impossible to place a bridge between all pair of adjacent islands in the required manner, print on a single  "No" (without the quotes), otherwise print in the "Yes" (without the quotes), and in the second line print n- 1 numbers b1, B2, ..., bn-1, which mean that between islands I and I + 1 There must is used a bridge number bi.

If There are multiple correct answers, print any of them. Note as this, problem it is necessary to print "Yes" and "No" in correct case.
Sample Test (s)
Input

4 4
1 4
7 8
9 10
12 14
4 5 3 8

Output

Yes
2 3 1

Input

2 2
11 14
17 18
2 9

Output

No

Input

2 1
1 1
1000000000000000000 1000000000000000000
999999999999999999

Output

Yes
1

Note

In the the ' the ' I sample test you can, for example, place the second bridge between points 3 and 8, where the third bridge BETW Een points 7 and place the between points and 14.

In the second sample test the ' the ' the ' the ' the ' the ' the ' the ' the ' 's Too short and the second bridge are too long, so the solution doesn ' t exist.

The question: There are N Islands, M bridges, if the length of the bridge can meet the island, and not more than the length of the island, then you can, ask all the island can connect up
Train of thought: each island (L,r), should try to find greater than or equal l, and is less than equal to r, with the smallest difference, then you can sort the island, and then maintain a set, used to find out the nearest bridge.

#include <bits/stdc++.h> using namespace std;
typedef long Long LL;
const int maxn=200010;
int n,m;
    struct A {LL l,r;
    int id;
    BOOL operator< (const A &a) Const {return l<a.l;
}}A[MAXN];
Pair<ll,int> P[MAXN];
Set<pair<ll,int>> St;
int ANS[MAXN];
    int main () {scanf ("%d%d", &n,&m);
    if (m<n-1) {printf ("no\n"); return 0;}
    LL l,r,ll=0,lr=0;
    n--;
    cin>>ll>>lr;
        for (int i=1;i<=n;i++) {cin>>l>>r;
        A[I].L=L-LR;
        A[i].r=r-ll;
        A[i].id=i;
    Ll=l,lr=r;
        for (int i=1;i<=m;i++) {cin>>l;
    P[i]=make_pair (L,i);
    Sort (a+1,a+1+n);
    Sort (p+1,p+1+m);
        for (int j=m,i=n;i>0;i--) {while (J&GT;0&AMP;&AMP;P[J].FIRST&GT;=A[I].L) St.insert (p[j)), j--;
        Set<pair<ll,int>>::iterator It=st.upper_bound (Make_pair (a[i].r+1,0));
       if (It==st.begin ()) {     printf ("no\n");
        return 0;
        } it--;
        ans[a[i].id]=it->second;
    St.erase (IT);
    printf ("yes\n");
    for (int i=1;i<=n;i++) printf ("%d", ans[i]);
    printf ("\ n");
return 0; }
C. Case of chocolate

Andrewid The Android is a galaxy-known detective. Now it does not investigate the any case and are eating chocolate out of boredom.

A Bar of chocolate can be presented as a nxn table, where each cell represents one piece of chocolate. The columns of the table are numbered from-1 to N-from-to-right and the ' rows are numbered from-to-bottom. Let's call the anti-diagonal to be a diagonal so goes the lower left corner to the "upper right corner of the table." The Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following Q actions with the remaining triangular part:first, he chooses a piece on the Anti-diagona L and either direction ' up ' or ' left ', and then him begins to eat all the pieces starting the selected cell, moving in The selected direction until he reaches the already eaten or piece bar edge.

After each action, he wants to know how many pieces the he ate as the this action.
Input

The contains integers n (1≤n≤109) and Q (1≤q≤2 105)-the the size of the chocolate bar and the number of a Ctions.

Next q lines contain the descriptions of the actions:the i-th of them contains numbers XI and Yi (1≤xi, yi≤n, xi + yi = n + 1)-the numbers of the column and row of the chosen cell and the character that represents the direction (l-left , u-up).
Output

Print q Lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Sample Test (s)
Input

6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U

Output

4
3
2
1
2

Input

10 6
2 9 U
Ten 1 U
1 U
8 3 L
1 L
6 5 U

Output

9
1
10
6
0
2

Note

Pictures to the sample tests:

The pieces that were eaten the same action are the painted color. The pieces lying on the anti-diagonal contain the numbers of the "action as a" of which these pieces were.

In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, Starti Ng from the cell in the intersection of the 10-th column and the 1-st row, but this cell are already empty, so he does not Eat anything.

The upper triangle, each can select a point from the vice diagonal, and then choose to go up or left, ask how many squares can walk each time, walk through the can not go
Train of thought: first discretization, I am to the X coordinate discrete, and then maintain a left boundary and the upper boundary line, each time need to query, and then update, update the time to notice, is to update this point and he can go to the square between the x-coordinate of the point on the vice diagonal

#include <bits/stdc++.h> using namespace std;
const int maxn=200010;
int X[MAXN];
int n,m;
    struct node {int x,y;
Char d[5];
}OP[MAXN];
BOOL VIS[MAXN];
    struct Intervaltree {int up[maxn<<2];
    int down[maxn<<2];
    int set1[maxn<<2];
    int set2[maxn<<2];
        void build (int o,int l,int r) {up[o]=down[o]=0;
        set1[o]=set2[o]=0;
        if (l==r) return;
        int mid= (L+R) >>1;
        Build (O<<1,l,mid);
    Build (O<<1|1,mid+1,r);
            } void Update (int o,int l,int r,int q1,int q2,int x,bool f) {if (Q1&LT;=L&AMP;&AMP;R&LT;=Q2) {
            if (F&&x>up[o]) set1[o]=x,up[o]=x;
            else if (!f&&x>down[o]) set2[o]=x,down[o]=x;
        return;
        } pushdown (O,F);
        int mid= (L+R) >>1;
        if (q1<=mid) update (O&LT;&LT;1,L,MID,Q1,Q2,X,F);
    if (q2>mid) update (O&LT;&LT;1|1,MID+1,R,Q1,Q2,X,F); } void pushdown (int o, bool f) {if (f) {if (Set1[o]) {if (set1[o]>set1[o<<1
                ]) set1[o<<1]=set1[o],up[o<<1]=set1[o];
                if (set1[o]>set1[o<<1|1]) set1[o<<1|1]=set1[o],up[o<<1|1]=set1[o];
            set1[o]=0;
                    } else {if (Set2[o]) {if (set2[o]>set2[o<<1])
                set2[o<<1]=set2[o],down[o<<1]=set2[o];
                if (set2[o]>set2[o<<1|1]) set2[o<<1|1]=set2[o],down[o<<1|1]=set2[o];
            set2[o]=0;
        {if (l==r) return f?up[o]:d own[o] int query (int o,int l,int r,int pos,bool f)
        Pushdown (O,F);
        int mid= (L+R) >>1;
        if (pos<=mid) return query (O&LT;&LT;1,L,MID,POS,F);
    return query (O&LT;&LT;1|1,MID+1,R,POS,F); }}treE
    int main () {scanf ("%d%d", &m,&n);
        for (int i=1;i<=n;i++) {scanf ("%d%d%s", &AMP;OP[I].Y,&AMP;OP[I].X,OP[I].D);
    x[i]=op[i].x;
    Sort (x+1,x+1+n);
    Tree.build (1,1,n);
        for (int i=1;i<=n;i++) {int Tmp=lower_bound (x+1,x+1+n,op[i].x)-X;
        if (Vis[tmp]) {printf ("0\n"); continue;}
        Vis[tmp]=1;
            if (op[i].d[0]== ' U ') {int pos=tree.query (1,1,n,tmp,1);
            int Tmp1=lower_bound (X+1,X+1+N,POS)-X;
            if (pos==0) tmp1=0;
            if (tmp1+1<=tmp) tree.update (1,1,n,tmp1+1,tmp,op[i].y,0);
        printf ("%d\n", Op[i].x-pos);
            else {int pos=tree.query (1,1,n,tmp,0);
            int Tmp1=lower_bound (X+1,X+1+N,1+M-POS)-X;
            if (pos==0) tmp1=n+1;
            if (tmp1-1>=tmp) tree.update (1,1,n,tmp,tmp1-1,op[i].x,1);
        printf ("%d\n", Op[i].y-pos);
} return 0;
 }

D. Case of a top Secret
Time limit per test
2 seconds
Memory limit per test
256 Megabytes
Input
Standard input
Output
Standard output

Andrewid The Android is a galaxy-famous detective. Now it busy with a top secret case, the details of which are not subject to disclosure.

However, he needs help conducting one of the investigative experiment. There are n pegs put on a plane, they are numbered from 1 to n, the coordinates of the i-th of them are (xi, 0). Then, we tie to the bottom of one of the pegs a weight on a tight rope of length L (thus, its coordinates'll be equal to (xi,-L), where I is the number of the used peg). Then the "weight is pushed" to "right", so "it starts to rotate counterclockwise. At the same time, if the weight during rotation touches some of the other pegs, it then begins to rotate "that peg." Suppose that each peg itself are very thin and does not affect the rope of length while weight is rotating around it.

More formally, if on some moment the segment of the rope contains one or more pegs into addition to the peg around which the Weight is rotating, the weight'll then rotate around the farthermost one of them on a shorter segment of a rope. In particular, if the segment of the rope touches some peg by their endpoint, it is considered this weight starts to rot Ate around that peg on a segment of the rope of length 0.

At some moment the weight would begin to rotate around some peg, without affecting the rest of the pegs. Andrewid interested in determining the number of this peg.

Andrewid prepared m queries containing initial for conditions the pushing, help weight to him for each of the determine, Around what peg the weight would eventually rotate.
Input

The contains integers n and m (1≤n, m≤2 105)-the number of pegs and queries.

The next line contains n integers x1, x2, ... xn (-109≤xi≤109)-the coordinates of the pegs. It is guaranteed the coordinates of the pegs are distinct integers.

Next m lines contain the descriptions of the queries of pushing the weight, each consists of two integers ai (1≤ai≤n) and Li (1≤li≤109)-the number of the starting peg and the length of the rope.
Output

Print m lines, the i-th line should contain the number of the peg around which the weight'll eventually rotate after the I-th push.
Sample Test (s)
Input

3 2
0 3 5
2 3
1 8

Output

3
2

Input

4 4
1 5 7 15
1 4
2 15
3 16
1 28

Output

2
4
3
1

Note

Picture to the:

Picture to the second sample test:

Note this in the last query weight starts to rotate around the peg 1 attached to a rope segment of length 0.
Title: Give n Hang Point, m a heavy hammer, the first I heavy hammer initially hung in the first AI hanging point, rope length for Leni, the initial right swing, scraping to a heavy hammer will turn back, ask the last hanging on which heavy hammer

Ideas (from: Here to write link content): Each time the two points to find the location, record the path, if it is a−a−a on the output of the answer, if it is a−b−a to take the mold, so no more than log2len the length of the rope will become 0
Degree of Complexity O (M∗log2n∗log2len)

#include <bits/stdc++.h> using namespace std;
const int maxn=200010;
int POS[MAXN],A[MAXN],ID[MAXN];
int n,q;
int ST[MAXN];
    BOOL CMP (int x,int y) {return pos[x]<pos[y];} int main () {scanf ("%d%d", &n,&q);
    for (int i=1;i<=n;i++) scanf ("%d", &a[i]), pos[i]=a[i],id[i]=i;
    Sort (id+1,id+1+n,cmp);
    Sort (pos+1,pos+1+n);
    int X,len;
        while (q--) {scanf ("%d%d", &x,&len);
        int top=0;
        int Cur=lower_bound (pos+1,pos+1+n,a[x])-pos;
        St[top=1]=cur;
                while (true) {if (Top>=3&&st[top]==st[top-1]&&st[top]==st[top-2]) {
                printf ("%d\n", Id[st[top]);
            Break
            } if (Top>=3&&st[top]==st[top-2]) len%= (ABS (POS[ST[TOP]]-POS[ST[TOP-1))) *2;
            int next_pos;
            if (top&1) next_pos=upper_bound (Pos+1,pos+1+n,pos[cur]+len)-pos-1; else Next_pos=lower_bound (pos+1,pos+1+N,pos[cur]-len)-pos;
            Len=len-abs (Pos[cur]-pos[next_pos]);
            Cur=next_pos;
        St[++top]=cur;
} return 0;
 }
Related Article

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.