2018 杭电多校联赛第二场1007

题目链接

Problem Description

In a galaxy far, far away, there are two integer sequence a and b of length n.
b is a static permutation of 1 to n. Initially a is filled with zeroes.
There are two kind of operations:
1. add l r: add one for al,al+1...ar
2. query l r: query ∑ri=l⌊ai/bi⌋

Input

There are multiple test cases, please read till the end of input file.
For each test case, in the first line, two integers n,q, representing the length of a,b and the number of queries.
In the second line, n integers separated by spaces, representing permutation b.
In the following q lines, each line is either in the form 'add l r' or 'query l r', representing an operation.
1≤n,q≤100000, 1≤l≤r≤n, there're no more than 5 test cases.

Output

Output the answer for each 'query', each one line.

Sample Input

5 12

1 5 2 4 3

add 1 4

query 1 4

add 2 5

query 2 5

add 3 5

query 1 5

add 2 4

query 1 4

add 2 5

query 2 5

add 2 2

query 1 5

Sample Output

1

1

2

4

4

6

题意:

输入一个数组b,初始化一个数组a全为0,有两种操作:add,把数组a的【l,r】区间全部加一,query,查询区间【l,r】a/b的和

代码:


#include<algorithm>
#include<typeinfo>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<iomanip>
#include<stdio.h>
#include<math.h>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pi acos(-1)
#define mod (1e9+7)
#define fin(num,n) for(int aaa=1;aaa<=n;scanf("%d",&num[aaa++]))
#define fout(num,n) for(int aaa=1;aaa<=n;printf("%d%c",num[aaa++],aaa==n?'\n':' '))
#define ffin(num,n,m) for(int aaa=1;aaa<=n;aaa++)for(int bbb=1;bbb<=m;scanf("%d",&num[aaa][bbb++]))
#define ffout(num,n,m) for(int aaa=1;aaa<=n;aaa++)for(int bbb=1;bbb<=m;printf("%d%c", num[aaa][bbb++], bbb==m?'\n':' '))
ll gcd(ll x, ll y) { return x ? gcd(y%x, x) : y; }
ll lcm(ll x, ll y) { return x * y / gcd(x, y); }

int n, m, a[100005], b[100005], l, r;
char ch[100];

int main() {
	
	while (~scanf("%d%d", &n, &m)) {
		for (int i = 1; i <= n; i++)
			scanf("%d", &b[i]);
		memset(a, 0, sizeof(a));
		set<int>st;
		while (m--) {
			scanf("%s%d%d", ch, &l, &r);
			if (ch[0] == 'a')
				a[l]++, a[r + 1]--, st.insert(l), st.insert(r+1);
			else {
				int ji = 0, ans = 0;
				for (set<int>::iterator i = st.begin(); *i < l&&i != st.end(); i++)
					ji += a[*i];
				for (int i = l; i <= r; i++)
					ji += a[i], ans += ji / b[i];
				printf("%d\n", ans);
			}
		}
	}
	return 0;
}

t了,不会做

猜你喜欢

转载自blog.csdn.net/icliuli/article/details/81200933