Poj issue .a simple problem with integers Problem Solving report

Source: Internet
Author: User

Using a tree array and a line segment tree is relatively simple. Here we use this question to learn splay.

The first write, the code is ugly

/* Initialize and add a node with a sufficient key value to ensure that each required node has a successor */# include <iostream> # include <cstdio> # define ll long longusing namespace STD; const int maxn = 1111111, INF = 0x7fffffff; struct node {// The required record information ll key, Val, sum, lazy, size, CNT; // The pointer to the Son and Father: node * Ch [2], * pre; node () {pre = CH [0] = CH [1] = 0; size = 1; key = 0;} node (LL key): Key (key) {pre = CH [0] = CH [1] = 0; size = 1, CNT = 1, lazy = 0;} void csize () {size = CNT; If (CH [0]! = NULL) size + = CH [0]-> size; If (CH [1]! = NULL) size + = CH [1]-> size;} void csum () {sum = val; If (CH [0]! = NULL) sum + = CH [0]-> sum + CH [0]-> size * Ch [0]-> lazy; If (CH [1]! = NULL) sum + = CH [1]-> sum + CH [1]-> size * Ch [1]-> lazy ;}} nil (0 ), * nil = & nil; struct splay {node * root, nod [maxn]; int ncnt; // calculate the number of knots with different key values, de-duplicated splay () {ncnt = 0; root = & (nod [ncnt ++] = node (INF); root-> pre = nil; root-> val = root-> sum = 0;} void push_down (node * X) {If (X-> lazy! = 0) {If (X-> CH [0]! = NULL) x-> CH [0]-> lazy + = x-> lazy; If (X-> CH [1]! = NULL) x-> CH [1]-> lazy + = x-> lazy; X-> Val + = x-> lazy;} X-> lazy = 0 ;} void Update (node * X) {X-> csize (); X-> csum ();} void rotate (node * X, int Sta) {// single rotation operation, 0 left-hand, 1 right-hand node * P = x-> pre, * g = p-> pre; push_down (P), push_down (x); P-> CH [! Sta] = x-> CH [sta]; If (X-> CH [sta]! = NULL) x-> CH [sta]-> pre = P; X-> pre = g; If (G! = Nil) if (G-> CH [0] = P) g-> CH [0] = x; else G-> CH [1] = X; x-> CH [sta] = p, p-> pre = x, update (p); If (P = root) root = x ;} void splay (node * X, node * Y) {// splay operation, which means to convert node X to the root for (push_down (x); X-> pre! = Y;) {// pass the X mark down if (X-> pre = y) {// The target node is the parent node if (X-> pre-> CH [0] = x) rotate (x, 1); else rotate (x, 0 );} else {node * P = x-> pre, * g = p-> pre; If (G-> CH [0] = P) if (p-> CH [0] = x) rotate (P, 1), rotate (x, 1); // One-font double rotation else rotate (X, 0), rotate (x, 1); // <G id = "2", double rotation else if (p-> CH [1] = x) rotate (p, 0 ), rotate (x, 0); // \ one-font rotation else rotate (x, 1), rotate (x, 0); //> glyph rotation} Update (x ); // Maintain the x node} // locate the K node that is convenient in the middle order and rotate it to the bottom of node y. Void select (int K, node * Y) {int TEM; node * t; for (t = root;) {push_down (t ); // mark the downloading TEM = T-> CH [0]-> size; If (k = TEM + 1) break; // locate the k-th node T if (k <= TEM) t = T-> CH [0]; // The k-th node is in the left subtree else K-= TEM + 1, t = T-> CH [1]; // In the right subtree} splay (t, y);} bool search (LL key, node * Y) {node * t = root; For (; t! = NULL;) {push_down (t); If (t-> key & T-> CH [0]! = NULL) t = T-> CH [0]; else if (t-> key <key & T-> CH [1]! = NULL) t = T-> CH [1]; else break;} splay (T, Y); Return T-> key = key;} void insert (INT key, int Val) {If (search (Key, nil) Root-> CNT ++, root-> size ++; else {int d = Key> root-> key; node * t = & (nod [++ ncnt] = node (key); push_down (Root); t-> val = T-> sum = val; t-> CH [d] = root-> CH [d]; If (root-> CH [d]! = NULL) Root-> CH [d]-> pre = T; t-> CH [! D] = root; t-> pre = root-> pre; root-> pre = T; root-> CH [d] = NULL; Update (Root ); root = T;} Update (Root) ;}} sp; ll n, m, X; int main () {scanf ("% LLD", & N, & M); SP. insert (0, 0); SP. insert (n + 1, 0); For (INT I = 1; I <= N; I ++) {scanf ("% LLD", & X); SP. insert (I, x);} Char cmd; int L, R; For (INT I = 1; I <= m; I ++) {scanf ("\ n % C % d", & cmd, & L, & R); SP. search (L-1, nil); SP. search (R + 1, SP. root); If (cmd = 'q') {node * t = sp. root-> CH [1]-> CH [0]; ll ans = T-> sum + T-> size * t-> lazy; printf ("% LLD \ n", ANS);} If (cmd = 'C') {ll C; scanf ("% LLD", & C); SP. root-> CH [1]-> CH [0]-> lazy + = C ;}} return 0 ;}
View code

 

Poj issue .a simple problem with integers Problem Solving report

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.