Description
校门外有很多树,有苹果树,香蕉树,有会扔石头的,有可以吃掉补充体力的…… 如今学校决定在某个时刻在某一段种上一种树,保证任一时刻不会出现两段相同种类的树,现有两个操作: K=1,K=1,读入l、r表示在区间[l,r]中种上一种树,每次操作种的树的种类都不同 K=2,读入l,r表示询问l~r之间能见到多少种树 (l,r>0)
Input
第一行n,m表示道路总长为n,共有m个操作 接下来m行为m个操作
Output
对于每个k=2输出一个答案
Sample Input
5 4 1 1 3 2 2 5 1 2 4 2 3 5
Sample Output
1 2
Hint
范围:20%的数据保证,n,m<=100 60%的数据保证,n <=1000,m<=50000 100%的数据保证,n,m<=5000
代码:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
const int N = 1e5 + 100;
long long sum1[N], sum[N], ma;
int n, m;
void add(int x, int d, long long* c)
{
for (; x <= n; x += x & -x)
c[x] += d;
}
long long sumw(int x, long long* c)
{
long long ans = 0;
for (; x; x -= x & -x)
ans += c[x];
return ans;
}
int main()
{
int k, l, r;
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++)
{
scanf("%d", &k);
if (k == 1)
{
scanf("%d%d", &l, &r);
add(r, 1, sum);
add(l, 1, sum1);
ma++;
}
if (k == 2)
{
scanf("%d%d", &l, &r);
printf("%lld\n", ma - sumw(l - 1, sum) - (sumw(n, sum1) - sumw(r, sum1)));
}
}
return 0;
}