a題 4267 A Simple Problem with Integers
一直糾結在如何縮減區間, 後來發現直接更新就好了, 比如更新模10餘1的在1到20區間上, 我們需要更新的是1和11, 在新區間上應該是【1, 2】 但是其實直接去更新模10餘1那顆樹上的【1,20】其實就可以, 因爲查詢除模10餘1之外的數,是不會訪問到這顆樹的更新, 自然也不會影響其他位置的值
int a[maxn];
int main ()
{
int n;
while (~scanf("%d", &n))
{
memset (C, 0, sizeof(C));
N=n;
for (int i=1; i<=n; ++i)
scanf("%d", a+i);
int q;
scanf("%d", &q);
for (int kk=0; kk<q; ++kk)
{
int op; scanf("%d", &op);
if(op==1)
{
int a, b, k, c; scanf("%d%d%d%d", &a, &b, &k, &c);
int p=(k-1)*10+a%k;
IUpdate(a, b, c, p);
}
if(op==2)
{
int t; scanf("%d", &t);
int ans=0; ///Query(t+1, 0);
//debug(Query(t, 0));
for (int i=0; i<10; ++i)
{
int p=i*10+t%(i+1);
ans+=Query(t, p);
}
printf("%d\n", ans+a[t]);
}
}
}
return 0;
}
b題 4268
#include <cstdio>
#include <set>
#include <algorithm>
using namespace std;
const int maxn=100000+123;
///int ax[maxn], ay[maxn], bx[maxn], by[maxn];
multiset<int, greater<int> > card;
struct Node{
int x, y;
}a[maxn], b[maxn];
bool cmp(Node a, Node b)
{
return (a.x<b.x || (a.x==b.x && a.y<b.y));
}
bool cmp1(int a, int b)
{
return a>b;
}
int main()
{
int cas; scanf("%d", &cas);
while(cas--)
{
int n; scanf("%d", &n);
for (int i=0; i<n; ++i)
scanf("%d%d", &a[i].x, &a[i].y);
///scanf("%d%d", ax+i, ay+i);
for (int i=0; i<n; ++i)
scanf("%d%d", &b[i].x, &b[i].y);
///scanf("%d%d", bx+i, by+i);
sort(a, a+n, cmp); sort(b, b+n, cmp);
int cnt=0;
card.clear();
for (int i=0, k=0; i<n; ++i)
{
while (a[i].x>=b[k].x && k<n)
{
card.insert(b[k].y); k++;
}
multiset<int, greater<int> > :: iterator it=card.lower_bound(a[i].y);
///multiset<int, greater<int> > :: iterator it=lower_bound(card.begin(), card.end(), a[i].y, cmp1 );
/// 下面的這個用法會tle應該是常數太大的原因。 上面的1500ms過。
if(it!=card.end())
{
///it=max_element(card.begin(), --it);
///printf("%d %d %d\n", *it, a[i].x, a[i].y);
card.erase(it);
cnt++;
}
}
printf("%d\n", cnt);
}
return 0;
}