Cattle off winter training camp 3 - G

Topic Portal

2 hours over 6 questions, all in the rest of the time card ... G (is cry myself dish)
for two reasons WA's:
First: Take% Li and Cheese double take wrong ~ ~ (by their own stupid cry) ~~
SECOND,: int * int multiply the bombing (QWQ)
reason TLE on the metaphysics of:
First: endl replaced by '\ the n-'
SECOND,: added a quick read
the words of my train of thought seems more wonderful is not listed in a positive solution of the question of people's? ?
I was thinking:

F not find the answer to the whole thing ... the rest is already answers to simple calculations not to all?
Because we focus only 1 we record all positions under 1.
Well, suppose there is now vector p 1 , the p- 2 , ..., the p- the n- (ordered)
now if the p- k 1 0 into the position, the end result is the original answer minus the p- k 1 to 1 on all distances other position.
If the P K 0 into 1 at the position, the end result is the original answer plus P K 1 to 1 on all the other distances position.

The question then is how to quickly obtain P K all distances at the position 1 to the other one.
Is (P n- -p K ) + (P n-1- -p K ) + ... + (P K + 1 -p K ) + (the p- k -p k-1 ) + ... + (the p- k -p 1 ?) chant
simplification too:
i = k + 1 n p i i = 1 k 1 p i + ( k 1 ( n k ) ) p k \sum^{ n}_{i =k+1}{p_i}-\sum^{ k-1}_{i =1}{p_i}+(k-1-(n-k))*p_k
The first two sections is a simple and Fenwick tree can be simple maintenance, the key is to find the last of the k binary search can be easily done (provided that certain elements in your vector is ordered)
so the last thing ... the time complexity of
recursive seeking answers to the very beginning: the n-
2 Q range and seek times Fenwick tree: 2 Qlogn
Q times binary search: Qlogn
Q times to maintain an orderly vector: Qlogn
together is 4Qlogn + n

Paste the code:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <fstream>
#define MAX 100010
#define mod 1000000007
#define ll long long
using namespace std;
ll n, m;
ll tree[MAX];
ll ans = 0;
vector<int> ve;

void Read(ll& p)
{
	p = 0;
	int flag = 1;
	char c = getchar();
	while (c < '0' or c>'9')
	{
		if (c == '-') flag = -1;
		c = getchar();
	}
	while (c >= '0' and c <= '9')
		p = p * 10 + (c - '0'), c = getchar();
	p *= flag;
}

int b_find(int p)
{
	int l = 0;
	int r = ve.size();
	while (l < r)
	{
		int mid = (l + r) >> 1;
		if (ve[mid] == p)
			return mid;
		else if (ve[mid] > p)
		{
			r = mid;
		}
		else
			l = mid + 1;
	}
	return l;
}

int lowbit(int x)
{
	return (x & (-x));
}

void update(int x, int v)
{
	while (x <= MAX)
	{
		tree[x] = ((tree[x] + v) % mod + mod) % mod;
		x += lowbit(x);
	}
}

int find(int x)
{
	int ans = 0;
	while (x)
	{
		ans = ((ans + tree[x]) % mod + mod) % mod;
		x -= lowbit(x);
	}
	return ans;
}

int getres(int p)
{
	int a = (find(n) - find(p) + mod) % mod;
	int b = find(p - 1);
	int ans = ((a - b) + mod) % mod;
	int num = b_find(p) + 1;
	int k = num - ve.size();
	k += (num - 1);
	ans = ((ans + 1ll * k * p) % mod + mod) % mod;
	return ans;
}


ll getans(string a)
{
	ll ans = 0;
	ll num = 0;
	ll last = 0;
	ll p = 0;
	for (int i = 0; i < a.size(); i++)
	{
		if (a[i] == '1')
		{
			num++;
			last = (last + (num - 1) * (i - p) % mod) % mod;
			p = i;
			ans = (ans + last) % mod;
		}
	}
	return ans;
}

int main()
{
	Read(n);
	string k;
	cin >> k;
	for (int i = 0; i < k.size(); i++)
	{
		if (k[i] == '1')
		{
			update(i + 1, i + 1);
			ve.push_back(i + 1);
		}
	}
	ans = getans(k);
	cout << ans << "\n";
	Read(m);
	for (int i = 0; i < m; i++)
	{
		ll a, b;
		Read(a);
		Read(b);
		if (a == 1)
		{
			update(b, b);
			if (ve.empty() || ve[ve.size() - 1] < b)
				ve.push_back(b);
			else
				ve.insert(upper_bound(ve.begin(), ve.end(), b), b);
			ll res = getres(b);
			ans =(ans + res) % mod;
		}
		else
		{
			update(b, -b);
			ll res = getres(b);
			ve.erase(b_find(b) + ve.begin());
			ans = ((ans - res) % mod + mod) % mod;
		}
		cout << ans << "\n";
	}
	return 0;
}

Hate the ... Sa hours to find BUG (thin blue mushrooms)

Published 30 original articles · won praise 9 · views 1301

Guess you like

Origin blog.csdn.net/Zhang_sir00/article/details/104231874