[Vijos] 1881 twinkling stars (line tree + Special tips)

Source: Internet
Author: User

Https://vijos.org/p/1881

This competition is too difficult for sad. So I did not do it ..

At the beginning, I didn't even know about sad (I wouldn't have done it), and then I went on to YY .. After YY for a long time, why does YY split and query sets? (SAD, later I found out that I am so Sb, and it cannot be implemented at all ..)

Then I gave up my treatment, and I simply refused to play the game .. Sad

Later, I came to view the question and the enthusiastic guidance of the gods. This is the question of water ..

Sad.

We only need to maintain three values in the online segment tree. L indicates the longest feasible string length that can be extended from the left in the interval of this node, r indicates the length of the longest feasible string that can be extended from the right to the left in the interval of the node, and MX indicates the length of the longest feasible string in the interval.

In pushup, we only need to transfer it like this.

void pushup(int x, int len) {int l=lc, r=rc;t[x].x=t[l].x; t[x].y=t[r].y;t[x].lx=t[l].lx;t[x].rx=t[r].rx;t[x].mx=max(t[l].mx, t[r].mx);if(t[l].y!=t[r].x) {t[x].mx=max(t[x].mx, t[l].rx+t[r].lx);if(t[l].mx==(len-(len>>1))) t[x].lx=max(t[x].lx, t[l].mx+t[r].lx);if(t[r].mx==(len>>1)) t[x].rx=max(t[x].rx, t[r].mx+t[l].rx);}}

Think about it and you will know why... (Because I am a zookeeper, I cannot think of it. Orz

Then this question becomes a question of water.

#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <iostream>#include <algorithm>#include <queue>using namespace std;#define rep(i, n) for(int i=0; i<(n); ++i)#define for1(i,a,n) for(int i=(a);i<=(n);++i)#define for2(i,a,n) for(int i=(a);i<(n);++i)#define for3(i,a,n) for(int i=(a);i>=(n);--i)#define for4(i,a,n) for(int i=(a);i>(n);--i)#define CC(i,a) memset(i,a,sizeof(i))#define read(a) a=getint()#define print(a) printf("%d", a)#define dbg(x) cout << (#x) << " = " << (x) << endl#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }#define printarr1(a, b) for1(_, 1, b) cout << a[_] << ‘\t‘; cout << endlinline const int getint() { int r=0, k=1; char c=getchar(); for(; c<‘0‘||c>‘9‘; c=getchar()) if(c==‘-‘) k=-1; for(; c>=‘0‘&&c<=‘9‘; c=getchar()) r=r*10+c-‘0‘; return k*r; }inline const int max(const int &a, const int &b) { return a>b?a:b; }inline const int min(const int &a, const int &b) { return a<b?a:b; }const int N=200005;#define lc x<<1#define rc x<<1|1#define lson l, m, lc#define rson m+1, r, rc#define MID (l+r)>>1struct dat { int lx, rx, x, y, mx; }t[N<<2];int n, Q;void pushup(int x, int len) {int l=lc, r=rc;t[x].x=t[l].x; t[x].y=t[r].y;t[x].lx=t[l].lx;t[x].rx=t[r].rx;t[x].mx=max(t[l].mx, t[r].mx);if(t[l].y!=t[r].x) {t[x].mx=max(t[x].mx, t[l].rx+t[r].lx);if(t[l].mx==(len-(len>>1))) t[x].lx=max(t[x].lx, t[l].mx+t[r].lx);if(t[r].mx==(len>>1)) t[x].rx=max(t[x].rx, t[r].mx+t[l].rx);}}void build(int l, int r, int x) {t[x].mx=t[x].x=t[x].y=t[x].lx=t[x].rx=1;if(l==r) return;int m=MID;build(lson); build(rson);}void upd(int l, int r, int x, int p) {if(l==r) { t[x].x=t[x].y=!t[x].x; return; }int m=MID;if(p<=m) upd(lson, p); else upd(rson, p);pushup(x, r-l+1);}int main() {read(n); read(Q);build(1, n, 1);while(Q--) {upd(1, n, 1, getint());printf("%d\n", t[1].mx);}return 0;}

 

 

 

 

Background

Stars are shining-dark blue space
He once heard their correct words
Silent
Shimali
They praised each other deeply.

Description

Stars.
There are n stars in a column.

The stars are good children, and the island is directing them to dance.
They lit up before the dance began!

The small island refers to the small stars in I, and the small stars in I immediately change their status.
If it was previously on, it would immediately be destroyed.
If it was previously destroyed, it will be on now!

If so, there may be several consecutive stars.
Any two adjacent stars have different states.
That is the most beautiful.

Hope to know:
After each instruction is sent
Find the longest continuous stars to meet the above requirements
How long is it?

Format input format

The first line has two integers, N in the total number of stars and Q in the total number of commands.
1 <= n <= 200,000; 1 <= q <= 200,000.
Next to the Q row, each row has an integer I: 1 <= I <= N, indicating the instructions sent by the island.

Output Format

The output contains Q rows. Each I row has an integer.
Indicates the maximum number of consecutive star sequences that can be found after the I Directive of the island is issued?

Example 1 input 1 [copy]
6 224
Sample output 1 [copy]
35
Restrictions

For 20% of data: N, q <= 100.
For 30% of data: N, q <= 70000.
For 100% of data: 1 <= N, q <= 200,000.

Prompt

For example, the star sequence is in the following order: Oooooo-> oxoooo-> oxoxoxoo
Here, O is used to represent the bright stars, and X is used to represent the destroyed stars.

[Vijos] 1881 twinkling stars (line tree + Special tips)

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.