1546: Number of card segment tree

Source: Internet
Author: User

1546: Number of Cards
Time limit:1 Sec Memory limit:128 MB
Submit:31 Solved:6
[Submit] [Status] [Web Board]
Description
The train from Xi ' an to Hangzhou is too long, in order to pass the time, ZJC bought n cards (a piece sold you have not seen it ˉ▽ˉ) the position of the card is three-way,..., N, the face size of 3....,9,10,j,q,k,a,2, card of the color of a,b,c,d, respectively, the Red Peach , Spades, squares, clubs. As A9 said the Red Peach 9,d2 said Plum 2. Now there are m operations:
1 x y means the swap position is x and the position y is the card
2 K PQ is asked to ask the K-color is p, the card is the position of Q (PQ is a string, such as A9,D2, etc.)

Input
Enter multiple sets of data
The first line enters an n (1<=n<=100000) to indicate that ZJC bought n cards
The second line of input n cards, poker with PQ, p for the card color, Q for the card, p for A,b,c,d,q for 3....,9,10,j,q,k,a,2. The position of the first card is I (1<=i<=n).
The third line enters a number m (1<=m<=100000) to indicate that there are M queries
Next, enter an integer op (1 or 2) for each line of M line
Op=1, enter X, Y, (1<=x,y<=n), swap position x and position y cards
op=2, Input K, PQ, (1<=k<=n) asked the K-color for p, the card size is the position of Q, for each query, output corresponding position, if the card does not exist, then output-1

Output
For each query, the output card corresponds to the location, if the card does not exist, then output-1

Sample Input
5
A3 B9 B9 CJ D2
5
1 1 3
2 1 B9
1 2 5
2 2 B9
2 2 A3
Sample Output
1
5

-1

The node of the segment tree stores n-sheets of an array of each node that corresponds to the card that holds the number of this card for his or her son and

#include <bits/stdc++.h>using namespace std;const int maxn=100111;const int inf=999999999; #define Lson (rt< <1), L,m#define Rson (rt<<1|1), M+1,r#define M ((l+r) >>1) #define for (i,n) for (int i=0;i< (n); i++)    Template<class t>inline T Read (t&x) {char C;    while ((C=getchar ()) <=32);    BOOL Ok=false;    if (c== '-') Ok=true,c=getchar ();    for (x=0; c>32; C=getchar ()) x=x*10+c-' 0 ';    if (OK) x=-x; return x;}    Template<class t> inline void Read_ (t&x,t&y) {read (x); Read (y);}    Template<class t> inline void Write (T x) {if (x<0) Putchar ('-'), x=-x;    if (x<10) putchar (x+ ' 0 '); else write (X/10), Putchar (x%10+ ' 0 ');}    Template<class t>inline void Writeln (T x) {write (x); Putchar (' \ n ');}    -------IO template------struct node{int kind[54]; Node () {memset (kind,0,sizeof (kind));}}    P[maxn<<2];int A[maxn];int GetID () {int x=0;    Char tmp[2];    scanf ("%s", TMP);    x= (tmp[0]-' a ') *10; if (tmp[1]>= ' 0 ' &&tmp[1]<= ' 9 ') x+=tmp[1]-' 0 ';    else x+=tmp[1]-' J ' +11; return x;}       void update (int rt,int l,int r,int i,int Ax,int v) {if (l==r) {p[rt].kind[ax]+=v;        printf ("%d%d%d%d%d\n", Rt,l,r,i,ax,p[rt].kind[ax]);    return;    } if (i<=m) update (LSON,I,AX,V);    else update (RSON,I,AX,V); P[rt].kind[ax]=p[rt<<1].kind[ax]+p[rt<<1|1].kind[ax];}    int query (int rt,int l,int r,int x,int y) {if (l==r) {return R;    } if (p[rt<<1].kind[y]<x) return query (rson,x-p[rt<<1].kind[y],y); else return query (lson,x,y);}    int main () {//freopen ("In.txt", "R", stdin);    int n,m,i,j,k,t;           while (~SCANF ("%d", &n)) {for (i=1;i<=n;i++) {A[i]=getid ();           Update (1,1,n,i,a[i],1);        Writeln (A[i]); }//for (i=1;i<=1;i++)//{//for (j=0;j<54;j++)//if (P[i].kind[j]) printf ("**%d ", P[i].kind[j]);//            printf (") \ n");/} read (M);            while (m--) {int op;            int x;            Read_ (OP,X);                if (op==1) {int y;                Read (y);               if (x==y) continue;                cout<< "=====" <<endl;                Update (1,1,N,X,A[X],-1);                Update (1,1,n,y,a[x],1);                Update (1,1,n,x,a[y],1);                Update (1,1,N,Y,A[Y],-1);            Swap (a[x],a[y]);               } else {int y=getid ();             Writeln (y);                printf ("---%d\n", p[1].kind[y]);                if (p[1].kind[y]<x) {writeln (-1);                } else {writeln (query (1,1,n,x,y)); }}}} return 0;}


1546: Number of card segment tree

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.